263 lines
9.7 KiB
Java
263 lines
9.7 KiB
Java
package com.aoleyun.os.TTUtils;
|
|
|
|
import android.app.PendingIntent;
|
|
import android.content.ComponentName;
|
|
import android.content.Context;
|
|
import android.content.Intent;
|
|
import android.content.pm.ApplicationInfo;
|
|
import android.content.pm.PackageInfo;
|
|
import android.content.pm.PackageInstaller;
|
|
import android.content.pm.PackageManager;
|
|
import android.content.pm.ResolveInfo;
|
|
import android.os.Build;
|
|
import android.text.TextUtils;
|
|
import android.util.Log;
|
|
|
|
import androidx.annotation.RequiresApi;
|
|
|
|
import com.aoleyun.os.BuildConfig;
|
|
import com.aoleyun.os.receiver.InstallResultReceiver;
|
|
|
|
import java.io.BufferedReader;
|
|
import java.io.File;
|
|
import java.io.FileInputStream;
|
|
import java.io.IOException;
|
|
import java.io.InputStreamReader;
|
|
import java.io.OutputStream;
|
|
import java.util.List;
|
|
|
|
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;
|
|
}
|
|
|
|
//判断是否为系统应用
|
|
public static boolean isSystemApp(Context context, String pkgName) {
|
|
boolean isSystemApp = false;
|
|
PackageInfo pi = null;
|
|
try {
|
|
PackageManager pm = context.getPackageManager();
|
|
pi = pm.getPackageInfo(pkgName, 0);
|
|
} catch (PackageManager.NameNotFoundException e) {
|
|
e.printStackTrace();
|
|
Log.e("isSystemApp", e.getMessage(), e);
|
|
|
|
}
|
|
|
|
// 是系统中已安装的应用
|
|
if (pi != null) {
|
|
boolean isSysApp = (pi.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 1;
|
|
boolean isSysUpd = (pi.applicationInfo.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) == 1;
|
|
isSystemApp = isSysApp || isSysUpd;
|
|
}
|
|
return isSystemApp;
|
|
}
|
|
|
|
//获取APP版本号
|
|
public static String getAPPVersionName(Context context, String packageName) {
|
|
String versionName = "0";
|
|
|
|
if (TextUtils.isEmpty(packageName)) {
|
|
return versionName;
|
|
}
|
|
PackageManager pm = context.getPackageManager();
|
|
try {
|
|
PackageInfo packageInfo = pm.getPackageInfo(packageName, 0);
|
|
versionName = packageInfo.versionName;
|
|
} catch (PackageManager.NameNotFoundException e) {
|
|
e.printStackTrace();
|
|
}
|
|
return versionName;
|
|
}
|
|
|
|
public static void installApp(Context context, String filePath) {
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
|
|
installAppatPie(context, filePath);
|
|
} else {
|
|
installApps(filePath);
|
|
}
|
|
}
|
|
|
|
public static boolean installApps(String apkPath) {
|
|
Process process = null;
|
|
BufferedReader successResult = null;
|
|
BufferedReader errorResult = null;
|
|
StringBuilder successMsg = new StringBuilder();
|
|
StringBuilder errorMsg = new StringBuilder();
|
|
try {
|
|
process = new ProcessBuilder("pm", "install", "-i", BuildConfig.APPLICATION_ID, "--user", "0", apkPath).start();
|
|
successResult = new BufferedReader(new InputStreamReader(process.getInputStream()));
|
|
errorResult = new BufferedReader(new InputStreamReader(process.getErrorStream()));
|
|
String s;
|
|
while ((s = successResult.readLine()) != null) {
|
|
successMsg.append(s);
|
|
}
|
|
while ((s = errorResult.readLine()) != null) {
|
|
errorMsg.append(s);
|
|
}
|
|
} catch (Exception e) {
|
|
Log.e("installApps1", e.getMessage());
|
|
} finally {
|
|
try {
|
|
if (successResult != null) {
|
|
successResult.close();
|
|
}
|
|
if (errorResult != null) {
|
|
errorResult.close();
|
|
}
|
|
} catch (Exception e) {
|
|
Log.e("installApps2", e.getMessage());
|
|
}
|
|
if (process != null) {
|
|
process.destroy();
|
|
}
|
|
}
|
|
Log.e("result", "" + errorMsg.toString());
|
|
//如果含有“success”认为安装成功
|
|
Log.e("installApp", successMsg.toString());
|
|
// if (!successMsg.toString().equalsIgnoreCase("success")) {
|
|
// ApkUtils.install(context, new File(apkPath));
|
|
// }
|
|
return successMsg.toString().equalsIgnoreCase("success");
|
|
}
|
|
|
|
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
|
|
public static void installAppatPie(Context context, String apkFilePath) {
|
|
File file = new File(apkFilePath);
|
|
PackageInstaller packageInstaller = context.getPackageManager().getPackageInstaller();
|
|
PackageInstaller.SessionParams sessionParams = new PackageInstaller.SessionParams(PackageInstaller
|
|
.SessionParams.MODE_FULL_INSTALL);
|
|
sessionParams.setSize(file.length());
|
|
int sessionId = createSession(packageInstaller, sessionParams);
|
|
if (sessionId != -1) {
|
|
boolean copySuccess = copyApkFile(packageInstaller, sessionId, apkFilePath);
|
|
if (copySuccess) {
|
|
install(packageInstaller, sessionId, context);
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
|
|
private static void install(PackageInstaller packageInstaller, int sessionId, Context context) {
|
|
try {
|
|
PackageInstaller.Session session = packageInstaller.openSession(sessionId);
|
|
Intent intent = new Intent(context, InstallResultReceiver.class);
|
|
PendingIntent pendingIntent = PendingIntent.getBroadcast(
|
|
context,
|
|
1, intent,
|
|
PendingIntent.FLAG_UPDATE_CURRENT
|
|
);
|
|
session.commit(pendingIntent.getIntentSender());
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
|
|
private static int createSession(PackageInstaller packageInstaller, PackageInstaller.SessionParams sessionParams) {
|
|
int sessionId = -1;
|
|
try {
|
|
sessionId = packageInstaller.createSession(sessionParams);
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
return sessionId;
|
|
}
|
|
|
|
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
|
|
private static boolean copyApkFile(PackageInstaller pi, int sessionId, String apkFilePath) {
|
|
boolean success = false;
|
|
File apkFile = new File(apkFilePath);
|
|
PackageInstaller.Session session = null;
|
|
try {
|
|
session = pi.openSession(sessionId);
|
|
OutputStream out = session.openWrite("app.apk", 0, apkFile.length());
|
|
FileInputStream input = new FileInputStream(apkFile);
|
|
int read = 0;
|
|
byte[] buffer = new byte[65536];
|
|
// while (read != -1) {
|
|
// read = input.read(buffer);
|
|
// out.write(buffer, 0, read);
|
|
// }
|
|
while (true) {
|
|
read = input.read(buffer);
|
|
if (read == -1) {
|
|
session.fsync(out);
|
|
success = true;
|
|
out.close();
|
|
input.close();
|
|
break;
|
|
}
|
|
out.write(buffer, 0, read);
|
|
}
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
Log.e("fht", "copyApkFile" + e.getMessage());
|
|
}
|
|
return success;
|
|
}
|
|
|
|
/**
|
|
* 根据包名卸载应用
|
|
*
|
|
* @param packageName
|
|
*/
|
|
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
|
|
public static void uninstall(Context context, String packageName) {
|
|
Intent broadcastIntent = new Intent(context, InstallResultReceiver.class);
|
|
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1,
|
|
broadcastIntent, PendingIntent.FLAG_UPDATE_CURRENT);
|
|
PackageInstaller packageInstaller = context.getPackageManager().getPackageInstaller();
|
|
packageInstaller.uninstall(packageName, pendingIntent.getIntentSender());
|
|
}
|
|
}
|