/
ContextWrapper.kt
134 lines (125 loc) · 4.42 KB
/
ContextWrapper.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package com.iflytek.cyber.iot.show.core.utils
import android.app.Activity
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Process
import android.os.UserHandle
import java.lang.reflect.InvocationTargetException
object ContextWrapper {
fun startServiceAsUser(context: Context, intent: Intent, userHandle: String) {
try {
if (Process.myUid() == Process.SYSTEM_UID) {
val clz = context::class.java
val method =
clz.getMethod("startServiceAsUser", Intent::class.java, UserHandle::class.java)
method.isAccessible = true
method.invoke(context, intent, newUserHandle(userHandle))
} else {
context.startService(intent)
}
} catch (t: Throwable) {
if (t is InvocationTargetException) {
context.startService(intent)
} else {
t.printStackTrace()
}
}
}
fun startActivityAsUser(activity: Activity, intent: Intent, userHandle: String) {
try {
if (Process.myUid() == Process.SYSTEM_UID) {
val clz = activity::class.java
val method =
clz.getMethod("startActivityAsUser", Intent::class.java, UserHandle::class.java)
method.isAccessible = true
method.invoke(activity, intent, newUserHandle(userHandle))
}
} catch (t: Throwable) {
if (t is InvocationTargetException) {
activity.startActivity(intent)
} else {
t.printStackTrace()
}
}
}
fun getBroadcastAsUser(
context: Context?,
requestCode: Int,
intent: Intent,
flags: Int,
userHandle: String
): PendingIntent? {
try {
if (Process.myUid() == Process.SYSTEM_UID) {
val clz = PendingIntent::class.java
val method =
clz.getMethod(
"getBroadcastAsUser",
Context::class.java,
Int::class.java,
Intent::class.java,
Int::class.java,
UserHandle::class.java
)
method.isAccessible = true
return method.invoke(
null,
context,
requestCode,
intent,
flags,
newUserHandle(userHandle)
) as? PendingIntent
} else {
PendingIntent.getBroadcast(context, requestCode, intent, flags)
}
} catch (t: Throwable) {
if (t is InvocationTargetException) {
return PendingIntent.getBroadcast(context, requestCode, intent, flags)
} else {
t.printStackTrace()
}
}
return null
}
fun startForegroundServiceAsUser(context: Context, intent: Intent, userHandle: String) {
try {
if (Process.myUid() == Process.SYSTEM_UID) {
val clz = context::class.java
val method =
clz.getMethod(
"startForegroundServiceAsUser",
Intent::class.java,
UserHandle::class.java
)
method.isAccessible = true
method.invoke(context, intent, newUserHandle(userHandle))
} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(intent)
}
}
} catch (t: Throwable) {
if (t is InvocationTargetException) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(intent)
}
} else {
t.printStackTrace()
}
}
}
private fun newUserHandle(userHandle: String): UserHandle? {
try {
val clz = UserHandle::class.java
val all = clz.getDeclaredField(userHandle)
all.isAccessible = true
return all.get(null) as UserHandle
} catch (t: Throwable) {
t.printStackTrace()
}
return null
}
}