Migrating RecentsAnimationDeviceState and dependent objects to Dagger

Bug: 361850561
Flag: EXEMPT dagger
Test: Presubmit

Change-Id: I2c45ec49a913abb0532e83b09e797012e06eb8c4
This commit is contained in:
Sunny Goyal
2025-01-21 23:53:02 -08:00
parent e3220bec92
commit d7bfa76bcc
18 changed files with 272 additions and 172 deletions
@@ -21,7 +21,9 @@ import dagger.Module;
@Module(includes = {
WindowManagerProxyModule.class,
ApiWrapperModule.class,
PluginManagerWrapperModule.class
PluginManagerWrapperModule.class,
StaticObjectModule.class,
AppModule.class
})
public class LauncherAppModule {
}
@@ -27,6 +27,7 @@ import com.android.launcher3.util.ApiWrapper;
import com.android.launcher3.util.DaggerSingletonTracker;
import com.android.launcher3.util.DisplayController;
import com.android.launcher3.util.DynamicResource;
import com.android.launcher3.util.LockedUserState;
import com.android.launcher3.util.MSDLPlayerWrapper;
import com.android.launcher3.util.PackageManagerHelper;
import com.android.launcher3.util.PluginManagerWrapper;
@@ -68,6 +69,7 @@ public interface LauncherBaseAppComponent {
ThemeManager getThemeManager();
DisplayController getDisplayController();
WallpaperColorHints getWallpaperColorHints();
LockedUserState getLockedUserState();
/** Builder for LauncherBaseAppComponent. */
interface Builder {
@@ -20,10 +20,17 @@ import android.content.Intent
import android.os.Process
import android.os.UserManager
import androidx.annotation.VisibleForTesting
import com.android.launcher3.dagger.ApplicationContext
import com.android.launcher3.dagger.LauncherAppComponent
import com.android.launcher3.dagger.LauncherAppSingleton
import com.android.launcher3.util.Executors.MAIN_EXECUTOR
import com.android.launcher3.util.Executors.UI_HELPER_EXECUTOR
import javax.inject.Inject
class LockedUserState(private val mContext: Context) : SafeCloseable {
@LauncherAppSingleton
class LockedUserState
@Inject
constructor(@ApplicationContext private val context: Context, lifeCycle: DaggerSingletonTracker) {
val isUserUnlockedAtLauncherStartup: Boolean
var isUserUnlocked = false
private set(value) {
@@ -36,7 +43,7 @@ class LockedUserState(private val mContext: Context) : SafeCloseable {
private val mUserUnlockedActions: RunnableList = RunnableList()
@VisibleForTesting
val mUserUnlockedReceiver =
val userUnlockedReceiver =
SimpleBroadcastReceiver(UI_HELPER_EXECUTOR) {
if (Intent.ACTION_USER_UNLOCKED == it.action) {
isUserUnlocked = true
@@ -53,8 +60,8 @@ class LockedUserState(private val mContext: Context) : SafeCloseable {
isUserUnlocked = checkIsUserUnlocked()
isUserUnlockedAtLauncherStartup = isUserUnlocked
if (!isUserUnlocked) {
mUserUnlockedReceiver.register(
mContext,
userUnlockedReceiver.register(
context,
{
// If user is unlocked while registering broadcast receiver, we should update
// [isUserUnlocked], which will call [notifyUserUnlocked] in setter
@@ -62,22 +69,18 @@ class LockedUserState(private val mContext: Context) : SafeCloseable {
MAIN_EXECUTOR.execute { isUserUnlocked = true }
}
},
Intent.ACTION_USER_UNLOCKED
Intent.ACTION_USER_UNLOCKED,
)
}
lifeCycle.addCloseable { userUnlockedReceiver.unregisterReceiverSafely(context) }
}
private fun checkIsUserUnlocked() =
mContext.getSystemService(UserManager::class.java)!!.isUserUnlocked(Process.myUserHandle())
context.getSystemService(UserManager::class.java)!!.isUserUnlocked(Process.myUserHandle())
private fun notifyUserUnlocked() {
mUserUnlockedActions.executeAllAndDestroy()
mUserUnlockedReceiver.unregisterReceiverSafely(mContext)
}
/** Stops the receiver from listening for ACTION_USER_UNLOCK broadcasts. */
override fun close() {
mUserUnlockedReceiver.unregisterReceiverSafely(mContext)
userUnlockedReceiver.unregisterReceiverSafely(context)
}
/**
@@ -88,9 +91,7 @@ class LockedUserState(private val mContext: Context) : SafeCloseable {
mUserUnlockedActions.add(action)
}
/**
* Removes a previously queued `Runnable` to be run when the user is unlocked.
*/
/** Removes a previously queued `Runnable` to be run when the user is unlocked. */
fun removeOnUserUnlockedRunnable(action: Runnable) {
mUserUnlockedActions.remove(action)
}
@@ -98,7 +99,7 @@ class LockedUserState(private val mContext: Context) : SafeCloseable {
companion object {
@VisibleForTesting
@JvmField
val INSTANCE = MainThreadInitializedObject { LockedUserState(it) }
val INSTANCE = DaggerSingletonObject(LauncherAppComponent::getLockedUserState)
@JvmStatic fun get(context: Context): LockedUserState = INSTANCE.get(context)
}
@@ -34,11 +34,11 @@ import com.android.launcher3.dagger.ApplicationContext;
import com.android.launcher3.dagger.LauncherAppSingleton;
import com.android.launcher3.dagger.LauncherBaseAppComponent;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Function;
import javax.inject.Inject;
@@ -57,7 +57,7 @@ import javax.inject.Inject;
* Cache will also be updated if a key queried is missing (even if it has no listeners registered).
*/
@LauncherAppSingleton
public class SettingsCache extends ContentObserver implements SafeCloseable {
public class SettingsCache extends ContentObserver {
/** Hidden field Settings.Secure.NOTIFICATION_BADGING */
public static final Uri NOTIFICATION_BADGING_URI =
@@ -79,11 +79,17 @@ public class SettingsCache extends ContentObserver implements SafeCloseable {
private static final String SYSTEM_URI_PREFIX = Settings.System.CONTENT_URI.toString();
private static final String GLOBAL_URI_PREFIX = Settings.Global.CONTENT_URI.toString();
private final Function<Uri, CopyOnWriteArrayList<OnChangeListener>> mListenerMapper = uri -> {
registerUriAsync(uri);
return new CopyOnWriteArrayList<>();
};
/**
* Caches the last seen value for registered keys.
*/
private Map<Uri, Boolean> mKeyCache = new ConcurrentHashMap<>();
private final Map<Uri, CopyOnWriteArrayList<OnChangeListener>> mListenerMap = new HashMap<>();
private final Map<Uri, Boolean> mKeyCache = new ConcurrentHashMap<>();
private final Map<Uri, CopyOnWriteArrayList<OnChangeListener>> mListenerMap =
new ConcurrentHashMap<>();
protected final ContentResolver mResolver;
/**
@@ -96,12 +102,8 @@ public class SettingsCache extends ContentObserver implements SafeCloseable {
SettingsCache(@ApplicationContext Context context, DaggerSingletonTracker tracker) {
super(new Handler(Looper.getMainLooper()));
mResolver = context.getContentResolver();
tracker.addCloseable(this);
}
@Override
public void close() {
UI_HELPER_EXECUTOR.execute(() -> mResolver.unregisterContentObserver(this));
tracker.addCloseable(() ->
UI_HELPER_EXECUTOR.execute(() -> mResolver.unregisterContentObserver(this)));
}
@Override
@@ -109,11 +111,12 @@ public class SettingsCache extends ContentObserver implements SafeCloseable {
// We use default of 1, but if we're getting an onChange call, can assume a non-default
// value will exist
boolean newVal = updateValue(uri, 1 /* Effectively Unused */);
if (!mListenerMap.containsKey(uri)) {
List<OnChangeListener> listeners = mListenerMap.get(uri);
if (listeners == null) {
return;
}
for (OnChangeListener listener : mListenerMap.get(uri)) {
for (OnChangeListener listener : listeners) {
listener.onSettingsChanged(newVal);
}
}
@@ -138,22 +141,17 @@ public class SettingsCache extends ContentObserver implements SafeCloseable {
}
}
private void registerUriAsync(Uri uri) {
UI_HELPER_EXECUTOR.execute(() -> mResolver.registerContentObserver(uri, false, this));
}
/**
* Does not de-dupe if you add same listeners for the same key multiple times.
* Unregister once complete using {@link #unregister(Uri, OnChangeListener)}
*/
@UiThread
public void register(Uri uri, OnChangeListener changeListener) {
Preconditions.assertUIThread();
if (mListenerMap.containsKey(uri)) {
mListenerMap.get(uri).add(changeListener);
} else {
CopyOnWriteArrayList<OnChangeListener> l = new CopyOnWriteArrayList<>();
l.add(changeListener);
mListenerMap.put(uri, l);
UI_HELPER_EXECUTOR.execute(
() -> mResolver.registerContentObserver(uri, false, this));
}
mListenerMap.computeIfAbsent(uri, mListenerMapper).add(changeListener);
}
private boolean updateValue(Uri keyUri, int defaultValue) {