移除AdminManager具体实现,引入IMdmManager接口以实现平台解耦。 重构MainActivity,通过接口调用MDM功能。 新增honor、huawei、emui产品变体,支持多平台编译。 添加AGENTS.md开发规范文件。
411 lines
13 KiB
Java
411 lines
13 KiB
Java
package com.ttstd.sn.mdm;
|
||
|
||
import android.annotation.SuppressLint;
|
||
import android.app.admin.DevicePolicyManager;
|
||
import android.content.ComponentName;
|
||
import android.content.Context;
|
||
import android.content.pm.PackageManager;
|
||
import android.os.Build;
|
||
import android.text.TextUtils;
|
||
import android.util.Log;
|
||
|
||
import com.huawei.android.app.admin.DeviceApplicationManager;
|
||
import com.huawei.android.app.admin.DeviceControlManager;
|
||
import com.huawei.android.app.admin.DevicePackageManager;
|
||
import com.huawei.android.app.admin.DevicePasswordManager;
|
||
import com.huawei.android.app.admin.DeviceRestrictionManager;
|
||
import com.huawei.android.app.admin.DeviceSettingsManager;
|
||
import com.ttstd.sn.receiver.AoleDeviceAdminReceiver;
|
||
|
||
import java.util.ArrayList;
|
||
import java.util.List;
|
||
|
||
/**
|
||
* 华为 EMUI MDM 实现。
|
||
*
|
||
* <p>依赖 hw_emui_11.0.1_20210408.jar(com.huawei.android.app.admin.*),仅存在于 emui flavor。</p>
|
||
*/
|
||
public class AdminManager implements IMdmManager {
|
||
private static final String TAG = "AdminManager[emui]";
|
||
|
||
@SuppressLint("StaticFieldLeak")
|
||
private static AdminManager sInstance;
|
||
|
||
private Context mContext;
|
||
private ComponentName mAdminName;
|
||
private DevicePolicyManager mDevicePolicyManager;
|
||
/*设备应用程序管理类*/
|
||
private DeviceApplicationManager mDeviceApplicationManager;
|
||
/*设备控制相关的类*/
|
||
private DeviceControlManager mDeviceControlManager;
|
||
/*设备包管理类*/
|
||
private DevicePackageManager mDevicePackageManager;
|
||
/*密码相关类*/
|
||
private DevicePasswordManager mDevicePasswordManager;
|
||
/*禁用/启用管理类*/
|
||
private DeviceRestrictionManager mDeviceRestrictionManager;
|
||
/*设置相关类*/
|
||
private DeviceSettingsManager mDeviceSettingsManager;
|
||
|
||
private AdminManager(Context context) {
|
||
if (context == null) {
|
||
throw new RuntimeException("Context is NULL");
|
||
}
|
||
this.mContext = context;
|
||
this.mAdminName = new ComponentName(context, AoleDeviceAdminReceiver.class);
|
||
this.mDevicePolicyManager = (DevicePolicyManager) mContext.getSystemService(Context.DEVICE_POLICY_SERVICE);
|
||
this.mDeviceApplicationManager = new DeviceApplicationManager();
|
||
this.mDeviceControlManager = new DeviceControlManager();
|
||
this.mDevicePackageManager = new DevicePackageManager();
|
||
this.mDevicePasswordManager = new DevicePasswordManager();
|
||
this.mDeviceRestrictionManager = new DeviceRestrictionManager();
|
||
this.mDeviceSettingsManager = new DeviceSettingsManager();
|
||
removeTrustList();
|
||
}
|
||
|
||
public static void init(Context context) {
|
||
if (sInstance == null) {
|
||
Log.e(TAG, "init: ");
|
||
sInstance = new AdminManager(context);
|
||
}
|
||
}
|
||
|
||
public static AdminManager getInstance() {
|
||
if (sInstance == null) {
|
||
throw new IllegalStateException("You must be init AdminManager first");
|
||
}
|
||
return sInstance;
|
||
}
|
||
|
||
/*============================== 基础能力 ==============================*/
|
||
|
||
@Override
|
||
public ComponentName getAdminName() {
|
||
return mAdminName;
|
||
}
|
||
|
||
@Override
|
||
public boolean isActiveMe() {
|
||
if (mDevicePolicyManager == null) {
|
||
return false;
|
||
} else {
|
||
return mDevicePolicyManager.isAdminActive(mAdminName);
|
||
}
|
||
}
|
||
|
||
@Override
|
||
public String getSerial() {
|
||
String sn = "";
|
||
DevicePolicyManager dpm = (DevicePolicyManager) mContext.getSystemService(Context.DEVICE_POLICY_SERVICE);
|
||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||
try {
|
||
dpm.setPermissionPolicy(mAdminName, DevicePolicyManager.PERMISSION_GRANT_STATE_GRANTED);
|
||
} catch (Exception e) {
|
||
Log.e(TAG, "getSerial: " + e.getMessage());
|
||
}
|
||
}
|
||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||
try {
|
||
sn = Build.getSerial();
|
||
} catch (Exception e) {
|
||
Log.e(TAG, "getSerial: " + e.getMessage());
|
||
}
|
||
}
|
||
// 未获取到 MDM 权限时,Build.getSerial() 可能返回 "unknown",
|
||
// 此时传 "" 给接口,避免获取到不属于本设备的配置
|
||
if ("unknown".equalsIgnoreCase(sn)) {
|
||
sn = "";
|
||
}
|
||
return sn;
|
||
}
|
||
|
||
/*============================== 应用管理 ==============================*/
|
||
|
||
/**
|
||
* 添加阻止某应用启动运行名单(DeviceApplicationManager)
|
||
*/
|
||
@Override
|
||
public void addDisallowedRunningApp(String pkg) {
|
||
if (TextUtils.isEmpty(pkg)) return;
|
||
List<String> packageList = new ArrayList<>();
|
||
packageList.add(pkg);
|
||
try {
|
||
mDeviceApplicationManager.addDisallowedRunningApp(mAdminName, packageList);
|
||
} catch (Exception e) {
|
||
Log.e(TAG, "addDisallowedRunningApp: " + e.getMessage());
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 删除阻止某应用启动运行名单(DeviceApplicationManager)
|
||
*/
|
||
@Override
|
||
public void removeDisallowedRunningApp(String pkg) {
|
||
if (TextUtils.isEmpty(pkg)) return;
|
||
List<String> stringList = new ArrayList<>();
|
||
stringList.add(pkg);
|
||
try {
|
||
mDeviceApplicationManager.removeDisallowedRunningApp(mAdminName, stringList);
|
||
} catch (Exception e) {
|
||
Log.e(TAG, "removeDisallowedRunningApp: " + e.getMessage());
|
||
}
|
||
}
|
||
|
||
/*============================== 系统限制 ==============================*/
|
||
|
||
@Override
|
||
public void setMultiWindowDisabled(boolean disabled) {
|
||
try {
|
||
mDeviceRestrictionManager.setMultiWindowDisabled(mAdminName, disabled);
|
||
} catch (Exception e) {
|
||
Log.e(TAG, "setMultiWindowDisabled: " + e.getMessage());
|
||
}
|
||
}
|
||
|
||
@Override
|
||
public boolean isMultiWindowDisabled() {
|
||
try {
|
||
return mDeviceRestrictionManager.isMultiWindowDisabled(mAdminName);
|
||
} catch (Exception e) {
|
||
Log.e(TAG, "isMultiWindowDisabled: " + e.getMessage());
|
||
}
|
||
return false;
|
||
}
|
||
|
||
@Override
|
||
public void setVoiceAssistantButtonDisabled(boolean disabled) {
|
||
try {
|
||
mDeviceRestrictionManager.setVoiceAssistantButtonDisabled(mAdminName, disabled);
|
||
} catch (Exception e) {
|
||
Log.e(TAG, "setVoiceAssistantButtonDisabled: " + e.getMessage());
|
||
}
|
||
}
|
||
|
||
@Override
|
||
public boolean isVoiceAssistantButtonDisabled() {
|
||
try {
|
||
return mDeviceRestrictionManager.isVoiceAssistantButtonDisabled(mAdminName);
|
||
} catch (Exception e) {
|
||
Log.e(TAG, "isVoiceAssistantButtonDisabled: " + e.getMessage());
|
||
}
|
||
return false;
|
||
}
|
||
|
||
@Override
|
||
public void setUSBDataDisabled(boolean disabled) {
|
||
try {
|
||
mDeviceRestrictionManager.setUSBDataDisabled(mAdminName, disabled);
|
||
} catch (Exception e) {
|
||
Log.e(TAG, "setUSBDataDisabled: " + e.getMessage());
|
||
}
|
||
}
|
||
|
||
@Override
|
||
public boolean isUSBDataDisabled() {
|
||
try {
|
||
return mDeviceRestrictionManager.isUSBDataDisabled(mAdminName);
|
||
} catch (Exception e) {
|
||
Log.e(TAG, "isUSBDataDisabled: " + e.getMessage());
|
||
return false;
|
||
}
|
||
}
|
||
|
||
/*============================== 设备设置 ==============================*/
|
||
|
||
@Override
|
||
public boolean setRestoreFactoryDisabled(boolean disabled) {
|
||
try {
|
||
return mDeviceSettingsManager.setRestoreFactoryDisabled(mAdminName, disabled);
|
||
} catch (Exception e) {
|
||
Log.e(TAG, "setRestoreFactoryDisabled: " + e.getMessage());
|
||
}
|
||
return false;
|
||
}
|
||
|
||
@Override
|
||
public boolean isRestoreFactoryDisabled() {
|
||
try {
|
||
return mDeviceSettingsManager.isRestoreFactoryDisabled(mAdminName);
|
||
} catch (Exception e) {
|
||
Log.e(TAG, "isRestoreFactoryDisabled: " + e.getMessage());
|
||
}
|
||
return false;
|
||
}
|
||
|
||
/*============================== 快捷工具 ==============================*/
|
||
|
||
@Override
|
||
public void setQuickToolsDisabled(boolean disabled) {
|
||
try {
|
||
mDevicePasswordManager.setQuickToolsDisabled(mAdminName, disabled);
|
||
} catch (Exception e) {
|
||
Log.e(TAG, "setQuickToolsDisabled: " + e.getMessage());
|
||
}
|
||
}
|
||
|
||
@Override
|
||
public boolean isQuickToolsDisabled() {
|
||
try {
|
||
return mDevicePasswordManager.isQuickToolsDisabled(mAdminName);
|
||
} catch (Exception e) {
|
||
Log.e(TAG, "isQuickToolsDisabled: " + e.getMessage());
|
||
}
|
||
return false;
|
||
}
|
||
|
||
/*============================== 设备控制 ==============================*/
|
||
|
||
@Override
|
||
public void turnOnUsbDebugMode(boolean on) {
|
||
try {
|
||
mDeviceControlManager.turnOnUsbDebugMode(mAdminName, on);
|
||
} catch (Exception e) {
|
||
Log.e(TAG, "turnOnUsbDebugMode: " + e.getMessage());
|
||
}
|
||
}
|
||
|
||
@Override
|
||
public void setForcedActiveDeviceAdmin(Context activity) {
|
||
try {
|
||
mDeviceControlManager.setForcedActiveDeviceAdmin(mAdminName, activity);
|
||
} catch (Exception e) {
|
||
Log.e(TAG, "setForcedActiveDeviceAdmin: " + e.getMessage());
|
||
}
|
||
}
|
||
|
||
@Override
|
||
public void removeActiveDeviceAdmin() {
|
||
try {
|
||
mDeviceControlManager.removeActiveDeviceAdmin(mAdminName);
|
||
} catch (Exception e) {
|
||
Log.e(TAG, "removeActiveDeviceAdmin: " + e.getMessage());
|
||
}
|
||
}
|
||
|
||
@Override
|
||
public void clearDeviceOwnerApp() {
|
||
try {
|
||
mDeviceControlManager.clearDeviceOwnerApp();
|
||
} catch (Exception e) {
|
||
Log.e(TAG, "clearDeviceOwnerApp: " + e.getMessage());
|
||
}
|
||
}
|
||
|
||
@Override
|
||
public boolean isForcedActiveDeviceAdmin() {
|
||
try {
|
||
return mDeviceControlManager.isForcedActiveDeviceAdmin(mAdminName);
|
||
} catch (Exception e) {
|
||
Log.e(TAG, "isForcedActiveDeviceAdmin: " + e.getMessage());
|
||
}
|
||
return false;
|
||
}
|
||
|
||
/*============================== 平台差异 ==============================*/
|
||
|
||
/**
|
||
* 禁用/启用 YOYO 助手(EMUI 平台映射为 HiVoice 语音助手)
|
||
*/
|
||
@Override
|
||
public void setYoyoDisabled(boolean disable) {
|
||
Log.e(TAG, "setYoyoDisabled: " + disable + " (emui use voice assistant)");
|
||
setVoiceAssistantButtonDisabled(disable);
|
||
}
|
||
|
||
/*============================== EMUI 特有能力 ==============================*/
|
||
|
||
/**
|
||
* 静默激活设备管理员(DeviceControlManager)
|
||
*/
|
||
public void setSilentActiveAdmin() {
|
||
try {
|
||
mDeviceControlManager.setSilentActiveAdmin(mAdminName);
|
||
} catch (Exception e) {
|
||
Log.e(TAG, "setSilentActiveAdmin: " + e.getMessage());
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 设置管控模式(DeviceControlManager)
|
||
*
|
||
* @param mode {@link DeviceControlManager#CONTROL_MODE} 或 {@link DeviceControlManager#RELEASE_MODE}
|
||
*/
|
||
public boolean setControlMode(int mode) {
|
||
try {
|
||
return mDeviceControlManager.setControlMode(mAdminName, mode);
|
||
} catch (Exception e) {
|
||
Log.e(TAG, "setControlMode: " + e.getMessage());
|
||
}
|
||
return false;
|
||
}
|
||
|
||
/**
|
||
* 获取当前管控模式(DeviceControlManager)
|
||
*/
|
||
public int getControlMode() {
|
||
try {
|
||
return mDeviceControlManager.getControlMode(mAdminName);
|
||
} catch (Exception e) {
|
||
Log.e(TAG, "getControlMode: " + e.getMessage());
|
||
}
|
||
return DeviceControlManager.NORMAL_MODE;
|
||
}
|
||
|
||
/**
|
||
* 开启/关闭护眼模式(DeviceControlManager)
|
||
*/
|
||
public boolean turnOnEyeComfort(boolean on) {
|
||
try {
|
||
return mDeviceControlManager.turnOnEyeComfort(mAdminName, on);
|
||
} catch (Exception e) {
|
||
Log.e(TAG, "turnOnEyeComfort: " + e.getMessage());
|
||
}
|
||
return false;
|
||
}
|
||
|
||
/**
|
||
* 查询护眼模式是否开启(DeviceControlManager)
|
||
*/
|
||
public boolean isEyeComfortTurnedOn() {
|
||
try {
|
||
return mDeviceControlManager.isEyeComfortTurnedOn(mAdminName);
|
||
} catch (Exception e) {
|
||
Log.e(TAG, "isEyeComfortTurnedOn: " + e.getMessage());
|
||
}
|
||
return false;
|
||
}
|
||
|
||
/*============================== 初始化辅助 ==============================*/
|
||
|
||
/**
|
||
* 移除安装白名单(构造时调用,清理历史白名单配置)
|
||
*/
|
||
public void removeTrustList() {
|
||
try {
|
||
List<String> trustList = mDevicePackageManager.getInstallPackageWhiteList(mAdminName);
|
||
Log.e(TAG, "removeTrustList: " + trustList);
|
||
if (trustList != null && trustList.size() != 0) {
|
||
mDevicePackageManager.removeInstallPackageWhiteList(mAdminName, trustList);
|
||
}
|
||
} catch (Exception e) {
|
||
Log.e(TAG, "removeTrustList: " + e.getMessage());
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 判断应用是否已安装
|
||
*/
|
||
private boolean isAvailable(Context context, String pkg) {
|
||
if (TextUtils.isEmpty(pkg)) return false;
|
||
try {
|
||
PackageManager pm = context.getPackageManager();
|
||
pm.getPackageInfo(pkg, 0);
|
||
return true;
|
||
} catch (PackageManager.NameNotFoundException e) {
|
||
return false;
|
||
} catch (Exception e) {
|
||
return false;
|
||
}
|
||
}
|
||
}
|