Merge "Tie ContextualWifiSlice to UI instead of garbage collector"

This commit is contained in:
TreeHugger Robot
2019-03-15 21:41:29 +00:00
committed by Android (Google) Code Review
13 changed files with 113 additions and 91 deletions

View File

@@ -1,60 +0,0 @@
/*
* 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.Context;
import android.net.Uri;
import java.util.Map;
import java.util.WeakHashMap;
/**
* Manages custom {@link androidx.slice.Slice Slices}, which are all Slices not backed by
* preferences.
*/
public class CustomSliceManager {
private final Context mContext;
private final Map<Uri, CustomSliceable> mSliceableCache;
public CustomSliceManager(Context context) {
mContext = context.getApplicationContext();
mSliceableCache = new WeakHashMap<>();
}
/**
* Return a {@link CustomSliceable} associated to the Uri.
* <p>
* Do not change this method signature to accommodate for a special-case slicable - a context is
* the only thing that should be needed to create the object.
*/
public CustomSliceable getSliceableFromUri(Uri uri) {
final Uri newUri = CustomSliceRegistry.removeParameterFromUri(uri);
if (mSliceableCache.containsKey(newUri)) {
return mSliceableCache.get(newUri);
}
final Class clazz = CustomSliceRegistry.getSliceClassByUri(newUri);
if (clazz == null) {
throw new IllegalArgumentException("No Slice found for uri: " + uri);
}
final CustomSliceable sliceable = CustomSliceable.createInstance(mContext, clazz);
mSliceableCache.put(newUri, sliceable);
return sliceable;
}
}

View File

