Slice background worker with Wi-Fi Slice

Test: manual

Change-Id: Ic4fdc5713f511ff80f03728c99c68fda3d0cab02
This commit is contained in:
Jason Chiu
2018-10-08 12:06:26 +08:00
parent 4ae062b7c8
commit f17233ba7d
6 changed files with 313 additions and 10 deletions

View 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);
}
}
}