diff options
author | Sunny Goyal <sunnygoyal@google.com> | 2018-06-29 14:40:18 -0700 |
---|---|---|
committer | Sunny Goyal <sunnygoyal@google.com> | 2018-07-03 14:39:21 -0700 |
commit | d0e360a8586f17268d57eb6c4dc01d21f1883f32 (patch) | |
tree | 87402d5a20c5d3604d2312acf28803b461081641 | |
parent | 87555c56dc464186219538c2b530bd275f8a8d1d (diff) | |
download | android_packages_apps_Trebuchet-d0e360a8586f17268d57eb6c4dc01d21f1883f32.tar.gz android_packages_apps_Trebuchet-d0e360a8586f17268d57eb6c4dc01d21f1883f32.tar.bz2 android_packages_apps_Trebuchet-d0e360a8586f17268d57eb6c4dc01d21f1883f32.zip |
Lazy loading LauncherAppState
Separating InvarantDeviceProfile out of LauncherAppState and creating
LauncherAppState only when it is actually used
Change-Id: I2ee55f53cae01f11203f94675bb5f70c65ad2b9d
6 files changed, 88 insertions, 59 deletions
diff --git a/quickstep/src/com/android/quickstep/OverviewCommandHelper.java b/quickstep/src/com/android/quickstep/OverviewCommandHelper.java index eff94fcb5..7094a53db 100644 --- a/quickstep/src/com/android/quickstep/OverviewCommandHelper.java +++ b/quickstep/src/com/android/quickstep/OverviewCommandHelper.java @@ -197,14 +197,10 @@ public class OverviewCommandHelper { } public void onTip(int actionType, int viewType) { - mMainThreadExecutor.execute(new Runnable() { - @Override - public void run() { + mMainThreadExecutor.execute(() -> UserEventDispatcher.newInstance(mContext, - new InvariantDeviceProfile(mContext).getDeviceProfile(mContext)) - .logActionTip(actionType, viewType); - } - }); + InvariantDeviceProfile.INSTANCE.get(mContext).getDeviceProfile(mContext)) + .logActionTip(actionType, viewType)); } public ActivityControlHelper getActivityControlHelper() { diff --git a/quickstep/src/com/android/quickstep/RecentsActivity.java b/quickstep/src/com/android/quickstep/RecentsActivity.java index ed8b4d2b1..32079bf13 100644 --- a/quickstep/src/com/android/quickstep/RecentsActivity.java +++ b/quickstep/src/com/android/quickstep/RecentsActivity.java @@ -42,7 +42,6 @@ import com.android.launcher3.DeviceProfile; import com.android.launcher3.InvariantDeviceProfile; import com.android.launcher3.ItemInfo; import com.android.launcher3.LauncherAnimationRunner; -import com.android.launcher3.LauncherAppState; import com.android.launcher3.R; import com.android.launcher3.anim.Interpolators; import com.android.launcher3.badge.BadgeInfo; @@ -131,21 +130,13 @@ public class RecentsActivity extends BaseDraggingActivity { } private void initDeviceProfile() { - // In case we are reusing IDP, create a copy so that we dont conflict with Launcher + DeviceProfile dp = InvariantDeviceProfile.INSTANCE.get(this).getDeviceProfile(this); + + // In case we are reusing IDP, create a copy so that we don't conflict with Launcher // activity. - LauncherAppState appState = LauncherAppState.getInstanceNoCreate(); - if (isInMultiWindowModeCompat()) { - InvariantDeviceProfile idp = appState == null - ? new InvariantDeviceProfile(this) : appState.getInvariantDeviceProfile(); - DeviceProfile dp = idp.getDeviceProfile(this); - mDeviceProfile = mRecentsRootView == null ? dp.copy(this) - : dp.getMultiWindowProfile(this, mRecentsRootView.getLastKnownSize()); - } else { - // If we are reusing the Invariant device profile, make a copy. - mDeviceProfile = appState == null - ? new InvariantDeviceProfile(this).getDeviceProfile(this) - : appState.getInvariantDeviceProfile().getDeviceProfile(this).copy(this); - } + mDeviceProfile = (mRecentsRootView != null) && isInMultiWindowModeCompat() + ? dp.getMultiWindowProfile(this, mRecentsRootView.getLastKnownSize()) + : dp.copy(this); onDeviceProfileInitiated(); } diff --git a/quickstep/src/com/android/quickstep/WindowTransformSwipeHandler.java b/quickstep/src/com/android/quickstep/WindowTransformSwipeHandler.java index 15ff19e26..366263309 100644 --- a/quickstep/src/com/android/quickstep/WindowTransformSwipeHandler.java +++ b/quickstep/src/com/android/quickstep/WindowTransformSwipeHandler.java @@ -57,7 +57,6 @@ import com.android.launcher3.AbstractFloatingView; import com.android.launcher3.BaseDraggingActivity; import com.android.launcher3.DeviceProfile; import com.android.launcher3.InvariantDeviceProfile; -import com.android.launcher3.LauncherAppState; import com.android.launcher3.R; import com.android.launcher3.Utilities; import com.android.launcher3.anim.AnimationSuccessListener; @@ -611,10 +610,7 @@ public class WindowTransformSwipeHandler<T extends BaseDraggingActivity> { public void onRecentsAnimationStart(RecentsAnimationControllerCompat controller, RemoteAnimationTargetSet targets, Rect homeContentInsets, Rect minimizedHomeBounds) { - LauncherAppState appState = LauncherAppState.getInstanceNoCreate(); - InvariantDeviceProfile idp = appState == null ? - new InvariantDeviceProfile(mContext) : appState.getInvariantDeviceProfile(); - DeviceProfile dp = idp.getDeviceProfile(mContext); + DeviceProfile dp = InvariantDeviceProfile.INSTANCE.get(mContext).getDeviceProfile(mContext); final Rect overviewStackBounds; RemoteAnimationTargetCompat runningTaskTarget = targets.findTask(mRunningTaskId); diff --git a/src/com/android/launcher3/InvariantDeviceProfile.java b/src/com/android/launcher3/InvariantDeviceProfile.java index f63cce58d..22bc162b6 100644 --- a/src/com/android/launcher3/InvariantDeviceProfile.java +++ b/src/com/android/launcher3/InvariantDeviceProfile.java @@ -29,6 +29,8 @@ import android.view.Display; import android.view.WindowManager; import com.android.launcher3.config.FeatureFlags; +import com.android.launcher3.util.ConfigMonitor; +import com.android.launcher3.util.MainThreadInitializedObject; import com.android.launcher3.util.Thunk; import org.xmlpull.v1.XmlPullParser; @@ -41,8 +43,12 @@ import java.util.Comparator; public class InvariantDeviceProfile { - // This is a static that we use for the default icon size on a 4/5-inch phone - private static float DEFAULT_ICON_SIZE_DP = 60; + // We do not need any synchronization for this variable as its only written on UI thread. + public static final MainThreadInitializedObject<InvariantDeviceProfile> INSTANCE = + new MainThreadInitializedObject<>((c) -> { + new ConfigMonitor(c).register(); + return new InvariantDeviceProfile(c); + }); private static final float ICON_SIZE_DEFINED_IN_APP_DP = 48; @@ -118,7 +124,7 @@ public class InvariantDeviceProfile { } @TargetApi(23) - public InvariantDeviceProfile(Context context) { + private InvariantDeviceProfile(Context context) { WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); Display display = wm.getDefaultDisplay(); DisplayMetrics dm = new DisplayMetrics(); diff --git a/src/com/android/launcher3/LauncherAppState.java b/src/com/android/launcher3/LauncherAppState.java index a46692b0b..5159de17a 100644 --- a/src/com/android/launcher3/LauncherAppState.java +++ b/src/com/android/launcher3/LauncherAppState.java @@ -16,12 +16,13 @@ package com.android.launcher3; +import static com.android.launcher3.SettingsActivity.NOTIFICATION_BADGING; + import android.content.ComponentName; import android.content.ContentProviderClient; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; -import android.os.Looper; import android.util.Log; import com.android.launcher3.compat.LauncherAppsCompat; @@ -29,21 +30,17 @@ import com.android.launcher3.compat.PackageInstallerCompat; import com.android.launcher3.compat.UserManagerCompat; import com.android.launcher3.config.FeatureFlags; import com.android.launcher3.notification.NotificationListener; -import com.android.launcher3.util.ConfigMonitor; +import com.android.launcher3.util.MainThreadInitializedObject; import com.android.launcher3.util.Preconditions; import com.android.launcher3.util.SettingsObserver; -import java.util.concurrent.Callable; -import java.util.concurrent.ExecutionException; - -import static com.android.launcher3.SettingsActivity.NOTIFICATION_BADGING; - public class LauncherAppState { public static final String ACTION_FORCE_ROLOAD = "force-reload-launcher"; // We do not need any synchronization for this variable as its only written on UI thread. - private static LauncherAppState INSTANCE; + private static final MainThreadInitializedObject<LauncherAppState> INSTANCE = + new MainThreadInitializedObject<>((c) -> new LauncherAppState(c)); private final Context mContext; private final LauncherModel mModel; @@ -53,27 +50,11 @@ public class LauncherAppState { private final SettingsObserver mNotificationBadgingObserver; public static LauncherAppState getInstance(final Context context) { - if (INSTANCE == null) { - if (Looper.myLooper() == Looper.getMainLooper()) { - INSTANCE = new LauncherAppState(context.getApplicationContext()); - } else { - try { - return new MainThreadExecutor().submit(new Callable<LauncherAppState>() { - @Override - public LauncherAppState call() throws Exception { - return LauncherAppState.getInstance(context); - } - }).get(); - } catch (InterruptedException|ExecutionException e) { - throw new RuntimeException(e); - } - } - } - return INSTANCE; + return INSTANCE.get(context); } public static LauncherAppState getInstanceNoCreate() { - return INSTANCE; + return INSTANCE.getNoCreate(); } public Context getContext() { @@ -89,7 +70,7 @@ public class LauncherAppState { Preconditions.assertUIThread(); mContext = context; - mInvariantDeviceProfile = new InvariantDeviceProfile(mContext); + mInvariantDeviceProfile = InvariantDeviceProfile.INSTANCE.get(mContext); mIconCache = new IconCache(mContext, mInvariantDeviceProfile); mWidgetCache = new WidgetPreviewLoader(mContext, mIconCache); mModel = new LauncherModel(this, mIconCache, AppFilter.newInstance(mContext)); @@ -112,7 +93,6 @@ public class LauncherAppState { mContext.registerReceiver(mModel, filter); UserManagerCompat.getInstance(mContext).enableAndResetCache(); - new ConfigMonitor(mContext).register(); if (!mContext.getResources().getBoolean(R.bool.notification_badging_enabled)) { mNotificationBadgingObserver = null; @@ -171,7 +151,7 @@ public class LauncherAppState { * Shorthand for {@link #getInvariantDeviceProfile()} */ public static InvariantDeviceProfile getIDP(Context context) { - return LauncherAppState.getInstance(context).getInvariantDeviceProfile(); + return InvariantDeviceProfile.INSTANCE.get(context); } private static LauncherProvider getLocalProvider(Context context) { diff --git a/src/com/android/launcher3/util/MainThreadInitializedObject.java b/src/com/android/launcher3/util/MainThreadInitializedObject.java new file mode 100644 index 000000000..5747db1c1 --- /dev/null +++ b/src/com/android/launcher3/util/MainThreadInitializedObject.java @@ -0,0 +1,60 @@ +/* + * Copyright (C) 2018 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.content.Context; +import android.os.Looper; + +import com.android.launcher3.MainThreadExecutor; + +import java.util.concurrent.ExecutionException; + +/** + * Utility class for defining singletons which are initiated on main thread. + */ +public class MainThreadInitializedObject<T> { + + private final ObjectProvider<T> mProvider; + private T mValue; + + public MainThreadInitializedObject(ObjectProvider<T> provider) { + mProvider = provider; + } + + public T get(Context context) { + if (mValue == null) { + if (Looper.myLooper() == Looper.getMainLooper()) { + mValue = mProvider.get(context.getApplicationContext()); + } else { + try { + return new MainThreadExecutor().submit(() -> get(context)).get(); + } catch (InterruptedException|ExecutionException e) { + throw new RuntimeException(e); + } + } + } + return mValue; + } + + public T getNoCreate() { + return mValue; + } + + public interface ObjectProvider<T> { + + T get(Context context); + } +} |