Add getDescendants to Settings Slice Provider

getDescendants allows another app to collect a list
of all valid Uris for Settings Slices. Important for search,
assistant or launcher apps.

This means searching our database for all valid keys and building
a list of Uris for each of those keys.

Fixes: 77808328
Test: robotests
Change-Id: I3ae27e4661a7dcaab50b091ae2730013118af8a2
This commit is contained in:
Matthew Fritze
2018-03-26 17:30:20 -07:00
parent d743f20618
commit 6730a061b1
6 changed files with 392 additions and 33 deletions

View File

@@ -18,19 +18,26 @@ package com.android.settings.slices;
import android.app.PendingIntent;
import android.app.slice.SliceManager;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Icon;
import android.net.Uri;
import android.net.wifi.WifiManager;
import android.provider.SettingsSlicesContract;
import android.support.annotation.VisibleForTesting;
import android.support.v4.graphics.drawable.IconCompat;
import android.text.TextUtils;
import android.util.Log;
import android.util.Pair;
import com.android.settings.R;
import com.android.settingslib.utils.ThreadUtils;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;
@@ -144,6 +151,85 @@ public class SettingsSliceProvider extends SliceProvider {
return SliceBuilderUtils.buildSlice(getContext(), cachedSliceData);
}
/**
* Get a list of all valid Uris based on the keys indexed in the Slices database.
* <p>
* This will return a list of {@link Uri uris} depending on {@param uri}, following:
* 1. Authority & Full Path -> Only {@param uri}. It is only a prefix for itself.
* 2. Authority & No path -> A list of authority/action/$KEY$, where
* {@code $KEY$} is a list of all Slice-enabled keys for the authority.
* 3. Authority & action path -> A list of authority/action/$KEY$, where
* {@code $KEY$} is a list of all Slice-enabled keys for the authority.
* 4. Empty authority & path -> A list of Uris with all keys for both supported authorities.
* 5. Else -> Empty list.
* <p>
* Note that the authority will stay consistent with {@param uri}, and the list of valid Slice
* keys depends on if the authority is {@link SettingsSlicesContract#AUTHORITY} or
* {@link #SLICE_AUTHORITY}.
*
* @param uri The uri to look for descendants under.
* @returns all valid Settings uris for which {@param uri} is a prefix.
*/
@Override
public Collection<Uri> onGetSliceDescendants(Uri uri) {
final List<Uri> descendants = new ArrayList<>();
final Pair<Boolean, String> pathData = SliceBuilderUtils.getPathData(uri);
if (pathData != null) {
// Uri has a full path and will not have any descendants.
descendants.add(uri);
return descendants;
}
final String authority = uri.getAuthority();
final String pathPrefix = uri.getPath();
final boolean isPathEmpty = pathPrefix.isEmpty();
// No path nor authority. Return all possible Uris.
if (isPathEmpty && TextUtils.isEmpty(authority)) {
final List<String> platformKeys = mSlicesDatabaseAccessor.getSliceKeys(
true /* isPlatformSlice */);
final List<String> oemKeys = mSlicesDatabaseAccessor.getSliceKeys(
false /* isPlatformSlice */);
final List<Uri> allUris = buildUrisFromKeys(platformKeys,
SettingsSlicesContract.AUTHORITY);
allUris.addAll(buildUrisFromKeys(oemKeys, SettingsSliceProvider.SLICE_AUTHORITY));
return allUris;
}
// Path is anything but empty, "action", or "intent". Return empty list.
if (!isPathEmpty
&& !TextUtils.equals(pathPrefix, "/" + SettingsSlicesContract.PATH_SETTING_ACTION)
&& !TextUtils.equals(pathPrefix,
"/" + SettingsSlicesContract.PATH_SETTING_INTENT)) {
// Invalid path prefix, there are no valid Uri descendants.
return descendants;
}
// Can assume authority belongs to the provider. Return all Uris for the authority.
final boolean isPlatformUri = TextUtils.equals(authority, SettingsSlicesContract.AUTHORITY);
final List<String> keys = mSlicesDatabaseAccessor.getSliceKeys(isPlatformUri);
return buildUrisFromKeys(keys, authority);
}
private List<Uri> buildUrisFromKeys(List<String> keys, String authority) {
final List<Uri> descendants = new ArrayList<>();
final Uri.Builder builder = new Uri.Builder()
.scheme(ContentResolver.SCHEME_CONTENT)
.authority(authority)
.appendPath(SettingsSlicesContract.PATH_SETTING_ACTION);
final String newUriPathPrefix = SettingsSlicesContract.PATH_SETTING_ACTION + "/";
for (String key : keys) {
builder.path(newUriPathPrefix + key);
descendants.add(builder.build());
}
return descendants;
}
@VisibleForTesting
void loadSlice(Uri uri) {
long startBuildTime = System.currentTimeMillis();

View File

@@ -130,7 +130,7 @@ public class SliceBuilderUtils {
// Example: "/action/wifi" -> [{}, "action", "wifi"]
// "/action/longer/path" -> [{}, "action", "longer/path"]
if (split.length != 3) {
throw new IllegalArgumentException("Uri (" + uri + ") has incomplete path: " + path);
return null;
}
final boolean isInline = TextUtils.equals(SettingsSlicesContract.PATH_SETTING_ACTION,

View File

@@ -29,6 +29,9 @@ import android.util.Pair;
import com.android.settings.overlay.FeatureFactory;
import com.android.settings.slices.SlicesDatabaseHelper.IndexColumns;
import java.util.ArrayList;
import java.util.List;
import androidx.slice.Slice;
/**
@@ -51,10 +54,12 @@ public class SlicesDatabaseAccessor {
// Cursor value for boolean true
private final int TRUE = 1;
Context mContext;
private final Context mContext;
private final SlicesDatabaseHelper mHelper;
public SlicesDatabaseAccessor(Context context) {
mContext = context;
mHelper = SlicesDatabaseHelper.getInstance(mContext);
}
/**
@@ -79,16 +84,44 @@ public class SlicesDatabaseAccessor {
return buildSliceData(cursor, null /* uri */, false /* isInlineOnly */);
}
/**
* @return a list of keys in the Slices database matching on {@param isPlatformSlice}.
*/
public List<String> getSliceKeys(boolean isPlatformSlice) {
final String whereClause;
if (isPlatformSlice) {
whereClause = IndexColumns.PLATFORM_SLICE + " = 1";
} else {
whereClause = IndexColumns.PLATFORM_SLICE + " = 0";
}
final SQLiteDatabase database = mHelper.getReadableDatabase();
final String[] columns = new String[]{IndexColumns.KEY};
final List<String> keys = new ArrayList<>();
try (final Cursor resultCursor = database.query(TABLE_SLICES_INDEX, columns, whereClause,
null /* selection */, null /* groupBy */, null /* having */, null /* orderBy */)) {
if (!resultCursor.moveToFirst()) {
return keys;
}
do {
keys.add(resultCursor.getString(0 /* key index */));
} while (resultCursor.moveToNext());
}
return keys;
}
private Cursor getIndexedSliceData(String path) {
verifyIndexing();
final String whereClause = buildKeyMatchWhereClause();
final SlicesDatabaseHelper helper = SlicesDatabaseHelper.getInstance(mContext);
final SQLiteDatabase database = helper.getReadableDatabase();
final SQLiteDatabase database = mHelper.getReadableDatabase();
final String[] selection = new String[]{path};
Cursor resultCursor = database.query(TABLE_SLICES_INDEX, SELECT_COLUMNS_ALL, whereClause,
selection, null /* groupBy */, null /* having */, null /* orderBy */);
final Cursor resultCursor = database.query(TABLE_SLICES_INDEX, SELECT_COLUMNS_ALL,
whereClause, selection, null /* groupBy */, null /* having */, null /* orderBy */);
int numResults = resultCursor.getCount();