optimize activate and deactivate logic

This commit is contained in:
BinTianqi
2024-05-02 17:01:25 +08:00
parent 83a924378d
commit 3d09eb958b
6 changed files with 135 additions and 146 deletions

View File

@@ -1,74 +1,46 @@
package com.bintianqi.owndroid
import android.annotation.SuppressLint
import android.app.admin.DeviceAdminReceiver
import android.app.admin.DevicePolicyManager
import android.content.BroadcastReceiver
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.pm.PackageInstaller.*
import android.os.Build.VERSION
import android.os.PersistableBundle
import android.util.Log
import android.widget.Toast
import androidx.activity.ComponentActivity
import com.bintianqi.owndroid.dpm.isDeviceOwner
import com.bintianqi.owndroid.dpm.isProfileOwner
class Receiver : DeviceAdminReceiver() {
override fun onEnabled(context: Context, intent: Intent) {
super.onEnabled(context, intent)
Toast.makeText(context, "已启用", Toast.LENGTH_SHORT).show()
}
override fun onTransferOwnershipComplete(context: Context, bundle: PersistableBundle?) {
super.onTransferOwnershipComplete(context, bundle)
if(bundle!=null){
Toast.makeText(context,"转移所有权完毕,附加内容长度:${bundle.size()}",Toast.LENGTH_SHORT).show()
Log.d("TransferOwnerShip",bundle.toString())
}else{
Toast.makeText(context,"转移所有权完毕,无附加内容}",Toast.LENGTH_SHORT).show()
val myDpm = context.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager
val myComponent = ComponentName(context, this::class.java)
if(myDpm.isAdminActive(myComponent)||isProfileOwner(myDpm)||isDeviceOwner(myDpm)){
Toast.makeText(context, context.getString(R.string.onEnabled), Toast.LENGTH_SHORT).show()
if(myDpm.isAdminActive(myComponent)&&!isProfileOwner(myDpm)&&!isDeviceOwner(myDpm)){ backToHome=true }
}
}
override fun onProfileProvisioningComplete(context: Context, intent: Intent) {
super.onProfileProvisioningComplete(context, intent)
Toast.makeText(context, "创建工作资料完成", Toast.LENGTH_SHORT).show()
}
@SuppressLint("UnsafeProtectedBroadcastReceiver")
override fun onReceive(context: Context, intent: Intent) {
super.onReceive(context, intent)
}
override fun onNetworkLogsAvailable(context: Context, intent: Intent, batchToken: Long, networkLogsCount: Int) {
super.onNetworkLogsAvailable(context, intent, batchToken, networkLogsCount)
Toast.makeText(context,"可以收集网络日志",Toast.LENGTH_SHORT).show()
Log.e("","网络日志可用")
}
override fun onSecurityLogsAvailable(context: Context, intent: Intent) {
super.onSecurityLogsAvailable(context, intent)
Toast.makeText(context,"可以收集安全日志",Toast.LENGTH_SHORT).show()
Log.e("","安全日志可用")
}
override fun onDisableRequested(context: Context, intent: Intent): CharSequence {
Toast.makeText(context, "撤销授权", Toast.LENGTH_SHORT).show()
return "这是取消时的提示"
}
override fun onDisabled(context: Context, intent: Intent) {
super.onDisabled(context, intent)
Toast.makeText(context, "已禁用", Toast.LENGTH_SHORT).show()
Toast.makeText(context, context.getString(R.string.onDisabled), Toast.LENGTH_SHORT).show()
}
override fun onSystemUpdatePending(context: Context, intent: Intent, receivedTime: Long) {
if (VERSION.SDK_INT < 26) { return }
Toast.makeText(context, "新的系统更新!", Toast.LENGTH_SHORT).show()
override fun onProfileProvisioningComplete(context: Context, intent: Intent) {
super.onProfileProvisioningComplete(context, intent)
Toast.makeText(context, context.getString(R.string.create_work_profile_success), Toast.LENGTH_SHORT).show()
}
}
class PackageInstallerReceiver:BroadcastReceiver(){
override fun onReceive(context: Context, intent: Intent) {
val toastText = when(intent.getIntExtra(EXTRA_STATUS,666)){
STATUS_PENDING_USER_ACTION->"等待用户交互"
STATUS_SUCCESS->"成功"
STATUS_FAILURE->"失败"
STATUS_SUCCESS->context.getString(R.string.success)
STATUS_FAILURE->context.getString(R.string.fail)
STATUS_FAILURE_BLOCKED->"失败:被阻止"
STATUS_FAILURE_ABORTED->"失败:被打断"
STATUS_FAILURE_INVALID->"失败:无效"
@@ -76,8 +48,8 @@ class PackageInstallerReceiver:BroadcastReceiver(){
STATUS_FAILURE_STORAGE->"失败:空间不足"
STATUS_FAILURE_INCOMPATIBLE->"失败:不兼容"
STATUS_FAILURE_TIMEOUT->"失败:超时"
else->"未知"
else->context.getString(R.string.unknown)
}
Log.e("静默安装","${intent.getIntExtra(EXTRA_STATUS,666)}:$toastText")
Toast.makeText(context, toastText, Toast.LENGTH_SHORT).show()
}
}