version:1.4

update:2021-10-21 10:14:40
fix:
add:增加浏览器桌面检测升级,修复重复请求
This commit is contained in:
2021-12-03 14:33:03 +08:00
parent 768519e14e
commit 34fa9fcdb7
70 changed files with 3189 additions and 1503 deletions

View File

@@ -2,6 +2,7 @@ package com.aoleyun.sn.utils;
import android.app.ActivityManager;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
@@ -22,6 +23,7 @@ import androidx.annotation.RequiresApi;
import androidx.core.content.FileProvider;
import android.text.TextUtils;
import android.text.format.Formatter;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
@@ -83,7 +85,7 @@ public class ApkUtils {
return false;
}
} catch (Exception e) {
Logutils.e("*** DEBUG ***", "Unexpected error - Here is what I know: "
Log.e("*** DEBUG ***", "Unexpected error - Here is what I know: "
+ e.getMessage());
return false;
} finally {
@@ -116,6 +118,58 @@ public class ApkUtils {
}
}
public static boolean openPackage(Context context, String packageName) {
Context pkgContext = getPackageContext(context, packageName);
Intent intent = getAppOpenIntentByPackageName(context, packageName);
if (pkgContext != null && intent != null) {
pkgContext.startActivity(intent);
return true;
}
return false;
}
public static Context getPackageContext(Context context, String packageName) {
Context pkgContext = null;
if (context.getPackageName().equals(packageName)) {
pkgContext = context;
} else {
// 创建第三方应用的上下文环境
try {
pkgContext = context.createPackageContext(packageName,
Context.CONTEXT_IGNORE_SECURITY
| Context.CONTEXT_INCLUDE_CODE);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
}
return pkgContext;
}
public static Intent getAppOpenIntentByPackageName(Context context, String packageName) {
//Activity完整名
String mainAct = null;
//根据包名寻找
PackageManager pkgMag = context.getPackageManager();
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED | Intent.FLAG_ACTIVITY_NEW_TASK);
List<ResolveInfo> list = pkgMag.queryIntentActivities(intent,
PackageManager.GET_ACTIVITIES);
for (int i = 0; i < list.size(); i++) {
ResolveInfo info = list.get(i);
if (info.activityInfo.packageName.equals(packageName)) {
mainAct = info.activityInfo.name;
break;
}
}
if (TextUtils.isEmpty(mainAct)) {
return null;
}
intent.setComponent(new ComponentName(packageName, mainAct));
return intent;
}
/**
* 安装一个apk文件
*/
@@ -228,20 +282,19 @@ public class ApkUtils {
public static void installRx(final Context context, final String packageName, final String filePath) {
Observable.create(new ObservableOnSubscribe<Integer>() {
@Override
public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {
File file = new File(filePath);
if (TextUtils.isEmpty(filePath) || !file.exists()) {
Logutils.e("installRx", "filePath is empty");
Log.e("installRx", "filePath is empty");
emitter.onNext(0);
return;
}
// String[] args = { "pm", "install", "-r", filePath };
String[] args = {"pm", "install", "-i", "com.colorflykids", "--user", "0", filePath};
// String argss = "pm install -i " + "com.colorflykids" + " --user 0 " + filePath;
Logutils.e("installRx", "argss====" + args);
Log.e("installRx", "argss====" + args);
ProcessBuilder processBuilder = new ProcessBuilder(args);
Process process = null;
BufferedReader successResult = null;
@@ -254,15 +307,15 @@ public class ApkUtils {
errorResult = new BufferedReader(new InputStreamReader(process.getErrorStream()));
String s;
while ((s = successResult.readLine()) != null) {
Logutils.e("mjhseng", "successResult----------" + s);
Log.e("mjhseng", "successResult----------" + s);
successMsg.append(s);
}
while ((s = errorResult.readLine()) != null) {
Logutils.e("mjhseng", "errorResult----------" + s);
Log.e("mjhseng", "errorResult----------" + s);
errorMsg.append(s);
}
} catch (IOException e1) {
Logutils.e("installRx", "IOException e1)----------" + e1.toString());
Log.e("installRx", "IOException e1)----------" + e1.toString());
e1.printStackTrace();
} finally {
try {
@@ -273,7 +326,7 @@ public class ApkUtils {
errorResult.close();
}
} catch (IOException e1) {
Logutils.e("installRx", "IOException e11)---------" + e1.toString());
Log.e("installRx", "IOException e11)---------" + e1.toString());
e1.printStackTrace();
}
if (process != null) {
@@ -283,7 +336,7 @@ public class ApkUtils {
if (successMsg.toString().contains("Success") || successMsg.toString().contains("success")) {
emitter.onNext(2);
} else {
Logutils.e("installRx", "errormesg :" + errorMsg.toString());
Log.e("installRx", "errormesg :" + errorMsg.toString());
emitter.onNext(1);
}
}
@@ -300,10 +353,10 @@ public class ApkUtils {
if (value == 2) {
//安装成功
ToastUtil.show("安装成功");
Logutils.e("installRx", "-----------安装成功-----------");
Log.e("installRx", "-----------安装成功-----------");
} else {
//安装错误
Logutils.e("installRx", "------------安装错误-----------");
Log.e("installRx", "------------安装错误-----------");
ToastUtil.show("安装失败");
}
}
@@ -325,7 +378,7 @@ public class ApkUtils {
}
public static void installApp(Context context, String filePath) {
Logutils.e(TAG, "installApp: " + filePath);
Log.e(TAG, "installApp: " + filePath);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
installAppatPie(context, filePath);
} else {
@@ -368,9 +421,9 @@ public class ApkUtils {
process.destroy();
}
}
Logutils.e("result", "" + errorMsg.toString());
Log.e("result", "" + errorMsg.toString());
//如果含有“success”认为安装成功
Logutils.e("installApp", successMsg.toString());
Log.e("installApp", successMsg.toString());
// if (!successMsg.toString().equalsIgnoreCase("success")) {
// ApkUtils.install(context, new File(apkPath));
// }
@@ -451,7 +504,7 @@ public class ApkUtils {
}
} catch (IOException e) {
e.printStackTrace();
Logutils.e("fht", "copyApkFile" + e.getMessage());
Log.e("fht", "copyApkFile" + e.getMessage());
}
return success;
}
@@ -474,16 +527,16 @@ public class ApkUtils {
method.invoke(PackageManagerService, installPath, null, 0x00000040, packageName, getUserId(Binder.getCallingUid()));//getUserId
} catch (ClassNotFoundException e) {
e.printStackTrace();
Logutils.e("installApkInSilence", "ClassNotFoundException:" + e.getMessage());
Log.e("installApkInSilence", "ClassNotFoundException:" + e.getMessage());
} catch (NoSuchMethodException e) {
e.printStackTrace();
Logutils.e("installApkInSilence", "NoSuchMethodException:" + e.getMessage());
Log.e("installApkInSilence", "NoSuchMethodException:" + e.getMessage());
} catch (IllegalAccessException e) {
e.printStackTrace();
Logutils.e("installApkInSilence", "IllegalAccessException:" + e.getMessage());
Log.e("installApkInSilence", "IllegalAccessException:" + e.getMessage());
} catch (InvocationTargetException e) {
e.printStackTrace();
Logutils.e("installApkInSilence", "InvocationTargetException:" + e.getMessage());
Log.e("installApkInSilence", "InvocationTargetException:" + e.getMessage());
}
}
@@ -521,7 +574,7 @@ public class ApkUtils {
Observable.create(new ObservableOnSubscribe<String>() {
@Override
public void subscribe(ObservableEmitter<String> emitter) throws Exception {
Logutils.e("UninstallAPP", "call " + Thread.currentThread().getName());
Log.e("UninstallAPP", "call " + Thread.currentThread().getName());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
ApkUtils.uninstall(context, pkg);
} else {
@@ -534,23 +587,23 @@ public class ApkUtils {
.subscribe(new Observer<String>() {
@Override
public void onSubscribe(Disposable d) {
Logutils.e("UninstallAPP", "onSubscribe: ");
Log.e("UninstallAPP", "onSubscribe: ");
}
@Override
public void onNext(String s) {
Logutils.e("UninstallAPP", "onNext " + Thread.currentThread().getName());
Logutils.e("UninstallAPP", "onNext: " + s);
Log.e("UninstallAPP", "onNext " + Thread.currentThread().getName());
Log.e("UninstallAPP", "onNext: " + s);
}
@Override
public void onError(Throwable e) {
Logutils.e("UninstallAPP", "onError: " + e.getMessage());
Log.e("UninstallAPP", "onError: " + e.getMessage());
}
@Override
public void onComplete() {
Logutils.e("UninstallAPP", "onComplete: ");
Log.e("UninstallAPP", "onComplete: ");
}
});
@@ -642,13 +695,13 @@ public class ApkUtils {
List<ApplicationInfo> appInfos = pm.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES);// GET_UNINSTALLED_PACKAGES代表已删除但还有安装目录的
List<String> applicationInfos = new ArrayList<>();
for (ApplicationInfo app : appInfos) {
// Logutils.e("queryFilterAppInfo", String.valueOf(app.flags));
// Logutils.e("queryFilterAppInfo", String.valueOf((app.flags & mask)));
// Log.e("queryFilterAppInfo", String.valueOf(app.flags));
// Log.e("queryFilterAppInfo", String.valueOf((app.flags & mask)));
if ((app.flags & ApplicationInfo.FLAG_SYSTEM) == 1) {
//通过flag排除系统应用会将电话、短信也排除掉
} else {
applicationInfos.add(app.packageName);
Logutils.e("queryFilterAppInfo", app.packageName);
Log.e("queryFilterAppInfo", app.packageName);
}
}
return applicationInfos;
@@ -691,7 +744,7 @@ public class ApkUtils {
//根据需求内置
this.add("com.easyclient.activity");//移动课堂
this.add("com.jiandan.mobilelesson");//简单课堂
this.add("com.jiaoguanyi.store");//教官壹
this.add(PackageNames.APPSTORE);//教官壹
//展讯
this.add("com.android.calculator2");//计算器
this.add("com.android.email");//电子邮件
@@ -704,9 +757,19 @@ public class ApkUtils {
public static List<String> desktopAPP = new ArrayList<String>() {{
this.add("com.uiuios.jgy1");
this.add("com.uiuios.jgy2");
// this.add("com.aoleyunos.dop1");
// this.add("com.aoleyunos.dop2");
this.add("com.android.uiuios");
}};
public static List<String> aoleyunAPP = new ArrayList<String>() {{
this.add("com.aoleyun.appstore");
this.add("com.aoleyun.sn");
this.add("com.aoleyun.info");
this.add("com.aoleyun.os");
this.add("com.uiui.browser");
}};
//出厂自带的app
public static List<String> factoryapp = new ArrayList<String>() {{
this.add("com.android.fmradio");
@@ -903,7 +966,7 @@ public class ApkUtils {
try {
pm.setApplicationEnabledSetting(packageInfo.packageName, PackageManager.COMPONENT_ENABLED_STATE_DEFAULT, 0);
} catch (Exception e) {
Logutils.e(TAG, "showAllAPP: " + e.getMessage());
Log.e(TAG, "showAllAPP: " + e.getMessage());
}
}
}
@@ -912,7 +975,7 @@ public class ApkUtils {
try {
pm.setApplicationEnabledSetting(packageInfo.packageName, PackageManager.COMPONENT_ENABLED_STATE_DEFAULT, 0);
} catch (Exception e) {
Logutils.e(TAG, "showAllAPP: " + e.getMessage());
Log.e(TAG, "showAllAPP: " + e.getMessage());
}
hideSystemSettingAPP(context, packageInfo.packageName);
}
@@ -974,7 +1037,7 @@ public class ApkUtils {
PackageManager pm = context.getPackageManager();
pi = pm.getPackageInfo(pkgName, 0);
} catch (PackageManager.NameNotFoundException e) {
Logutils.e("isSystemApp: NameNotFoundException:", e.getMessage());
Log.e("isSystemApp: NameNotFoundException:", e.getMessage());
}
// 是系统中已安装的应用
if (pi != null) {
@@ -1007,7 +1070,7 @@ public class ApkUtils {
// 通过getPackageManager()的queryIntentActivities方法遍历
List<ResolveInfo> resolveinfoList = pm.queryIntentActivities(resolveIntent, 0);
for (ResolveInfo resolveInfo : resolveinfoList) {
Logutils.d("", "resolveInfo:" + resolveInfo);
Log.d("", "resolveInfo:" + resolveInfo);
}
ResolveInfo resolveinfo = resolveinfoList.iterator().next();
if (resolveinfo != null) {
@@ -1045,11 +1108,11 @@ public class ApkUtils {
Settings.System.putString(context.getContentResolver(), "qch_launcher_icon_app", "");
return;
}
Logutils.e("addShortcut", "addShortcut: " + result);
Log.e("addShortcut", "addShortcut: " + result);
String[] stringList = result.split(",");
HashSet<String> packages = new HashSet<>(Arrays.asList(stringList));
String romapps = Settings.System.getString(context.getContentResolver(), "jgy_customromapp");
Logutils.e(TAG, "addShortcut: romapps: " + romapps);
Log.e(TAG, "addShortcut: romapps: " + romapps);
HashSet<String> appSet = new HashSet<>();
if (!TextUtils.isEmpty(romapps)) {
appSet = new HashSet<>(Arrays.asList(romapps.split(",")));
@@ -1079,23 +1142,23 @@ public class ApkUtils {
installedListBuilder.append(",");
}
installedListBuilder.append(s);
Logutils.i("addShortcut", "packages: " + s);
Log.i("addShortcut", "packages: " + s);
}
String installedList = installedListBuilder.toString();
boolean qch_force_app = Settings.System.putString(context.getContentResolver(), "qch_launcher_icon_app", installedList);
// String old = Settings.System.getString(context.getContentResolver(), "qch_launcher_icon_app");
// Logutils.e("addShortcut", old);
Logutils.e("addShortcut", "installedList:" + installedList);
Logutils.e("addShortcut", "putstring:" + qch_force_app);
// Log.e("addShortcut", old);
Log.e("addShortcut", "installedList:" + installedList);
Log.e("addShortcut", "putstring:" + qch_force_app);
}
public static void getAppInfo(Context context) {
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningServiceInfo> infoList = activityManager.getRunningServices(Integer.MAX_VALUE);
for (ActivityManager.RunningServiceInfo info : infoList) {
// Logutils.e("fht", "getAppInfo1: "+info.process);
// Logutils.e("fht", "getAppInfo2: "+info.service.getPackageName());
// Logutils.e("fht", "getAppInfo3: "+info.service.getClassName());
// Log.e("fht", "getAppInfo1: "+info.process);
// Log.e("fht", "getAppInfo2: "+info.service.getPackageName());
// Log.e("fht", "getAppInfo3: "+info.service.getClassName());
}
ArrayList<UploadAppInfo> appList = new ArrayList<UploadAppInfo>(); //用来存储获取的应用信息数据
List<PackageInfo> packages = context.getPackageManager().getInstalledPackages(0);
@@ -1104,6 +1167,7 @@ public class ApkUtils {
//排除桌面app和出厂自带app
if (desktopAPP.contains(packageName)
|| factoryapp.contains(packageName)
// || aoleyunAPP.contains(packageName)
) {
continue;
}
@@ -1134,7 +1198,7 @@ public class ApkUtils {
uploadAppInfo.setApp_name(packageInfo.applicationInfo.loadLabel(context.getPackageManager()).toString());
uploadAppInfo.setPackage_name(packageInfo.packageName);
Logutils.e("getAppInfo", "getAppInfo:" + packageInfo.packageName);
Log.e("getAppInfo", "getAppInfo:" + packageInfo.packageName);
// uploadAppInfo.setId(i);
// String firstInstallTime = Utils.transferLongToDate(packageInfo.firstInstallTime);
// String lastUpdateTime = Utils.transferLongToDate(packageInfo.lastUpdateTime);
@@ -1143,18 +1207,20 @@ public class ApkUtils {
uploadAppInfo.setVersionCode(String.valueOf(packageInfo.versionCode));
uploadAppInfo.setState(0);
uploadAppInfo.setVersionName(packageInfo.versionName);
String appInstallDir = packageInfo.applicationInfo.publicSourceDir;
int size = Integer.valueOf((int) new File(appInstallDir).length());
uploadAppInfo.setApp_size(Formatter.formatFileSize(context, size));
for (ActivityManager.RunningServiceInfo info : infoList) {
if (info.process.contains(packageInfo.packageName)) {
uploadAppInfo.setState(1);
Logutils.e("getAppInfo", "getAppInfo running: " + packageInfo.packageName);
Log.e("getAppInfo", "getAppInfo running: " + packageInfo.packageName);
}
}
appList.add(uploadAppInfo);
}
Gson gson = new Gson();
String jsonString = gson.toJson(appList);
Logutils.e(TAG, "getAppInfo: " + jsonString);
Log.e(TAG, "getAppInfo: " + jsonString);
NetInterfaceManager.getInstance()
.getUploadAppInfoApi()
@@ -1171,7 +1237,7 @@ public class ApkUtils {
@Override
public void onNext(ResponseBody responseBody) {
try {
Logutils.e("getAppInfo", "上传的结果" + responseBody.string());
Log.e("getAppInfo", "上传的结果" + responseBody.string());
} catch (IOException e) {
e.printStackTrace();
}
@@ -1179,12 +1245,12 @@ public class ApkUtils {
@Override
public void onError(Throwable e) {
Logutils.e("getAppInfo", "UploadAppInfoApi onError: " + e.getMessage());
Log.e("getAppInfo", "UploadAppInfoApi onError: " + e.getMessage());
}
@Override
public void onComplete() {
Logutils.e("getAppInfo", "onComplete: ");
Log.e("getAppInfo", "onComplete: ");
}
});
}
@@ -1202,7 +1268,7 @@ public class ApkUtils {
String extendField = Aria.download(this).load(id).getExtendField();
JSONObject jsonObject = JSON.parseObject(extendField);
if (packageName.equals(jsonObject.getString("app_package"))) {
Logutils.e("RemoveTask", "subscribe: " + "删除文件:" + entity.getFilePath());
Log.e("RemoveTask", "subscribe: " + "删除文件:" + entity.getFilePath());
Aria.download(this).load(id).cancel(true);
}
}
@@ -1213,22 +1279,22 @@ public class ApkUtils {
.subscribe(new Observer<String>() {
@Override
public void onSubscribe(Disposable d) {
Logutils.e("RemoveTask", "onSubscribe: ");
Log.e("RemoveTask", "onSubscribe: ");
}
@Override
public void onNext(String s) {
Logutils.e("RemoveTask", "onNext: ");
Log.e("RemoveTask", "onNext: ");
}
@Override
public void onError(Throwable e) {
Logutils.e("RemoveTask", "onError: " + e.getMessage());
Log.e("RemoveTask", "onError: " + e.getMessage());
}
@Override
public void onComplete() {
Logutils.e("RemoveTask", "onComplete: ");
Log.e("RemoveTask", "onComplete: ");
}
});