update:2020.10.21
fix:蓝牙文件传输管控改为蓝牙开关管控 add:摄像头管控,影音格式管控,电话功能管控
This commit is contained in:
@@ -1,29 +1,35 @@
|
||||
package com.info.sn.utils;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.PendingIntent;
|
||||
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.net.Uri;
|
||||
import android.os.Binder;
|
||||
import android.os.Build;
|
||||
import android.provider.Settings;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.annotation.RequiresApi;
|
||||
import androidx.core.content.FileProvider;
|
||||
|
||||
|
||||
import com.info.sn.BuildConfig;
|
||||
import com.info.sn.R;
|
||||
import com.info.sn.receiver.InstallResultReceiver;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
@@ -113,15 +119,17 @@ public class ApkUtils {
|
||||
}
|
||||
|
||||
/**
|
||||
* 卸载一个app
|
||||
* 根据包名卸载应用
|
||||
*
|
||||
* @param packageName
|
||||
*/
|
||||
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
|
||||
public static void uninstall(Context context, String packageName) {
|
||||
//通过程序的包名创建URI
|
||||
Uri packageURI = Uri.parse("package:" + packageName);
|
||||
//创建Intent意图
|
||||
Intent intent = new Intent(Intent.ACTION_DELETE, packageURI);
|
||||
//执行卸载程序
|
||||
context.startActivity(intent);
|
||||
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());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -337,7 +345,15 @@ public class ApkUtils {
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean installApp(Context context, String apkPath) {
|
||||
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) {
|
||||
ToastUtil.show("正在安装应用...");
|
||||
Process process = null;
|
||||
BufferedReader successResult = null;
|
||||
@@ -356,7 +372,7 @@ public class ApkUtils {
|
||||
errorMsg.append(s);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
|
||||
Log.e("installApps1", e.getMessage());
|
||||
} finally {
|
||||
try {
|
||||
if (successResult != null) {
|
||||
@@ -366,7 +382,7 @@ public class ApkUtils {
|
||||
errorResult.close();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
|
||||
Log.e("installApps2", e.getMessage());
|
||||
}
|
||||
if (process != null) {
|
||||
process.destroy();
|
||||
@@ -381,6 +397,92 @@ public class ApkUtils {
|
||||
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) {
|
||||
ToastUtil.show("正在安装应用");
|
||||
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;
|
||||
}
|
||||
|
||||
public static void uninstallApp(Context context, String packageName) {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
ApkUtils.uninstall(context, packageName);
|
||||
} else {
|
||||
ApkUtils.deleteApkInSilence(packageName);
|
||||
}
|
||||
}
|
||||
|
||||
public static void deleteApkInSilence(String packageName) {
|
||||
Class<?> pmService;
|
||||
Class<?> activityTherad;
|
||||
@@ -405,6 +507,7 @@ public class ApkUtils {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static Class<?>[] getParamTypes(Class<?> cls, String mName) {
|
||||
Class<?> cs[] = null;
|
||||
Method[] mtd = cls.getMethods();
|
||||
@@ -498,4 +601,46 @@ public class ApkUtils {
|
||||
context.startActivity(intent);
|
||||
}
|
||||
|
||||
public static void hideSystemSettingAPP(Context context, String pkage) {
|
||||
int hide = 0;
|
||||
//后台0显示,1隐藏
|
||||
try {
|
||||
if (pkage.equalsIgnoreCase("com.mediatek.camera")) {
|
||||
if (Settings.System.getInt(context.getContentResolver(), "qch_app_camera") == 1) {
|
||||
hide = 1;
|
||||
}
|
||||
} else if (pkage.equalsIgnoreCase("com.android.deskclock")) {
|
||||
if (Settings.System.getInt(context.getContentResolver(), "qch_app_deskclock") == 1) {
|
||||
hide = 1;
|
||||
}
|
||||
} else if (pkage.equalsIgnoreCase("com.android.soundrecorder")) {
|
||||
if (Settings.System.getInt(context.getContentResolver(), "qch_app_soundrecorder") == 1) {
|
||||
hide = 1;
|
||||
}
|
||||
} else if (pkage.equalsIgnoreCase("com.android.music")) {
|
||||
if (Settings.System.getInt(context.getContentResolver(), "qch_app_music") == 1) {
|
||||
hide = 1;
|
||||
}
|
||||
} else if (pkage.equalsIgnoreCase("com.android.gallery3d")) {
|
||||
if (Settings.System.getInt(context.getContentResolver(), "qch_app_gallery") == 1) {
|
||||
hide = 1;
|
||||
}
|
||||
} else if (pkage.equalsIgnoreCase("com.android.documentsui")
|
||||
|| pkage.equalsIgnoreCase("com.mediatek.filemanager")) {
|
||||
if (Settings.System.getInt(context.getContentResolver(), "qch_app_filemanager") == 1) {
|
||||
hide = 1;
|
||||
}
|
||||
}
|
||||
} catch (Settings.SettingNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
PackageManager pm = context.getPackageManager();
|
||||
if (hide == 0) {
|
||||
pm.setApplicationEnabledSetting(pkage, PackageManager.COMPONENT_ENABLED_STATE_DEFAULT, 0);
|
||||
} else {
|
||||
pm.setApplicationEnabledSetting(pkage, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user