lineage-sdk: Add call methods for list and delete to our settings provider

To match changes in fw/b settings provider

Change-Id: Ie4683fe29b9109091d0ebd4910d31b7b4c714daa
This commit is contained in:
Sam Mortimer
2019-09-18 13:29:25 -07:00
parent e530d7a126
commit 7e03d078bc
2 changed files with 137 additions and 46 deletions

View File

@@ -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();
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;
return null;
} else if (LineageSettings.CALL_METHOD_MIGRATE_SETTINGS_FOR_USER.equals(method)) {
migrateLineageSettingsForUser(callingUserId);
// 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);
return null;
}
// 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;
// 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);
}
// 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);
// 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);
// 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));

View File

@@ -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.