Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 37fd15d8 authored by d34d's avatar d34d Committed by Sam Mortimer
Browse files

SettingsProvider: Allow accessing LineageSettings via settings command

This patch allows the settings command line tool to be used with
the LineageSettings provider by passing in --lineage

* Updates for ten (@sam3000)

Change-Id: Ie0906c9957e9de8418164d7cc84983d57fa4a015
parent ad5eb839
Loading
Loading
Loading
Loading
+1 −1
Original line number Original line Diff line number Diff line
@@ -9,7 +9,7 @@ android_app {
        "telephony-common",
        "telephony-common",
        "ims-common",
        "ims-common",
    ],
    ],
    static_libs: ["junit"],
    static_libs: ["junit", "org.lineageos.platform.internal"],
    platform_apis: true,
    platform_apis: true,
    certificate: "platform",
    certificate: "platform",
    privileged: true,
    privileged: true,
+95 −33
Original line number Original line Diff line number Diff line
@@ -17,10 +17,12 @@
package com.android.providers.settings;
package com.android.providers.settings;


import android.app.ActivityManager;
import android.app.ActivityManager;
import android.app.ContentProviderHolder;
import android.content.IContentProvider;
import android.content.IContentProvider;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager;
import android.os.Binder;
import android.os.Binder;
import android.os.Bundle;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Process;
import android.os.Process;
import android.os.RemoteException;
import android.os.RemoteException;
import android.os.ResultReceiver;
import android.os.ResultReceiver;
@@ -30,6 +32,8 @@ import android.os.UserHandle;
import android.os.UserManager;
import android.os.UserManager;
import android.provider.Settings;
import android.provider.Settings;


import lineageos.providers.LineageSettings;

