New file |
| | |
| | | package com.anyun.exam.lib; |
| | | |
| | | import android.content.Context; |
| | | import android.content.pm.PackageInfo; |
| | | import android.content.pm.PackageManager; |
| | | import android.content.pm.Signature; |
| | | import android.util.Log; |
| | | |
| | | import java.io.ByteArrayOutputStream; |
| | | import java.io.File; |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.security.MessageDigest; |
| | | import java.security.NoSuchAlgorithmException; |
| | | import java.util.List; |
| | | import java.util.Locale; |
| | | |
| | | public class InstallUtil { |
| | | public static final String TAG = InstallUtil.class.getCanonicalName(); |
| | | private static InstallUtil instance = null; |
| | | private Context context; |
| | | |
| | | public static InstallUtil getInstance(Context context) { |
| | | if (instance == null) { |
| | | synchronized (InstallUtil.class) { |
| | | if (instance == null) { |
| | | instance = new InstallUtil(context); |
| | | } |
| | | } |
| | | } |
| | | return instance; |
| | | } |
| | | |
| | | private InstallUtil(Context context) { |
| | | this.context = context; |
| | | } |
| | | |
| | | public int getVerCode() { |
| | | int verCode = 0; |
| | | try { |
| | | verCode = context.getPackageManager(). |
| | | getPackageInfo(context.getPackageName(), 0).versionCode; |
| | | } catch (PackageManager.NameNotFoundException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | |
| | | Log.d(TAG, "APP 版本 = " + verCode); |
| | | |
| | | return verCode; |
| | | } |
| | | |
| | | public String getVerName() { |
| | | String verName = ""; |
| | | try { |
| | | verName = context.getPackageManager(). |
| | | getPackageInfo(context.getPackageName(), 0).versionName; |
| | | } catch (PackageManager.NameNotFoundException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return verName; |
| | | } |
| | | |
| | | public String getPackageName() { |
| | | String name = ""; |
| | | try { |
| | | name = context.getPackageManager(). |
| | | getPackageInfo(context.getPackageName(), 0).packageName; |
| | | } catch (PackageManager.NameNotFoundException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return name; |
| | | } |
| | | |
| | | public class AppInfo { |
| | | public String appName=""; |
| | | public String packageName=""; |
| | | public String versionName=""; |
| | | public int versionCode=0; |
| | | } |
| | | |
| | | public AppInfo getAppInfo(String packageName) { |
| | | List<PackageInfo> packages = context.getPackageManager().getInstalledPackages(0); |
| | | |
| | | for (PackageInfo packageInfo : packages) { |
| | | if (packageName.equals(packageInfo.packageName)) { |
| | | AppInfo appInfo = new AppInfo(); |
| | | |
| | | appInfo.appName= packageInfo.applicationInfo.loadLabel(context.getPackageManager()).toString(); |
| | | appInfo.packageName = packageInfo.packageName; |
| | | appInfo.versionName = packageInfo.versionName; |
| | | appInfo.versionCode = packageInfo.versionCode; |
| | | |
| | | return appInfo; |
| | | } |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | public Signature[] getSignature() { |
| | | Signature[] signs = null; |
| | | |
| | | try { |
| | | PackageInfo info = context.getPackageManager().getPackageInfo(context.getPackageName(), PackageManager.GET_SIGNATURES); |
| | | signs = info.signatures; |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return signs; |
| | | } |
| | | |
| | | public Signature[] getSignature(String archiveFilePath) { |
| | | Signature[] signs = null; |
| | | try { |
| | | PackageInfo info = context.getPackageManager().getPackageArchiveInfo(archiveFilePath, PackageManager.GET_SIGNATURES); |
| | | signs = info.signatures; |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return signs; |
| | | } |
| | | |
| | | public String getVersionNameFromApk(Context context, String archiveFilePath) { |
| | | PackageManager pm = context.getPackageManager(); |
| | | PackageInfo packInfo = pm.getPackageArchiveInfo(archiveFilePath, PackageManager.GET_ACTIVITIES); |
| | | return packInfo.versionName; |
| | | } |
| | | |
| | | public int getVersionCodeFromApk(Context context, String archiveFilePath) { |
| | | PackageManager pm = context.getPackageManager(); |
| | | PackageInfo packInfo = pm.getPackageArchiveInfo(archiveFilePath, PackageManager.GET_ACTIVITIES); |
| | | return packInfo.versionCode; |
| | | } |
| | | |
| | | public String getPackageNameFromApk(Context context, String archiveFilePath) { |
| | | PackageManager pm = context.getPackageManager(); |
| | | PackageInfo packInfo = pm.getPackageArchiveInfo(archiveFilePath, PackageManager.GET_ACTIVITIES); |
| | | return packInfo.packageName; |
| | | } |
| | | |
| | | public void InstallApp(String path) { |
| | | File file = new File(path); |
| | | |
| | | if (file.exists() && file.isFile()) { |
| | | try { |
| | | Log.d(TAG, "升级app 文件存在 当前ver " + getVerCode() + " 当前Name " + getVerName() + " 目标ver " + getVersionCodeFromApk(context, file.getAbsolutePath()) + " 目标name " + getVersionNameFromApk(context, file.getAbsolutePath())); |
| | | |
| | | // Signature[] sig = getSignature(); |
| | | // Signature[] sig2 = getSignature(file.getAbsolutePath()); |
| | | |
| | | /*if ( getFingerprint(sig[0], "SHA-1").equals(getFingerprint(sig2[0], "SHA-1")))*/ { |
| | | if ( getVerCode() <= getVersionCodeFromApk(context, file.getAbsolutePath()) ) { |
| | | Log.d(TAG, "安装文件 " + file.getAbsolutePath()); |
| | | String result = execCommand("pm", "install", "-i", getPackageName(), "--user", "0", "-r", "-d", file.getAbsolutePath()); |
| | | Log.d(TAG, "安装结果 " + result); |
| | | } else { |
| | | file.delete(); |
| | | } |
| | | } /*else { |
| | | file.delete(); |
| | | }*/ |
| | | } catch (Exception e) { |
| | | Log.e(TAG, "安装发生错误 " + e.getMessage()); |
| | | } |
| | | } |
| | | } |
| | | |
| | | public String getFingerprint(Signature signature, String hashAlgorithm) { |
| | | if (signature == null) { |
| | | return null; |
| | | } |
| | | try { |
| | | MessageDigest digest = MessageDigest.getInstance(hashAlgorithm); |
| | | return toHexadecimalString(digest.digest(signature.toByteArray())); |
| | | } catch(NoSuchAlgorithmException e) { |
| | | // ignore |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | private String toHexadecimalString(byte[] value) { |
| | | StringBuffer sb = new StringBuffer(); |
| | | int len = value.length; |
| | | for (int i = 0; i < len; i++) { |
| | | int num = ((int) value[i]) & 0xff; |
| | | if (num < 0x10) { |
| | | sb.append('0'); |
| | | } |
| | | sb.append(Integer.toHexString(num)); |
| | | if (i < len - 1) { |
| | | sb.append(':'); |
| | | } |
| | | } |
| | | return sb.toString().toUpperCase(Locale.US); |
| | | } |
| | | |
| | | public String execCommand(String ...command) { |
| | | java.lang.Process process=null; |
| | | InputStream errIs=null; |
| | | InputStream inIs=null; |
| | | String result=""; |
| | | |
| | | try { |
| | | process = new ProcessBuilder().command(command).start(); |
| | | |
| | | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| | | int read = -1; |
| | | errIs=process.getErrorStream(); |
| | | while((read=errIs.read())!=-1){ |
| | | baos.write(read); |
| | | } |
| | | inIs=process.getInputStream(); |
| | | while((read=inIs.read())!=-1){ |
| | | baos.write(read); |
| | | } |
| | | result=new String(baos.toByteArray()); |
| | | if(inIs!=null) |
| | | inIs.close(); |
| | | if(errIs!=null) |
| | | errIs.close(); |
| | | process.destroy(); |
| | | } catch (IOException e) { |
| | | result = e.getMessage(); |
| | | } |
| | | return result; |
| | | } |
| | | } |