diff --git a/quickstep/src/com/android/launcher3/model/QuickstepModelDelegate.java b/quickstep/src/com/android/launcher3/model/QuickstepModelDelegate.java index 6af5a30b07..4014f069cc 100644 --- a/quickstep/src/com/android/launcher3/model/QuickstepModelDelegate.java +++ b/quickstep/src/com/android/launcher3/model/QuickstepModelDelegate.java @@ -237,7 +237,7 @@ public class QuickstepModelDelegate extends ModelDelegate { InstanceId instanceId = new InstanceIdSequence().newInstanceId(); for (ItemInfo info : itemsIdMap) { CollectionInfo parent = getContainer(info, itemsIdMap); - StatsLogCompatManager.writeSnapshot(info.buildProto(parent), instanceId); + StatsLogCompatManager.writeSnapshot(info.buildProto(parent, mContext), instanceId); } additionalSnapshotEvents(instanceId); prefs.put(LAST_SNAPSHOT_TIME_MILLIS, now); @@ -274,7 +274,7 @@ public class QuickstepModelDelegate extends ModelDelegate { for (ItemInfo info : itemsIdMap) { CollectionInfo parent = getContainer(info, itemsIdMap); - LauncherAtom.ItemInfo itemInfo = info.buildProto(parent); + LauncherAtom.ItemInfo itemInfo = info.buildProto(parent, mContext); Log.d(TAG, itemInfo.toString()); StatsEvent statsEvent = StatsLogCompatManager.buildStatsEvent(itemInfo, instanceId); diff --git a/quickstep/src/com/android/quickstep/logging/StatsLogCompatManager.java b/quickstep/src/com/android/quickstep/logging/StatsLogCompatManager.java index 1d4160d600..2daaaf9fe5 100644 --- a/quickstep/src/com/android/quickstep/logging/StatsLogCompatManager.java +++ b/quickstep/src/com/android/quickstep/logging/StatsLogCompatManager.java @@ -386,12 +386,12 @@ public class StatsLogCompatManager extends StatsLogManager { // and then write to StatsLog. app.getModel().enqueueModelUpdateTask((taskController, dataModel, apps) -> write(event, applyOverwrites(mItemInfo.buildProto( - dataModel.collections.get(mItemInfo.container))))); + dataModel.collections.get(mItemInfo.container), mContext)))); })) { // Write log on the model thread so that logs do not go out of order // (for eg: drop comes after drag) Executors.MODEL_EXECUTOR.execute( - () -> write(event, applyOverwrites(mItemInfo.buildProto()))); + () -> write(event, applyOverwrites(mItemInfo.buildProto(mContext)))); } } diff --git a/src/com/android/launcher3/dagger/LauncherBaseAppComponent.java b/src/com/android/launcher3/dagger/LauncherBaseAppComponent.java index d39c5deffe..088277b5ca 100644 --- a/src/com/android/launcher3/dagger/LauncherBaseAppComponent.java +++ b/src/com/android/launcher3/dagger/LauncherBaseAppComponent.java @@ -21,6 +21,7 @@ import android.content.Context; import com.android.launcher3.pm.InstallSessionHelper; import com.android.launcher3.util.DaggerSingletonTracker; import com.android.launcher3.util.ScreenOnTracker; +import com.android.launcher3.util.SettingsCache; import dagger.BindsInstance; @@ -36,6 +37,7 @@ public interface LauncherBaseAppComponent { DaggerSingletonTracker getDaggerSingletonTracker(); InstallSessionHelper getInstallSessionHelper(); ScreenOnTracker getScreenOnTracker(); + SettingsCache getSettingsCache(); /** Builder for LauncherBaseAppComponent. */ interface Builder { diff --git a/src/com/android/launcher3/model/data/AppPairInfo.kt b/src/com/android/launcher3/model/data/AppPairInfo.kt index 2eb6154476..3496c17e1a 100644 --- a/src/com/android/launcher3/model/data/AppPairInfo.kt +++ b/src/com/android/launcher3/model/data/AppPairInfo.kt @@ -74,7 +74,7 @@ class AppPairInfo() : CollectionInfo() { (ActivityContext.lookupContext(context) as ActivityContext).getDeviceProfile().isTablet return Pair( isTablet || !getFirstApp().isNonResizeable(), - isTablet || !getSecondApp().isNonResizeable() + isTablet || !getSecondApp().isNonResizeable(), ) } @@ -105,10 +105,10 @@ class AppPairInfo() : CollectionInfo() { } /** Generates an ItemInfo for logging. */ - override fun buildProto(cInfo: CollectionInfo?): LauncherAtom.ItemInfo { + override fun buildProto(cInfo: CollectionInfo?, context: Context): LauncherAtom.ItemInfo { val appPairIcon = LauncherAtom.FolderIcon.newBuilder().setCardinality(contents.size) appPairIcon.setLabelInfo(title.toString()) - return getDefaultItemInfoBuilder() + return getDefaultItemInfoBuilder(context) .setFolderIcon(appPairIcon) .setRank(rank) .setContainerInfo(getContainerInfo()) diff --git a/src/com/android/launcher3/model/data/CollectionInfo.kt b/src/com/android/launcher3/model/data/CollectionInfo.kt index 4f5e12fd30..12ba164713 100644 --- a/src/com/android/launcher3/model/data/CollectionInfo.kt +++ b/src/com/android/launcher3/model/data/CollectionInfo.kt @@ -17,7 +17,6 @@ package com.android.launcher3.model.data import com.android.launcher3.LauncherSettings -import com.android.launcher3.logger.LauncherAtom import com.android.launcher3.util.ContentWriter import java.util.function.Predicate @@ -42,9 +41,4 @@ abstract class CollectionInfo : ItemInfo() { super.onAddToDatabase(writer) writer.put(LauncherSettings.Favorites.TITLE, title) } - - /** Returns the collection wrapped as {@link LauncherAtom.ItemInfo} for logging. */ - override fun buildProto(): LauncherAtom.ItemInfo { - return buildProto(null) - } } diff --git a/src/com/android/launcher3/model/data/FolderInfo.java b/src/com/android/launcher3/model/data/FolderInfo.java index 18d2b85172..f0f289222e 100644 --- a/src/com/android/launcher3/model/data/FolderInfo.java +++ b/src/com/android/launcher3/model/data/FolderInfo.java @@ -24,6 +24,8 @@ import static com.android.launcher3.logger.LauncherAtom.Attribute.EMPTY_LABEL; import static com.android.launcher3.logger.LauncherAtom.Attribute.MANUAL_LABEL; import static com.android.launcher3.logger.LauncherAtom.Attribute.SUGGESTED_LABEL; +import android.content.Context; + import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -245,13 +247,13 @@ public class FolderInfo extends CollectionInfo { @NonNull @Override - public LauncherAtom.ItemInfo buildProto(@Nullable CollectionInfo cInfo) { + public LauncherAtom.ItemInfo buildProto(@Nullable CollectionInfo cInfo, Context context) { FolderIcon.Builder folderIcon = FolderIcon.newBuilder() .setCardinality(getContents().size()); if (LabelState.SUGGESTED.equals(getLabelState())) { folderIcon.setLabelInfo(title.toString()); } - return getDefaultItemInfoBuilder() + return getDefaultItemInfoBuilder(context) .setFolderIcon(folderIcon) .setRank(rank) .addItemAttributes(getLabelState().mLogAttribute) diff --git a/src/com/android/launcher3/model/data/ItemInfo.java b/src/com/android/launcher3/model/data/ItemInfo.java index b706d249f2..c22a8a51b1 100644 --- a/src/com/android/launcher3/model/data/ItemInfo.java +++ b/src/com/android/launcher3/model/data/ItemInfo.java @@ -36,6 +36,7 @@ import static com.android.launcher3.shortcuts.ShortcutKey.EXTRA_SHORTCUT_ID; import android.content.ComponentName; import android.content.ContentValues; +import android.content.Context; import android.content.Intent; import android.net.Uri; import android.os.Process; @@ -352,16 +353,16 @@ public class ItemInfo { * Creates {@link LauncherAtom.ItemInfo} with important fields and parent container info. */ @NonNull - public LauncherAtom.ItemInfo buildProto() { - return buildProto(null); + public LauncherAtom.ItemInfo buildProto(Context context) { + return buildProto(null, context); } /** * Creates {@link LauncherAtom.ItemInfo} with important fields and parent container info. */ @NonNull - public LauncherAtom.ItemInfo buildProto(@Nullable final CollectionInfo cInfo) { - LauncherAtom.ItemInfo.Builder itemBuilder = getDefaultItemInfoBuilder(); + public LauncherAtom.ItemInfo buildProto(@Nullable final CollectionInfo cInfo, Context context) { + LauncherAtom.ItemInfo.Builder itemBuilder = getDefaultItemInfoBuilder(context); Optional nullableComponent = Optional.ofNullable(getTargetComponent()); switch (itemType) { case ITEM_TYPE_APPLICATION: @@ -434,10 +435,10 @@ public class ItemInfo { } @NonNull - protected LauncherAtom.ItemInfo.Builder getDefaultItemInfoBuilder() { + protected LauncherAtom.ItemInfo.Builder getDefaultItemInfoBuilder(Context context) { LauncherAtom.ItemInfo.Builder itemBuilder = LauncherAtom.ItemInfo.newBuilder(); - SettingsCache.INSTANCE.executeIfCreated(cache -> - itemBuilder.setIsKidsMode(cache.getValue(NAV_BAR_KIDS_MODE, 0))); + itemBuilder.setIsKidsMode( + SettingsCache.INSTANCE.get(context).getValue(NAV_BAR_KIDS_MODE, 0)); UserCache.INSTANCE.executeIfCreated(cache -> itemBuilder.setUserType(getUserType(cache.getUserInfo(user)))); itemBuilder.setRank(rank); diff --git a/src/com/android/launcher3/model/data/LauncherAppWidgetInfo.java b/src/com/android/launcher3/model/data/LauncherAppWidgetInfo.java index 361f09d418..7569ed512b 100644 --- a/src/com/android/launcher3/model/data/LauncherAppWidgetInfo.java +++ b/src/com/android/launcher3/model/data/LauncherAppWidgetInfo.java @@ -24,6 +24,7 @@ import static com.android.launcher3.LauncherSettings.Favorites.CONTAINER_WIDGETS import android.appwidget.AppWidgetHostView; import android.content.ComponentName; +import android.content.Context; import android.content.Intent; import android.content.res.Resources; import android.os.Process; @@ -270,8 +271,9 @@ public class LauncherAppWidgetInfo extends ItemInfo { @NonNull @Override - public LauncherAtom.ItemInfo buildProto(@Nullable CollectionInfo collectionInfo) { - LauncherAtom.ItemInfo info = super.buildProto(collectionInfo); + public LauncherAtom.ItemInfo buildProto( + @Nullable CollectionInfo collectionInfo, Context context) { + LauncherAtom.ItemInfo info = super.buildProto(collectionInfo, context); return info.toBuilder() .setWidget(info.getWidget().toBuilder().setWidgetFeatures(widgetFeatures)) .addItemAttributes(getAttribute(sourceContainer)) diff --git a/src/com/android/launcher3/util/SettingsCache.java b/src/com/android/launcher3/util/SettingsCache.java index cd6701df93..a52f75bb43 100644 --- a/src/com/android/launcher3/util/SettingsCache.java +++ b/src/com/android/launcher3/util/SettingsCache.java @@ -25,14 +25,21 @@ import android.content.Context; import android.database.ContentObserver; import android.net.Uri; import android.os.Handler; +import android.os.Looper; import android.provider.Settings; +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 javax.inject.Inject; + /** * ContentObserver over Settings keys that also has a caching layer. * Consumers can register for callbacks via {@link #register(Uri, OnChangeListener)} and @@ -47,6 +54,7 @@ import java.util.concurrent.CopyOnWriteArrayList; * * 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 { /** Hidden field Settings.Secure.NOTIFICATION_BADGING */ @@ -79,12 +87,14 @@ public class SettingsCache extends ContentObserver implements SafeCloseable { /** * Singleton instance */ - public static MainThreadInitializedObject INSTANCE = - new MainThreadInitializedObject<>(SettingsCache::new); + public static final DaggerSingletonObject INSTANCE = + new DaggerSingletonObject<>(LauncherBaseAppComponent::getSettingsCache); - private SettingsCache(Context context) { - super(new Handler()); + @Inject + SettingsCache(@ApplicationContext final Context context, DaggerSingletonTracker tracker) { + super(new Handler(Looper.getMainLooper())); mResolver = context.getContentResolver(); + ExecutorUtil.executeSyncOnMainOrFail(() -> tracker.addCloseable(this)); } @Override diff --git a/src/com/android/launcher3/widget/PendingAddWidgetInfo.java b/src/com/android/launcher3/widget/PendingAddWidgetInfo.java index a9162527af..23ab0fb6a2 100644 --- a/src/com/android/launcher3/widget/PendingAddWidgetInfo.java +++ b/src/com/android/launcher3/widget/PendingAddWidgetInfo.java @@ -82,8 +82,9 @@ public class PendingAddWidgetInfo extends PendingAddItemInfo { @NonNull @Override - public LauncherAtom.ItemInfo buildProto(@Nullable CollectionInfo collectionInfo) { - LauncherAtom.ItemInfo info = super.buildProto(collectionInfo); + public LauncherAtom.ItemInfo buildProto( + @Nullable CollectionInfo collectionInfo, Context context) { + LauncherAtom.ItemInfo info = super.buildProto(collectionInfo, context); return info.toBuilder() .addItemAttributes(LauncherAppWidgetInfo.getAttribute(sourceContainer)) .build();