Slice background worker with Wi-Fi Slice
Test: manual Change-Id: Ic4fdc5713f511ff80f03728c99c68fda3d0cab02
This commit is contained in:
@@ -89,6 +89,16 @@ public interface CustomSliceable {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Settings Slices which can represent component lists that are updatable by the
|
||||
* {@link SliceBackgroundWorker} returned here.
|
||||
*
|
||||
* @return a {@link SliceBackgroundWorker} for fetching the list of results in the background.
|
||||
*/
|
||||
default SliceBackgroundWorker getBackgroundWorker() {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Standardize the intents returned to indicate actions by the Slice.
|
||||
* <p>
|
||||
|
||||
@@ -28,6 +28,7 @@ import android.os.StrictMode;
|
||||
import android.provider.Settings;
|
||||
import android.provider.SettingsSlicesContract;
|
||||
import android.text.TextUtils;
|
||||
import android.util.ArrayMap;
|
||||
import android.util.ArraySet;
|
||||
import android.util.KeyValueListParser;
|
||||
import android.util.Log;
|
||||
@@ -131,6 +132,8 @@ public class SettingsSliceProvider extends SliceProvider {
|
||||
|
||||
final Set<Uri> mRegisteredUris = new ArraySet<>();
|
||||
|
||||
final Map<Uri, SliceBackgroundWorker> mWorkerMap = new ArrayMap<>();
|
||||
|
||||
public SettingsSliceProvider() {
|
||||
super(READ_SEARCH_INDEXABLES);
|
||||
}
|
||||
@@ -165,6 +168,7 @@ public class SettingsSliceProvider extends SliceProvider {
|
||||
if (filter != null) {
|
||||
registerIntentToUri(filter, sliceUri);
|
||||
}
|
||||
startBackgroundWorker(sliceable);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -191,6 +195,7 @@ public class SettingsSliceProvider extends SliceProvider {
|
||||
SliceBroadcastRelay.unregisterReceivers(getContext(), sliceUri);
|
||||
mRegisteredUris.remove(sliceUri);
|
||||
}
|
||||
stopBackgroundWorker(sliceUri);
|
||||
mSliceDataCache.remove(sliceUri);
|
||||
}
|
||||
|
||||
@@ -348,6 +353,31 @@ public class SettingsSliceProvider extends SliceProvider {
|
||||
}
|
||||
}
|
||||
|
||||
private void startBackgroundWorker(CustomSliceable sliceable) {
|
||||
final SliceBackgroundWorker worker = sliceable.getBackgroundWorker();
|
||||
if (worker == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
final Uri uri = sliceable.getUri();
|
||||
Log.d(TAG, "Starting background worker for: " + uri);
|
||||
if (mWorkerMap.containsKey(uri)) {
|
||||
return;
|
||||
}
|
||||
|
||||
mWorkerMap.put(uri, worker);
|
||||
worker.onSlicePinned();
|
||||
}
|
||||
|
||||
private void stopBackgroundWorker(Uri uri) {
|
||||
final SliceBackgroundWorker worker = mWorkerMap.get(uri);
|
||||
if (worker != null) {
|
||||
Log.d(TAG, "Stopping background worker for: " + uri);
|
||||
worker.onSliceUnpinned();
|
||||
mWorkerMap.remove(uri);
|
||||
}
|
||||
}
|
||||
|
||||
private List<Uri> buildUrisFromKeys(List<String> keys, String authority) {
|
||||
final List<Uri> descendants = new ArrayList<>();
|
||||
|
||||
|
||||
85
src/com/android/settings/slices/SliceBackgroundWorker.java
Normal file
85
src/com/android/settings/slices/SliceBackgroundWorker.java
Normal file
@@ -0,0 +1,85 @@
|
||||
/*
|
||||
* Copyright (C) 2018 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License
|
||||
*/
|
||||
|
||||
package com.android.settings.slices;
|
||||
|
||||
import android.content.ContentResolver;
|
||||
import android.net.Uri;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* The Slice background worker is used to make Settings Slices be able to work with data that is
|
||||
* changing continuously, e.g. available Wi-Fi networks.
|
||||
*
|
||||
* The background worker will be started at {@link SettingsSliceProvider#onSlicePinned(Uri)}, and be
|
||||
* stopped at {@link SettingsSliceProvider#onSliceUnpinned(Uri)}.
|
||||
*
|
||||
* {@link SliceBackgroundWorker} caches the results, uses the cache to compare if there is any data
|
||||
* changed, and then notifies the Slice {@link Uri} to update.
|
||||
*/
|
||||
public abstract class SliceBackgroundWorker<E> {
|
||||
|
||||
private final ContentResolver mContentResolver;
|
||||
private final Uri mUri;
|
||||
|
||||
private List<E> mCachedResults;
|
||||
|
||||
protected SliceBackgroundWorker(ContentResolver cr, Uri uri) {
|
||||
mContentResolver = cr;
|
||||
mUri = uri;
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when the Slice is pinned. This is the place to register callbacks or initialize scan
|
||||
* tasks.
|
||||
*/
|
||||
protected abstract void onSlicePinned();
|
||||
|
||||
/**
|
||||
* Called when the Slice is unpinned. This is the place to unregister callbacks or perform any
|
||||
* final cleanup.
|
||||
*/
|
||||
protected abstract void onSliceUnpinned();
|
||||
|
||||
/**
|
||||
* @return a {@link List} of cached results
|
||||
*/
|
||||
public final List<E> getResults() {
|
||||
return mCachedResults == null ? null : new ArrayList<>(mCachedResults);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the results when data changes
|
||||
*/
|
||||
protected final void updateResults(List<E> results) {
|
||||
boolean needNotify = false;
|
||||
|
||||
if (results == null) {
|
||||
if (mCachedResults != null) {
|
||||
needNotify = true;
|
||||
}
|
||||
} else {
|
||||
needNotify = !results.equals(mCachedResults);
|
||||
}
|
||||
|
||||
if (needNotify) {
|
||||
mCachedResults = results;
|
||||
mContentResolver.notifyChange(mUri, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user