import java.io.FileDescriptor;
import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.ArrayList;
@@ -114,6 +118,7 @@ final public class SettingsService extends Binder {
        String mTag = null;
        String mTag = null;
        int mResetMode = -1;
        int mResetMode = -1;
        boolean mMakeDefault;
        boolean mMakeDefault;
        boolean mUseLineageSettingsProvider = false;


        MyShellCommand(SettingsProvider provider, boolean dumping) {
        MyShellCommand(SettingsProvider provider, boolean dumping) {
            mProvider = provider;
            mProvider = provider;
@@ -142,6 +147,8 @@ final public class SettingsService extends Binder {
                        perr.println("Invalid user: all");
                        perr.println("Invalid user: all");
                        return -1;
                        return -1;
                    }
                    }
                } else if ("--lineage".equals(arg)) {
                    mUseLineageSettingsProvider = true;
                } else if (mVerb == CommandVerb.UNSPECIFIED) {
                } else if (mVerb == CommandVerb.UNSPECIFIED) {
                    if ("get".equalsIgnoreCase(arg)) {
                    if ("get".equalsIgnoreCase(arg)) {
                        mVerb = CommandVerb.GET;
                        mVerb = CommandVerb.GET;
@@ -267,40 +274,71 @@ final public class SettingsService extends Binder {
                return -1;
                return -1;
            }
            }


            final IContentProvider iprovider = mProvider.getIContentProvider();
            IBinder token = new Binder();
            final ContentProviderHolder holder;
            try {
                holder = ActivityManager.getService()
                        .getContentProviderExternal(mUseLineageSettingsProvider ?
                                LineageSettings.AUTHORITY : Settings.AUTHORITY,
                                UserHandle.USER_SYSTEM, token, null);
                if (holder == null) {
                    throw new IllegalStateException("Could not find settings provider");
                }
            } catch (RemoteException e) {
                throw new RuntimeException("Error while accessing settings provider", e);
            }

            final IContentProvider provider = holder.provider;
            final PrintWriter pout = getOutPrintWriter();
            final PrintWriter pout = getOutPrintWriter();
            switch (mVerb) {
            switch (mVerb) {
                case GET:
                case GET:
                    pout.println(getForUser(iprovider, mUser, mTable, mKey));
                    pout.println(getForUser(provider, mUser, mTable, mKey));
                    break;
                    break;
                case PUT:
                case PUT:
                    putForUser(iprovider, mUser, mTable, mKey, mValue, mTag, mMakeDefault);
                    putForUser(provider, mUser, mTable, mKey, mValue, mTag, mMakeDefault);
                    break;
                    break;
                case DELETE:
                case DELETE:
                    pout.println("Deleted "
                    pout.println("Deleted "
                            + deleteForUser(iprovider, mUser, mTable, mKey) + " rows");
                            + deleteForUser(provider, mUser, mTable, mKey) + " rows");
                    break;
                    break;
                case LIST:
                case LIST:
                    for (String line : listForUser(iprovider, mUser, mTable)) {
                    for (String line : listForUser(provider, mUser, mTable)) {
                        pout.println(line);
                        pout.println(line);
                    }
                    }
                    break;
                    break;
                case RESET:
                case RESET:
                    resetForUser(iprovider, mUser, mTable, mTag);
                    resetForUser(provider, mUser, mTable, mTag);
                    break;
                    break;
                default:
                default:
                    perr.println("Unspecified command");
                    perr.println("Unspecified command");
                    return -1;
                    return -1;
            }
            }


            try {
                if (provider != null) {
                    ActivityManager.getService()
                            .removeContentProviderExternal(mUseLineageSettingsProvider ?
                                    LineageSettings.AUTHORITY : Settings.AUTHORITY, token);
                }
            } catch (RemoteException e) {
                throw new RuntimeException("Error while accessing settings provider", e);
            }

            return 0;
            return 0;
        }
        }


        List<String> listForUser(IContentProvider provider, int userHandle, String table) {
        List<String> listForUser(IContentProvider provider, int userHandle, String table) {
            final String callListCommand;
            final String callListCommand;
            if ("system".equals(table)) callListCommand = Settings.CALL_METHOD_LIST_SYSTEM;
            final String systemListCommand = mUseLineageSettingsProvider ?
            else if ("secure".equals(table)) callListCommand = Settings.CALL_METHOD_LIST_SECURE;
                    LineageSettings.CALL_METHOD_LIST_SYSTEM : Settings.CALL_METHOD_LIST_SYSTEM;
            else if ("global".equals(table)) callListCommand = Settings.CALL_METHOD_LIST_GLOBAL;
            final String secureListCommand = mUseLineageSettingsProvider ?
                    LineageSettings.CALL_METHOD_LIST_SECURE : Settings.CALL_METHOD_LIST_SECURE;
            final String globalListCommand = mUseLineageSettingsProvider ?
                    LineageSettings.CALL_METHOD_LIST_GLOBAL : Settings.CALL_METHOD_LIST_GLOBAL;

            if ("system".equals(table)) callListCommand = systemListCommand;
            else if ("secure".equals(table)) callListCommand = secureListCommand;
            else if ("global".equals(table)) callListCommand = globalListCommand;
            else {
            else {
                getErrPrintWriter().println("Invalid table; no list performed");
                getErrPrintWriter().println("Invalid table; no list performed");
                throw new IllegalArgumentException("Invalid table " + table);
                throw new IllegalArgumentException("Invalid table " + table);
@@ -309,8 +347,8 @@ final public class SettingsService extends Binder {
            try {
            try {
                Bundle arg = new Bundle();
                Bundle arg = new Bundle();
                arg.putInt(Settings.CALL_METHOD_USER_KEY, userHandle);
                arg.putInt(Settings.CALL_METHOD_USER_KEY, userHandle);
                Bundle result = provider.call(resolveCallingPackage(), Settings.AUTHORITY,
                Bundle result = provider.call(resolveCallingPackage(), mUseLineageSettingsProvider ?
                        callListCommand, null, arg);
                        LineageSettings.AUTHORITY : Settings.AUTHORITY, callListCommand, null, arg);
                lines.addAll(result.getStringArrayList(SettingsProvider.RESULT_SETTINGS_LIST));
                lines.addAll(result.getStringArrayList(SettingsProvider.RESULT_SETTINGS_LIST));
                Collections.sort(lines);
                Collections.sort(lines);
            } catch (RemoteException e) {
            } catch (RemoteException e) {
@@ -321,10 +359,16 @@ final public class SettingsService extends Binder {


        String getForUser(IContentProvider provider, int userHandle,
        String getForUser(IContentProvider provider, int userHandle,
                final String table, final String key) {
                final String table, final String key) {
            final String systemGetCommand = mUseLineageSettingsProvider ?
                    LineageSettings.CALL_METHOD_GET_SYSTEM : Settings.CALL_METHOD_GET_SYSTEM;
            final String secureGetCommand = mUseLineageSettingsProvider ?
                    LineageSettings.CALL_METHOD_GET_SECURE : Settings.CALL_METHOD_GET_SECURE;
            final String globalGetCommand = mUseLineageSettingsProvider ?
                    LineageSettings.CALL_METHOD_GET_GLOBAL : Settings.CALL_METHOD_GET_GLOBAL;
            final String callGetCommand;
            final String callGetCommand;
            if ("system".equals(table)) callGetCommand = Settings.CALL_METHOD_GET_SYSTEM;
            if ("system".equals(table)) callGetCommand = systemGetCommand;
            else if ("secure".equals(table)) callGetCommand = Settings.CALL_METHOD_GET_SECURE;
            else if ("secure".equals(table)) callGetCommand = secureGetCommand;
            else if ("global".equals(table)) callGetCommand = Settings.CALL_METHOD_GET_GLOBAL;
            else if ("global".equals(table)) callGetCommand = globalGetCommand;
            else {
            else {
                getErrPrintWriter().println("Invalid table; no put performed");
                getErrPrintWriter().println("Invalid table; no put performed");
                throw new IllegalArgumentException("Invalid table " + table);
                throw new IllegalArgumentException("Invalid table " + table);
@@ -333,9 +377,11 @@ final public class SettingsService extends Binder {
            String result = null;
            String result = null;
            try {
            try {
                Bundle arg = new Bundle();
                Bundle arg = new Bundle();
                arg.putInt(Settings.CALL_METHOD_USER_KEY, userHandle);
                arg.putInt(mUseLineageSettingsProvider ?
                Bundle b = provider.call(resolveCallingPackage(), Settings.AUTHORITY,
                        LineageSettings.CALL_METHOD_USER_KEY : Settings.CALL_METHOD_USER_KEY,
                        callGetCommand, key, arg);
                        userHandle);
                Bundle b = provider.call(resolveCallingPackage(), mUseLineageSettingsProvider ?
                        LineageSettings.AUTHORITY : Settings.AUTHORITY, callGetCommand, key, arg);
                if (b != null) {
                if (b != null) {
                    result = b.getPairValue();
                    result = b.getPairValue();
                }
                }
@@ -347,16 +393,22 @@ final public class SettingsService extends Binder {


        void putForUser(IContentProvider provider, int userHandle, final String table,
        void putForUser(IContentProvider provider, int userHandle, final String table,
                final String key, final String value, String tag, boolean makeDefault) {
                final String key, final String value, String tag, boolean makeDefault) {
            final String systemPutCommand = mUseLineageSettingsProvider ?
                    LineageSettings.CALL_METHOD_PUT_SYSTEM : Settings.CALL_METHOD_PUT_SYSTEM;
            final String securePutCommand = mUseLineageSettingsProvider ?
                    LineageSettings.CALL_METHOD_PUT_SECURE : Settings.CALL_METHOD_PUT_SECURE;
            final String globalPutCommand = mUseLineageSettingsProvider ?
                    LineageSettings.CALL_METHOD_PUT_GLOBAL : Settings.CALL_METHOD_PUT_GLOBAL;
            final String callPutCommand;
            final String callPutCommand;
            if ("system".equals(table)) {
            if ("system".equals(table)) {
                callPutCommand = Settings.CALL_METHOD_PUT_SYSTEM;
                callPutCommand = systemPutCommand;
                if (makeDefault) {
                if (makeDefault) {
                    getOutPrintWriter().print("Ignored makeDefault - "
                    getOutPrintWriter().print("Ignored makeDefault - "
                            + "doesn't apply to system settings");
                            + "doesn't apply to system settings");
                    makeDefault = false;
                    makeDefault = false;
                }
                }
            } else if ("secure".equals(table)) callPutCommand = Settings.CALL_METHOD_PUT_SECURE;
            } else if ("secure".equals(table)) callPutCommand = securePutCommand;
            else if ("global".equals(table)) callPutCommand = Settings.CALL_METHOD_PUT_GLOBAL;
            else if ("global".equals(table)) callPutCommand = globalPutCommand;
            else {
            else {
                getErrPrintWriter().println("Invalid table; no put performed");
                getErrPrintWriter().println("Invalid table; no put performed");
                return;
                return;
@@ -365,15 +417,17 @@ final public class SettingsService extends Binder {
            try {
            try {
                Bundle arg = new Bundle();
                Bundle arg = new Bundle();
                arg.putString(Settings.NameValueTable.VALUE, value);
                arg.putString(Settings.NameValueTable.VALUE, value);
                arg.putInt(Settings.CALL_METHOD_USER_KEY, userHandle);
                arg.putInt(mUseLineageSettingsProvider ?
                        LineageSettings.CALL_METHOD_USER_KEY : Settings.CALL_METHOD_USER_KEY,
                        userHandle);
                if (tag != null) {
                if (tag != null) {
                    arg.putString(Settings.CALL_METHOD_TAG_KEY, tag);
                    arg.putString(Settings.CALL_METHOD_TAG_KEY, tag);
                }
                }
                if (makeDefault) {
                if (makeDefault) {
                    arg.putBoolean(Settings.CALL_METHOD_MAKE_DEFAULT_KEY, true);
                    arg.putBoolean(Settings.CALL_METHOD_MAKE_DEFAULT_KEY, true);
                }
                }
                provider.call(resolveCallingPackage(), Settings.AUTHORITY,
                provider.call(resolveCallingPackage(), mUseLineageSettingsProvider ?
                        callPutCommand, key, arg);
                        LineageSettings.AUTHORITY : Settings.AUTHORITY, callPutCommand, key, arg);
            } catch (RemoteException e) {
            } catch (RemoteException e) {
                throw new RuntimeException("Failed in IPC", e);
                throw new RuntimeException("Failed in IPC", e);
            }
            }
@@ -382,12 +436,18 @@ final public class SettingsService extends Binder {
        int deleteForUser(IContentProvider provider, int userHandle,
        int deleteForUser(IContentProvider provider, int userHandle,
                final String table, final String key) {
                final String table, final String key) {
            final String callDeleteCommand;
            final String callDeleteCommand;
            final String systemDeleteCommand = mUseLineageSettingsProvider ?
                    LineageSettings.CALL_METHOD_DELETE_SYSTEM : Settings.CALL_METHOD_DELETE_SYSTEM;
            final String secureDeleteCommand = mUseLineageSettingsProvider ?
                    LineageSettings.CALL_METHOD_DELETE_SECURE : Settings.CALL_METHOD_DELETE_SECURE;
            final String globalDeleteCommand = mUseLineageSettingsProvider ?
                    LineageSettings.CALL_METHOD_DELETE_GLOBAL : Settings.CALL_METHOD_DELETE_GLOBAL;
            if ("system".equals(table)) {
            if ("system".equals(table)) {
                callDeleteCommand = Settings.CALL_METHOD_DELETE_SYSTEM;
                callDeleteCommand = systemDeleteCommand;
            } else if ("secure".equals(table)) {
            } else if ("secure".equals(table)) {
                callDeleteCommand = Settings.CALL_METHOD_DELETE_SECURE;
                callDeleteCommand = secureDeleteCommand;
            } else if ("global".equals(table)) {
            } else if ("global".equals(table)) {
                callDeleteCommand = Settings.CALL_METHOD_DELETE_GLOBAL;
                callDeleteCommand = globalDeleteCommand;
            } else {
            } else {
                getErrPrintWriter().println("Invalid table; no delete performed");
                getErrPrintWriter().println("Invalid table; no delete performed");
                throw new IllegalArgumentException("Invalid table " + table);
                throw new IllegalArgumentException("Invalid table " + table);
@@ -396,8 +456,9 @@ final public class SettingsService extends Binder {
            try {
            try {
                Bundle arg = new Bundle();
                Bundle arg = new Bundle();
                arg.putInt(Settings.CALL_METHOD_USER_KEY, userHandle);
                arg.putInt(Settings.CALL_METHOD_USER_KEY, userHandle);
                Bundle result = provider.call(resolveCallingPackage(), Settings.AUTHORITY,
                Bundle result = provider.call(resolveCallingPackage(), mUseLineageSettingsProvider
                        callDeleteCommand, key, arg);
                        ? LineageSettings.AUTHORITY : Settings.AUTHORITY, callDeleteCommand, key,
                        arg);
                return result.getInt(SettingsProvider.RESULT_ROWS_DELETED);
                return result.getInt(SettingsProvider.RESULT_ROWS_DELETED);
            } catch (RemoteException e) {
            } catch (RemoteException e) {
                throw new RuntimeException("Failed in IPC", e);
                throw new RuntimeException("Failed in IPC", e);
@@ -423,7 +484,8 @@ final public class SettingsService extends Binder {
                }
                }
                String packageName = mPackageName != null ? mPackageName : resolveCallingPackage();
                String packageName = mPackageName != null ? mPackageName : resolveCallingPackage();
                arg.putInt(Settings.CALL_METHOD_USER_KEY, userHandle);
                arg.putInt(Settings.CALL_METHOD_USER_KEY, userHandle);
                provider.call(packageName, Settings.AUTHORITY, callResetCommand, null, arg);
                provider.call(packageName, mUseLineageSettingsProvider ? LineageSettings.AUTHORITY
                        : Settings.AUTHORITY, callResetCommand, null, arg);
            } catch (RemoteException e) {
            } catch (RemoteException e) {
                throw new RuntimeException("Failed in IPC", e);
                throw new RuntimeException("Failed in IPC", e);
            }
            }
@@ -461,18 +523,18 @@ final public class SettingsService extends Binder {
                pw.println("Settings provider (settings) commands:");
                pw.println("Settings provider (settings) commands:");
                pw.println("  help");
                pw.println("  help");
                pw.println("      Print this help text.");
                pw.println("      Print this help text.");
                pw.println("  get [--user <USER_ID> | current] NAMESPACE KEY");
                pw.println("  get [--user <USER_ID> | current] [--lineage] NAMESPACE KEY");
                pw.println("      Retrieve the current value of KEY.");
                pw.println("      Retrieve the current value of KEY.");
                pw.println("  put [--user <USER_ID> | current] NAMESPACE KEY VALUE [TAG] [default]");
                pw.println("  put [--user <USER_ID> | current] [--lineage] NAMESPACE KEY VALUE [TAG] [default]");
                pw.println("      Change the contents of KEY to VALUE.");
                pw.println("      Change the contents of KEY to VALUE.");
                pw.println("      TAG to associate with the setting.");
                pw.println("      TAG to associate with the setting.");
                pw.println("      {default} to set as the default, case-insensitive only for global/secure namespace");
                pw.println("      {default} to set as the default, case-insensitive only for global/secure namespace");
                pw.println("  delete [--user <USER_ID> | current] NAMESPACE KEY");
                pw.println("  delete [--user <USER_ID> | current] [--lineage] NAMESPACE KEY");
                pw.println("      Delete the entry for KEY.");
                pw.println("      Delete the entry for KEY.");
                pw.println("  reset [--user <USER_ID> | current] NAMESPACE {PACKAGE_NAME | RESET_MODE}");
                pw.println("  reset [--user <USER_ID> | current] NAMESPACE {PACKAGE_NAME | RESET_MODE}");
                pw.println("      Reset the global/secure table for a package with mode.");
                pw.println("      Reset the global/secure table for a package with mode.");
                pw.println("      RESET_MODE is one of {untrusted_defaults, untrusted_clear, trusted_defaults}, case-insensitive");
                pw.println("      RESET_MODE is one of {untrusted_defaults, untrusted_clear, trusted_defaults}, case-insensitive");
                pw.println("  list [--user <USER_ID> | current] NAMESPACE");
                pw.println("  list [--user <USER_ID> | current] [--lineage] NAMESPACE");
                pw.println("      Print all defined keys.");
                pw.println("      Print all defined keys.");
                pw.println("      NAMESPACE is one of {system, secure, global}, case-insensitive");
                pw.println("      NAMESPACE is one of {system, secure, global}, case-insensitive");
            }
            }