@@ -331,7 +331,7 @@ public class CustomSliceRegistry {
/**
* Returns {@code true} if {@param uri} is a valid Slice Uri handled by
* {@link CustomSliceManager}.
* {@link CustomSliceRegistry}.
*/
public static boolean isValidUri(Uri uri) {
return sUriToSlice.containsKey(removeParameterFromUri(uri));
@@ -339,7 +339,7 @@ public class CustomSliceRegistry {
/**
* Returns {@code true} if {@param action} is a valid intent action handled by
* {@link CustomSliceManager}.
* {@link CustomSliceRegistry}.
*/
public static boolean isValidAction(String action) {
return isValidUri(Uri.parse(action));

View File

@@ -110,7 +110,7 @@ public interface CustomSliceable extends Sliceable {
try {
final Constructor<? extends CustomSliceable> constructor =
sliceable.getConstructor(Context.class);
final Object[] params = new Object[]{context};
final Object[] params = new Object[]{context.getApplicationContext()};
return constructor.newInstance(params);
} catch (NoSuchMethodException | InstantiationException |
IllegalArgumentException | InvocationTargetException | IllegalAccessException e) {

View File

@@ -117,9 +117,6 @@ public class SettingsSliceProvider extends SliceProvider {
private static final KeyValueListParser KEY_VALUE_LIST_PARSER = new KeyValueListParser(',');
@VisibleForTesting
CustomSliceManager mCustomSliceManager;
@VisibleForTesting
SlicesDatabaseAccessor mSlicesDatabaseAccessor;
@@ -140,15 +137,15 @@ public class SettingsSliceProvider extends SliceProvider {
mSlicesDatabaseAccessor = new SlicesDatabaseAccessor(getContext());
mSliceDataCache = new ConcurrentHashMap<>();
mSliceWeakDataCache = new WeakHashMap<>();
mCustomSliceManager = FeatureFactory.getFactory(
getContext()).getSlicesFeatureProvider().getCustomSliceManager(getContext());
return true;
}
@Override
public void onSlicePinned(Uri sliceUri) {
if (CustomSliceRegistry.isValidUri(sliceUri)) {
final CustomSliceable sliceable = mCustomSliceManager.getSliceableFromUri(sliceUri);
final Context context = getContext();
final CustomSliceable sliceable = FeatureFactory.getFactory(context)
.getSlicesFeatureProvider().getSliceableFromUri(context, sliceUri);
final IntentFilter filter = sliceable.getIntentFilter();
if (filter != null) {
registerIntentToUri(filter, sliceUri);
@@ -195,9 +192,10 @@ public class SettingsSliceProvider extends SliceProvider {
// Before adding a slice to {@link CustomSliceManager}, please get approval
// from the Settings team.
if (CustomSliceRegistry.isValidUri(sliceUri)) {
final CustomSliceable sliceable = mCustomSliceManager.getSliceableFromUri(
sliceUri);
return sliceable.getSlice();
final Context context = getContext();
return FeatureFactory.getFactory(context)
.getSlicesFeatureProvider().getSliceableFromUri(context, sliceUri)
.getSlice();
}
if (CustomSliceRegistry.WIFI_CALLING_URI.equals(sliceUri)) {

View File

@@ -1,6 +1,7 @@
package com.android.settings.slices;
import android.content.Context;
import android.net.Uri;
import com.android.settings.network.telephony.Enhanced4gLteSliceHelper;
import com.android.settings.wifi.calling.WifiCallingSliceHelper;
@@ -14,6 +15,20 @@ public interface SlicesFeatureProvider {
SliceDataConverter getSliceDataConverter(Context context);
/**
* Starts a new UI session for the purpose of using Slices.
*
* A UI session is defined as an duration of time when user stays in a UI screen. Screen
* rotation does not break the continuation of session, going to a sub-page and coming out does
* not break the continuation either. Leaving the page and coming back breaks it.
*/
void newUiSession();
/**
* Returns the token created in {@link #newUiSession}.
*/
long getUiSessionToken();
/**
* Asynchronous call to index the data used to build Slices.
* If the data is already indexed, the data will not change.
@@ -26,7 +41,14 @@ public interface SlicesFeatureProvider {
*/
void indexSliceData(Context context);
CustomSliceManager getCustomSliceManager(Context context);
/**
* Return a {@link CustomSliceable} associated to the Uri.
* <p>
* Do not change this method signature to accommodate for a special-case sliceable - a context
* is the only thing that should be needed to create the object.
*/
CustomSliceable getSliceableFromUri(Context context, Uri uri);
/**
* Gets new WifiCallingSliceHelper object

View File

@@ -17,19 +17,23 @@
package com.android.settings.slices;
import android.content.Context;
import android.net.Uri;
import android.os.SystemClock;
import com.android.settings.network.telephony.Enhanced4gLteSliceHelper;
import com.android.settings.wifi.calling.WifiCallingSliceHelper;
import com.android.settingslib.utils.ThreadUtils;
import java.util.Random;
/**
* Manages Slices in Settings.
*/
public class SlicesFeatureProviderImpl implements SlicesFeatureProvider {
private long mUiSessionToken;
private SlicesIndexer mSlicesIndexer;
private SliceDataConverter mSliceDataConverter;
private CustomSliceManager mCustomSliceManager;
@Override
public SliceDataConverter getSliceDataConverter(Context context) {
@@ -40,11 +44,13 @@ public class SlicesFeatureProviderImpl implements SlicesFeatureProvider {
}
@Override
public CustomSliceManager getCustomSliceManager(Context context) {
if (mCustomSliceManager == null) {
mCustomSliceManager = new CustomSliceManager(context.getApplicationContext());
}
return mCustomSliceManager;
public void newUiSession() {
mUiSessionToken = SystemClock.elapsedRealtime();
}
@Override
public long getUiSessionToken() {
return mUiSessionToken;
}
@Override
@@ -69,6 +75,18 @@ public class SlicesFeatureProviderImpl implements SlicesFeatureProvider {
return new Enhanced4gLteSliceHelper(context);
}
@Override
public CustomSliceable getSliceableFromUri(Context context, Uri uri) {
final Uri newUri = CustomSliceRegistry.removeParameterFromUri(uri);
final Class clazz = CustomSliceRegistry.getSliceClassByUri(newUri);
if (clazz == null) {
throw new IllegalArgumentException("No Slice found for uri: " + uri);
}
final CustomSliceable sliceable = CustomSliceable.createInstance(context, clazz);
return sliceable;
}
private SlicesIndexer getSliceIndexer(Context context) {
if (mSlicesIndexer == null) {
mSlicesIndexer = new SlicesIndexer(context.getApplicationContext());