0

I've a requirement to enable or disable Text-to-Speech option from my app.
We will provide a button for the user. If TTS is enabled and user presses the button TTS will get disabled and if TTS is disabled and user presses the button TTS will get enabled.

Any idea on how can we enable/disable TTS on this button press?
Any help on this will be appreciated..

Note : This app will be signed as a system app.

Alien Geography
  • 383
  • 1
  • 2
  • 14

1 Answers1

0

I found the Answer for this question.
Answer: First we need to find out if we have Accessibility Services installed or not.

AccessibilityManager am = (AccessibilityManager)(Extension.mainContext.getSystemService(Context.ACCESSIBILITY_SERVICE));
List<AccessibilityServiceInfo> services = am.getInstalledAccessibilityServiceList();  

Above given two lines will give us the install services for Accessibility/Talk Back.
After we have the services we can enable the permissions which we want for those services. Please see the enableTalkBack() below to see how to do that.

Note: To write into Secure Settings your android app needs to be signed as a system app because only system apps have permissions to write into secure settings.

public static void enableTalkBack()
{
    try {
         AccessibilityManager am = (AccessibilityManager)(Extension.mainContext.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;
        // Try to find a service supporting explore by touch.
        if (!enableTouchExploration) {
            final int serviceCount = services.size();
            for (int i = 1; i < serviceCount; i++) {
                AccessibilityServiceInfo candidate = services.get(i);
                if ((candidate.flags & AccessibilityServiceInfo
                            .FLAG_REQUEST_TOUCH_EXPLORATION_MODE) != 0) {
                    enableTouchExploration = true;
                    service = candidate;
                    break;
                }
            }
        }

        ServiceInfo serviceInfo = service.getResolveInfo().serviceInfo;
        ComponentName componentName = new ComponentName(serviceInfo.packageName, serviceInfo.name);
        String enabledServiceString = componentName.flattenToString();
        ContentResolver resolver = Extension.mainContext.getContentResolver();

        Settings.Secure.putString(resolver, "enabled_accessibility_services", enabledServiceString);
        Settings.Secure.putString(resolver,
                "touch_exploration_granted_accessibility_services",
                enabledServiceString);
        if (enableTouchExploration) {
            Settings.Secure.putInt(resolver, "touch_exploration_enabled", 1);
        }
        Settings.Secure.putInt(resolver, "accessibility_script_injection", 1);
        Settings.Secure.putInt(resolver, "accessibility_enabled", 1);
    }
    catch(Exception e) {
        Log.e("Device", "Failed to enable accessibility: " + e);
    }
}
Alien Geography
  • 383
  • 1
  • 2
  • 14