Fix settings slice caching
Previously if there were multiple clients it would trigger an infinite loop as the cache gits dropped after the first bind, and the second client would trigger another load. Instead now cache as long as slices are pinned since thats the intended behavior of caching. Test: make RunSettingsRoboTests Change-Id: I7d29fab87573120b34cd55e1696c4c5b70fc891c Fixes: 78471335
This commit is contained in:
@@ -29,6 +29,7 @@ import android.provider.SettingsSlicesContract;
|
||||
import android.support.annotation.VisibleForTesting;
|
||||
import android.support.v4.graphics.drawable.IconCompat;
|
||||
import android.text.TextUtils;
|
||||
import android.util.ArrayMap;
|
||||
import android.util.Log;
|
||||
import android.util.Pair;
|
||||
|
||||
@@ -38,6 +39,7 @@ import com.android.settingslib.utils.ThreadUtils;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.WeakHashMap;
|
||||
@@ -111,12 +113,14 @@ public class SettingsSliceProvider extends SliceProvider {
|
||||
SlicesDatabaseAccessor mSlicesDatabaseAccessor;
|
||||
|
||||
@VisibleForTesting
|
||||
Map<Uri, SliceData> mSliceWeakDataCache;
|
||||
Map<Uri, SliceData> mSliceDataCache;
|
||||
|
||||
@Override
|
||||
public boolean onCreateSliceProvider() {
|
||||
mSlicesDatabaseAccessor = new SlicesDatabaseAccessor(getContext());
|
||||
mSliceDataCache = new WeakHashMap<>();
|
||||
mSliceDataCache = new ArrayMap<>();
|
||||
mSliceWeakDataCache = new WeakHashMap<>();
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -131,6 +135,17 @@ public class SettingsSliceProvider extends SliceProvider {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSlicePinned(Uri sliceUri) {
|
||||
// Start warming the slice, we expect someone will want it soon.
|
||||
loadSliceInBackground(sliceUri);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSliceUnpinned(Uri sliceUri) {
|
||||
mSliceDataCache.remove(sliceUri);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Slice onBindSlice(Uri sliceUri) {
|
||||
String path = sliceUri.getPath();
|
||||
@@ -141,14 +156,16 @@ public class SettingsSliceProvider extends SliceProvider {
|
||||
return createWifiSlice(sliceUri);
|
||||
}
|
||||
|
||||
SliceData cachedSliceData = mSliceDataCache.get(sliceUri);
|
||||
SliceData cachedSliceData = mSliceWeakDataCache.get(sliceUri);
|
||||
if (cachedSliceData == null) {
|
||||
loadSliceInBackground(sliceUri);
|
||||
return getSliceStub(sliceUri);
|
||||
}
|
||||
|
||||
// Remove the SliceData from the cache after it has been used to prevent a memory-leak.
|
||||
mSliceDataCache.remove(sliceUri);
|
||||
if (!mSliceDataCache.containsKey(sliceUri)) {
|
||||
mSliceWeakDataCache.remove(sliceUri);
|
||||
}
|
||||
return SliceBuilderUtils.buildSlice(getContext(), cachedSliceData);
|
||||
}
|
||||
|
||||
@@ -236,7 +253,12 @@ public class SettingsSliceProvider extends SliceProvider {
|
||||
long startBuildTime = System.currentTimeMillis();
|
||||
|
||||
final SliceData sliceData = mSlicesDatabaseAccessor.getSliceDataFromUri(uri);
|
||||
mSliceDataCache.put(uri, sliceData);
|
||||
List<Uri> pinnedSlices = getContext().getSystemService(
|
||||
SliceManager.class).getPinnedSlices();
|
||||
if (pinnedSlices.contains(uri)) {
|
||||
mSliceDataCache.put(uri, sliceData);
|
||||
}
|
||||
mSliceWeakDataCache.put(uri, sliceData);
|
||||
getContext().getContentResolver().notifyChange(uri, null /* content observer */);
|
||||
|
||||
Log.d(TAG, "Built slice (" + uri + ") in: " +
|
||||
|
Reference in New Issue
Block a user