Merge "Clear indexing before adding all Slices" into pi-dev

This commit is contained in:
TreeHugger Robot
2018-05-23 13:26:19 +00:00
committed by Android (Google) Code Review
5 changed files with 88 additions and 3 deletions

View File

@@ -31,6 +31,7 @@ import com.android.settings.Utils;
import com.android.settings.slices.SettingsSliceProvider;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
public interface DeviceIndexFeatureProvider {
@@ -39,15 +40,21 @@ public interface DeviceIndexFeatureProvider {
String TAG = "DeviceIndex";
String INDEX_VERSION = "settings:index_version";
String INDEX_LANGUAGE = "settings:language";
// Increment when new items are added to ensure they get pushed to the device index.
String VERSION = Build.FINGERPRINT;
// When the device language changes, re-index so Slices trigger in device language.
Locale LANGUAGE = Locale.getDefault();
boolean isIndexingEnabled();
void index(Context context, CharSequence title, Uri sliceUri, Uri launchUri,
List<String> keywords);
void clearIndex(Context context);
default void updateIndex(Context context, boolean force) {
if (!isIndexingEnabled()) {
Log.w(TAG, "Skipping: device index is not enabled");
@@ -59,12 +66,14 @@ public interface DeviceIndexFeatureProvider {
return;
}
if (!force && Objects.equals(
Settings.Secure.getString(context.getContentResolver(), INDEX_VERSION), VERSION)) {
if (!force && skipIndex(context)) {
// No need to update.
return;
}
// Prevent scheduling multiple jobs
setIndexState(context);
final ComponentName jobComponent = new ComponentName(context.getPackageName(),
DeviceIndexUpdateJobService.class.getName());
final int jobId = context.getResources().getInteger(R.integer.device_index_update);
@@ -77,7 +86,6 @@ public interface DeviceIndexFeatureProvider {
.setOverrideDeadline(1)
.build());
Settings.Secure.putString(context.getContentResolver(), INDEX_VERSION, VERSION);
}
static Uri createDeepLink(String s) {
@@ -86,4 +94,18 @@ public interface DeviceIndexFeatureProvider {
.appendQueryParameter(INTENT, s)
.build();
}
static boolean skipIndex(Context context) {
final boolean isSameVersion = Objects.equals(
Settings.Secure.getString(context.getContentResolver(), INDEX_VERSION), VERSION);
final boolean isSameLanguage = Objects.equals(
Settings.Secure.getString(context.getContentResolver(), INDEX_LANGUAGE), LANGUAGE);
return isSameLanguage && isSameVersion;
}
static void setIndexState(Context context) {
Settings.Secure.putString(context.getContentResolver(), INDEX_VERSION, VERSION);
Settings.Secure.putString(context.getContentResolver(), INDEX_LANGUAGE,
LANGUAGE.toString());
}
}

View File

@@ -31,4 +31,9 @@ public class DeviceIndexFeatureProviderImpl implements DeviceIndexFeatureProvide
List<String> keywords) {
// Not enabled by default.
}
@Override
public void clearIndex(Context context) {
// Not enabled by default.
}
}

View File

@@ -25,6 +25,7 @@ import android.content.ContentResolver;
import android.content.Intent;
import android.net.Uri;
import android.net.Uri.Builder;
import android.provider.SettingsSlicesContract;
import android.util.Log;
import com.android.internal.annotations.VisibleForTesting;
@@ -84,11 +85,19 @@ public class DeviceIndexUpdateJobService extends JobService {
.scheme(ContentResolver.SCHEME_CONTENT)
.authority(SettingsSliceProvider.SLICE_AUTHORITY)
.build();
final Uri platformBaseUri = new Builder()
.scheme(ContentResolver.SCHEME_CONTENT)
.authority(SettingsSlicesContract.AUTHORITY)
.build();
final Collection<Uri> slices = manager.getSliceDescendants(baseUri);
slices.addAll(manager.getSliceDescendants(platformBaseUri));
if (DEBUG) {
Log.d(TAG, "Indexing " + slices.size() + " slices");
}
indexProvider.clearIndex(this /* context */);
for (Uri slice : slices) {
if (!mRunningJob) {
return;