Fixing MainThreadInitializedObject
> Making SafeCloseable implementation mandatory, to prevent leaks during test and preview > Removing getNoCreate method and defining executeIfCreated to avoid null pointer exceptions > Fixing sandbox value leaking into main, by Checking sandbox against App context > Converting sanbox to an interface instead a class Bug: 335280439 Test: Presubmit Flag: None Change-Id: I951dcde871898e745ff6490a1c4f8fd1512888f5
This commit is contained in:
1 parent
1f40fa0e7f
commit
10fa016352
35 files changed
+233
-245
No files matched your search
@@ -59,6 +59,7 @@ import com.android.launcher3.util.DisplayController.Info;
|
||||
import com.android.launcher3.util.LockedUserState;
|
||||
import com.android.launcher3.util.MainThreadInitializedObject;
|
||||
import com.android.launcher3.util.Partner;
|
||||
import com.android.launcher3.util.SafeCloseable;
|
||||
import com.android.launcher3.util.WindowBounds;
|
||||
import com.android.launcher3.util.window.WindowManagerProxy;
|
||||
|
||||
@@ -75,7 +76,7 @@ import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class InvariantDeviceProfile {
|
||||
public class InvariantDeviceProfile implements SafeCloseable {
|
||||
|
||||
public static final String TAG = "IDP";
|
||||
// We do not need any synchronization for this variable as its only written on UI thread.
|
||||
@@ -229,9 +230,8 @@ public class InvariantDeviceProfile {
|
||||
if (!newGridName.equals(gridName)) {
|
||||
LauncherPrefs.get(context).put(GRID_NAME, newGridName);
|
||||
}
|
||||
LockedUserState.get(context).runOnUserUnlocked(() -> {
|
||||
new DeviceGridState(this).writeToPrefs(context);
|
||||
});
|
||||
LockedUserState.get(context).runOnUserUnlocked(() ->
|
||||
new DeviceGridState(this).writeToPrefs(context));
|
||||
|
||||
DisplayController.INSTANCE.get(context).setPriorityListener(
|
||||
(displayContext, info, flags) -> {
|
||||
@@ -295,6 +295,11 @@ public class InvariantDeviceProfile {
|
||||
initGrid(context, myInfo, result, deviceType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
DisplayController.INSTANCE.executeIfCreated(dc -> dc.setPriorityListener(null));
|
||||
}
|
||||
|
||||
/**
|
||||
* Reinitialize the current grid after a restore, where some grids might now be disabled.
|
||||
*/
|
||||
|
||||
@@ -78,14 +78,10 @@ public class LauncherAppState implements SafeCloseable {
|
||||
|
||||
private final RunnableList mOnTerminateCallback = new RunnableList();
|
||||
|
||||
public static LauncherAppState getInstance(final Context context) {
|
||||
public static LauncherAppState getInstance(Context context) {
|
||||
return INSTANCE.get(context);
|
||||
}
|
||||
|
||||
public static LauncherAppState getInstanceNoCreate() {
|
||||
return INSTANCE.getNoCreate();
|
||||
}
|
||||
|
||||
public Context getContext() {
|
||||
return mContext;
|
||||
}
|
||||
|
||||
@@ -30,6 +30,7 @@ import com.android.launcher3.provider.RestoreDbTask.FIRST_LOAD_AFTER_RESTORE_KEY
|
||||
import com.android.launcher3.states.RotationHelper
|
||||
import com.android.launcher3.util.DisplayController
|
||||
import com.android.launcher3.util.MainThreadInitializedObject
|
||||
import com.android.launcher3.util.SafeCloseable
|
||||
import com.android.launcher3.util.Themes
|
||||
|
||||
/**
|
||||
@@ -37,7 +38,7 @@ import com.android.launcher3.util.Themes
|
||||
*
|
||||
* TODO(b/262721340): Replace all direct SharedPreference refs with LauncherPrefs / Item methods.
|
||||
*/
|
||||
class LauncherPrefs(private val encryptedContext: Context) {
|
||||
class LauncherPrefs(private val encryptedContext: Context) : SafeCloseable {
|
||||
private val deviceProtectedStorageContext =
|
||||
encryptedContext.createDeviceProtectedStorageContext()
|
||||
|
||||
@@ -242,6 +243,8 @@ class LauncherPrefs(private val encryptedContext: Context) {
|
||||
}
|
||||
}
|
||||
|
||||
override fun close() {}
|
||||
|
||||
companion object {
|
||||
@VisibleForTesting const val BOOT_AWARE_PREFS_KEY = "boot_aware_prefs"
|
||||
|
||||
|
||||
@@ -48,11 +48,11 @@ public class LauncherProvider extends ContentProvider {
|
||||
*/
|
||||
@Override
|
||||
public void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
|
||||
LauncherAppState appState = LauncherAppState.getInstanceNoCreate();
|
||||
if (appState == null || !appState.getModel().isModelLoaded()) {
|
||||
return;
|
||||
}
|
||||
appState.getModel().dumpState("", fd, writer, args);
|
||||
LauncherAppState.INSTANCE.executeIfCreated(appState -> {
|
||||
if (appState.getModel().isModelLoaded()) {
|
||||
appState.getModel().dumpState("", fd, writer, args);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -66,7 +66,6 @@ import com.android.launcher3.Hotseat;
|
||||
import com.android.launcher3.InsettableFrameLayout;
|
||||
import com.android.launcher3.InvariantDeviceProfile;
|
||||
import com.android.launcher3.LauncherAppState;
|
||||
import com.android.launcher3.LauncherPrefs;
|
||||
import com.android.launcher3.LauncherSettings.Favorites;
|
||||
import com.android.launcher3.R;
|
||||
import com.android.launcher3.Utilities;
|
||||
@@ -88,14 +87,11 @@ import com.android.launcher3.model.data.FolderInfo;
|
||||
import com.android.launcher3.model.data.ItemInfo;
|
||||
import com.android.launcher3.model.data.LauncherAppWidgetInfo;
|
||||
import com.android.launcher3.model.data.WorkspaceItemInfo;
|
||||
import com.android.launcher3.pm.InstallSessionHelper;
|
||||
import com.android.launcher3.pm.UserCache;
|
||||
import com.android.launcher3.util.ComponentKey;
|
||||
import com.android.launcher3.util.DisplayController;
|
||||
import com.android.launcher3.util.IntArray;
|
||||
import com.android.launcher3.util.IntSet;
|
||||
import com.android.launcher3.util.MainThreadInitializedObject.SandboxContext;
|
||||
import com.android.launcher3.util.PluginManagerWrapper;
|
||||
import com.android.launcher3.util.WindowBounds;
|
||||
import com.android.launcher3.util.window.WindowManagerProxy;
|
||||
import com.android.launcher3.views.ActivityContext;
|
||||
@@ -104,7 +100,6 @@ import com.android.launcher3.widget.BaseLauncherAppWidgetHostView;
|
||||
import com.android.launcher3.widget.LauncherAppWidgetProviderInfo;
|
||||
import com.android.launcher3.widget.LauncherWidgetHolder;
|
||||
import com.android.launcher3.widget.LocalColorExtractor;
|
||||
import com.android.launcher3.widget.custom.CustomWidgetManager;
|
||||
import com.android.launcher3.widget.util.WidgetSizes;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -136,13 +131,10 @@ public class LauncherPreviewRenderer extends ContextWrapper
|
||||
new ConcurrentLinkedQueue<>();
|
||||
|
||||
public PreviewContext(Context base, InvariantDeviceProfile idp) {
|
||||
super(base, UserCache.INSTANCE, InstallSessionHelper.INSTANCE, LauncherPrefs.INSTANCE,
|
||||
LauncherAppState.INSTANCE, InvariantDeviceProfile.INSTANCE,
|
||||
CustomWidgetManager.INSTANCE, PluginManagerWrapper.INSTANCE,
|
||||
WindowManagerProxy.INSTANCE, DisplayController.INSTANCE);
|
||||
super(base);
|
||||
mIdp = idp;
|
||||
mObjectMap.put(InvariantDeviceProfile.INSTANCE, idp);
|
||||
mObjectMap.put(LauncherAppState.INSTANCE,
|
||||
putObject(InvariantDeviceProfile.INSTANCE, idp);
|
||||
putObject(LauncherAppState.INSTANCE,
|
||||
new LauncherAppState(this, null /* iconCacheFileName */));
|
||||
}
|
||||
|
||||
|
||||
@@ -45,7 +45,6 @@ import com.android.launcher3.InvariantDeviceProfile;
|
||||
import com.android.launcher3.Launcher;
|
||||
import com.android.launcher3.LauncherAppState;
|
||||
import com.android.launcher3.LauncherSettings.Favorites;
|
||||
import com.android.launcher3.Utilities;
|
||||
import com.android.launcher3.logging.FileLog;
|
||||
import com.android.launcher3.model.data.ItemInfo;
|
||||
import com.android.launcher3.model.data.LauncherAppWidgetInfo;
|
||||
@@ -55,6 +54,7 @@ import com.android.launcher3.shortcuts.ShortcutRequest;
|
||||
import com.android.launcher3.util.MainThreadInitializedObject;
|
||||
import com.android.launcher3.util.PersistedItemArray;
|
||||
import com.android.launcher3.util.Preconditions;
|
||||
import com.android.launcher3.util.SafeCloseable;
|
||||
import com.android.launcher3.widget.LauncherAppWidgetProviderInfo;
|
||||
|
||||
import java.util.HashSet;
|
||||
@@ -65,7 +65,7 @@ import java.util.stream.Stream;
|
||||
/**
|
||||
* Class to maintain a queue of pending items to be added to the workspace.
|
||||
*/
|
||||
public class ItemInstallQueue {
|
||||
public class ItemInstallQueue implements SafeCloseable {
|
||||
|
||||
private static final String LOG = "ItemInstallQueue";
|
||||
|
||||
@@ -99,6 +99,9 @@ public class ItemInstallQueue {
|
||||
mContext = context;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {}
|
||||
|
||||
@WorkerThread
|
||||
private void ensureQueueLoaded() {
|
||||
Preconditions.assertWorkerThread();
|
||||
|
||||
@@ -427,12 +427,10 @@ public class ItemInfo {
|
||||
@NonNull
|
||||
protected LauncherAtom.ItemInfo.Builder getDefaultItemInfoBuilder() {
|
||||
LauncherAtom.ItemInfo.Builder itemBuilder = LauncherAtom.ItemInfo.newBuilder();
|
||||
UserIconInfo info = getUserInfo();
|
||||
itemBuilder.setIsWork(info != null && info.isWork());
|
||||
itemBuilder.setUserType(getUserType(info));
|
||||
SettingsCache settingsCache = SettingsCache.INSTANCE.getNoCreate();
|
||||
boolean isKidsMode = settingsCache != null && settingsCache.getValue(NAV_BAR_KIDS_MODE, 0);
|
||||
itemBuilder.setIsKidsMode(isKidsMode);
|
||||
SettingsCache.INSTANCE.executeIfCreated(cache ->
|
||||
itemBuilder.setIsKidsMode(cache.getValue(NAV_BAR_KIDS_MODE, 0)));
|
||||
UserCache.INSTANCE.executeIfCreated(cache ->
|
||||
itemBuilder.setUserType(getUserType(cache.getUserInfo(user))));
|
||||
itemBuilder.setRank(rank);
|
||||
return itemBuilder;
|
||||
}
|
||||
@@ -526,15 +524,6 @@ public class ItemInfo {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
private UserIconInfo getUserInfo() {
|
||||
UserCache userCache = UserCache.INSTANCE.getNoCreate();
|
||||
if (userCache == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return userCache.getUserInfo(user);
|
||||
}
|
||||
|
||||
private int getUserType(UserIconInfo info) {
|
||||
if (info == null) {
|
||||
return SysUiStatsLog.LAUNCHER_UICHANGED__USER_TYPE__TYPE_UNKNOWN;
|
||||
|
||||
@@ -32,7 +32,6 @@ import androidx.annotation.WorkerThread;
|
||||
import com.android.launcher3.Flags;
|
||||
import com.android.launcher3.LauncherPrefs;
|
||||
import com.android.launcher3.SessionCommitReceiver;
|
||||
import com.android.launcher3.Utilities;
|
||||
import com.android.launcher3.logging.FileLog;
|
||||
import com.android.launcher3.model.ItemInstallQueue;
|
||||
import com.android.launcher3.util.IntArray;
|
||||
@@ -41,6 +40,7 @@ import com.android.launcher3.util.MainThreadInitializedObject;
|
||||
import com.android.launcher3.util.PackageManagerHelper;
|
||||
import com.android.launcher3.util.PackageUserKey;
|
||||
import com.android.launcher3.util.Preconditions;
|
||||
import com.android.launcher3.util.SafeCloseable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
@@ -52,7 +52,7 @@ import java.util.Objects;
|
||||
* Utility class to tracking install sessions
|
||||
*/
|
||||
@SuppressWarnings("NewApi")
|
||||
public class InstallSessionHelper {
|
||||
public class InstallSessionHelper implements SafeCloseable {
|
||||
|
||||
@NonNull
|
||||
private static final String LOG = "InstallSessionHelper";
|
||||
@@ -89,6 +89,9 @@ public class InstallSessionHelper {
|
||||
mLauncherApps = context.getSystemService(LauncherApps.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() { }
|
||||
|
||||
@WorkerThread
|
||||
@NonNull
|
||||
private IntSet getPromiseIconIds() {
|
||||
|
||||
@@ -526,10 +526,7 @@ public class RestoreDbTask {
|
||||
}
|
||||
|
||||
logFavoritesTable(controller.getDb(), "launcher db after remap widget ids", null, null);
|
||||
LauncherAppState app = LauncherAppState.getInstanceNoCreate();
|
||||
if (app != null) {
|
||||
app.getModel().forceReload();
|
||||
}
|
||||
LauncherAppState.INSTANCE.executeIfCreated(app -> app.getModel().forceReload());
|
||||
}
|
||||
|
||||
private static void logDatabaseWidgetInfo(ModelDbController controller) {
|
||||
|
||||
@@ -94,13 +94,10 @@ public class TestInformationHandler implements ResourceBasedOverride {
|
||||
|
||||
protected Context mContext;
|
||||
protected DeviceProfile mDeviceProfile;
|
||||
protected LauncherAppState mLauncherAppState;
|
||||
|
||||
public void init(Context context) {
|
||||
mContext = context;
|
||||
mDeviceProfile = InvariantDeviceProfile.INSTANCE.
|
||||
get(context).getDeviceProfile(context);
|
||||
mLauncherAppState = LauncherAppState.getInstanceNoCreate();
|
||||
mDeviceProfile = InvariantDeviceProfile.INSTANCE.get(context).getDeviceProfile(context);
|
||||
if (sActivityLifecycleCallbacks == null) {
|
||||
sActivityLifecycleCallbacks = new ActivityLifecycleCallbacksAdapter() {
|
||||
@Override
|
||||
|
||||
@@ -1,63 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2020 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.launcher3.util;
|
||||
|
||||
import android.database.ContentObserver;
|
||||
import android.net.Uri;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
|
||||
import androidx.annotation.WorkerThread;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* Utility class to define an object which does most of it's processing on a
|
||||
* dedicated background thread.
|
||||
*/
|
||||
public abstract class BgObjectWithLooper {
|
||||
|
||||
/**
|
||||
* Start initialization of the object
|
||||
*/
|
||||
public final void initializeInBackground(String threadName) {
|
||||
new Thread(this::runOnThread, threadName).start();
|
||||
}
|
||||
|
||||
private void runOnThread() {
|
||||
Looper.prepare();
|
||||
onInitialized(Looper.myLooper());
|
||||
Looper.loop();
|
||||
}
|
||||
|
||||
/**
|
||||
* Called on the background thread to handle initialization
|
||||
*/
|
||||
@WorkerThread
|
||||
protected abstract void onInitialized(Looper looper);
|
||||
|
||||
/**
|
||||
* Helper method to create a content provider
|
||||
*/
|
||||
protected static ContentObserver newContentObserver(Handler handler, Consumer<Uri> command) {
|
||||
return new ContentObserver(handler) {
|
||||
@Override
|
||||
public void onChange(boolean selfChange, Uri uri) {
|
||||
command.accept(uri);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -33,7 +33,8 @@ import com.android.systemui.plugins.ResourceProvider;
|
||||
*
|
||||
* To allow customization for a particular resource, add them to dynamic_resources.xml
|
||||
*/
|
||||
public class DynamicResource implements ResourceProvider, PluginListener<ResourceProvider> {
|
||||
public class DynamicResource implements
|
||||
ResourceProvider, PluginListener<ResourceProvider>, SafeCloseable {
|
||||
|
||||
private static final MainThreadInitializedObject<DynamicResource> INSTANCE =
|
||||
new MainThreadInitializedObject<>(DynamicResource::new);
|
||||
@@ -47,6 +48,11 @@ public class DynamicResource implements ResourceProvider, PluginListener<Resourc
|
||||
ResourceProvider.class, false /* allowedMultiple */);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
PluginManagerWrapper.INSTANCE.get(mContext).removePluginListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getInt(@IntegerRes int resId) {
|
||||
return mContext.getResources().getInteger(resId);
|
||||
|
||||
@@ -28,17 +28,15 @@ import androidx.annotation.VisibleForTesting;
|
||||
import com.android.launcher3.util.ResourceBasedOverride.Overrides;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* Utility class for defining singletons which are initiated on main thread.
|
||||
*/
|
||||
public class MainThreadInitializedObject<T> {
|
||||
public class MainThreadInitializedObject<T extends SafeCloseable> {
|
||||
|
||||
private final ObjectProvider<T> mProvider;
|
||||
private T mValue;
|
||||
@@ -48,14 +46,14 @@ public class MainThreadInitializedObject<T> {
|
||||
}
|
||||
|
||||
public T get(Context context) {
|
||||
if (context instanceof SandboxContext sc) {
|
||||
Context app = context.getApplicationContext();
|
||||
if (app instanceof SandboxApplication sc) {
|
||||
return sc.getObject(this);
|
||||
}
|
||||
|
||||
if (mValue == null) {
|
||||
if (Looper.myLooper() == Looper.getMainLooper()) {
|
||||
mValue = TraceHelper.allowIpcs("main.thread.object",
|
||||
() -> mProvider.get(context.getApplicationContext()));
|
||||
mValue = TraceHelper.allowIpcs("main.thread.object", () -> mProvider.get(app));
|
||||
} else {
|
||||
try {
|
||||
return MAIN_EXECUTOR.submit(() -> get(context)).get();
|
||||
@@ -67,8 +65,18 @@ public class MainThreadInitializedObject<T> {
|
||||
return mValue;
|
||||
}
|
||||
|
||||
public T getNoCreate() {
|
||||
return mValue;
|
||||
/**
|
||||
* Executes the callback is the value is already created
|
||||
* @return true if the callback was executed, false otherwise
|
||||
*/
|
||||
public boolean executeIfCreated(Consumer<T> callback) {
|
||||
T v = mValue;
|
||||
if (v != null) {
|
||||
callback.accept(v);
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
@@ -79,8 +87,8 @@ public class MainThreadInitializedObject<T> {
|
||||
/**
|
||||
* Initializes a provider based on resource overrides
|
||||
*/
|
||||
public static <T extends ResourceBasedOverride> MainThreadInitializedObject<T> forOverride(
|
||||
Class<T> clazz, int resourceId) {
|
||||
public static <T extends ResourceBasedOverride & SafeCloseable> MainThreadInitializedObject<T>
|
||||
forOverride(Class<T> clazz, int resourceId) {
|
||||
return new MainThreadInitializedObject<>(c -> Overrides.getObject(clazz, c, resourceId));
|
||||
}
|
||||
|
||||
@@ -89,24 +97,36 @@ public class MainThreadInitializedObject<T> {
|
||||
T get(Context context);
|
||||
}
|
||||
|
||||
public interface SandboxApplication {
|
||||
|
||||
/**
|
||||
* Find a cached object from mObjectMap if we have already created one. If not, generate
|
||||
* an object using the provider.
|
||||
*/
|
||||
<T extends SafeCloseable> T getObject(MainThreadInitializedObject<T> object);
|
||||
|
||||
@UiThread
|
||||
default <T extends SafeCloseable> T createObject(MainThreadInitializedObject<T> object) {
|
||||
return object.mProvider.get((Context) this);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Abstract Context which allows custom implementations for
|
||||
* {@link MainThreadInitializedObject} providers
|
||||
*/
|
||||
public static class SandboxContext extends ContextWrapper {
|
||||
public static class SandboxContext extends ContextWrapper implements SandboxApplication {
|
||||
|
||||
private static final String TAG = "SandboxContext";
|
||||
|
||||
protected final Set<MainThreadInitializedObject> mAllowedObjects;
|
||||
protected final Map<MainThreadInitializedObject, Object> mObjectMap = new HashMap<>();
|
||||
protected final ArrayList<Object> mOrderedObjects = new ArrayList<>();
|
||||
private final Map<MainThreadInitializedObject, Object> mObjectMap = new HashMap<>();
|
||||
private final ArrayList<SafeCloseable> mOrderedObjects = new ArrayList<>();
|
||||
|
||||
private final Object mDestroyLock = new Object();
|
||||
private boolean mDestroyed = false;
|
||||
|
||||
public SandboxContext(Context base, MainThreadInitializedObject... allowedObjects) {
|
||||
public SandboxContext(Context base) {
|
||||
super(base);
|
||||
mAllowedObjects = new HashSet<>(Arrays.asList(allowedObjects));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -118,20 +138,14 @@ public class MainThreadInitializedObject<T> {
|
||||
synchronized (mDestroyLock) {
|
||||
// Destroy in reverse order
|
||||
for (int i = mOrderedObjects.size() - 1; i >= 0; i--) {
|
||||
Object o = mOrderedObjects.get(i);
|
||||
if (o instanceof SafeCloseable) {
|
||||
((SafeCloseable) o).close();
|
||||
}
|
||||
mOrderedObjects.get(i).close();
|
||||
}
|
||||
mDestroyed = true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Find a cached object from mObjectMap if we have already created one. If not, generate
|
||||
* an object using the provider.
|
||||
*/
|
||||
protected <T> T getObject(MainThreadInitializedObject<T> object) {
|
||||
@Override
|
||||
public <T extends SafeCloseable> T getObject(MainThreadInitializedObject<T> object) {
|
||||
synchronized (mDestroyLock) {
|
||||
if (mDestroyed) {
|
||||
Log.e(TAG, "Static object access with a destroyed context");
|
||||
@@ -142,12 +156,6 @@ public class MainThreadInitializedObject<T> {
|
||||
}
|
||||
if (Looper.myLooper() == Looper.getMainLooper()) {
|
||||
t = createObject(object);
|
||||
// Check if we've explicitly allowed the object or if it's a SafeCloseable,
|
||||
// it will get destroyed in onDestroy()
|
||||
if (!mAllowedObjects.contains(object) && !(t instanceof SafeCloseable)) {
|
||||
throw new IllegalStateException("Leaking unknown objects "
|
||||
+ object + " " + object.mProvider + " " + t);
|
||||
}
|
||||
mObjectMap.put(object, t);
|
||||
mOrderedObjects.add(t);
|
||||
return t;
|
||||
@@ -161,17 +169,12 @@ public class MainThreadInitializedObject<T> {
|
||||
}
|
||||
}
|
||||
|
||||
@UiThread
|
||||
protected <T> T createObject(MainThreadInitializedObject<T> object) {
|
||||
return object.mProvider.get(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Put a value into mObjectMap, can be used to put mocked MainThreadInitializedObject
|
||||
* instances into SandboxContext.
|
||||
*/
|
||||
@VisibleForTesting
|
||||
public <T> void putObject(MainThreadInitializedObject<T> object, T value) {
|
||||
public <T extends SafeCloseable> void putObject(
|
||||
MainThreadInitializedObject<T> object, T value) {
|
||||
mObjectMap.put(object, value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ import java.util.concurrent.CopyOnWriteArrayList;
|
||||
/**
|
||||
* Utility class for tracking if the screen is currently on or off
|
||||
*/
|
||||
public class ScreenOnTracker {
|
||||
public class ScreenOnTracker implements SafeCloseable {
|
||||
|
||||
public static final MainThreadInitializedObject<ScreenOnTracker> INSTANCE =
|
||||
new MainThreadInitializedObject<>(ScreenOnTracker::new);
|
||||
@@ -35,14 +35,21 @@ public class ScreenOnTracker {
|
||||
private final SimpleBroadcastReceiver mReceiver = new SimpleBroadcastReceiver(this::onReceive);
|
||||
private final CopyOnWriteArrayList<ScreenOnListener> mListeners = new CopyOnWriteArrayList<>();
|
||||
|
||||
private final Context mContext;
|
||||
private boolean mIsScreenOn;
|
||||
|
||||
private ScreenOnTracker(Context context) {
|
||||
// Assume that the screen is on to begin with
|
||||
mContext = context;
|
||||
mIsScreenOn = true;
|
||||
mReceiver.register(context, ACTION_SCREEN_ON, ACTION_SCREEN_OFF, ACTION_USER_PRESENT);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
mReceiver.unregisterReceiverSafely(mContext);
|
||||
}
|
||||
|
||||
private void onReceive(Intent intent) {
|
||||
String action = intent.getAction();
|
||||
if (ACTION_SCREEN_ON.equals(action)) {
|
||||
|
||||
@@ -39,7 +39,7 @@ import com.android.launcher3.Utilities;
|
||||
/**
|
||||
* Wrapper around {@link Vibrator} to easily perform haptic feedback where necessary.
|
||||
*/
|
||||
public class VibratorWrapper {
|
||||
public class VibratorWrapper implements SafeCloseable {
|
||||
|
||||
public static final MainThreadInitializedObject<VibratorWrapper> INSTANCE =
|
||||
new MainThreadInitializedObject<>(VibratorWrapper::new);
|
||||
@@ -77,6 +77,7 @@ public class VibratorWrapper {
|
||||
private final Vibrator mVibrator;
|
||||
private final boolean mHasVibrator;
|
||||
|
||||
private ContentObserver mHapticFeedbackObserver;
|
||||
private boolean mIsHapticFeedbackEnabled;
|
||||
|
||||
private VibratorWrapper(Context context) {
|
||||
@@ -86,14 +87,14 @@ public class VibratorWrapper {
|
||||
if (mHasVibrator) {
|
||||
final ContentResolver resolver = context.getContentResolver();
|
||||
mIsHapticFeedbackEnabled = isHapticFeedbackEnabled(resolver);
|
||||
final ContentObserver observer = new ContentObserver(MAIN_EXECUTOR.getHandler()) {
|
||||
mHapticFeedbackObserver = new ContentObserver(MAIN_EXECUTOR.getHandler()) {
|
||||
@Override
|
||||
public void onChange(boolean selfChange) {
|
||||
mIsHapticFeedbackEnabled = isHapticFeedbackEnabled(resolver);
|
||||
}
|
||||
};
|
||||
resolver.registerContentObserver(Settings.System.getUriFor(HAPTIC_FEEDBACK_ENABLED),
|
||||
false /* notifyForDescendants */, observer);
|
||||
false /* notifyForDescendants */, mHapticFeedbackObserver);
|
||||
} else {
|
||||
mIsHapticFeedbackEnabled = false;
|
||||
}
|
||||
@@ -126,6 +127,13 @@ public class VibratorWrapper {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
if (mHapticFeedbackObserver != null) {
|
||||
mContext.getContentResolver().unregisterContentObserver(mHapticFeedbackObserver);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This is called when the user swipes to/from all apps. This is meant to be used in between
|
||||
* long animation progresses so that it gives a dragging texture effect. For a better
|
||||
|
||||
@@ -59,6 +59,7 @@ import com.android.launcher3.testing.shared.ResourceUtils;
|
||||
import com.android.launcher3.util.MainThreadInitializedObject;
|
||||
import com.android.launcher3.util.NavigationMode;
|
||||
import com.android.launcher3.util.ResourceBasedOverride;
|
||||
import com.android.launcher3.util.SafeCloseable;
|
||||
import com.android.launcher3.util.WindowBounds;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -67,7 +68,7 @@ import java.util.List;
|
||||
/**
|
||||
* Utility class for mocking some window manager behaviours
|
||||
*/
|
||||
public class WindowManagerProxy implements ResourceBasedOverride {
|
||||
public class WindowManagerProxy implements ResourceBasedOverride, SafeCloseable {
|
||||
|
||||
private static final String TAG = "WindowManagerProxy";
|
||||
public static final int MIN_TABLET_WIDTH = 600;
|
||||
@@ -305,12 +306,12 @@ public class WindowManagerProxy implements ResourceBasedOverride {
|
||||
|
||||
navBarHeightPortrait = isTablet
|
||||
? (mTaskbarDrawnInProcess
|
||||
? 0 : systemRes.getDimensionPixelSize(R.dimen.taskbar_size))
|
||||
? 0 : context.getResources().getDimensionPixelSize(R.dimen.taskbar_size))
|
||||
: getDimenByName(systemRes, NAVBAR_HEIGHT);
|
||||
|
||||
navBarHeightLandscape = isTablet
|
||||
? (mTaskbarDrawnInProcess
|
||||
? 0 : systemRes.getDimensionPixelSize(R.dimen.taskbar_size))
|
||||
? 0 : context.getResources().getDimensionPixelSize(R.dimen.taskbar_size))
|
||||
: (isTabletOrGesture
|
||||
? getDimenByName(systemRes, NAVBAR_HEIGHT_LANDSCAPE) : 0);
|
||||
navbarWidthLandscape = isTabletOrGesture
|
||||
@@ -474,6 +475,9 @@ public class WindowManagerProxy implements ResourceBasedOverride {
|
||||
NavigationMode.THREE_BUTTONS;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() { }
|
||||
|
||||
/**
|
||||
* @see DisplayCutout#getSafeInsets
|
||||
*/
|
||||
|
||||
Reference in new issue
Block a user