refactor(mdm): 抽象IMdmManager接口并支持多平台构建
移除AdminManager具体实现,引入IMdmManager接口以实现平台解耦。 重构MainActivity,通过接口调用MDM功能。 新增honor、huawei、emui产品变体,支持多平台编译。 添加AGENTS.md开发规范文件。
This commit is contained in:
5
AGENTS.md
Normal file
5
AGENTS.md
Normal file
@@ -0,0 +1,5 @@
|
||||
# AGENTS.md — ElderlyDialerMdm
|
||||
|
||||
本文件是 `ElderlyDialerMdm/` 工程的 **AI 协作规则与开发规范**
|
||||
## 1.所有ai生成的临时文件全部放在app/build/temp 目录下
|
||||
## 2.所有实现IMdmManager接口的类,在调用以Manager结尾的类的方法时,都要加入try-catch块,防止异常
|
||||
@@ -37,9 +37,6 @@ android {
|
||||
//选择要添加的对应 cpu 类型的 .so 库。
|
||||
abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86'
|
||||
}
|
||||
|
||||
buildConfigField "String", "platform", '"honor"'
|
||||
|
||||
}
|
||||
|
||||
compileOptions {
|
||||
@@ -53,6 +50,25 @@ android {
|
||||
aidl true
|
||||
}
|
||||
|
||||
// 平台维度:honor(荣耀 MagicOS)/ huawei(华为 MDM Kit)/ emui(华为 EMUI)
|
||||
flavorDimensions "platform"
|
||||
productFlavors {
|
||||
honor {
|
||||
dimension "platform"
|
||||
versionNameSuffix "_honor"
|
||||
buildConfigField "String", "PLATFORM", '"honor"'
|
||||
}
|
||||
huawei {
|
||||
dimension "platform"
|
||||
versionNameSuffix "_huawei"
|
||||
buildConfigField "String", "PLATFORM", '"huawei"'
|
||||
}
|
||||
emui {
|
||||
dimension "platform"
|
||||
versionNameSuffix "_emui"
|
||||
buildConfigField "String", "PLATFORM", '"emui"'
|
||||
}
|
||||
}
|
||||
|
||||
buildTypes {
|
||||
debug {
|
||||
@@ -73,17 +89,27 @@ android {
|
||||
applicationVariants.all { variant ->
|
||||
variant.outputs.each { output ->
|
||||
def buildType = variant.buildType.name
|
||||
def flavorName = variant.flavorName
|
||||
if (buildType.contains("debug")) {
|
||||
output.outputFileName = "app_debug.apk"
|
||||
output.outputFileName = "app_${flavorName}_debug.apk"
|
||||
} else {
|
||||
output.outputFileName = "${appName()}_${variant.versionCode}_V${variant.versionName}_${releaseTime()}_${buildType}.apk"
|
||||
output.outputFileName = "${appName()}_${flavorName}_${variant.versionCode}_V${variant.versionName}_${releaseTime()}_${buildType}.apk"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
compileOnly files('libs/magic-android-34.38.1.200.jar')
|
||||
// 荣耀 MDM SDK(compileOnly,仅编译期)
|
||||
honorCompileOnly files('libs/magic-android-34.38.1.200.jar')
|
||||
|
||||
// 华为 MDM Kit(compileOnly,仅编译期)
|
||||
huaweiCompileOnly 'com.huawei.mdm:mdmkit:11.1.0.300'
|
||||
// 华为 HEM License SDK(可选,需要授权校验时放开)
|
||||
//huaweiImplementation 'com.huawei.hms:hemsdk:1.0.4.303'
|
||||
|
||||
// 华为 EMUI MDM SDK(compileOnly,仅编译期)
|
||||
emuiCompileOnly files('libs/hw_emui_11.0.1_20210408.jar')
|
||||
|
||||
implementation 'androidx.appcompat:appcompat:1.3.1'
|
||||
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
|
||||
|
||||
BIN
app/libs/hw_emui_11.0.1_20210408.jar
Normal file
BIN
app/libs/hw_emui_11.0.1_20210408.jar
Normal file
Binary file not shown.
27
app/src/emui/AndroidManifest.xml
Normal file
27
app/src/emui/AndroidManifest.xml
Normal file
@@ -0,0 +1,27 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="com.ttstd.sn">
|
||||
|
||||
<!--华为 EMUI MDM 权限-->
|
||||
<uses-permission android:name="com.huawei.permission.sec.MDM" />
|
||||
<uses-permission android:name="com.huawei.permission.sec.MDM_USB" />
|
||||
<uses-permission android:name="com.huawei.permission.sec.MDM_WIFI" />
|
||||
<uses-permission android:name="com.huawei.permission.sec.MDM_PHONE" />
|
||||
<uses-permission android:name="com.huawei.permission.sec.MDM_PHONE_MANAGER" />
|
||||
<uses-permission android:name="com.huawei.permission.sec.MDM_SETTINGS_RESTRICTION" />
|
||||
<uses-permission android:name="com.huawei.permission.sec.MDM_APP_MANAGEMENT" />
|
||||
<uses-permission android:name="com.huawei.permission.sec.MDM_DEVICE_MANAGER" />
|
||||
<uses-permission android:name="com.huawei.permission.sec.SDK_LAUNCHER" />
|
||||
<uses-permission android:name="com.huawei.permission.sec.MDM_CAPTURE_SCREEN" />
|
||||
<uses-permission android:name="com.huawei.permission.sec.MDM_SDCARD" />
|
||||
<uses-permission android:name="com.huawei.permission.sec.MDM_NETWORK_MANAGER" />
|
||||
<uses-permission android:name="com.huawei.permission.sec.MDM_NFC" />
|
||||
<uses-permission android:name="com.huawei.permission.sec.MDM_BLUETOOTH" />
|
||||
<uses-permission android:name="com.huawei.permission.sec.MDM_CAMERA" />
|
||||
<uses-permission android:name="com.huawei.permission.sec.MDM_CONNECTIVITY" />
|
||||
<uses-permission android:name="com.huawei.permission.sec.MDM_MMS" />
|
||||
<uses-permission android:name="com.huawei.permission.sec.MDM_TELEPHONY" />
|
||||
<uses-permission android:name="com.huawei.permission.sec.MDM_DEVICE_OWNER" />
|
||||
<uses-permission android:name="com.huawei.systemmanager.permission.ACCESS_INTERFACE" />
|
||||
|
||||
</manifest>
|
||||
410
app/src/emui/java/com/ttstd/sn/mdm/AdminManager.java
Normal file
410
app/src/emui/java/com/ttstd/sn/mdm/AdminManager.java
Normal file
@@ -0,0 +1,410 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
28
app/src/honor/AndroidManifest.xml
Normal file
28
app/src/honor/AndroidManifest.xml
Normal file
@@ -0,0 +1,28 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="com.ttstd.sn">
|
||||
|
||||
<!--荣耀mdm权限-->
|
||||
<!--已申请的-->
|
||||
<uses-permission android:name="com.hihonor.permission.sec.MDM_APP_MANAGEMENT" />
|
||||
<uses-permission android:name="com.hihonor.permission.sec.MDM_BLUETOOTH" />
|
||||
<uses-permission android:name="com.hihonor.permission.sec.MDM_CAPTURE_SCREEN" />
|
||||
<uses-permission android:name="com.hihonor.permission.sec.MDM_DEVICE_MANAGER" />
|
||||
<uses-permission android:name="com.hihonor.permission.sec.MDM_DEVICE_OWNER" />
|
||||
<uses-permission android:name="com.hihonor.permission.sec.MDM_NETWORK_MANAGER" />
|
||||
<uses-permission android:name="com.hihonor.permission.sec.MDM_NFC" />
|
||||
<uses-permission android:name="com.hihonor.permission.sec.MDM_PHONE_MANAGER" />
|
||||
<uses-permission android:name="com.hihonor.permission.sec.MDM_SETTINGS_RESTRICTION" />
|
||||
<uses-permission android:name="com.hihonor.permission.sec.MDM_UPDATESTATE_MANAGER" />
|
||||
<uses-permission android:name="com.hihonor.permission.sec.MDM_USB" />
|
||||
<uses-permission android:name="com.hihonor.permission.sec.MDM_WIFI" />
|
||||
<uses-permission android:name="com.hihonor.permission.sec.MDM_CAMERA" />
|
||||
<uses-permission android:name="com.hihonor.permission.sec.MDM_CONNECTIVITY" />
|
||||
<uses-permission android:name="com.hihonor.permission.sec.MDM_MMS" />
|
||||
<uses-permission android:name="com.hihonor.permission.sec.MDM_PHONE" />
|
||||
<uses-permission android:name="com.hihonor.permission.sec.MDM_SDCARD" />
|
||||
<uses-permission android:name="com.hihonor.permission.sec.MDM_TELEPHONY" />
|
||||
<uses-permission android:name="com.hihonor.permission.sec.SDK_LAUNCHER" />
|
||||
<uses-permission android:name="com.hihonor.systemmanager.permission.ACCESS_INTERFACE" />
|
||||
|
||||
</manifest>
|
||||
339
app/src/honor/java/com/ttstd/sn/mdm/AdminManager.java
Normal file
339
app/src/honor/java/com/ttstd/sn/mdm/AdminManager.java
Normal file
@@ -0,0 +1,339 @@
|
||||
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.hihonor.android.app.admin.DeviceApplicationManager;
|
||||
import com.hihonor.android.app.admin.DeviceControlManager;
|
||||
import com.hihonor.android.app.admin.DevicePasswordManager;
|
||||
import com.hihonor.android.app.admin.DeviceRestrictionManager;
|
||||
import com.hihonor.android.app.admin.DeviceSettingsManager;
|
||||
import com.ttstd.sn.receiver.AoleDeviceAdminReceiver;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 荣耀 MagicOS MDM 实现。
|
||||
*
|
||||
* <p>依赖 magic-android SDK(com.hihonor.android.app.admin.*),仅存在于 honor flavor。</p>
|
||||
*/
|
||||
public class AdminManager implements IMdmManager {
|
||||
private static final String TAG = "AdminManager[honor]";
|
||||
|
||||
@SuppressLint("StaticFieldLeak")
|
||||
private static AdminManager sInstance;
|
||||
|
||||
private Context mContext;
|
||||
private ComponentName mAdminName;
|
||||
private DevicePolicyManager mDevicePolicyManager;
|
||||
/*设备应用程序管理类*/
|
||||
private DeviceApplicationManager mDeviceApplicationManager;
|
||||
/*设备控制相关的类*/
|
||||
private DeviceControlManager mDeviceControlManager;
|
||||
/*密码相关类*/
|
||||
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.mDevicePasswordManager = new DevicePasswordManager();
|
||||
this.mDeviceRestrictionManager = new DeviceRestrictionManager();
|
||||
this.mDeviceSettingsManager = new DeviceSettingsManager();
|
||||
|
||||
Log.e(TAG, "AdminManager: isActiveMe = " + isActiveMe());
|
||||
if (isActiveMe()) {
|
||||
setYoyoDisabled(true);
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
Log.e(TAG, "addDisallowedRunningApp: pkg = " + pkg);
|
||||
if (TextUtils.isEmpty(pkg)) return;
|
||||
if (!isAvailable(mContext, pkg)) return;
|
||||
List<String> packageList = new ArrayList<>();
|
||||
packageList.add(pkg);
|
||||
try {
|
||||
mDeviceApplicationManager.addDisallowedRunningApp(mAdminName, packageList);
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "addDisallowedRunningApp: " + e.getMessage());
|
||||
}
|
||||
Log.e(TAG, "addDisallowedRunningApp: getDisallowedRunningApp = " + mDeviceApplicationManager.getDisallowedRunningApp(mAdminName));
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除阻止某应用启动运行名单(DeviceApplicationManager)
|
||||
*/
|
||||
@Override
|
||||
public void removeDisallowedRunningApp(String pkg) {
|
||||
Log.e(TAG, "removeDisallowedRunningApp: pkg = " + pkg);
|
||||
if (TextUtils.isEmpty(pkg)) return;
|
||||
List<String> stringList = new ArrayList<>();
|
||||
stringList.add(pkg);
|
||||
mDeviceApplicationManager.removeDisallowedRunningApp(mAdminName, stringList);
|
||||
Log.e(TAG, "removeDisallowedRunningApp: getDisallowedRunningApp = " + mDeviceApplicationManager.getDisallowedRunningApp(mAdminName));
|
||||
}
|
||||
|
||||
/*============================== 系统限制 ==============================*/
|
||||
|
||||
@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 助手(通过黑名单禁用 com.hihonor.servicecenter)
|
||||
*/
|
||||
@Override
|
||||
public void setYoyoDisabled(boolean disable) {
|
||||
Log.e(TAG, "setYoyoDisabled: " + disable);
|
||||
try {
|
||||
if (disable) {
|
||||
addDisallowedRunningApp("com.hihonor.servicecenter");
|
||||
} else {
|
||||
removeDisallowedRunningApp("com.hihonor.servicecenter");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "setYoyoDisabled: " + 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
26
app/src/huawei/AndroidManifest.xml
Normal file
26
app/src/huawei/AndroidManifest.xml
Normal file
@@ -0,0 +1,26 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="com.ttstd.sn">
|
||||
|
||||
<!--华为 MDM 权限-->
|
||||
<uses-permission android:name="com.huawei.permission.sec.MDM_APP_MANAGEMENT" />
|
||||
<uses-permission android:name="com.huawei.permission.sec.MDM_BLUETOOTH" />
|
||||
<uses-permission android:name="com.huawei.permission.sec.MDM_CAPTURE_SCREEN" />
|
||||
<uses-permission android:name="com.huawei.permission.sec.MDM_DEVICE_MANAGER" />
|
||||
<uses-permission android:name="com.huawei.permission.sec.MDM_DEVICE_OWNER" />
|
||||
<uses-permission android:name="com.huawei.permission.sec.MDM_NETWORK_MANAGER" />
|
||||
<uses-permission android:name="com.huawei.permission.sec.MDM_NFC" />
|
||||
<uses-permission android:name="com.huawei.permission.sec.MDM_PHONE_MANAGER" />
|
||||
<uses-permission android:name="com.huawei.permission.sec.MDM_SETTINGS_RESTRICTION" />
|
||||
<uses-permission android:name="com.huawei.permission.sec.MDM_UPDATESTATE_MANAGER" />
|
||||
<uses-permission android:name="com.huawei.permission.sec.MDM_USB" />
|
||||
<uses-permission android:name="com.huawei.permission.sec.MDM_WIFI" />
|
||||
<uses-permission android:name="com.huawei.permission.sec.MDM_CAMERA" />
|
||||
<uses-permission android:name="com.huawei.permission.sec.MDM_CONNECTIVITY" />
|
||||
<uses-permission android:name="com.huawei.permission.sec.MDM_MMS" />
|
||||
<uses-permission android:name="com.huawei.permission.sec.MDM_PHONE" />
|
||||
<uses-permission android:name="com.huawei.permission.sec.MDM_SDCARD" />
|
||||
<uses-permission android:name="com.huawei.permission.sec.MDM_TELEPHONY" />
|
||||
<uses-permission android:name="com.huawei.permission.sec.MDM_WIFI_DEVICE" />
|
||||
|
||||
</manifest>
|
||||
339
app/src/huawei/java/com/ttstd/sn/mdm/AdminManager.java
Normal file
339
app/src/huawei/java/com/ttstd/sn/mdm/AdminManager.java
Normal file
@@ -0,0 +1,339 @@
|
||||
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;
|
||||
|
||||
/**
|
||||
* 华为 MDM Kit 实现。
|
||||
*
|
||||
* <p>依赖 com.huawei.mdm:mdmkit(com.huawei.android.app.admin.*),仅存在于 huawei flavor。</p>
|
||||
*/
|
||||
public class AdminManager implements IMdmManager {
|
||||
private static final String TAG = "AdminManager[huawei]";
|
||||
|
||||
@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) {
|
||||
Log.w(TAG, "setQuickToolsDisabled is not supported on huawei platform: " + disabled);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isQuickToolsDisabled() {
|
||||
Log.w(TAG, "isQuickToolsDisabled is not supported on huawei platform");
|
||||
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 {
|
||||
mDevicePolicyManager.clearDeviceOwnerApp(mContext.getPackageName());
|
||||
} 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 助手(华为平台映射为 HiVoice 语音助手)
|
||||
*/
|
||||
@Override
|
||||
public void setYoyoDisabled(boolean disable) {
|
||||
Log.e(TAG, "setYoyoDisabled: " + disable + " (huawei use voice assistant)");
|
||||
setVoiceAssistantButtonDisabled(disable);
|
||||
}
|
||||
|
||||
/*============================== 初始化辅助 ==============================*/
|
||||
|
||||
/**
|
||||
* 移除安装白名单(构造时调用,清理历史白名单配置)
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,29 +2,6 @@
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="com.ttstd.sn">
|
||||
|
||||
<!--荣耀mdm权限-->
|
||||
<!--已申请的-->
|
||||
<uses-permission android:name="com.hihonor.permission.sec.MDM_APP_MANAGEMENT" />
|
||||
<uses-permission android:name="com.hihonor.permission.sec.MDM_BLUETOOTH" />
|
||||
<uses-permission android:name="com.hihonor.permission.sec.MDM_CAPTURE_SCREEN" />
|
||||
<uses-permission android:name="com.hihonor.permission.sec.MDM_DEVICE_MANAGER" />
|
||||
<uses-permission android:name="com.hihonor.permission.sec.MDM_DEVICE_OWNER" />
|
||||
<uses-permission android:name="com.hihonor.permission.sec.MDM_NETWORK_MANAGER" />
|
||||
<uses-permission android:name="com.hihonor.permission.sec.MDM_NFC" />
|
||||
<uses-permission android:name="com.hihonor.permission.sec.MDM_PHONE_MANAGER" />
|
||||
<uses-permission android:name="com.hihonor.permission.sec.MDM_SETTINGS_RESTRICTION" />
|
||||
<uses-permission android:name="com.hihonor.permission.sec.MDM_UPDATESTATE_MANAGER" />
|
||||
<uses-permission android:name="com.hihonor.permission.sec.MDM_USB" />
|
||||
<uses-permission android:name="com.hihonor.permission.sec.MDM_WIFI" />
|
||||
<uses-permission android:name="com.hihonor.permission.sec.MDM_CAMERA" />
|
||||
<uses-permission android:name="com.hihonor.permission.sec.MDM_CONNECTIVITY" />
|
||||
<uses-permission android:name="com.hihonor.permission.sec.MDM_MMS" />
|
||||
<uses-permission android:name="com.hihonor.permission.sec.MDM_PHONE" />
|
||||
<uses-permission android:name="com.hihonor.permission.sec.MDM_SDCARD" />
|
||||
<uses-permission android:name="com.hihonor.permission.sec.MDM_TELEPHONY" />
|
||||
<uses-permission android:name="com.hihonor.permission.sec.SDK_LAUNCHER" />
|
||||
<uses-permission android:name="com.hihonor.systemmanager.permission.ACCESS_INTERFACE" />
|
||||
|
||||
<!--系统权限-->
|
||||
<uses-permission android:name="android.permission.BATTERY_STATS" />
|
||||
<uses-permission android:name="android.permission.CHANGE_CONFIGURATION" />
|
||||
|
||||
@@ -1,473 +0,0 @@
|
||||
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.ttstd.sn.receiver.AoleDeviceAdminReceiver;
|
||||
import com.hihonor.android.app.admin.DeviceApplicationManager;
|
||||
import com.hihonor.android.app.admin.DeviceBluetoothManager;
|
||||
import com.hihonor.android.app.admin.DeviceCameraManager;
|
||||
import com.hihonor.android.app.admin.DeviceControlManager;
|
||||
import com.hihonor.android.app.admin.DeviceEmailManager;
|
||||
import com.hihonor.android.app.admin.DeviceFirewallManager;
|
||||
import com.hihonor.android.app.admin.DeviceHwSystemManager;
|
||||
import com.hihonor.android.app.admin.DeviceInfraredManager;
|
||||
import com.hihonor.android.app.admin.DeviceLocationManager;
|
||||
import com.hihonor.android.app.admin.DeviceNetworkManager;
|
||||
import com.hihonor.android.app.admin.DeviceP2PManager;
|
||||
import com.hihonor.android.app.admin.DevicePackageManager;
|
||||
import com.hihonor.android.app.admin.DevicePasswordManager;
|
||||
import com.hihonor.android.app.admin.DevicePhoneManager;
|
||||
import com.hihonor.android.app.admin.DeviceRestrictionManager;
|
||||
import com.hihonor.android.app.admin.DeviceSettingsManager;
|
||||
import com.hihonor.android.app.admin.DeviceStorageManagerEx;
|
||||
import com.hihonor.android.app.admin.DeviceTelephonyManager;
|
||||
import com.hihonor.android.app.admin.DeviceVpnManager;
|
||||
import com.hihonor.android.app.admin.DeviceWifiPolicyManager;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class AdminManager {
|
||||
private static final String TAG = "AdminManager";
|
||||
|
||||
public static final List<String> mDeleteMenus = new ArrayList<String>() {{
|
||||
this.add("honor_id");
|
||||
this.add("super_device");
|
||||
this.add("vpn_settings");
|
||||
this.add("apps_clone");
|
||||
this.add("apps_startup_management");
|
||||
this.add("apps_management");
|
||||
this.add("apps_honor_tips");
|
||||
this.add("pengine_settings");
|
||||
this.add("screen_wallpaper");
|
||||
}};
|
||||
|
||||
private static final int STATUS_DISABLE = 0;
|
||||
private static final int STATUS_ENABLE = 1;
|
||||
|
||||
@SuppressLint("StaticFieldLeak")
|
||||
private static AdminManager sInstance;
|
||||
|
||||
private Context mContext;
|
||||
private ComponentName mAdminName;
|
||||
private DevicePolicyManager mDevicePolicyManager;
|
||||
|
||||
/*设备应用程序管理类*/
|
||||
private DeviceApplicationManager mDeviceApplicationManager;
|
||||
/*蓝牙相关的管理类*/
|
||||
private DeviceBluetoothManager mDeviceBluetoothManager;
|
||||
/*相机相关的类*/
|
||||
private DeviceCameraManager mDeviceCameraManager;
|
||||
/*设备控制相关的类*/
|
||||
private DeviceControlManager mDeviceControlManager;
|
||||
/*邮箱相关的管理类*/
|
||||
private DeviceEmailManager mDeviceEmailManager;
|
||||
/*防火墙相关管理类*/
|
||||
private DeviceFirewallManager mDeviceFirewallManager;
|
||||
/*系统相关类*/
|
||||
private DeviceHwSystemManager mDeviceHwSystemManager;
|
||||
/*设备红外管理类*/
|
||||
private DeviceInfraredManager mDeviceInfraredManager;
|
||||
/*地理位置相关的类*/
|
||||
private DeviceLocationManager mDeviceLocationManager;
|
||||
/*网络管理相关的类*/
|
||||
private DeviceNetworkManager mDeviceNetworkManager;
|
||||
/*Wifi 直连权限管理授权*/
|
||||
private DeviceP2PManager mDeviceP2PManager;
|
||||
/*设备包管理类*/
|
||||
private DevicePackageManager mDevicePackageManager;
|
||||
/*密码相关类*/
|
||||
private DevicePasswordManager mDevicePasswordManager;
|
||||
/*设备通话管理类*/
|
||||
private DevicePhoneManager mDevicePhoneManager;
|
||||
/*禁用/启用管理类*/
|
||||
private DeviceRestrictionManager mDeviceRestrictionManager;
|
||||
/*设置相关类*/
|
||||
private DeviceSettingsManager mDeviceSettingsManager;
|
||||
/*设备存储管理类*/
|
||||
private DeviceStorageManagerEx mDeviceStorageManagerEx;
|
||||
/*通讯服务管理类*/
|
||||
private DeviceTelephonyManager mDeviceTelephonyManager;
|
||||
/*VPN 相关管理类*/
|
||||
private DeviceVpnManager mDeviceVpnManager;
|
||||
/*WIFI 管理相关类*/
|
||||
private DeviceWifiPolicyManager mDeviceWifiPolicyManager;
|
||||
|
||||
|
||||
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.mDeviceBluetoothManager = new DeviceBluetoothManager();
|
||||
this.mDeviceCameraManager = new DeviceCameraManager();
|
||||
this.mDeviceControlManager = new DeviceControlManager();
|
||||
this.mDeviceEmailManager = new DeviceEmailManager();
|
||||
this.mDeviceFirewallManager = new DeviceFirewallManager();
|
||||
this.mDeviceHwSystemManager = new DeviceHwSystemManager();
|
||||
this.mDeviceInfraredManager = new DeviceInfraredManager();
|
||||
this.mDeviceLocationManager = new DeviceLocationManager();
|
||||
this.mDeviceNetworkManager = new DeviceNetworkManager();
|
||||
this.mDeviceP2PManager = new DeviceP2PManager();
|
||||
this.mDevicePackageManager = new DevicePackageManager();
|
||||
this.mDevicePasswordManager = new DevicePasswordManager();
|
||||
this.mDevicePhoneManager = new DevicePhoneManager();
|
||||
this.mDeviceRestrictionManager = new DeviceRestrictionManager();
|
||||
this.mDeviceSettingsManager = new DeviceSettingsManager();
|
||||
this.mDeviceStorageManagerEx = new DeviceStorageManagerEx();
|
||||
this.mDeviceTelephonyManager = new DeviceTelephonyManager();
|
||||
this.mDeviceVpnManager = new DeviceVpnManager();
|
||||
this.mDeviceWifiPolicyManager = new DeviceWifiPolicyManager();
|
||||
|
||||
Log.e(TAG, "AdminManager: isActiveMe = " + isActiveMe());
|
||||
if (isActiveMe()) {
|
||||
setYoyoDisabled(true);
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public ComponentName getAdminName() {
|
||||
return mAdminName;
|
||||
}
|
||||
|
||||
public DeviceApplicationManager getDeviceApplicationManager() {
|
||||
return mDeviceApplicationManager;
|
||||
}
|
||||
|
||||
public DeviceBluetoothManager getDeviceBluetoothManager() {
|
||||
return mDeviceBluetoothManager;
|
||||
}
|
||||
|
||||
public DeviceCameraManager getDeviceCameraManager() {
|
||||
return mDeviceCameraManager;
|
||||
}
|
||||
|
||||
public DeviceControlManager getDeviceControlManager() {
|
||||
return mDeviceControlManager;
|
||||
}
|
||||
|
||||
public DeviceEmailManager getDeviceEmailManager() {
|
||||
return mDeviceEmailManager;
|
||||
}
|
||||
|
||||
public DeviceFirewallManager getDeviceFirewallManager() {
|
||||
return mDeviceFirewallManager;
|
||||
}
|
||||
|
||||
public DeviceHwSystemManager getDeviceHwSystemManager() {
|
||||
return mDeviceHwSystemManager;
|
||||
}
|
||||
|
||||
public DeviceInfraredManager getDeviceInfraredManager() {
|
||||
return mDeviceInfraredManager;
|
||||
}
|
||||
|
||||
public DeviceLocationManager getDeviceLocationManager() {
|
||||
return mDeviceLocationManager;
|
||||
}
|
||||
|
||||
public DeviceNetworkManager getDeviceNetworkManager() {
|
||||
return mDeviceNetworkManager;
|
||||
}
|
||||
|
||||
public DeviceP2PManager getDeviceP2PManager() {
|
||||
return mDeviceP2PManager;
|
||||
}
|
||||
|
||||
public DevicePackageManager getDevicePackageManager() {
|
||||
return mDevicePackageManager;
|
||||
}
|
||||
|
||||
public DevicePasswordManager getDevicePasswordManager() {
|
||||
return mDevicePasswordManager;
|
||||
}
|
||||
|
||||
public DevicePhoneManager getDevicePhoneManager() {
|
||||
return mDevicePhoneManager;
|
||||
}
|
||||
|
||||
public DeviceRestrictionManager getDeviceRestrictionManager() {
|
||||
return mDeviceRestrictionManager;
|
||||
}
|
||||
|
||||
public DeviceSettingsManager getDeviceSettingsManager() {
|
||||
return mDeviceSettingsManager;
|
||||
}
|
||||
|
||||
public DeviceStorageManagerEx getDeviceStorageManagerEx() {
|
||||
return mDeviceStorageManagerEx;
|
||||
}
|
||||
|
||||
public DeviceTelephonyManager getDeviceTelephonyManager() {
|
||||
return mDeviceTelephonyManager;
|
||||
}
|
||||
|
||||
public DeviceVpnManager getDeviceVpnManager() {
|
||||
return mDeviceVpnManager;
|
||||
}
|
||||
|
||||
public DeviceWifiPolicyManager getDeviceWifiPolicyManager() {
|
||||
return mDeviceWifiPolicyManager;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public boolean isActiveMe() {
|
||||
if (mDevicePolicyManager == null) {
|
||||
return false;
|
||||
} else {
|
||||
return mDevicePolicyManager.isAdminActive(mAdminName);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除白名单
|
||||
*/
|
||||
public void removeTrustList() {
|
||||
try {
|
||||
List<String> trustList = mDevicePackageManager.getInstallPackageTrustList(mAdminName);
|
||||
Log.e(TAG, "removeTrustList: " + trustList);
|
||||
if (trustList != null && trustList.size() != 0) {
|
||||
mDevicePackageManager.removeInstallPackageTrustList(mAdminName, trustList);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "removeTrustList: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加保持某应用始终运行名单
|
||||
*
|
||||
* @param pkg
|
||||
*/
|
||||
public void addPersistentApp(String pkg) {
|
||||
Log.e(TAG, "addPersistentApp: " + pkg);
|
||||
List<String> forceAppList = new ArrayList<>();
|
||||
forceAppList.add(pkg);
|
||||
try {
|
||||
// 添加保持某应用始终运行名单
|
||||
mDeviceApplicationManager.addPersistentApp(mAdminName, forceAppList);
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "addPersistentApp: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加保持某应用始终运行名单
|
||||
*
|
||||
* @param packageNames
|
||||
*/
|
||||
public void addPersistentApp(List<String> packageNames) {
|
||||
Log.e(TAG, "addPersistentApp: " + packageNames);
|
||||
try {
|
||||
// 添加保持某应用始终运行名单
|
||||
mDeviceApplicationManager.addPersistentApp(mAdminName, packageNames);
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "addPersistentApp: Exception " + e.getMessage());
|
||||
}
|
||||
Log.e(TAG, "setDisallowUninstall: getPersistentApp: " + mDeviceApplicationManager.getPersistentApp(mAdminName));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param pkg 添加保持某应用始终运行名单
|
||||
*/
|
||||
public void removePersistentApp(String pkg) {
|
||||
Log.e(TAG, "removePersistentApp: " + pkg);
|
||||
List<String> packageNames = new ArrayList<>();
|
||||
packageNames.add(pkg);
|
||||
try {
|
||||
// 添加保持某应用始终运行名单
|
||||
mDeviceApplicationManager.removePersistentApp(mAdminName, packageNames);
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "removePersistentApp: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void removePersistentApp(List<String> packageNames) {
|
||||
Log.e(TAG, "removePersistentApp: " + packageNames);
|
||||
try {
|
||||
// 添加保持某应用始终运行名单
|
||||
mDeviceApplicationManager.removePersistentApp(mAdminName, packageNames);
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "removePersistentApp: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加阻止某应用启动运行名单
|
||||
*
|
||||
* @param packageNames
|
||||
*/
|
||||
public void addDisallowedRunningApp(List<String> packageNames) {
|
||||
if (packageNames == null || packageNames.size() == 0) return;
|
||||
Log.e(TAG, "addDisallowedRunningApp: packageNames = " + packageNames);
|
||||
List<String> packageList = mDeviceApplicationManager.getDisallowedRunningApp(mAdminName);
|
||||
Log.e(TAG, "addDisallowedRunningApp: getDisallowedRunningApp = " + packageList);
|
||||
try {
|
||||
mDeviceApplicationManager.addDisallowedRunningApp(mAdminName, packageNames);
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "addDisallowedRunningApp: " + e.getMessage());
|
||||
}
|
||||
Log.e(TAG, "addDisallowedRunningApp: getDisallowedRunningApp = " + mDeviceApplicationManager.getDisallowedRunningApp(mAdminName));
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加阻止某应用启动运行名单
|
||||
*
|
||||
* @param pkg 添加后,黑名单中的应用不能打开,正在使用的应用也会被停止运行。
|
||||
*/
|
||||
public void addDisallowedRunningApp(String pkg) {
|
||||
Log.e(TAG, "addDisallowedRunningApp: pkg = " + pkg);
|
||||
if (TextUtils.isEmpty(pkg)) return;
|
||||
if (!isAvailable(mContext, pkg)) return;
|
||||
List<String> packageList = new ArrayList<>();
|
||||
packageList.add(pkg);
|
||||
try {
|
||||
mDeviceApplicationManager.addDisallowedRunningApp(mAdminName, packageList);
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "addDisallowedRunningApp: " + e.getMessage());
|
||||
}
|
||||
Log.e(TAG, "addDisallowedRunningApp: getDisallowedRunningApp = " + mDeviceApplicationManager.getDisallowedRunningApp(mAdminName));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 删除阻止某应用启动运行名单
|
||||
*
|
||||
* @param packageNames
|
||||
*/
|
||||
public void removeDisallowedRunningApp(List<String> packageNames) {
|
||||
if (packageNames == null || packageNames.size() == 0) return;
|
||||
mDeviceApplicationManager.removeDisallowedRunningApp(mAdminName, packageNames);
|
||||
Log.e(TAG, "removeDisallowedRunningApp: getDisallowedRunningApp = " + mDeviceApplicationManager.getDisallowedRunningApp(mAdminName));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 删除阻止某应用启动运行名单
|
||||
*
|
||||
* @param pkg 待删除的应用包名列表
|
||||
*/
|
||||
public void removeDisallowedRunningApp(String pkg) {
|
||||
Log.e(TAG, "removeDisallowedRunningApp: pkg = " + pkg);
|
||||
if (TextUtils.isEmpty(pkg)) return;
|
||||
List<String> stringList = new ArrayList<>();
|
||||
stringList.add(pkg);
|
||||
mDeviceApplicationManager.removeDisallowedRunningApp(mAdminName, stringList);
|
||||
Log.e(TAG, "removeDisallowedRunningApp: getDisallowedRunningApp = " + mDeviceApplicationManager.getDisallowedRunningApp(mAdminName));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param pkg
|
||||
* @return 是否阻止某应用启动
|
||||
*/
|
||||
public boolean isDisallowedRunningApp(String pkg) {
|
||||
List<String> packageList = mDeviceApplicationManager.getDisallowedRunningApp(mAdminName);
|
||||
Log.e(TAG, "isDisallowedRunningApp: packageList = " + packageList);
|
||||
return packageList.contains(pkg);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置 yoyo 助手
|
||||
*
|
||||
* @param disable true: 禁用 yoyo 助手
|
||||
*/
|
||||
public void setYoyoDisable(boolean disable) {
|
||||
if (disable) {
|
||||
setYoyoDisabled(true);
|
||||
} else {
|
||||
setYoyoDisabled(false);
|
||||
}
|
||||
}
|
||||
|
||||
public void setYoyoDisabled(boolean disable) {
|
||||
Log.e(TAG, "setYoyoDisabled: " + disable);
|
||||
try {
|
||||
if (disable) {
|
||||
addDisallowedRunningApp("com.hihonor.servicecenter");
|
||||
} else {
|
||||
removeDisallowedRunningApp("com.hihonor.servicecenter");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "setYoyoDisabled: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加忽略频繁重新启动的应用
|
||||
*
|
||||
* @param packageNames
|
||||
*/
|
||||
public void addIgnoreFrequentRelaunchAppList(ArrayList<String> packageNames) {
|
||||
Log.e(TAG, "addIgnoreFrequentRelaunchAppList: " + packageNames);
|
||||
try {
|
||||
if (packageNames != null && packageNames.size() > 0) {
|
||||
mDeviceApplicationManager.addIgnoreFrequentRelaunchAppList(mAdminName, packageNames);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "addIgnoreFrequentRelaunchAppList: " + 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
124
app/src/main/java/com/ttstd/sn/mdm/IMdmManager.java
Normal file
124
app/src/main/java/com/ttstd/sn/mdm/IMdmManager.java
Normal file
@@ -0,0 +1,124 @@
|
||||
package com.ttstd.sn.mdm;
|
||||
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
|
||||
/**
|
||||
* MDM 能力抽象接口。
|
||||
*
|
||||
* <p>荣耀(com.hihonor.android.app.admin.*)、华为(com.huawei.android.app.admin.*)
|
||||
* 与 EMUI(com.huawei.android.app.admin.*)三套平台 SDK 的 MDM 能力在此统一抽象,
|
||||
* 业务层(App / Activity)只依赖本接口,平台差异由各 flavor 中的 {@code AdminManager} 实现。</p>
|
||||
*
|
||||
* <p>方法按底层使用的 SDK Manager 分组排列,并在注释中标注对应 Manager;</p>
|
||||
*/
|
||||
public interface IMdmManager {
|
||||
|
||||
/*============================== 基础能力(DevicePolicyManager / Build) ==============================*/
|
||||
|
||||
/**
|
||||
* 设备管理员组件(DevicePolicyManager)
|
||||
*/
|
||||
ComponentName getAdminName();
|
||||
|
||||
/**
|
||||
* 设备管理员是否已激活(DevicePolicyManager)
|
||||
*/
|
||||
boolean isActiveMe();
|
||||
|
||||
/**
|
||||
* 获取设备序列号(Build.getSerial,依赖 MDM 权限)
|
||||
*/
|
||||
String getSerial();
|
||||
|
||||
/*============================== 应用管理(DeviceApplicationManager) ==============================*/
|
||||
|
||||
/**
|
||||
* 添加阻止某应用启动运行名单(黑名单)
|
||||
*/
|
||||
void addDisallowedRunningApp(String pkg);
|
||||
|
||||
/**
|
||||
* 删除阻止某应用启动运行名单
|
||||
*/
|
||||
void removeDisallowedRunningApp(String pkg);
|
||||
|
||||
/*============================== 系统限制(DeviceRestrictionManager) ==============================*/
|
||||
|
||||
/**
|
||||
* 禁用/启用多窗口(DeviceRestrictionManager)
|
||||
*/
|
||||
void setMultiWindowDisabled(boolean disabled);
|
||||
|
||||
boolean isMultiWindowDisabled();
|
||||
|
||||
/**
|
||||
* 禁用/启用语音助手按键(DeviceRestrictionManager)
|
||||
*/
|
||||
void setVoiceAssistantButtonDisabled(boolean disabled);
|
||||
|
||||
boolean isVoiceAssistantButtonDisabled();
|
||||
|
||||
/**
|
||||
* 禁用/启用 USB 数据传输(DeviceRestrictionManager)
|
||||
*/
|
||||
void setUSBDataDisabled(boolean disabled);
|
||||
|
||||
boolean isUSBDataDisabled();
|
||||
|
||||
/*============================== 设备设置(DeviceSettingsManager) ==============================*/
|
||||
|
||||
/**
|
||||
* 禁用/启用恢复出厂设置(DeviceSettingsManager)
|
||||
*
|
||||
* @return true:配置成功;false:配置失败
|
||||
*/
|
||||
boolean setRestoreFactoryDisabled(boolean disabled);
|
||||
|
||||
boolean isRestoreFactoryDisabled();
|
||||
|
||||
/*============================== 快捷工具(DevicePasswordManager) ==============================*/
|
||||
|
||||
/**
|
||||
* 禁用/启用快捷工具(DevicePasswordManager)
|
||||
*/
|
||||
void setQuickToolsDisabled(boolean disabled);
|
||||
|
||||
boolean isQuickToolsDisabled();
|
||||
|
||||
/*============================== 设备控制(DeviceControlManager) ==============================*/
|
||||
|
||||
/**
|
||||
* 开启/关闭 USB 调试模式(DeviceControlManager)
|
||||
*/
|
||||
void turnOnUsbDebugMode(boolean on);
|
||||
|
||||
/**
|
||||
* 强制激活设备管理员,静默激活,需系统签名(DeviceControlManager)
|
||||
*/
|
||||
void setForcedActiveDeviceAdmin(Context activity);
|
||||
|
||||
/**
|
||||
* 移除设备管理员(DeviceControlManager)
|
||||
*/
|
||||
void removeActiveDeviceAdmin();
|
||||
|
||||
/**
|
||||
* 清除 Device Owner(DeviceControlManager)
|
||||
*/
|
||||
void clearDeviceOwnerApp();
|
||||
|
||||
/**
|
||||
* 是否已强制激活设备管理员(DeviceControlManager)
|
||||
*/
|
||||
boolean isForcedActiveDeviceAdmin();
|
||||
|
||||
/*============================== 平台差异(无统一 Manager) ==============================*/
|
||||
|
||||
/**
|
||||
* 禁用/启用 YOYO 助手。
|
||||
* 荣耀平台通过 {@link #addDisallowedRunningApp(String)} 禁用 com.hihonor.servicecenter;
|
||||
* 华为 / EMUI 平台映射为 {@link #setVoiceAssistantButtonDisabled(boolean)}(HiVoice)。
|
||||
*/
|
||||
void setYoyoDisabled(boolean disable);
|
||||
}
|
||||
@@ -1,41 +1,20 @@
|
||||
package com.ttstd.sn.ui.main;
|
||||
|
||||
import android.app.admin.DevicePolicyManager;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.widget.CompoundButton;
|
||||
|
||||
import com.ttstd.sn.R;
|
||||
import com.ttstd.sn.mdm.AdminManager;
|
||||
import com.ttstd.sn.mdm.IMdmManager;
|
||||
import com.ttstd.sn.databinding.ActivityMainBinding;
|
||||
import com.ttstd.sn.receiver.AoleDeviceAdminReceiver;
|
||||
import com.ttstd.sn.ui.base.mvvm.BaseMvvmActivity;
|
||||
import com.hihonor.android.app.admin.DeviceApplicationManager;
|
||||
import com.hihonor.android.app.admin.DeviceControlManager;
|
||||
import com.hihonor.android.app.admin.DeviceHwSystemManager;
|
||||
import com.hihonor.android.app.admin.DevicePackageManager;
|
||||
import com.hihonor.android.app.admin.DevicePasswordManager;
|
||||
import com.hihonor.android.app.admin.DeviceRestrictionManager;
|
||||
import com.hihonor.android.app.admin.DeviceSettingsManager;
|
||||
|
||||
|
||||
public class MainActivity extends BaseMvvmActivity<MainViewModel, ActivityMainBinding> {
|
||||
private static final String TAG = "DebugActivity";
|
||||
private static final String TAG = "MainActivity";
|
||||
|
||||
private DevicePolicyManager mDevicePolicyManager;
|
||||
private DeviceRestrictionManager mDeviceRestrictionManager;
|
||||
private DeviceControlManager mDeviceControlManager;
|
||||
private DeviceApplicationManager mDeviceApplicationManager;
|
||||
private DeviceHwSystemManager mDeviceHwSystemManager;
|
||||
private DeviceSettingsManager mDeviceSettingsManager;
|
||||
private DevicePasswordManager mDevicePasswordManager;
|
||||
|
||||
/*设备包管理类*/
|
||||
private DevicePackageManager mDevicePackageManager;
|
||||
|
||||
private ComponentName mAdminName;
|
||||
private IMdmManager mMdmManager;
|
||||
|
||||
@Override
|
||||
protected int getLayoutId() {
|
||||
@@ -59,22 +38,13 @@ public class MainActivity extends BaseMvvmActivity<MainViewModel, ActivityMainBi
|
||||
|
||||
@Override
|
||||
protected void initView() {
|
||||
mDevicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
|
||||
mAdminName = new ComponentName(this, AoleDeviceAdminReceiver.class);
|
||||
|
||||
mDeviceRestrictionManager = new DeviceRestrictionManager();
|
||||
mDeviceControlManager = new DeviceControlManager();
|
||||
mDeviceApplicationManager = new DeviceApplicationManager();
|
||||
mDeviceHwSystemManager = new DeviceHwSystemManager();
|
||||
mDeviceSettingsManager = new DeviceSettingsManager();
|
||||
mDevicePackageManager = new DevicePackageManager();
|
||||
mDevicePasswordManager = new DevicePasswordManager();
|
||||
mMdmManager = AdminManager.getInstance();
|
||||
|
||||
mViewDataBinding.swWindow.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
|
||||
@Override
|
||||
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
|
||||
try {
|
||||
mDeviceRestrictionManager.setMultiWindowDisabled(mAdminName, isChecked);
|
||||
mMdmManager.setMultiWindowDisabled(isChecked);
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "onCheckedChanged: " + e.getMessage());
|
||||
}
|
||||
@@ -83,35 +53,35 @@ public class MainActivity extends BaseMvvmActivity<MainViewModel, ActivityMainBi
|
||||
mViewDataBinding.swYoyo.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
|
||||
@Override
|
||||
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
|
||||
mDeviceRestrictionManager.setVoiceAssistantButtonDisabled(mAdminName, isChecked);
|
||||
mMdmManager.setVoiceAssistantButtonDisabled(isChecked);
|
||||
if (isChecked) {
|
||||
AdminManager.getInstance().removeDisallowedRunningApp("com.hihonor.servicecenter");
|
||||
mMdmManager.removeDisallowedRunningApp("com.hihonor.servicecenter");
|
||||
} else {
|
||||
AdminManager.getInstance().addDisallowedRunningApp("com.hihonor.servicecenter");
|
||||
mMdmManager.addDisallowedRunningApp("com.hihonor.servicecenter");
|
||||
}
|
||||
}
|
||||
});
|
||||
mViewDataBinding.swRestore.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
|
||||
@Override
|
||||
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
|
||||
mDeviceSettingsManager.setRestoreFactoryDisabled(mAdminName, isChecked);
|
||||
mMdmManager.setRestoreFactoryDisabled(isChecked);
|
||||
}
|
||||
});
|
||||
mViewDataBinding.swUsbDebug.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
|
||||
@Override
|
||||
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
|
||||
mDeviceRestrictionManager.setUSBDataDisabled(mAdminName, isChecked);
|
||||
mDeviceControlManager.turnOnUsbDebugMode(mAdminName, !isChecked);
|
||||
mMdmManager.setUSBDataDisabled(isChecked);
|
||||
mMdmManager.turnOnUsbDebugMode(!isChecked);
|
||||
}
|
||||
});
|
||||
mViewDataBinding.swRemoveAdmin.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
|
||||
@Override
|
||||
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
|
||||
if (isChecked) {
|
||||
mDeviceControlManager.setForcedActiveDeviceAdmin(mAdminName, MainActivity.this);
|
||||
mMdmManager.setForcedActiveDeviceAdmin(MainActivity.this);
|
||||
} else {
|
||||
mDeviceControlManager.removeActiveDeviceAdmin(mAdminName);
|
||||
mDeviceControlManager.clearDeviceOwnerApp();
|
||||
mMdmManager.removeActiveDeviceAdmin();
|
||||
mMdmManager.clearDeviceOwnerApp();
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -119,7 +89,7 @@ public class MainActivity extends BaseMvvmActivity<MainViewModel, ActivityMainBi
|
||||
@Override
|
||||
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
|
||||
try {
|
||||
mDevicePasswordManager.setQuickToolsDisabled(mAdminName, isChecked);
|
||||
mMdmManager.setQuickToolsDisabled(isChecked);
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "onCheckedChanged: " + e.getMessage());
|
||||
}
|
||||
@@ -139,35 +109,19 @@ public class MainActivity extends BaseMvvmActivity<MainViewModel, ActivityMainBi
|
||||
}
|
||||
|
||||
private void getAdminStatus() {
|
||||
if (isActiveMe()) {
|
||||
if (mMdmManager.isActiveMe()) {
|
||||
mViewDataBinding.tvAdmin.setText("已激活");
|
||||
mViewDataBinding.swWindow.setChecked(mDeviceRestrictionManager.isMultiWindowDisabled(mAdminName));
|
||||
mViewDataBinding.swYoyo.setChecked(mDeviceRestrictionManager.isVoiceAssistantButtonDisabled(mAdminName));
|
||||
mViewDataBinding.swRestore.setChecked(mDeviceSettingsManager.isRestoreFactoryDisabled(mAdminName));
|
||||
mViewDataBinding.swUsbDebug.setChecked(mDeviceRestrictionManager.isUSBDataDisabled(mAdminName));
|
||||
mViewDataBinding.swRemoveAdmin.setChecked(mDeviceControlManager.isForcedActiveDeviceAdmin(mAdminName));
|
||||
mViewDataBinding.swQuickTools.setChecked(mDevicePasswordManager.isQuickToolsDisabled(mAdminName));
|
||||
mViewDataBinding.swWindow.setChecked(mMdmManager.isMultiWindowDisabled());
|
||||
mViewDataBinding.swYoyo.setChecked(mMdmManager.isVoiceAssistantButtonDisabled());
|
||||
mViewDataBinding.swRestore.setChecked(mMdmManager.isRestoreFactoryDisabled());
|
||||
mViewDataBinding.swUsbDebug.setChecked(mMdmManager.isUSBDataDisabled());
|
||||
mViewDataBinding.swRemoveAdmin.setChecked(mMdmManager.isForcedActiveDeviceAdmin());
|
||||
mViewDataBinding.swQuickTools.setChecked(mMdmManager.isQuickToolsDisabled());
|
||||
} else {
|
||||
mViewDataBinding.tvAdmin.setText("未激活");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否激活
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
private boolean isActiveMe() {
|
||||
boolean active;
|
||||
if (mDevicePolicyManager == null) {
|
||||
active = false;
|
||||
} else {
|
||||
active = mDevicePolicyManager.isAdminActive(mAdminName);
|
||||
}
|
||||
Log.e(TAG, "isActiveMe: active = " + active);
|
||||
return active;
|
||||
}
|
||||
|
||||
public class BtnClick {
|
||||
|
||||
public void refresh(View view) {
|
||||
|
||||
Reference in New Issue
Block a user