Skip to content

Commit b22fb08

Browse files
committed
fix lock screen orientation issues
- Implement `setUninstallBlocked` in `DeviceAdmin` to programmatically prevent app uninstallation when anti-uninstall is enabled - Update `PasswordOverlayActivity` to handle configuration changes (orientation, screen size) without finishing the activity - Fix logic in `PasswordOverlayScreen.kt` to prevent the lock screen from dismissing during configuration changes - Refactor `AppLockAccessibilityService` to improve detection of device admin settings pages and simplify deactivation checks - Remove redundant `ADMIN_CONFIG_CLASSES` and simplify exclusion logic in `ExperimentalAppLockService` - Add logging for orientation changes in `PasswordOverlayScreen`
1 parent 4a0effc commit b22fb08

File tree

6 files changed

+22
-24
lines changed

6 files changed

+22
-24
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555

5656
<activity
5757
android:name=".features.lockscreen.ui.PasswordOverlayActivity"
58+
android:configChanges="orientation|screenSize|screenLayout|keyboardHidden"
5859
android:excludeFromRecents="true"
5960
android:exported="false"
6061
android:taskAffinity=""

app/src/main/java/dev/pranav/applock/core/broadcast/DeviceAdmin.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package dev.pranav.applock.core.broadcast
22

33
import android.app.admin.DeviceAdminReceiver
4+
import android.content.ComponentName
45
import android.content.Context
56
import android.content.SharedPreferences
67
import androidx.core.content.edit
@@ -16,6 +17,10 @@ class DeviceAdmin : DeviceAdminReceiver() {
1617
context.getSharedPreferences("app_lock_settings", Context.MODE_PRIVATE).edit {
1718
putBoolean("anti_uninstall", true)
1819
}
20+
21+
val component = ComponentName(context, DeviceAdmin::class.java)
22+
23+
getManager(context).setUninstallBlocked(component, context.packageName, true)
1924
}
2025

2126
override fun onDisabled(context: Context, intent: android.content.Intent) {

app/src/main/java/dev/pranav/applock/features/lockscreen/ui/PasswordOverlayScreen.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package dev.pranav.applock.features.lockscreen.ui
22

33
import android.content.Context
4+
import android.content.res.Configuration
45
import android.os.Build
56
import android.os.Bundle
67
import android.util.Log
@@ -107,6 +108,10 @@ class PasswordOverlayActivity : FragmentActivity() {
107108
}
108109
}
109110

111+
override fun onConfigurationChanged(newConfig: Configuration) {
112+
super.onConfigurationChanged(newConfig)
113+
Log.d(TAG, "Configuration changed - orientation: ${newConfig.orientation}")
114+
}
110115

