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:
Sunny Goyal
2024-04-21 00:13:35 -07:00
parent 1f40fa0e7f
commit 10fa016352
35 changed files with 233 additions and 245 deletions
@@ -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