Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accessibility Service Overlay MVP #329

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
10 changes: 10 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -153,5 +153,15 @@
<action android:name="android.service.quicksettings.action.QS_TILE" />
</intent-filter>
</service>

<service
android:name=".service.AccessibilityFilterService"
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE"
android:label="@string/overlay_accessibility_service_label"
android:exported="true">
<intent-filter>
<action android:name="android.accessibilityservice.AccessibilityService" />
</intent-filter>
</service>
</application>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@
package com.jmstudios.redmoon

import android.animation.Animator
import android.app.Service
import com.jmstudios.redmoon.service.AccessibilityFilterService
import com.jmstudios.redmoon.service.FilterService

class CommandAnimatorListener(
private val cmd: Command,
private val svc: FilterService)
private val cmd: Command,
private val svc: FilterService,
)
: Animator.AnimatorListener {

override fun onAnimationStart (a: Animator?) = cmd.onAnimationStart (svc)
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/com/jmstudios/redmoon/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ object Config : Preferences(appContext) {
}

var useRoot by BooleanPreference(R.string.pref_key_use_root, false)

var useAccessibilityService by BooleanPreference(R.string.pref_key_use_accessibility_service, false)
//endregion

//region application
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/com/jmstudios/redmoon/EventBus.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,6 @@ class overlayPermissionDenied : Event
class locationAccessDenied : Event
class changeBrightnessDenied : Event

class accessibilityServiceCommand(val command: Command) : Event

data class locationService(val isSearching: Boolean, val isRunning: Boolean = true) : Event
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
*/
package com.jmstudios.redmoon.fragment

import android.content.Intent
import android.os.Bundle
import android.provider.Settings
import android.view.ViewGroup
import android.widget.TextView
import android.widget.Toast
Expand Down Expand Up @@ -57,6 +59,9 @@ class SettingsFragment : PreferenceFragmentCompat() {
private val rootPref: SwitchPreference
get() = pref(R.string.pref_key_use_root) as SwitchPreference

private val accessibilityServicePref: SwitchPreference
get() = pref(R.string.pref_key_use_accessibility_service) as SwitchPreference

private var mSnackbar: Snackbar? = null

override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
Expand Down Expand Up @@ -121,6 +126,26 @@ class SettingsFragment : PreferenceFragmentCompat() {
return@setOnPreferenceChangeListener true
}

accessibilityServicePref.setOnPreferenceChangeListener { _, newValue ->
// Make sure root is available before enabling
if (newValue as Boolean) {
val prefString = Settings.Secure.getString(context?.contentResolver, Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES)
Log.i("FOO 20 FOO 10 ${context?.contentResolver } $prefString")
if(prefString == null || !prefString.contains("com.jmstudios.redmoon.service.AccessibilityFilterService")) {
startActivity(Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS));
return@setOnPreferenceChangeListener false
}
}
if (filterIsOn) {
// It would be better to toggle back on again using the new mode but it's more work
// to add that, since the filter service needs to restart, so we need to wait until
// the fade-out finishes to start again. Or maybe better, the filter service could
// listen for this setting changing and automatically toggle itself. But this works.
AdamNiederer marked this conversation as resolved.
Show resolved Hide resolved
Command.toggle(on = false)
}
return@setOnPreferenceChangeListener true
}

updatePrefs()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.jmstudios.redmoon.service

import android.accessibilityservice.AccessibilityService
import android.animation.ValueAnimator
import android.graphics.PixelFormat
import android.view.View
import android.view.WindowManager
import android.view.accessibility.AccessibilityEvent
import com.jmstudios.redmoon.*
import com.jmstudios.redmoon.helper.Logger
import org.greenrobot.eventbus.Subscribe


class AccessibilityFilterService : AccessibilityService() {
lateinit var mFilter: Overlay

override fun onAccessibilityEvent(event: AccessibilityEvent) {}
override fun onInterrupt() {}

override fun onServiceConnected() {
instance = this;
enabled = true;
mFilter = Overlay(applicationContext)
val lp = WindowManager.LayoutParams().apply {
type = WindowManager.LayoutParams.TYPE_ACCESSIBILITY_OVERLAY
format = PixelFormat.TRANSLUCENT
flags = WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE.or(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)
}
(getSystemService(WINDOW_SERVICE) as WindowManager).addView(mFilter, lp)

mFilter.visibility = View.GONE;

EventBus.register(this)
}

override fun onDestroy() {
super.onDestroy()
EventBus.unregister(this);
enabled = false;
}

@Subscribe
fun turnOnOrOff(cmd: accessibilityServiceCommand) {
Log.i("${cmd.command}")
mFilter.setBackgroundColor(activeProfile.filterColor)
mFilter.visibility = if (cmd.command.turnOn) View.VISIBLE else View.GONE
}

companion object : Logger() {
var enabled = false;
lateinit var instance: AccessibilityFilterService
}

// override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
// return super.onStartCommand(intent, flags, startId)
// }
}
24 changes: 10 additions & 14 deletions app/src/main/java/com/jmstudios/redmoon/service/FilterService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,13 @@ import android.app.Service
import android.content.Intent
import android.os.IBinder
import android.widget.Toast
import com.jmstudios.redmoon.*
import java.util.concurrent.Executors

import com.jmstudios.redmoon.activeProfile
import com.jmstudios.redmoon.Command
import com.jmstudios.redmoon.CommandAnimatorListener
import com.jmstudios.redmoon.Config
import com.jmstudios.redmoon.EventBus
import com.jmstudios.redmoon.Filter
import com.jmstudios.redmoon.filterIsOn
import com.jmstudios.redmoon.filter.surfaceflinger.SurfaceFlinger
import com.jmstudios.redmoon.helper.Logger
import com.jmstudios.redmoon.helper.Permission
import com.jmstudios.redmoon.manager.CurrentAppMonitor
import com.jmstudios.redmoon.Notification
import com.jmstudios.redmoon.Overlay
import com.jmstudios.redmoon.overlayPermissionDenied
import com.jmstudios.redmoon.Profile
import com.jmstudios.redmoon.ProfileEvaluator
import com.jmstudios.redmoon.R
import com.jmstudios.redmoon.secureSuspendChanged

import org.greenrobot.eventbus.Subscribe
import com.topjohnwu.superuser.Shell
Expand Down Expand Up @@ -107,6 +94,11 @@ class FilterService : Service() {
Config.useRoot = false;
stopForeground(false)
}
} else if (Config.useAccessibilityService) {
EventBus.post(accessibilityServiceCommand(Command.getCommand(intent)))
filterIsOn = Command.getCommand(intent).turnOn
mCurrentAppMonitor.monitoring = Command.getCommand(intent).turnOn && Config.secureSuspend
Log.i("$filterIsOn")
} else {
if (Permission.Overlay.isGranted) {
fadeInOrOut()
Expand Down Expand Up @@ -138,12 +130,16 @@ class FilterService : Service() {
filterIsOn = false
mCurrentAppMonitor.monitoring = false
}
if(Config.useAccessibilityService) {
EventBus.post(accessibilityServiceCommand(Command.OFF))
}
mFilter.onDestroy()
executor.shutdownNow()
super.onDestroy()
}

@Subscribe fun onProfileUpdated(profile: Profile) {
Log.i("onProfileUpdated")
mFilter.profile = profile
startForeground(NOTIFICATION_ID, mNotification.build(true))
}
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/donottranslate.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<resources>
<!-- Preference Keys -->
<string name="pref_key_use_root" translatable="false">pref_key_use_root</string>
<string name="pref_key_use_accessibility_service" translatable="false">pref_key_use_accessibility_service</string>
<string name="pref_key_profile_spinner" translatable="false">pref_key_profile_spinner</string>
<string name="pref_key_dim" translatable="false">pref_key_shades_dim_level</string>
<string name="pref_key_intensity" translatable="false">pref_key_shades_intensity_level</string>
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,7 @@
<!-- Notification channel descriptions -->
<string name="notification_channel_overlay_name">Overlay Control</string>
<string name="notification_channel_overlay_description">The persistent overlay control notification</string>
<string name="overlay_accessibility_service_label">Red Moon Overlay</string>
<string name="pref_title_use_accessibility_service">Use Accessibiltiy Service</string>
<string name="pref_summary_use_accessibility_service">Cover the home screen (Android 12+) and status/navigation bars (Android 10+)</string>
</resources>
9 changes: 9 additions & 0 deletions app/src/main/res/xml/settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@
android:summary="@string/pref_summary_use_root"
/>

<SwitchPreference
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:defaultValue="false"
android:key="@string/pref_key_use_accessibility_service"
android:title="@string/pref_title_use_accessibility_service"
android:summary="@string/pref_summary_use_accessibility_service"
/>

<ListPreference
android:key="@string/pref_key_button_backlight"
android:title="@string/pref_title_button_backlight"
Expand Down