blob: 93753a201374c53388eac4c0adc2c5fb7009bc7b [file] [log] [blame]
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.launcher3;
18
Sunny Goyal4bbf4192014-11-11 12:23:59 -080019import android.annotation.TargetApi;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040020import android.app.SearchManager;
Sunny Goyale755d462014-07-22 13:48:29 -070021import android.content.ComponentName;
Sunny Goyale755d462014-07-22 13:48:29 -070022import android.content.Context;
23import android.content.Intent;
24import android.content.IntentFilter;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040025import android.content.res.Configuration;
Michael Jurka104c4562013-07-08 18:03:46 -070026import android.content.res.Resources;
Sunny Goyal33d44382014-10-16 09:24:19 -070027import android.graphics.Point;
Sunny Goyal4bbf4192014-11-11 12:23:59 -080028import android.os.Build;
Sunny Goyal33d44382014-10-16 09:24:19 -070029import android.util.DisplayMetrics;
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040030import android.util.Log;
Sunny Goyal33d44382014-10-16 09:24:19 -070031import android.view.Display;
32import android.view.WindowManager;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080033
Sunny Goyal83a8f042015-05-19 12:52:12 -070034import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Kenny Guyed131872014-04-30 03:02:21 +010035import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal4390ace2014-10-13 11:33:11 -070036import com.android.launcher3.compat.PackageInstallerCompat;
Adam Cohen091440a2015-03-18 14:16:05 -070037import com.android.launcher3.util.Thunk;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080038
Daniel Sandlercc8befa2013-06-11 14:45:48 -040039import java.lang.ref.WeakReference;
40
Winson Chung6e1c0d32013-10-25 15:24:24 -070041public class LauncherAppState implements DeviceProfile.DeviceProfileCallbacks {
Chris Wrenaeff7ea2014-02-14 16:59:24 -050042
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -080043 private final AppFilter mAppFilter;
44 private final BuildInfo mBuildInfo;
Adam Cohen091440a2015-03-18 14:16:05 -070045 @Thunk final LauncherModel mModel;
Sunny Goyale0f58d72014-11-10 18:05:31 -080046 private final IconCache mIconCache;
Sunny Goyal5b0e6692015-03-19 14:31:19 -070047 private final WidgetPreviewLoader mWidgetCache;
Sunny Goyale0f58d72014-11-10 18:05:31 -080048
49 private final boolean mIsScreenLarge;
50 private final float mScreenDensity;
51 private final int mLongPressTimeout = 300;
52
Michael Jurkaa6a05472013-11-13 17:59:46 +010053 private boolean mWallpaperChangedSinceLastCheck;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040054
Daniel Sandlere4f98912013-06-25 15:13:26 -040055 private static WeakReference<LauncherProvider> sLauncherProvider;
56 private static Context sContext;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040057
Daniel Sandlere4f98912013-06-25 15:13:26 -040058 private static LauncherAppState INSTANCE;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040059
Adam Cohen2e6da152015-05-06 11:42:25 -070060 private InvariantDeviceProfile mInvariantDeviceProfile;
Adam Cohenc9735cf2015-01-23 16:11:55 -080061 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Winson Chung5f8afe62013-08-12 16:19:28 -070062
Daniel Sandlercc8befa2013-06-11 14:45:48 -040063 public static LauncherAppState getInstance() {
Daniel Sandlere4f98912013-06-25 15:13:26 -040064 if (INSTANCE == null) {
65 INSTANCE = new LauncherAppState();
66 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040067 return INSTANCE;
68 }
69
Chris Wrend8fe6de2013-10-04 10:42:14 -040070 public static LauncherAppState getInstanceNoCreate() {
71 return INSTANCE;
72 }
73
Daniel Sandlercc8befa2013-06-11 14:45:48 -040074 public Context getContext() {
Daniel Sandlere4f98912013-06-25 15:13:26 -040075 return sContext;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040076 }
77
Daniel Sandlere4f98912013-06-25 15:13:26 -040078 public static void setApplicationContext(Context context) {
79 if (sContext != null) {
Daniel Sandlere060b0b2013-06-27 21:47:55 -040080 Log.w(Launcher.TAG, "setApplicationContext called twice! old=" + sContext + " new=" + context);
Daniel Sandlere4f98912013-06-25 15:13:26 -040081 }
82 sContext = context.getApplicationContext();
83 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040084
Daniel Sandlere4f98912013-06-25 15:13:26 -040085 private LauncherAppState() {
86 if (sContext == null) {
87 throw new IllegalStateException("LauncherAppState inited before app context set");
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040088 }
89
Daniel Sandlere4f98912013-06-25 15:13:26 -040090 Log.v(Launcher.TAG, "LauncherAppState inited");
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040091
Daniel Sandlere4f98912013-06-25 15:13:26 -040092 if (sContext.getResources().getBoolean(R.bool.debug_memory_enabled)) {
93 MemoryTracker.startTrackingMe(sContext, "L");
94 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040095
Daniel Sandlere4f98912013-06-25 15:13:26 -040096 // set sIsScreenXLarge and mScreenDensity *before* creating icon cache
Michael Jurka104c4562013-07-08 18:03:46 -070097 mIsScreenLarge = isScreenLarge(sContext.getResources());
Daniel Sandlere4f98912013-06-25 15:13:26 -040098 mScreenDensity = sContext.getResources().getDisplayMetrics().density;
Adam Cohen2e6da152015-05-06 11:42:25 -070099 mInvariantDeviceProfile = new InvariantDeviceProfile(sContext);
100 mIconCache = new IconCache(sContext, mInvariantDeviceProfile);
101 mWidgetCache = new WidgetPreviewLoader(sContext, mInvariantDeviceProfile, mIconCache);
Bjorn Bringert1307f632013-10-03 22:31:03 +0100102
103 mAppFilter = AppFilter.loadByName(sContext.getString(R.string.app_filter_class));
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800104 mBuildInfo = BuildInfo.loadByName(sContext.getString(R.string.build_info_class));
Bjorn Bringert1307f632013-10-03 22:31:03 +0100105 mModel = new LauncherModel(this, mIconCache, mAppFilter);
Sunny Goyal27595792015-03-19 13:18:44 -0700106
107 LauncherAppsCompat.getInstance(sContext).addOnAppsChangedCallback(mModel);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400108
109 // Register intent receivers
Kenny Guyed131872014-04-30 03:02:21 +0100110 IntentFilter filter = new IntentFilter();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400111 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
112 filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400113 filter.addAction(SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400114 filter.addAction(SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED);
Sunny Goyal957c13f2015-05-01 13:02:20 -0700115 // For handling managed profiles
116 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED);
117 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED);
118
Daniel Sandlere4f98912013-06-25 15:13:26 -0400119 sContext.registerReceiver(mModel, filter);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400120 }
Kenny Guy1317e2d2014-05-08 18:52:50 +0100121
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400122 /**
123 * Call from Application.onTerminate(), which is not guaranteed to ever be called.
124 */
125 public void onTerminate() {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400126 sContext.unregisterReceiver(mModel);
Kenny Guy1317e2d2014-05-08 18:52:50 +0100127 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(sContext);
Kenny Guyc2bd8102014-06-30 12:30:31 +0100128 launcherApps.removeOnAppsChangedCallback(mModel);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700129 PackageInstallerCompat.getInstance(sContext).onStop();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400130 }
131
132 /**
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700133 * Reloads the workspace items from the DB and re-binds the workspace. This should generally
134 * not be called as DB updates are automatically followed by UI update
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400135 */
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700136 public void reloadWorkspace() {
137 mModel.resetLoadedState(false, true);
138 mModel.startLoaderFromBackground();
139 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400140
141 LauncherModel setLauncher(Launcher launcher) {
142 mModel.initialize(launcher);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800143 mAccessibilityDelegate = ((launcher != null) && Utilities.isLmpOrAbove()) ?
144 new LauncherAccessibilityDelegate(launcher) : null;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400145 return mModel;
146 }
147
Hyunyoung Song3f471442015-04-08 19:01:34 -0700148 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800149 return mAccessibilityDelegate;
150 }
151
Sunny Goyal34942622014-08-29 17:20:55 -0700152 public IconCache getIconCache() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400153 return mIconCache;
154 }
155
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700156 public LauncherModel getModel() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400157 return mModel;
158 }
159
Bjorn Bringert1307f632013-10-03 22:31:03 +0100160 boolean shouldShowAppOrWidgetProvider(ComponentName componentName) {
161 return mAppFilter == null || mAppFilter.shouldShowApp(componentName);
162 }
163
Daniel Sandlere4f98912013-06-25 15:13:26 -0400164 static void setLauncherProvider(LauncherProvider provider) {
165 sLauncherProvider = new WeakReference<LauncherProvider>(provider);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400166 }
167
Daniel Sandlere4f98912013-06-25 15:13:26 -0400168 static LauncherProvider getLauncherProvider() {
169 return sLauncherProvider.get();
Daniel Sandler924b9932013-06-12 00:38:25 -0400170 }
171
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400172 public static String getSharedPreferencesKey() {
Helena Josol28db2802014-10-09 17:04:09 +0100173 return LauncherFiles.SHARED_PREFERENCES_KEY;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400174 }
175
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700176 public WidgetPreviewLoader getWidgetCache() {
177 return mWidgetCache;
178 }
179
Daniel Sandlere4f98912013-06-25 15:13:26 -0400180 public boolean isScreenLarge() {
181 return mIsScreenLarge;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400182 }
183
Michael Jurka104c4562013-07-08 18:03:46 -0700184 // Need a version that doesn't require an instance of LauncherAppState for the wallpaper picker
185 public static boolean isScreenLarge(Resources res) {
186 return res.getBoolean(R.bool.is_large_tablet);
187 }
188
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400189 public static boolean isScreenLandscape(Context context) {
190 return context.getResources().getConfiguration().orientation ==
191 Configuration.ORIENTATION_LANDSCAPE;
192 }
193
Daniel Sandlere4f98912013-06-25 15:13:26 -0400194 public float getScreenDensity() {
195 return mScreenDensity;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400196 }
197
Daniel Sandlere4f98912013-06-25 15:13:26 -0400198 public int getLongPressTimeout() {
199 return mLongPressTimeout;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400200 }
Winson Chung6e1c0d32013-10-25 15:24:24 -0700201
Michael Jurkaa6a05472013-11-13 17:59:46 +0100202 public void onWallpaperChanged() {
203 mWallpaperChangedSinceLastCheck = true;
204 }
205
206 public boolean hasWallpaperChangedSinceLastCheck() {
207 boolean result = mWallpaperChangedSinceLastCheck;
208 mWallpaperChangedSinceLastCheck = false;
209 return result;
210 }
211
Adam Cohen2e6da152015-05-06 11:42:25 -0700212 public InvariantDeviceProfile getInvariantDeviceProfile() {
213 return mInvariantDeviceProfile;
214 }
215
Winson Chung6e1c0d32013-10-25 15:24:24 -0700216 @Override
217 public void onAvailableSizeChanged(DeviceProfile grid) {
218 Utilities.setIconSize(grid.iconSizePx);
219 }
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800220
Jorim Jaggieedb00a2014-01-13 13:45:07 -0800221 public static boolean isDogfoodBuild() {
222 return getInstance().mBuildInfo.isDogfoodBuild();
223 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400224}