| | |
| | | package com.anyun.exam.lib; |
| | | |
| | | import android.Manifest; |
| | | import android.content.Context; |
| | | import android.content.Intent; |
| | | import android.content.pm.PackageInfo; |
| | | import android.content.pm.PackageManager; |
| | | import android.content.pm.Signature; |
| | | import android.net.Uri; |
| | | import android.os.Build; |
| | | import android.provider.Settings; |
| | | import android.util.Log; |
| | | |
| | | import java.io.ByteArrayOutputStream; |
| | |
| | | import java.security.NoSuchAlgorithmException; |
| | | import java.util.List; |
| | | import java.util.Locale; |
| | | |
| | | import androidx.annotation.NonNull; |
| | | import androidx.core.app.ActivityCompat; |
| | | import androidx.core.content.FileProvider; |
| | | |
| | | public class InstallUtil { |
| | | public static final String TAG = InstallUtil.class.getCanonicalName(); |
| | |
| | | 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()); |
| | | 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()) ) { |
| | | if ( getFingerprint(sig[0], "SHA-1").equals(getFingerprint(sig2[0], "SHA-1"))) { |
| | | if ( getVerCode() < getVersionCodeFromApk(context, file.getAbsolutePath()) || |
| | | (getVerCode() == getVersionCodeFromApk(context, file.getAbsolutePath()) && |
| | | !getVerName().equals(getVersionNameFromApk(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 { |
| | | } else { |
| | | file.delete(); |
| | | }*/ |
| | | } |
| | | } catch (Exception e) { |
| | | Log.e(TAG, "安装发生错误 " + e.getMessage()); |
| | | } |
| | | } |
| | | } |
| | | |
| | | public void InstallAppNormal(Context context, String path) { |
| | | File file = new File(path); |
| | | Log.d(TAG, "ManualInstall " + path); |
| | | |
| | | if (file.exists() && file.isFile()) { |
| | | Signature[] sig = getSignature(); |
| | | Signature[] sig2 = getSignature(file.getAbsolutePath()); |
| | | |
| | | if (getFingerprint(sig[0], "SHA-1").equals(getFingerprint(sig2[0], "SHA-1")) && |
| | | (getVerCode() < getVersionCodeFromApk(context, file.getAbsolutePath()) || |
| | | (getVerCode() == getVersionCodeFromApk(context, file.getAbsolutePath()) && |
| | | !getVerName().equals(getVersionNameFromApk(context, file.getAbsolutePath()))))) { |
| | | |
| | | Intent intent = new Intent(Intent.ACTION_VIEW); |
| | | Uri data; |
| | | String type = "application/vnd.android.package-archive"; |
| | | if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) { |
| | | data = Uri.fromFile(file); |
| | | } else { |
| | | intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); |
| | | String authority = context.getPackageName() + ".fileProvider"; |
| | | data = FileProvider.getUriForFile(context, authority, file); |
| | | } |
| | | intent.setDataAndType(data, type); |
| | | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| | | context.startActivity(intent); |
| | | } else { |
| | | Log.d(TAG, "File illegal"); |
| | | } |
| | | } else { |
| | | Log.d(TAG, "File not exist"); |
| | | } |
| | | } |
| | | |
| | |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | /* |
| | | private void checkIsAndroidO() { |
| | | if (Build.VERSION.SDK_INT >= 26) { |
| | | if (context.getPackageManager().canRequestPackageInstalls()) { |
| | | mMainPresenter.installApk(); |
| | | } else { |
| | | //请求安装未知应用来源的权限 |
| | | ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.REQUEST_INSTALL_PACKAGES},INSTALL_PACKAGES_REQUEST_CODE); |
| | | } |
| | | } else { |
| | | mMainPresenter.installApk(); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { |
| | | super.onRequestPermissionsResult(requestCode, permissions, grantResults); |
| | | switch (requestCode) { |
| | | case INSTALL_PACKAGES_REQUEST_CODE: |
| | | if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { |
| | | mMainPresenter.installApk(); |
| | | } else { |
| | | // 引导用户手动开启安装权限 |
| | | Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES); |
| | | startActivityForResult(intent, GET_UNKNOWN_APP_SOURCES); |
| | | } |
| | | break; |
| | | default: |
| | | break; |
| | | |
| | | } |
| | | }*/ |
| | | } |