diff --git a/.idea/misc.xml b/.idea/misc.xml index 74dd639..b2c751a 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,3 @@ - diff --git a/app/src/main/java/rip/min/digica/service/DigicaService.kt b/app/src/main/java/rip/min/digica/service/DigicaService.kt index 8394656..047dc68 100644 --- a/app/src/main/java/rip/min/digica/service/DigicaService.kt +++ b/app/src/main/java/rip/min/digica/service/DigicaService.kt @@ -7,12 +7,15 @@ import android.os.Handler import android.provider.Settings import android.util.Log import android.view.accessibility.AccessibilityEvent +import androidx.collection.emptyLongSet import java.util.Calendar class DigicaService : AccessibilityService() { private lateinit var appLabel: String private lateinit var serviceLabel: String + private var wasLastViewingAppInfo = false + override fun onCreate() { super.onCreate() @@ -29,15 +32,31 @@ class DigicaService : AccessibilityService() { if (event.eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) { val pkgName = event.packageName.toString() + val className = event.className; + + // Is force stop dialog possibly open? + val possiblyForceStop = pkgName == "com.android.settings" + && className == "androidx.compose.ui.window.DialogWrapper" + + // Check if we are on App info page in settings + if (pkgName == "com.android.settings") { + if (className == "com.android.settings.spa.SpaActivity" + && event.text.getOrNull(0) == "App info" + ) wasLastViewingAppInfo = true; + // If we did not switch to what is maybe the force stop dialog, + // reset the flag + else if (!possiblyForceStop) + wasLastViewingAppInfo = false; + } // Is user trying to uninstall this app? val attemptingUninstall = pkgName == "com.google.android.packageinstaller" - && event.className == "android.app.AlertDialog" + && className == "android.app.AlertDialog" && event.text.getOrNull(0) == appLabel // Is user trying to disable the service? val attemptingDisableService = pkgName == "com.android.settings" - && event.className == "android.app.AlertDialog" + && className == "android.app.AlertDialog" && event.text.size == 3 && event.text[0].contains(serviceLabel) @@ -46,13 +65,16 @@ class DigicaService : AccessibilityService() { || pkgName == "com.aliucord" || pkgName == "app.revanced.android.youtube" + // Is user trying to force stop? + val attemptingForceStop = possiblyForceStop && wasLastViewingAppInfo; + // Get time val cal = Calendar.getInstance() val day = cal.get(Calendar.DAY_OF_WEEK) val hour = cal.get(Calendar.HOUR_OF_DAY) // Is it late? - val lateHour = when(day) { + val lateHour = when (day) { // No early classes on these days // Late is 1:00AM Calendar.THURSDAY, Calendar.FRIDAY, Calendar.SATURDAY -> 1 @@ -65,13 +87,17 @@ class DigicaService : AccessibilityService() { // Is date set automatically? val autoDate = Settings.Global.getInt(contentResolver, Settings.Global.AUTO_TIME) == 1 - && Settings.Global.getInt(contentResolver, Settings.Global.AUTO_TIME_ZONE) == 1 + && Settings.Global.getInt( + contentResolver, + Settings.Global.AUTO_TIME_ZONE + ) == 1 if ( (isLate || !autoDate) && (attemptingUninstall - || attemptingDisableService - || usingApp) + || attemptingDisableService + || attemptingForceStop + || usingApp) ) { // Stop them from doing that Log.i("DigicaService", "Preventing action") diff --git a/app/src/main/res/xml/accessibility_service_config.xml b/app/src/main/res/xml/accessibility_service_config.xml index 1e31b22..51ea260 100644 --- a/app/src/main/res/xml/accessibility_service_config.xml +++ b/app/src/main/res/xml/accessibility_service_config.xml @@ -1,9 +1,9 @@ \ No newline at end of file