Add Battery slice in Contextual Settings Homepage
- Add Battery card that implements CustomSliceable in Contextual Settings Homepage. - Add test case for Battery slice. - Created a loadBatteryInfo method for BatterySlice. - Add a map in CustomSliceManager to cache CustomSliceable instances, let existing battery slice be able to update battery info. - Use a flag to avoid triggering an infinite loop when calling notifyChange in the callback function. Bug: 114796623, 115971399 Test: manual, robotests Change-Id: I4b785708bf8456c6c4de7cae4b44f8a060bccbae
This commit is contained in:
@@ -20,12 +20,14 @@ import android.content.Context;
|
||||
import android.net.Uri;
|
||||
import android.util.ArrayMap;
|
||||
|
||||
import com.android.settings.homepage.deviceinfo.BatterySlice;
|
||||
import com.android.settings.homepage.deviceinfo.DataUsageSlice;
|
||||
import com.android.settings.homepage.deviceinfo.DeviceInfoSlice;
|
||||
import com.android.settings.homepage.deviceinfo.StorageSlice;
|
||||
import com.android.settings.wifi.WifiSlice;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.WeakHashMap;
|
||||
|
||||
/**
|
||||
* Manages custom {@link androidx.slice.Slice Slices}, which are all Slices not backed by
|
||||
@@ -39,10 +41,12 @@ public class CustomSliceManager {
|
||||
protected final Map<Uri, Class<? extends CustomSliceable>> mUriMap;
|
||||
|
||||
private final Context mContext;
|
||||
private final Map<Uri, CustomSliceable> mSliceableCache;
|
||||
|
||||
public CustomSliceManager(Context context) {
|
||||
mContext = context.getApplicationContext();
|
||||
mUriMap = new ArrayMap<>();
|
||||
mSliceableCache = new WeakHashMap<>();
|
||||
addSlices();
|
||||
}
|
||||
|
||||
@@ -53,13 +57,18 @@ public class CustomSliceManager {
|
||||
* the only thing that should be needed to create the object.
|
||||
*/
|
||||
public CustomSliceable getSliceableFromUri(Uri uri) {
|
||||
final Class clazz = mUriMap.get(uri);
|
||||
if (mSliceableCache.containsKey(uri)) {
|
||||
return mSliceableCache.get(uri);
|
||||
}
|
||||
|
||||
final Class clazz = mUriMap.get(uri);
|
||||
if (clazz == null) {
|
||||
throw new IllegalArgumentException("No Slice found for uri: " + uri);
|
||||
}
|
||||
|
||||
return CustomSliceable.createInstance(mContext, clazz);
|
||||
final CustomSliceable sliceable = CustomSliceable.createInstance(mContext, clazz);
|
||||
mSliceableCache.put(uri, sliceable);
|
||||
return sliceable;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -93,5 +102,6 @@ public class CustomSliceManager {
|
||||
mUriMap.put(DataUsageSlice.DATA_USAGE_CARD_URI, DataUsageSlice.class);
|
||||
mUriMap.put(DeviceInfoSlice.DEVICE_INFO_CARD_URI, DeviceInfoSlice.class);
|
||||
mUriMap.put(StorageSlice.STORAGE_CARD_URI, StorageSlice.class);
|
||||
mUriMap.put(BatterySlice.BATTERY_CARD_URI, BatterySlice.class);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user