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
|
package moe.yuuta.workmode.access
import android.annotation.SuppressLint
import android.content.Context
import android.content.pm.ApplicationInfo
import android.content.pm.IPackageManager
import android.content.pm.LauncherApps
import android.content.pm.PackageManager
import android.os.Bundle
import android.os.Parcel
import android.os.PersistableBundle
import android.os.UserHandle
import android.system.Os
import moe.yuuta.workmode.BuildConfig
import java.lang.reflect.Field
import java.lang.reflect.Method
/**
* An layer to access package suspending related APIs, it is a low-level layer which is used to call System APIs directly.
*/
internal class AccessLayer(internal val mContext: Context) {
private val mPM: PackageManager = mContext.packageManager
@SuppressLint("PrivateApi")
fun setPackagesSuspended(packageNames: Array<String>, suspended: Boolean,
appExtras: PersistableBundle, launcherExtras: PersistableBundle,
dialogMessage: String, userId: Int): Array<String> {
// ApplicationPackageManager ALWAYS uses hostContext.getOpPackageName() as the argument "callingPackage"
// My callingPackage MUSTN'T equals to 'android'
// If we are using packageName of 'android', system will show disabled
// by admin dialog instead of suspended dialog
// F**k Google
// It's an unstable design
val iPM: Field = mPM::class.java.getDeclaredField("mPM")
iPM.isAccessible = true
val pm = iPM.get(mPM) as IPackageManager
return pm.setPackagesSuspendedAsUser(packageNames,
suspended,
appExtras,
launcherExtras,
dialogMessage,
BuildConfig.APPLICATION_ID,
userId) as Array<String>
}
/**
* This method will SET your UID and you WON'T BE ABLE TO GO BACK.
* Create a new process and access it.
*/
fun getSuspendedPackageAppExtras(packageName: String, userId: Int): PersistableBundle? {
Os.setuid(mPM.getPackageUid(packageName, PackageManager.MATCH_DISABLED_COMPONENTS))
// ApplicationPackageManager ALWAYS uses hostContext.getOpPackageName() as the package name
// F**k Google
val func: Method = Class.forName("android.content.pm.IPackageManager")
.getDeclaredMethod("getSuspendedPackageAppExtras",
String::class.java,
Int::class.java)
// It's an unstable design
val iPM: Field = mPM::class.java.getDeclaredField("mPM")
iPM.isAccessible = true
return func.invoke(iPM.get(mPM),
packageName,
userId) as PersistableBundle?
}
@Throws(PackageManager.NameNotFoundException::class)
fun isPackageSuspended(packageName: String, userId: Int): Boolean {
val func: Method = PackageManager::class.java.getDeclaredMethod("isPackageSuspendedForUser",
String::class.java,
Int::class.java)
return func.invoke(mPM, packageName, userId) as Boolean
}
fun getSuspendedPackageLauncherExtras(packageName: String, userId: Int): Bundle? =
mContext.getSystemService(LauncherApps::class.java).getSuspendedPackageLauncherExtras(packageName,
createUserHandleWithUserID(userId))
fun getInstalledApplicationsAsUser(@PackageManager.ApplicationInfoFlags flags: Int, userId: Int): List<ApplicationInfo> =
mPM.getInstalledApplicationsAsUser(flags, userId)
companion object {
fun createUserHandleWithUserID(userId: Int): UserHandle {
val parcel = Parcel.obtain()
parcel.writeInt(userId)
// I bet that it won't change a lot
val userHandle = UserHandle(parcel)
parcel.recycle()
return userHandle
}
}
}
|