0

I am developing an Accessibility app in Android Studio, Java.

I enable Talkback programmatically as an accessibility service:

public class TalkBackToggler {

//In the terminal run the command line:

//    adb shell pm grant com.MyApp android.permission.WRITE_SECURE_SETTINGS

private static final String VALUE_DISABLED = "0";
private static final String VALUE_ENABLED = "1";

private final ContentResolver contentResolver;
private final Callback callback;
private Context context;

TalkBackToggler(ContentResolver contentResolver, Callback callback, Context context) {
    this.contentResolver = contentResolver;
    this.callback = callback;
    this.context = context;
}


public void enableTalkBack()
{
    try {
        AccessibilityManager am = (AccessibilityManager)(context.getSystemService(Context.ACCESSIBILITY_SERVICE));
        List<AccessibilityServiceInfo> services = am.getInstalledAccessibilityServiceList();

        if (services.isEmpty()) {
            return;
        }

        AccessibilityServiceInfo service = services.get(0);

        boolean enableTouchExploration = (service.flags
            & AccessibilityServiceInfo.FLAG_REQUEST_TOUCH_EXPLORATION_MODE) != 0;

        ServiceInfo serviceInfo;
        ComponentName componentName;
        String enabledServiceString = "";

        if (!enableTouchExploration) {
            final int serviceCount = services.size();
            for (int i = 1; i < serviceCount; i++) {
                AccessibilityServiceInfo candidate = services.get(i);
                serviceInfo = candidate.getResolveInfo().serviceInfo;
                componentName = new ComponentName(serviceInfo.packageName, serviceInfo.name);
                enabledServiceString = componentName.flattenToString();

                if (enabledServiceString.contains("talkback")) {
                    if ((candidate.flags & AccessibilityServiceInfo.FLAG_REQUEST_TOUCH_EXPLORATION_MODE) == 0) {
                        candidate.flags = candidate.flags | AccessibilityServiceInfo.FLAG_REQUEST_TOUCH_EXPLORATION_MODE;
                }
                    enableTouchExploration = true;

                    break;
                }
            }
        }

        ContentResolver resolver = context.getContentResolver();

        Settings.Secure.putString(resolver, Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES, enabledServiceString);

        if (enableTouchExploration) {
            Settings.Secure.putString(resolver, Settings.Secure.TOUCH_EXPLORATION_ENABLED, VALUE_ENABLED);
        }

            Settings.Secure.putString(resolver, Settings.Secure.ACCESSIBILITY_ENABLED, VALUE_ENABLED);
        }
        catch(Exception e) {
             Log.e("Device", "Failed to enable accessibility: " + e);
        }
    }
}

I have a custom AccessibiltyService that I also enable programmatically:

public class MyAccessibiltyService extends AccessibilityService {

    @Override
    protected void onServiceConnected() {
        super.onServiceConnected();
        Log.v(TAG, "onServiceConnected");
        AccessibilityServiceInfo info = new AccessibilityServiceInfo();
        info.flags = AccessibilityServiceInfo.DEFAULT;
        info.eventTypes = AccessibilityEvent.TYPES_ALL_MASK;
        info.feedbackType = AccessibilityServiceInfo.FEEDBACK_GENERIC;
        setServiceInfo(info);
    }
 

In AndroidManifest.xml I have registered the two services:

<service
    android:name=".activities.MyAccessibiltyService"
    android:enabled="true"
    android:exported="true"
    android:label="Keynoa"
    android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
<intent-filter>
    <action android:name="android.accessibilityservice.AccessibilityService" />
</intent-filter>

<meta-data
    android:name="android.accessibilityservice"
    android:resource="@xml/my_accessibility_service_config" />
</service>

<service
    android:name=".activities.TalkBackToggleService"
    android:enabled="true"
    android:exported="true"
    android:label="Keynoa"
    android:permission="android.permission.BIND_QUICK_SETTINGS_TILE">
<intent-filter>
    <action android:name="android.service.quicksettings.action.QS_TILE" />
</intent-filter>

 <meta-data
     android:name="android.service.quicksettings.ACTIVE_TILE"
     android:value="true" />
</service>

THE PROBLEM: I cannot enable simultaneously both Talkback service and my Accessibility service programmatically; when I enable one service the other one is disabled.

BUT! when I enable Talkback manually from the device settings, my Accessibility Service stays enabled simultaneously with Talkback!

I there a way to enable both services programmatically and have them stay enabled simultaneously?

Thank you.

1 Answers1

0

You're overwriting the setting that has your service enabled, so the list of enabled services only contains TalkBack. If you look at the Settings code, there's a read-modify-write when a service is toggled.

You're well off the path of public APIs, starting with a permission grant that's system-only, so you should expect brittleness.

Phil Weaver
  • 738
  • 3
  • 7