暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

让Android自动化辅助APP成为设备所有者(一)

岛哥手记 2021-06-23
2243

阅读本文大约需要1.1分钟。

背景

我们之所以需要将Android自动化测试的辅助APP设置成设备所有者是为了更好的控制系统的一些行为从而让整个测试过程更稳定。


DeviceOwner简介

DeviceOwner 是指在设备上以管理员身份运行的应用程序,该应用程序可以使用 DevicePolicyManager 类中的 API 来控制设备的一些行为,例如:重启设备、设置锁屏方式、设置密码、强制清除密码、设置状态栏、设置系统更新策略等。


Android 提供了三种设备管理方案:DeviceAdmin(设备管理员)、ProfileOwner(配置文件所有者) 和 DeviceOwner(设备所有者),这三种设备管理方案的权限大小分别为:DeviceAdmin < ProfileOwner < DeviceOwner。应用需要最大的授权才能成为DeviceOwner,DeviceOwner具有设备的最高权限。


创建DeviceOwner

基本配置

首先在res/xml目录下新建device_admin.xml文件,如下:

<?xml version="1.0" encoding="utf-8"?>
<device-admin xmlns:android="http://schemas.android.com/apk/res/android" >
<uses-policies>
<!-- 设置密码规则 -->
<limit-password />
<!-- 监视屏幕解锁尝试次数 -->
<watch-login />
<!-- 更改解锁密码 -->
<reset-password />
<!-- 锁定屏幕 -->
<force-lock />
<!-- 清除数据,恢复出厂模式,在不发出警告的情况下 -->
<wipe-data />
<!-- 锁屏密码有效期 -->
<expire-password />
<!-- 对存储的应用数据加密 -->
<encrypted-storage />
<!-- 禁用锁屏信息 -->
<disable-keyguard-features/>
<!-- 禁用摄像头 -->
<disable-camera />
</uses-policies>
</device-admin>
复制

注册一个自定义广播接收器继承自DeviceAdminReceiver

代码如下:

package com.android.jarvis.receivers


import android.app.admin.DeviceAdminReceiver
import android.app.admin.DevicePolicyManager
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.os.Build
import android.util.Log
import androidx.annotation.RequiresApi


/**
* adb shell dpm set-device-owner com.android.jarvis/.receivers.JarvisAdminReceiver
*/
class JarvisAdminReceiver : DeviceAdminReceiver() {


@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
override fun onEnabled(context: Context, intent: Intent) {
Log.d("JarvisAdminReceiver", "onEnabled")
val devicePolicyManager =
context.getSystemService(Context.DEVICE_POLICY_SERVICE) as DevicePolicyManager
//设置应用不可卸载
devicePolicyManager.setUninstallBlocked(
getComponentName(context),
context.packageName,
true
)
super.onEnabled(context, intent)
}


/**
* 获取ComponentName,DevicePolicyManager的大多数方法都会用到
*/
private fun getComponentName(context: Context): ComponentName {
return ComponentName(
context.applicationContext,
JarvisAdminReceiver::class.java
)
}
}
复制

在AndroidManifest.xml中注册广播

代码如下:

        <receiver
android:name=".receivers.JarvisAdminReceiver"
android:permission="android.permission.BIND_DEVICE_ADMIN">
<meta-data
android:name="android.app.device_admin"
android:resource="@xml/device_policies" />


<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
<action android:name="android.app.action.DEVICE_ADMIN_DISABLE_REQUESTED" />
<action android:name="android.app.action.DEVICE_ADMIN_DISABLED" />
</intent-filter>
</receiver>
复制

激活DeviceOwner

先安装应用,然后在命令行中执行:

adb shell dpm set-device-owner com.android.jarvis/.receivers.JarvisAdminReceiver
复制

移除DeviceOwner

当一个APP成为DeviceOwner后,这个APP是不能被卸载的,也无法在设置中关闭其权限,要想卸载这个APP就必须移除DeviceOwner权限,首先需要在AndroidManifest.xml文件中的<application/>节点添加android:testOnly="true",然后可以通过如下命令移除:

adb shell dpm remove-active-admin com.android.jarvis/.receivers.JarvisAdminReceiver
复制
但是在有些机型上即使设置了testOnly=true也是无法移除,会报以下错误:
java.lang.SecurityException: Attempt to remove non-test admin ComponentInfo{....AppAdminReceiver} 0
复制
这个时候就需要通过代码的方式来移除了,我们可以新建一个广播接收器:
package com.android.jarvis.receivers


import android.app.admin.DevicePolicyManager
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.os.Build
import androidx.annotation.RequiresApi


/**
*adb shell am broadcast -a com.android.jarvis.action.DEVICE_ADMIN_DISABLED
*/
class JarvisDeviceReceiver(
private val ACTION_DEVICE_ADMIN_DISABLED: String = "com.android.jarvis.action.DEVICE_ADMIN_DISABLED"
) :
BroadcastReceiver() {


@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
override fun onReceive(context: Context, intent: Intent) {
val action = intent.action
if (action == ACTION_DEVICE_ADMIN_DISABLED) {
val devicePolicyManager =
context.getSystemService(Context.DEVICE_POLICY_SERVICE) as DevicePolicyManager
devicePolicyManager.clearDeviceOwnerApp(context.packageName)
}
}
}
复制

然后在AndroidManifest.xml文件中注册:

        <receiver android:name=".receivers.JarvisDeviceReceiver">
<intent-filter>
<action android:name="com.android.jarvis.action.DEVICE_ADMIN_DISABLED" />
</intent-filter>
</receiver>
复制

最后在命令行中执行:

adb shell am broadcast -a com.android.jarvis.action.DEVICE_ADMIN_DISABLED
复制

这样就可以成功移除DeviceOwner权限了。


推荐阅读:

Android自动化中动态设置网络代理

移动端UI自动化过程中的难点及应对策略

实战 | Android过度绘制自动化测试

实战 | Telegraf+ InfluxDB+Grafana 搭建服务器性能监控平台

Android自动化辅助APP保活配置


想要明白些道理,遇见些有趣的事 —— 离岛

文章转载自岛哥手记,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论