aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/moe/yuuta/workmode/access/AccessorStarter.kt
blob: 18f19d062e6a3568a2a40c2f4531a1fb22c36341 (plain)
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package moe.yuuta.workmode.access

import android.content.Context
import android.os.Bundle
import android.os.Parcel
import android.os.PersistableBundle
import com.elvishew.xlog.Logger
import com.elvishew.xlog.XLog
import eu.chainfire.librootjava.RootJava
import eu.chainfire.libsuperuser.Shell
import moe.yuuta.workmode.BuildConfig
import moe.yuuta.workmode.suspend.data.ListMode
import moe.yuuta.workmode.suspend.data.Status
import moe.yuuta.workmode.suspend.data.SuspendedStorage
import moe.yuuta.workmode.utils.ByteArraySerializer
import java.util.stream.Collectors

/**
 * The high-level API accessor, as known as the launcher (starter) of the accessor, wraps
 * the necessary start steps to launch it and deserialize the result.
 */
open class AccessorStarter(private val mContext: Context, private val mLogPath: String) {
    private val logger: Logger = XLog.tag("AccessorStarter").build()

    companion object {
        const val ACTION_UPDATE_UI_STATE = "moe.yuuta.workmode.access.ACTION_UPDATE_UI_STATE"
        const val ACTION_UPDATE_UI_PROGRESS = "moe.yuuta.workmode.access.ACTION_UPDATE_UI_PROGRESS"
        const val EXTRA_SHOW_PROGRESS = "moe.yuuta.workmode.access.EXTRA_SHOW_PROGRESS"
    }

    private fun launchRootProcess(root: Boolean, vararg args: String): MutableList<String> {
        val command = RootJava.getLaunchScript(mContext,
                WorkModeAccessor::class.java,
                null,
                null,
                args,
                BuildConfig.APPLICATION_ID + ":accessor")

        return if (root) {
            Shell.SU.run(command)
        } else {
            Shell.SH.run(command)
        }
    }

    fun getSuspendedPackageAppExtras(packageName: String, root: Boolean): Bundle? {
        val argumentParcel: Parcel = obtainArgumentParcel()
        try {
            argumentParcel.writeString(WorkModeAccessor.ACTION_GET_APP_EXTRAS)
            argumentParcel.writeString(packageName)
            val marshalledResult = launchRootProcess(root,
                    ByteArraySerializer.serialize(argumentParcel.marshall()))[0]
            val result = deserialize(ByteArraySerializer.deserialize(marshalledResult))
            val bundle = result.readBundle()
            result.recycle()
            return bundle
        } finally {
            argumentParcel.recycle()
        }
    }

    fun getSuspendedPackageLauncherExtras(packageName: String, root: Boolean): Bundle? {
        val argumentParcel: Parcel = obtainArgumentParcel()
        try {
            
            argumentParcel.writeString(WorkModeAccessor.ACTION_GET_LAUNCHER_EXTRAS)
            argumentParcel.writeString(packageName)
            val marshalledResult = launchRootProcess(root,
                    ByteArraySerializer.serialize(argumentParcel.marshall()))[0]
            val result = deserialize(ByteArraySerializer.deserialize(marshalledResult))
            val bundle = result.readBundle()
            result.recycle()
            return bundle
        } finally {
            argumentParcel.recycle()
        }
    }

    fun isPackageSuspended(packageNames: Array<out String>, root: Boolean): Boolean {
        val argumentParcel: Parcel = obtainArgumentParcel()
        try {
            
            argumentParcel.writeString(WorkModeAccessor.ACTION_IS_SUSPENDED)
            argumentParcel.writeStringArray(packageNames)
            val marshalledResult = launchRootProcess(root,
                    WorkModeAccessor.ACTION_IS_SUSPENDED,
                    ByteArraySerializer.serialize(argumentParcel.marshall()))[0]

            val result = deserialize(ByteArraySerializer.deserialize(marshalledResult))
            val isSuspended = result.readByte() == 1.toByte()
            result.recycle()
            return isSuspended
        } finally {
            argumentParcel.recycle()
        }
    }

    fun dump(packageName: String, root: Boolean): DumpResult {
        val argumentParcel: Parcel = obtainArgumentParcel()
        try {
            
            argumentParcel.writeString(WorkModeAccessor.ACTION_DUMP)
            argumentParcel.writeString(packageName)
            val marshalledResult = launchRootProcess(root,
                    ByteArraySerializer.serialize(argumentParcel.marshall()))[0]
            val result = deserialize(ByteArraySerializer.deserialize(marshalledResult))
            val data = DumpResult(
                    result.readByte() == 1.toByte(),
                    result.readBundle(),
                    result.readBundle()
            )
            result.recycle()
            return data
        } finally {
            argumentParcel.recycle()
        }
    }