111116
private fun setupWindow() {
112117
window.addFlags(
@@ -285,7 +290,7 @@ class PasswordOverlayActivity : FragmentActivity() {
285290

286291
override fun onPause() {
287292
super.onPause()
288-
if (!isBiometricPromptShowingLocal && !movedToBackground) {
293+
if (!isChangingConfigurations() && !isBiometricPromptShowingLocal && !movedToBackground) {
289294
AppLockManager.isLockScreenShown.set(false)
290295
AppLockManager.reportBiometricAuthFinished()
291296
finish()
@@ -301,7 +306,7 @@ class PasswordOverlayActivity : FragmentActivity() {
301306
super.onStop()
302307
movedToBackground = true
303308
AppLockManager.isLockScreenShown.set(false)
304-
if (!isFinishing && !isDestroyed) {
309+
if (!isChangingConfigurations() && !isFinishing && !isDestroyed) {
305310
AppLockManager.reportBiometricAuthFinished()
306311
finish()
307312
}

app/src/main/java/dev/pranav/applock/services/AppLockAccessibilityService.kt

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import dev.pranav.applock.data.repository.AppLockRepository
2222
import dev.pranav.applock.data.repository.BackendImplementation
2323
import dev.pranav.applock.features.lockscreen.ui.PasswordOverlayActivity
2424
import dev.pranav.applock.services.AppLockConstants.ACCESSIBILITY_SETTINGS_CLASSES
25-
import dev.pranav.applock.services.AppLockConstants.ADMIN_CONFIG_CLASSES
2625
import dev.pranav.applock.services.AppLockConstants.EXCLUDED_APPS
2726
import rikka.shizuku.Shizuku
2827

@@ -112,15 +111,10 @@ class AppLockAccessibilityService : AccessibilityService() {
112111
}
113112

114113
private fun handleAccessibilityEvent(event: AccessibilityEvent) {
115-
// Check for device admin deactivation (anti-uninstall feature)
116114
if (appLockRepository.isAntiUninstallEnabled() &&
117115
event.packageName == DEVICE_ADMIN_SETTINGS_PACKAGE
118116
) {
119-
try {
120-
checkForDeviceAdminDeactivation(event)
121-
} catch (e: Exception) {
122-
logError("Error checking device admin deactivation", e)
123-
}
117+
checkForDeviceAdminDeactivation(event)
124118
}
125119

126120
// Early return if protection is disabled or service is not running
@@ -372,8 +366,6 @@ class AppLockAccessibilityService : AccessibilityService() {
372366
}
373367

374368
private fun checkForDeviceAdminDeactivation(event: AccessibilityEvent) {
375-
val rootNode = rootInActiveWindow ?: return
376-
377369
Log.d(TAG, "Checking for device admin deactivation for event: $event")
378370

379371
// Check if user is trying to deactivate the accessibility service
@@ -385,10 +377,12 @@ class AppLockAccessibilityService : AccessibilityService() {
385377

386378
// Check if on device admin page and our app is visible
387379
val isDeviceAdminPage = isDeviceAdminPage(event)
388-
val isOurAppVisible = findNodeWithTextContaining(rootNode, "App Lock") != null ||
389-
findNodeWithTextContaining(rootNode, "AppLock") != null
380+
//val isOurAppVisible = findNodeWithTextContaining(rootNode, "App Lock") != null ||
381+
// findNodeWithTextContaining(rootNode, "AppLock") != null
390382

391-
if (!isDeviceAdminPage || !isOurAppVisible) {
383+
LogUtils.d(TAG, "User is on device admin page: $isDeviceAdminPage, $event")
384+
385+
if (!isDeviceAdminPage) {
392386
return
393387
}
394388

@@ -404,7 +398,6 @@ class AppLockAccessibilityService : AccessibilityService() {
404398
event.packageName == "com.google.android.packageinstaller" && event.className == "android.app.AlertDialog" && event.text.toString()
405399
.lowercase().contains("App Lock")
406400

407-
408401
return isAccessibilitySettings || isSubSettings || isAlertDialog
409402
}
410403

@@ -423,8 +416,9 @@ class AppLockAccessibilityService : AccessibilityService() {
423416
val hasDeviceAdminDescription = event.contentDescription?.toString()?.lowercase()
424417
?.contains("Device admin app") == true &&
425418
event.className == "android.widget.FrameLayout"
419+
426420
val isAdminConfigClass =
427-
event.className!!.endsWith("DeviceAdminAdd") || event.className in ADMIN_CONFIG_CLASSES
421+
event.className!!.contains("DeviceAdminAdd") || event.className!!.contains("DeviceAdminSettings")
428422

429423
return hasDeviceAdminDescription || isAdminConfigClass
430424
}

app/src/main/java/dev/pranav/applock/services/AppLockManager.kt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,6 @@ object AppLockConstants {
3232
"com.google.android.webview"
3333
)
3434

35-
val ADMIN_CONFIG_CLASSES = setOf(
36-
"com.android.settings.deviceadmin.DeviceAdminSettings"
37-
)
38-
// com.android.settings.spa.SpaActivity
39-
4035
val ACCESSIBILITY_SETTINGS_CLASSES = setOf(
4136
"com.android.settings.accessibility.AccessibilitySettings",
4237
"com.android.settings.accessibility.AccessibilityMenuActivity",

app/src/main/java/dev/pranav/applock/services/ExperimentalAppLockService.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,7 @@ class ExperimentalAppLockService : Service() {
159159
if (event.eventType != UsageEvents.Event.ACTIVITY_RESUMED) continue
160160
if (event.className == "dev.pranav.applock.features.lockscreen.ui.PasswordOverlayActivity") continue
161161

162-
if (event.className in AppLockConstants.KNOWN_RECENTS_CLASSES ||
163-
event.className in AppLockConstants.ADMIN_CONFIG_CLASSES ||
164-
event.className in AppLockConstants.ACCESSIBILITY_SETTINGS_CLASSES
162+
if (event.className in AppLockConstants.KNOWN_RECENTS_CLASSES
165163
) {
166164
continue
167165
}

0 commit comments

Comments
 (0)