diff options
author | Sam Mortimer <sam@mortimer.me.uk> | 2019-09-18 13:29:25 -0700 |
---|---|---|
committer | Sam Mortimer <sam@mortimer.me.uk> | 2019-09-19 22:08:52 -0700 |
commit | 7e03d078bc4577defc09f48f20f411ca82291e5e (patch) | |
tree | 30f05e41cec96f3fbab3864d144d9f2b79538523 | |
parent | e530d7a1264b4deedc75710505ae77d6935fa088 (diff) | |
download | lineage-sdk-7e03d078bc4577defc09f48f20f411ca82291e5e.tar.gz lineage-sdk-7e03d078bc4577defc09f48f20f411ca82291e5e.tar.bz2 lineage-sdk-7e03d078bc4577defc09f48f20f411ca82291e5e.zip |
lineage-sdk: Add call methods for list and delete to our settings provider
To match changes in fw/b settings provider
Change-Id: Ie4683fe29b9109091d0ebd4910d31b7b4c714daa
-rw-r--r-- | packages/LineageSettingsProvider/src/org/lineageos/lineagesettings/LineageSettingsProvider.java | 161 | ||||
-rw-r--r-- | sdk/src/java/lineageos/providers/LineageSettings.java | 30 |
2 files changed, 141 insertions, 50 deletions
diff --git a/packages/LineageSettingsProvider/src/org/lineageos/lineagesettings/LineageSettingsProvider.java b/packages/LineageSettingsProvider/src/org/lineageos/lineagesettings/LineageSettingsProvider.java index 5dfb04e1..fa944dd6 100644 --- a/packages/LineageSettingsProvider/src/org/lineageos/lineagesettings/LineageSettingsProvider.java +++ b/packages/LineageSettingsProvider/src/org/lineageos/lineagesettings/LineageSettingsProvider.java @@ -1,5 +1,6 @@ /** - * Copyright (c) 2015, The CyanogenMod Project + * Copyright (C) 2015 The CyanogenMod Project + * Copyright (C) 2019 The LineageOS Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -79,6 +80,11 @@ public class LineageSettingsProvider extends ContentProvider { private static final String ITEM_MATCHER = "/*"; private static final String NAME_SELECTION = Settings.NameValueTable.NAME + " = ?"; + // Must match definitions in fw/b + // packages/SettingsProvider/src/com/android/providers/settings/SettingsProvider.java + public static final String RESULT_ROWS_DELETED = "result_rows_deleted"; + public static final String RESULT_SETTINGS_LIST = "result_settings_list"; + private static final UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH); static { @@ -309,54 +315,65 @@ public class LineageSettingsProvider extends ContentProvider { } } - // Migrate methods - if (LineageSettings.CALL_METHOD_MIGRATE_SETTINGS.equals(method)) { - migrateLineageSettingsForExistingUsersIfNeeded(); - - return null; - } else if (LineageSettings.CALL_METHOD_MIGRATE_SETTINGS_FOR_USER.equals(method)) { - migrateLineageSettingsForUser(callingUserId); - - return null; - } - - // Get methods - if (LineageSettings.CALL_METHOD_GET_SYSTEM.equals(method)) { - return lookupSingleValue(callingUserId, LineageSettings.System.CONTENT_URI, request); - } - else if (LineageSettings.CALL_METHOD_GET_SECURE.equals(method)) { - return lookupSingleValue(callingUserId, LineageSettings.Secure.CONTENT_URI, request); - } - else if (LineageSettings.CALL_METHOD_GET_GLOBAL.equals(method)) { - return lookupSingleValue(callingUserId, LineageSettings.Global.CONTENT_URI, request); - } - - // Put methods - new value is in the args bundle under the key named by - // the Settings.NameValueTable.VALUE static. - final String newValue = (args == null) - ? null : args.getString(Settings.NameValueTable.VALUE); - - // Framework can't do automatic permission checking for calls, so we need - // to do it here. - if (LineageSettings.CALL_METHOD_PUT_SYSTEM.equals(method)) { - enforceWritePermission(lineageos.platform.Manifest.permission.WRITE_SETTINGS); - } else { - enforceWritePermission(lineageos.platform.Manifest.permission.WRITE_SECURE_SETTINGS); - } - - // Put methods - final ContentValues values = new ContentValues(); - values.put(Settings.NameValueTable.NAME, request); - values.put(Settings.NameValueTable.VALUE, newValue); - - if (LineageSettings.CALL_METHOD_PUT_SYSTEM.equals(method)) { - insertForUser(callingUserId, LineageSettings.System.CONTENT_URI, values); - } - else if (LineageSettings.CALL_METHOD_PUT_SECURE.equals(method)) { - insertForUser(callingUserId, LineageSettings.Secure.CONTENT_URI, values); - } - else if (LineageSettings.CALL_METHOD_PUT_GLOBAL.equals(method)) { - insertForUser(callingUserId, LineageSettings.Global.CONTENT_URI, values); + switch (method) { + // Migrate methods + case LineageSettings.CALL_METHOD_MIGRATE_SETTINGS: + migrateLineageSettingsForExistingUsersIfNeeded(); + return null; + case LineageSettings.CALL_METHOD_MIGRATE_SETTINGS_FOR_USER: + migrateLineageSettingsForUser(callingUserId); + return null; + + // Get methods + case LineageSettings.CALL_METHOD_GET_SYSTEM: + return lookupSingleValue(callingUserId, LineageSettings.System.CONTENT_URI, + request); + case LineageSettings.CALL_METHOD_GET_SECURE: + return lookupSingleValue(callingUserId, LineageSettings.Secure.CONTENT_URI, + request); + case LineageSettings.CALL_METHOD_GET_GLOBAL: + return lookupSingleValue(callingUserId, LineageSettings.Global.CONTENT_URI, + request); + + // Put methods + case LineageSettings.CALL_METHOD_PUT_SYSTEM: + enforceWritePermission(lineageos.platform.Manifest.permission.WRITE_SETTINGS); + callHelperPut(callingUserId, LineageSettings.System.CONTENT_URI, request, args); + return null; + case LineageSettings.CALL_METHOD_PUT_SECURE: + enforceWritePermission( + lineageos.platform.Manifest.permission.WRITE_SECURE_SETTINGS); + callHelperPut(callingUserId, LineageSettings.Secure.CONTENT_URI, request, args); + return null; + case LineageSettings.CALL_METHOD_PUT_GLOBAL: + enforceWritePermission( + lineageos.platform.Manifest.permission.WRITE_SECURE_SETTINGS); + callHelperPut(callingUserId, LineageSettings.Global.CONTENT_URI, request, args); + return null; + + // List methods + case LineageSettings.CALL_METHOD_LIST_SYSTEM: + return callHelperList(callingUserId, LineageSettings.System.CONTENT_URI); + case LineageSettings.CALL_METHOD_LIST_SECURE: + return callHelperList(callingUserId, LineageSettings.Secure.CONTENT_URI); + case LineageSettings.CALL_METHOD_LIST_GLOBAL: + return callHelperList(callingUserId, LineageSettings.Global.CONTENT_URI); + + // Delete methods + case LineageSettings.CALL_METHOD_DELETE_SYSTEM: + enforceWritePermission(lineageos.platform.Manifest.permission.WRITE_SETTINGS); + return callHelperDelete(callingUserId, LineageSettings.System.CONTENT_URI, + request); + case LineageSettings.CALL_METHOD_DELETE_SECURE: + enforceWritePermission( + lineageos.platform.Manifest.permission.WRITE_SECURE_SETTINGS); + return callHelperDelete(callingUserId, LineageSettings.Secure.CONTENT_URI, + request); + case LineageSettings.CALL_METHOD_DELETE_GLOBAL: + enforceWritePermission( + lineageos.platform.Manifest.permission.WRITE_SECURE_SETTINGS); + return callHelperDelete(callingUserId, LineageSettings.Global.CONTENT_URI, + request); } return null; @@ -371,6 +388,46 @@ public class LineageSettingsProvider extends ContentProvider { } } + // Helper for call() CALL_METHOD_DELETE_* methods + private Bundle callHelperDelete(int callingUserId, Uri contentUri, String key) { + final int rowsDeleted = deleteForUser(callingUserId, contentUri, NAME_SELECTION, + new String[]{ key }); + final Bundle ret = new Bundle(); + ret.putInt(RESULT_ROWS_DELETED, rowsDeleted); + return ret; + } + + // Helper for call() CALL_METHOD_LIST_* methods + private Bundle callHelperList(int callingUserId, Uri contentUri) { + final ArrayList<String> lines = new ArrayList<String>(); + final Cursor cursor = queryForUser(callingUserId, contentUri, null, null, null, null); + try { + while (cursor != null && cursor.moveToNext()) { + lines.add(cursor.getString(1) + "=" + cursor.getString(2)); + } + } finally { + if (cursor != null) { + cursor.close(); + } + } + final Bundle ret = new Bundle(); + ret.putStringArrayList(RESULT_SETTINGS_LIST, lines); + return ret; + } + + // Helper for call() CALL_METHOD_PUT_* methods + private void callHelperPut(int callingUserId, Uri contentUri, String key, Bundle args) { + // New value is in the args bundle under the key named by + // Settings.NameValueTable.VALUE + final String newValue = (args == null) + ? null : args.getString(Settings.NameValueTable.VALUE); + final ContentValues values = new ContentValues(); + values.put(Settings.NameValueTable.NAME, key); + values.put(Settings.NameValueTable.VALUE, newValue); + + insertForUser(callingUserId, contentUri, values); + } + /** * Looks up a single value for a specific user, uri, and key. * @param userId The id of the user to perform the lookup for. @@ -571,6 +628,11 @@ public class LineageSettingsProvider extends ContentProvider { @Override public int delete(Uri uri, String selection, String[] selectionArgs) { + return deleteForUser(UserHandle.getCallingUserId(), uri, selection, selectionArgs); + } + + private int deleteForUser(int callingUserId, Uri uri, String selection, + String[] selectionArgs) { if (uri == null) { throw new IllegalArgumentException("Uri cannot be null"); } @@ -583,7 +645,6 @@ public class LineageSettingsProvider extends ContentProvider { String tableName = getTableNameFromUri(uri); checkWritePermissions(tableName); - int callingUserId = UserHandle.getCallingUserId(); LineageDatabaseHelper dbHelper = getOrEstablishDatabase(getUserIdForTable(tableName, callingUserId)); diff --git a/sdk/src/java/lineageos/providers/LineageSettings.java b/sdk/src/java/lineageos/providers/LineageSettings.java index 6f34aac9..0e2a9488 100644 --- a/sdk/src/java/lineageos/providers/LineageSettings.java +++ b/sdk/src/java/lineageos/providers/LineageSettings.java @@ -138,6 +138,36 @@ public final class LineageSettings { */ public static final String CALL_METHOD_MIGRATE_SETTINGS_FOR_USER = "migrate_settings_for_user"; + /** + * @hide - Private call() method to list the entire system table + */ + public static final String CALL_METHOD_LIST_SYSTEM = "LIST_system"; + + /** + * @hide - Private call() method to list the entire secure table + */ + public static final String CALL_METHOD_LIST_SECURE = "LIST_secure"; + + /** + * @hide - Private call() method to list the entire global table + */ + public static final String CALL_METHOD_LIST_GLOBAL = "LIST_global"; + + /** + * @hide - Private call() method to delete an entry from the system table + */ + public static final String CALL_METHOD_DELETE_SYSTEM = "DELETE_system"; + + /** + * @hide - Private call() method to delete an entry from the secure table + */ + public static final String CALL_METHOD_DELETE_SECURE = "DELETE_secure"; + + /** + * @hide - Private call() method to delete an entry from the global table + */ + public static final String CALL_METHOD_DELETE_GLOBAL = "DELETE_global"; + // endregion // Thread-safe. |