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
|
package moe.yuuta.workmode.suspend.data
import android.annotation.SystemApi
import android.content.pm.PackageManager
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.os.Parcel
import android.os.Parcelable
import android.os.Process
import android.text.TextUtils
import com.elvishew.xlog.XLog
/**
* The data of Suspended app which can be transfered through IPC only
*/
data class TransferableSuspendedApp(
val userId: Int,
val packageName: String,
// Null if it has the same user id with the host
var label: CharSequence?,
// -1 if not available
var uid: Int,
// Null if it has the same user id with the host
var icon: Bitmap?
) : Parcelable {
constructor(parcel: Parcel) : this(
parcel.readInt(),
parcel.readString(),
TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel),
parcel.readInt(),
parcel.readParcelable<Bitmap>(Bitmap::class.java.classLoader)
)
fun trimToPersistable(): PersistableSuspendedApp =
PersistableSuspendedApp(userId, packageName)
/**
* Fill all fields.
*/
@SystemApi
fun fillData(pm: PackageManager, ignoreLargeData: Boolean) {
val info = pm.getApplicationInfoAsUser(packageName,
0,
userId)
label = if (ignoreLargeData) null
else pm.getApplicationLabel(info)
uid = info.uid
if (ignoreLargeData) {
icon = null
} else {
XLog.d("Loading icon for ${info.packageName}")
val res = pm.getResourcesForApplicationAsUser(info.packageName, userId)
icon = BitmapFactory.decodeResource(res, info.icon)
}
}
/**
* Remove unnecessary data for in-app transaction
*/
fun trimData() {
label = null
uid = -1
icon = null
}
companion object {
fun of(packageName: String): TransferableSuspendedApp {
return of(packageName, Process.myUserHandle().hashCode())
}
fun of(packageName: String, userId: Int): TransferableSuspendedApp {
return PersistableSuspendedApp(userId, packageName).copyToSimpleTransferableInfo()
}
@JvmField
val CREATOR = object : Parcelable.Creator<TransferableSuspendedApp> {
override fun createFromParcel(parcel: Parcel): TransferableSuspendedApp {
return TransferableSuspendedApp(parcel)
}
override fun newArray(size: Int): Array<TransferableSuspendedApp?> {
return arrayOfNulls(size)
}
}
}
override fun writeToParcel(parcel: Parcel, flags: Int) {
parcel.writeInt(userId)
parcel.writeString(packageName)
TextUtils.writeToParcel(label, parcel, flags)
parcel.writeInt(uid)
parcel.writeParcelable(icon, 0)
}
override fun describeContents(): Int {
return 0
}
fun essentiallyEqual(that: TransferableSuspendedApp): Boolean = that.userId == this.userId &&
that.packageName == this.packageName
}
|