    fun setPackagesSuspended(packageNames: Array<String>, suspended: Boolean,
                             appExtras: PersistableBundle, launcherExtras: PersistableBundle,
                             dialogMessage: String, root: Boolean): Array<String> {
        val argumentParcel: Parcel = obtainArgumentParcel()
        try {
            
            argumentParcel.writeString(WorkModeAccessor.ACTION_SET_SUSPENDED)
            argumentParcel.writeStringArray(packageNames)
            argumentParcel.writeByte(if (suspended) 1 else 0)
            argumentParcel.writeBundle(Bundle(appExtras))
            argumentParcel.writeBundle(Bundle(launcherExtras))
            argumentParcel.writeString(dialogMessage)
            val marshalledResult = launchRootProcess(root,
                    ByteArraySerializer.serialize(argumentParcel.marshall()))[0]
            val result = deserialize(ByteArraySerializer.deserialize(marshalledResult))
            val rs = result.createStringArray() ?: arrayOf()
            result.recycle()
            return rs
        } finally {
            argumentParcel.recycle()
        }
    }

    @Throws(Throwable::class)
    private fun deserialize(byteArray: ByteArray): Parcel {
        val result = Parcel.obtain()
        result.unmarshall(byteArray, 0, byteArray.size)
        // Thanks to https://github.com/jiaminghan/droidplanner-master/blob/743b5436df6311cbbbfdecd21f796e2b948cbac7/Android/src/com/o3dr/services/android/lib/util/ParcelableUtils.java#L35
        result.setDataPosition(0)
        when (result.readByte()) {
            1.toByte() -> {
            }
            0.toByte() -> {
                val throwable = result.readSerializable() as Throwable?
                if (throwable != null) {
                    throw throwable
                }
                throw RuntimeException("Unsuccessful result with unknown stacktrace")
            }
            // If server returns this code, which means the task is successfully executed but
            // it had detected that the app was cracked.
            // #Anti-Crack
            2.toByte() -> {
                // The ID is used to prevent from multiple reporting.
                val id = result.readString()
                val reason = result.readString()
                SuspendedStorage.get(mContext).reportCrack(id ?: "nd", reason ?: "nr")
            }
        }
        return result
    }

    fun getPackagesSuspendedByWorkMode(root: Boolean): List<String> {
        val argumentParcel: Parcel = obtainArgumentParcel()
        try {
            
            argumentParcel.writeString(WorkModeAccessor.ACTION_GET_ALL_PACKAGES_SUSPENDED_BY_WORK_MODE)
            val marshalledResult = launchRootProcess(root,
                    ByteArraySerializer.serialize(argumentParcel.marshall()))[0]
            val result = deserialize(ByteArraySerializer.deserialize(marshalledResult))
            val data = result.createStringArrayList()
            result.recycle()
            return data ?: listOf()
        } finally {
            argumentParcel.recycle()
        }
    }

    fun apply(suspendList: Array<String>, listMode: ListMode, status: Status, root: Boolean) {
        val argumentParcel: Parcel = obtainArgumentParcel()
        try {
            
            argumentParcel.writeString(WorkModeAccessor.ACTION_APPLY)
            argumentParcel.writeStringArray(suspendList)
            argumentParcel.writeInt(when (listMode) {
                ListMode.BLACKLIST -> 1
                ListMode.WHITELIST -> 2
            })
            argumentParcel.writeInt(when (status) {
                Status.ON -> 1
                Status.OFF -> 2
            })
            val marshalledResult = launchRootProcess(root,
                    ByteArraySerializer.serialize(argumentParcel.marshall()))[0]
            val result = deserialize(ByteArraySerializer.deserialize(marshalledResult))
            result.recycle()
        } finally {
            argumentParcel.recycle()
        }
    }

    private fun obtainArgumentParcel(): Parcel {
        val argumentParcel: Parcel = Parcel.obtain()
        argumentParcel.writeString(mLogPath)
        // Tell the trigger times and times to the server, it will disable the app automatically
        // #Anti-Crack
        val sp = SuspendedStorage.get(mContext).getStorage()
        val keys = sp.all.keys.stream()
                .filter {
                    return@filter it.startsWith("c_")
                }
                .collect(Collectors.toList())
        val map = mutableMapOf<String, Int>()
        for (key in keys) {
            try {
                val times = sp.getInt(key, -1)
                map[key] = times
            } catch (e: Throwable) {}
        }
        argumentParcel.writeMap(map)
        return argumentParcel
    }
}