1

I want to read global settings programatically in Android.
Using ADB on AndroidStudio, the list command works perfectly:

adb shell settings list global

and returns the list of settings:

...
sound_trigger_detection_service_op_timeout=15000
sqlite_compatibility_wal_flags=
stay_on_while_plugged_in=7
subscription_mode=0
...

and also the put works as expected

adb shell settings put global stay_on_while_plugged_in 0

The problem is when I want to do it programmatically.
There are no exception but the "settings list global" command is not returning lines.
As you can see I tried also to ask the ACTION_MANAGE_WRITE_SETTINGS but doesn't works:

 private void getCurrentSettings(){
    if (Settings.System.canWrite(this)){
        try {
            String command = "settings list global";

            Process shell = Runtime.getRuntime().exec(command);
            shell.waitFor();
            BufferedReader br = new BufferedReader(new InputStreamReader(shell.getInputStream()));
            String line;

            while ((line = br.readLine()) != null) {
                Log.d("settings", line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    } else {
        Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
        intent.setData(Uri.parse("package:"+getApplicationContext().getPackageName()));
        startActivity(intent);
    }
}

UPDATE 30 Sep 2021: Thanks to the comment by @TDG I can get the error of the exec command.
This is the error in case of list command: "settings list global"

Exception occurred while executing 'list':
java.lang.SecurityException: Permission Denial: getCurrentUser() from pid=32475, uid=10603 requires android.permission.INTERACT_ACROSS_USERS
 at com.android.server.am.UserController.getCurrentUser(UserController.java:2168)
 at com.android.server.am.ActivityManagerService.getCurrentUser(ActivityManagerService.java:18772)
 at com.android.providers.settings.SettingsService$MyShellCommand.onCommand(SettingsService.java:259)
 at android.os.BasicShellCommandHandler.exec(BasicShellCommandHandler.java:98)
 at android.os.ShellCommand.exec(ShellCommand.java:44)
 at com.android.providers.settings.SettingsService.onShellCommand(SettingsService.java:49)
 at android.os.Binder.shellCommand(Binder.java:929)
 at android.os.Binder.onTransact(Binder.java:813)
 at android.os.Binder.execTransactInternal(Binder.java:1159)
 at android.os.Binder.execTransact(Binder.java:1123)

This is the error in case of get command: "settings get global stay_on_while_plugged_in"

Exception occurred while executing 'get':
java.lang.SecurityException: Permission Denial: getCurrentUser() from pid=32124, uid=10603 requires android.permission.INTERACT_ACROSS_USERS
 at com.android.server.am.UserController.getCurrentUser(UserController.java:2168)
 at com.android.server.am.ActivityManagerService.getCurrentUser(ActivityManagerService.java:18772)
 at com.android.providers.settings.SettingsService$MyShellCommand.onCommand(SettingsService.java:259)
 at android.os.BasicShellCommandHandler.exec(BasicShellCommandHandler.java:98)
 at android.os.ShellCommand.exec(ShellCommand.java:44)
 at com.android.providers.settings.SettingsService.onShellCommand(SettingsService.java:49)
 at android.os.Binder.shellCommand(Binder.java:929)
 at android.os.Binder.onTransact(Binder.java:813)
 at android.os.Binder.execTransactInternal(Binder.java:1159)
 at android.os.Binder.execTransact(Binder.java:1123)
Giovesoft
  • 580
  • 6
  • 21
  • 1
    Probably there is an error, but since you ignore the stderror stream you miss it. Try to add `shell.getErrorStream()` to your code. – TDG Sep 29 '21 at 16:53
  • Thank you @TDG you are right there is an error: java.lang.SecurityException: Permission Denial: getCurrentUser() from pid=17188, uid=10603 requires android.permission.INTERACT_ACROSS_USERS – Giovesoft Sep 30 '21 at 07:32
  • what about permissions included in the manifest? `UserController`'s `getCurrentUser` method requires INTERACT_ACROSS_USERS and INTERACT_ACROSS_USERS_FULL... [here](https://cs.android.com/android/platform/superproject/+/master:frameworks/base/services/core/java/com/android/server/am/UserController.java;l=2158) source code – Lino Sep 30 '21 at 19:32
  • Ciao @Lino I tried to put the permissions but doesn't solve the issue. I think that permissions are for system apps. as you can see in this answer: https://stackoverflow.com/a/59378575/880395 – Giovesoft Oct 05 '21 at 13:09

0 Answers0