javaandroidaccessibilitytalkbackbraille

Can I Turn ON / OFF Talkback programmatically?


I'm working on a university project for blind people who use Android phones (a Braille Keyboard) .

So i need disable talkback when user use the keyboard and then enable it again. (for using multitouch on the screen). So I created a new sample simple project for android 8.0, empty activity and I added a couple things.

My sample android manifest:

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.WRITE_SETTINGS" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

And my simple MainActivity:

package com.jaime.prueba;
import android.os.Bundle;
import android.provider.Settings;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);


    updateTalkBackState(true);


    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
}

private static final String TALKBACK_SERVICE_NAME = "com.google.android.marvin.talkback/.TalkBackService";

private void updateTalkBackState(boolean enableTalkBack) {
    if (enableTalkBack) {
        enableAccessibilityService(TALKBACK_SERVICE_NAME);
    } else {
        disableAccessibilityServices();
    }
}

private void enableAccessibilityService(String name) {
    Settings.Secure.putString(getContentResolver(), Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES, name);
    Settings.Secure.putString(getContentResolver(), Settings.Secure.ACCESSIBILITY_ENABLED, "1");
}

private void disableAccessibilityServices() {
    Settings.Secure.putString(getContentResolver(), Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES, "");
    Settings.Secure.putString(getContentResolver(), Settings.Secure.ACCESSIBILITY_ENABLED, "0");
}




@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

So...

Firstly: I checked this answer: https://stackoverflow.com/a/44461236/10802519

//  It's not possible to turn TalkBack on or off from within your app unless you have been granted the system permission WRITE_SECURE_SETTINGS (via ADB) by the user.

private static final String TALKBACK_SERVICE_NAME = "com.google.android.marvin.talkback/.TalkBackService";

private void updateTalkBackState(boolean enableTalkBack) {
    if (enableTalkBack) {
        enableAccessibilityService(TALKBACK_SERVICE_NAME);
    } else {
        disableAccessibilityServices();
    }
}

private void enableAccessibilityService(String name) {
    Settings.Secure.putString(getContentResolver(), Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES, name);
    Settings.Secure.putString(getContentResolver(), Settings.Secure.ACCESSIBILITY_ENABLED, VALUE_ENABLED);
}

private void disableAccessibilityServices() {
    Settings.Secure.putString(getContentResolver(), Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES, "");
    Settings.Secure.putString(getContentResolver(), Settings.Secure.ACCESSIBILITY_ENABLED, VALUE_DISABLED);
}

Secondly: In my Terminal I typed:

adb shell pm grant com.jaime.prueba android.permission.WRITE_SECURE_SETTINGS

Thirdly: But I got the following:

java.lang.SecurityException: Package com.jaime.prueba has not requested permission android.permission.WRITE_SECURE_SETTINGS
at com.android.server.pm.permission.BasePermission.enforceDeclaredUsedAndRuntimeOrDevelopment(BasePermission.java:379)
at com.android.server.pm.permission.PermissionManagerService.grantRuntimePermission(PermissionManagerService.java:1404)
at com.android.server.pm.permission.PermissionManagerService.access$900(PermissionManagerService.java:89)
at com.android.server.pm.permission.PermissionManagerService$PermissionManagerInternalImpl.grantRuntimePermission(PermissionManagerService.java:2093)
at com.android.server.pm.PackageManagerService.grantRuntimePermission(PackageManagerService.java:5411)
at com.android.server.pm.PackageManagerShellCommand.runGrantRevokePermission(PackageManagerShellCommand.java:1730)
at com.android.server.pm.PackageManagerShellCommand.onCommand(PackageManagerShellCommand.java:217)
at android.os.ShellCommand.exec(ShellCommand.java:103)
at com.android.server.pm.PackageManagerService.onShellCommand(PackageManagerService.java:21330)
at android.os.Binder.shellCommand(Binder.java:634)
at android.os.Binder.onTransact(Binder.java:532)
at android.content.pm.IPackageManager$Stub.onTransact(IPackageManager.java:2821)
at com.android.server.pm.PackageManagerService.onTransact(PackageManagerService.java:3856)
at android.os.Binder.execTransact(Binder.java:731)

Fourthly I build my app on Android Studio, Everything fine, nothing red, so I run the app on my phone... and my app does not open, it does not work.

I hope my question is understandable. Let me know, if my questions need something more -First question, and in foreign language- Thank you!


Solution

  • Notice in manifest file:

    <uses-permission android:name="android.permission.WRITE_SETTINGS" />
    

    but in terminal you input:

    adb shell pm grant com.jaime.prueba android.permission.WRITE_SECURE_SETTINGS
    

    WRITE_SETTINGS and WRITE_SECURE_SETTINGS are two different permissions.

    and about WRITE_SECURE_SETTINGS, you could read this post.