-
-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathShizukuAppLockService.kt
More file actions
255 lines (208 loc) · 9.56 KB
/
ShizukuAppLockService.kt
File metadata and controls
255 lines (208 loc) · 9.56 KB
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package dev.pranav.applock.services
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.Service
import android.app.admin.DevicePolicyManager
import android.content.ComponentName
import android.content.Intent
import android.content.pm.PackageManager
import android.content.pm.ServiceInfo
import android.os.Build
import android.os.IBinder
import android.util.Log
import androidx.annotation.RequiresApi
import androidx.core.app.NotificationCompat
import dev.pranav.applock.R
import dev.pranav.applock.core.broadcast.DeviceAdmin
import dev.pranav.applock.core.utils.appLockRepository
import dev.pranav.applock.data.repository.AppLockRepository
import dev.pranav.applock.data.repository.AppLockRepository.Companion.shouldStartService
import dev.pranav.applock.data.repository.BackendImplementation
import dev.pranav.applock.features.lockscreen.ui.PasswordOverlayActivity
import dev.pranav.applock.shizuku.ShizukuActivityManager
import rikka.shizuku.Shizuku
class ShizukuAppLockService : Service() {
private val appLockRepository: AppLockRepository by lazy { applicationContext.appLockRepository() }
private var shizukuActivityManager: ShizukuActivityManager? = null
private var previousForegroundPackage = ""
private val notificationManager: NotificationManager by lazy {
getSystemService(NotificationManager::class.java)
}
companion object {
private const val TAG = "ShizukuAppLockService"
private const val NOTIFICATION_ID = 112
private const val CHANNEL_ID = "ShizukuAppLockServiceChannel"
@Volatile
var isServiceRunning = false
}
override fun onCreate() {
super.onCreate()
Log.d(TAG, "ShizukuAppLockService created")
AppLockManager.isLockScreenShown.set(false)
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
Log.d(TAG, "ShizukuAppLockService started. Running: $isServiceRunning")
if (isServiceRunning) return START_STICKY
isServiceRunning = true
if (!shouldStartService(appLockRepository, this::class.java) || !isShizukuAvailable()) {
Log.e(TAG, "Service not needed or Shizuku not ready. Triggering fallback if necessary.")
isServiceRunning = false
AppLockManager.startFallbackServices(this, ShizukuAppLockService::class.java)
stopSelf()
return START_NOT_STICKY
}
AppLockManager.resetRestartAttempts(TAG)
appLockRepository.setActiveBackend(BackendImplementation.SHIZUKU)
AppLockManager.stopAllOtherServices(this, this::class.java)
setupShizukuActivityManager()
val shizukuStarted = shizukuActivityManager?.start() == true
if (!shizukuStarted) {
Log.e(TAG, "Shizuku failed to start, triggering fallback")
isServiceRunning = false
AppLockManager.startFallbackServices(this, ShizukuAppLockService::class.java)
stopSelf()
return START_NOT_STICKY
}
startForegroundService()
return START_STICKY
}
override fun onDestroy() {
Log.d(TAG, "ShizukuAppLockService destroyed.")
shizukuActivityManager?.stop()
if (isServiceRunning && shouldStartService(appLockRepository, this::class.java)) {
Log.d(TAG, "Service destroyed unexpectedly, starting fallback")
AppLockManager.startFallbackServices(this, ShizukuAppLockService::class.java)
}
isServiceRunning = false
notificationManager.cancel(NOTIFICATION_ID)
super.onDestroy()
}
override fun onBind(intent: Intent?): IBinder? = null
override fun onUnbind(intent: Intent?): Boolean {
Log.d(TAG, "ShizukuAppLockService unbound. Checking for necessary restart.")
if (shouldStartService(appLockRepository, this::class.java)) {
AppLockManager.startFallbackServices(this, ShizukuAppLockService::class.java)
}
return super.onUnbind(intent)
}
private fun isShizukuAvailable(): Boolean {
return Shizuku.pingBinder() && Shizuku.checkSelfPermission() == PackageManager.PERMISSION_GRANTED
}
@RequiresApi(Build.VERSION_CODES.P)
private fun startForegroundService() {
createNotificationChannel()
val notification = createNotification()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
val type = determineForegroundServiceType()
startForeground(NOTIFICATION_ID, notification, type)
} else {
startForeground(NOTIFICATION_ID, notification)
}
}
@RequiresApi(Build.VERSION_CODES.P)
private fun determineForegroundServiceType(): Int {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
val dpm = getSystemService(DEVICE_POLICY_SERVICE) as DevicePolicyManager
val component = ComponentName(this, DeviceAdmin::class.java)
return if (dpm.isAdminActive(component)) {
ServiceInfo.FOREGROUND_SERVICE_TYPE_SYSTEM_EXEMPTED
} else {
ServiceInfo.FOREGROUND_SERVICE_TYPE_SPECIAL_USE
}
}
return 0
}
private fun createNotificationChannel() {
val serviceChannel = NotificationChannel(
CHANNEL_ID,
"AppLock Service",
NotificationManager.IMPORTANCE_DEFAULT
)
notificationManager.createNotificationChannel(serviceChannel)
}
private fun createNotification(): Notification {
return NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("AppLock")
.setContentText("Protecting your apps with Shizuku")
.setSmallIcon(R.drawable.baseline_shield_24)
.setPriority(NotificationCompat.PRIORITY_MIN)
.setOngoing(true)
.build()
}
private fun setupShizukuActivityManager() {
shizukuActivityManager =
ShizukuActivityManager(this, appLockRepository) { packageName, _, timeMillis ->
try {
val triggeringPackage = previousForegroundPackage
previousForegroundPackage = packageName
if (AppLockManager.isLockScreenShown.get() || packageName == this.packageName) {
return@ShizukuActivityManager
}
val triggerExclusions = appLockRepository.getTriggerExcludedApps()
if (triggeringPackage in triggerExclusions) {
Log.d(
TAG,
"Trigger app $triggeringPackage is excluded, skipping lock for $packageName"
)
return@ShizukuActivityManager
}
Log.d(TAG, "Current package=$packageName, trigger=$triggeringPackage")
checkAndLockApp(packageName, triggeringPackage, timeMillis)
} catch (e: Exception) {
Log.e(TAG, "Error in ShizukuActivityManager callback", e)
}
}
}
private fun checkAndLockApp(packageName: String, triggeringPackage: String, currentTime: Long) {
val lockedApps = appLockRepository.getLockedApps()
if (packageName !in lockedApps) return
val unlockDurationMinutes = appLockRepository.getUnlockTimeDuration()
// If duration is 0 (lock immediately), clear any unlock state
if (unlockDurationMinutes == 0) {
AppLockManager.appUnlockTimes.remove(packageName)
}
val unlockTimestamp = AppLockManager.appUnlockTimes[packageName] ?: 0L
Log.d(
TAG,
"checkAndLockApp: pkg=$packageName, duration=$unlockDurationMinutes min, unlockTime=$unlockTimestamp, currentTime=$currentTime, isLockScreenShown=${AppLockManager.isLockScreenShown.get()}"
)
if (unlockDurationMinutes > 0 && unlockTimestamp > 0) {
if (unlockDurationMinutes >= 10_000) {
return
}
val durationMillis = unlockDurationMinutes.toLong() * 60_000L
val elapsedMillis = currentTime - unlockTimestamp
Log.d(
TAG,
"Grace period check: elapsed=${elapsedMillis}ms (${elapsedMillis / 1000}s), duration=${durationMillis}ms (${durationMillis / 1000}s)"
)
if (elapsedMillis < durationMillis) {
return
}
Log.d(TAG, "Unlock grace period expired for $packageName. Clearing timestamp.")
AppLockManager.appUnlockTimes.remove(packageName)
}
if (AppLockManager.isLockScreenShown.get()) {
Log.d(TAG, "Lock screen already shown, skipping")
return
}
Log.d(TAG, "Locked app detected: $packageName. Showing overlay.")
AppLockManager.isLockScreenShown.set(true)
val intent = Intent(this, PasswordOverlayActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK or
Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS or
Intent.FLAG_ACTIVITY_NO_ANIMATION or
Intent.FLAG_FROM_BACKGROUND or
Intent.FLAG_ACTIVITY_REORDER_TO_FRONT
putExtra("locked_package", packageName)
putExtra("triggering_package", triggeringPackage)
}
try {
startActivity(intent)
} catch (e: Exception) {
AppLockManager.isLockScreenShown.set(false)
Log.e(TAG, "Failed to start password overlay: ${e.message}", e)
}
}
}