blob: d4b41e67106f00b40174dca4294187d47fc84a1b [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
19import android.app.SearchManager;
Sunny Goyale755d462014-07-22 13:48:29 -070020import android.content.ComponentName;
Sunny Goyale755d462014-07-22 13:48:29 -070021import android.content.Context;
22import android.content.Intent;
23import android.content.IntentFilter;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040024import android.content.res.Configuration;
Michael Jurka104c4562013-07-08 18:03:46 -070025import android.content.res.Resources;
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040026import android.util.Log;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080027
Sunny Goyal83a8f042015-05-19 12:52:12 -070028import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Kenny Guyed131872014-04-30 03:02:21 +010029import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal4390ace2014-10-13 11:33:11 -070030import com.android.launcher3.compat.PackageInstallerCompat;
Adam Cohen091440a2015-03-18 14:16:05 -070031import com.android.launcher3.util.Thunk;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080032
Daniel Sandlercc8befa2013-06-11 14:45:48 -040033import java.lang.ref.WeakReference;
34
Sunny Goyalc6205602015-05-21 20:46:33 -070035public class LauncherAppState {
Chris Wrenaeff7ea2014-02-14 16:59:24 -050036
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -080037 private final AppFilter mAppFilter;
38 private final BuildInfo mBuildInfo;
Adam Cohen091440a2015-03-18 14:16:05 -070039 @Thunk final LauncherModel mModel;
Sunny Goyale0f58d72014-11-10 18:05:31 -080040 private final IconCache mIconCache;
Sunny Goyal5b0e6692015-03-19 14:31:19 -070041 private final WidgetPreviewLoader mWidgetCache;
Sunny Goyale0f58d72014-11-10 18:05:31 -080042
43 private final boolean mIsScreenLarge;
44 private final float mScreenDensity;
45 private final int mLongPressTimeout = 300;
46
Michael Jurkaa6a05472013-11-13 17:59:46 +010047 private boolean mWallpaperChangedSinceLastCheck;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040048
Daniel Sandlere4f98912013-06-25 15:13:26 -040049 private static WeakReference<LauncherProvider> sLauncherProvider;
50 private static Context sContext;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040051
Daniel Sandlere4f98912013-06-25 15:13:26 -040052 private static LauncherAppState INSTANCE;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040053
Adam Cohen2e6da152015-05-06 11:42:25 -070054 private InvariantDeviceProfile mInvariantDeviceProfile;
Adam Cohenc9735cf2015-01-23 16:11:55 -080055 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Winson Chung5f8afe62013-08-12 16:19:28 -070056
Daniel Sandlercc8befa2013-06-11 14:45:48 -040057 public static LauncherAppState getInstance() {
Daniel Sandlere4f98912013-06-25 15:13:26 -040058 if (INSTANCE == null) {
59 INSTANCE = new LauncherAppState();
60 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040061 return INSTANCE;
62 }
63
Chris Wrend8fe6de2013-10-04 10:42:14 -040064 public static LauncherAppState getInstanceNoCreate() {
65 return INSTANCE;
66 }
67
Daniel Sandlercc8befa2013-06-11 14:45:48 -040068 public Context getContext() {
Daniel Sandlere4f98912013-06-25 15:13:26 -040069 return sContext;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040070 }
71
Daniel Sandlere4f98912013-06-25 15:13:26 -040072 public static void setApplicationContext(Context context) {
73 if (sContext != null) {
Daniel Sandlere060b0b2013-06-27 21:47:55 -040074 Log.w(Launcher.TAG, "setApplicationContext called twice! old=" + sContext + " new=" + context);
Daniel Sandlere4f98912013-06-25 15:13:26 -040075 }
76 sContext = context.getApplicationContext();
77 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040078
Daniel Sandlere4f98912013-06-25 15:13:26 -040079 private LauncherAppState() {
80 if (sContext == null) {
81 throw new IllegalStateException("LauncherAppState inited before app context set");
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040082 }
83
Daniel Sandlere4f98912013-06-25 15:13:26 -040084 Log.v(Launcher.TAG, "LauncherAppState inited");
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040085
Daniel Sandlere4f98912013-06-25 15:13:26 -040086 if (sContext.getResources().getBoolean(R.bool.debug_memory_enabled)) {
87 MemoryTracker.startTrackingMe(sContext, "L");
88 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040089
Daniel Sandlere4f98912013-06-25 15:13:26 -040090 // set sIsScreenXLarge and mScreenDensity *before* creating icon cache
Michael Jurka104c4562013-07-08 18:03:46 -070091 mIsScreenLarge = isScreenLarge(sContext.getResources());
Daniel Sandlere4f98912013-06-25 15:13:26 -040092 mScreenDensity = sContext.getResources().getDisplayMetrics().density;
Adam Cohen2e6da152015-05-06 11:42:25 -070093 mInvariantDeviceProfile = new InvariantDeviceProfile(sContext);
94 mIconCache = new IconCache(sContext, mInvariantDeviceProfile);
95 mWidgetCache = new WidgetPreviewLoader(sContext, mInvariantDeviceProfile, mIconCache);
Bjorn Bringert1307f632013-10-03 22:31:03 +010096
97 mAppFilter = AppFilter.loadByName(sContext.getString(R.string.app_filter_class));
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -080098 mBuildInfo = BuildInfo.loadByName(sContext.getString(R.string.build_info_class));
Bjorn Bringert1307f632013-10-03 22:31:03 +010099 mModel = new LauncherModel(this, mIconCache, mAppFilter);
Sunny Goyal27595792015-03-19 13:18:44 -0700100
101 LauncherAppsCompat.getInstance(sContext).addOnAppsChangedCallback(mModel);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400102
103 // Register intent receivers
Kenny Guyed131872014-04-30 03:02:21 +0100104 IntentFilter filter = new IntentFilter();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400105 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
106 filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400107 filter.addAction(SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400108 filter.addAction(SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED);
Sunny Goyal957c13f2015-05-01 13:02:20 -0700109 // For handling managed profiles
110 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED);
111 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED);
112
Daniel Sandlere4f98912013-06-25 15:13:26 -0400113 sContext.registerReceiver(mModel, filter);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400114 }
Kenny Guy1317e2d2014-05-08 18:52:50 +0100115
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400116 /**
117 * Call from Application.onTerminate(), which is not guaranteed to ever be called.
118 */
119 public void onTerminate() {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400120 sContext.unregisterReceiver(mModel);
Kenny Guy1317e2d2014-05-08 18:52:50 +0100121 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(sContext);
Kenny Guyc2bd8102014-06-30 12:30:31 +0100122 launcherApps.removeOnAppsChangedCallback(mModel);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700123 PackageInstallerCompat.getInstance(sContext).onStop();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400124 }
125
126 /**
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700127 * Reloads the workspace items from the DB and re-binds the workspace. This should generally
128 * not be called as DB updates are automatically followed by UI update
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400129 */
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700130 public void reloadWorkspace() {
131 mModel.resetLoadedState(false, true);
132 mModel.startLoaderFromBackground();
133 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400134
135 LauncherModel setLauncher(Launcher launcher) {
136 mModel.initialize(launcher);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800137 mAccessibilityDelegate = ((launcher != null) && Utilities.isLmpOrAbove()) ?
138 new LauncherAccessibilityDelegate(launcher) : null;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400139 return mModel;
140 }
141
Hyunyoung Song3f471442015-04-08 19:01:34 -0700142 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800143 return mAccessibilityDelegate;
144 }
145
Sunny Goyal34942622014-08-29 17:20:55 -0700146 public IconCache getIconCache() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400147 return mIconCache;
148 }
149
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700150 public LauncherModel getModel() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400151 return mModel;
152 }
153
Bjorn Bringert1307f632013-10-03 22:31:03 +0100154 boolean shouldShowAppOrWidgetProvider(ComponentName componentName) {
155 return mAppFilter == null || mAppFilter.shouldShowApp(componentName);
156 }
157
Daniel Sandlere4f98912013-06-25 15:13:26 -0400158 static void setLauncherProvider(LauncherProvider provider) {
159 sLauncherProvider = new WeakReference<LauncherProvider>(provider);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400160 }
161
Daniel Sandlere4f98912013-06-25 15:13:26 -0400162 static LauncherProvider getLauncherProvider() {
163 return sLauncherProvider.get();
Daniel Sandler924b9932013-06-12 00:38:25 -0400164 }
165
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400166 public static String getSharedPreferencesKey() {
Helena Josol28db2802014-10-09 17:04:09 +0100167 return LauncherFiles.SHARED_PREFERENCES_KEY;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400168 }
169
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700170 public WidgetPreviewLoader getWidgetCache() {
171 return mWidgetCache;
172 }
173
Daniel Sandlere4f98912013-06-25 15:13:26 -0400174 public boolean isScreenLarge() {
175 return mIsScreenLarge;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400176 }
177
Michael Jurka104c4562013-07-08 18:03:46 -0700178 // Need a version that doesn't require an instance of LauncherAppState for the wallpaper picker
179 public static boolean isScreenLarge(Resources res) {
180 return res.getBoolean(R.bool.is_large_tablet);
181 }
182
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400183 public static boolean isScreenLandscape(Context context) {
184 return context.getResources().getConfiguration().orientation ==
185 Configuration.ORIENTATION_LANDSCAPE;
186 }
187
Daniel Sandlere4f98912013-06-25 15:13:26 -0400188 public float getScreenDensity() {
189 return mScreenDensity;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400190 }
191
Daniel Sandlere4f98912013-06-25 15:13:26 -0400192 public int getLongPressTimeout() {
193 return mLongPressTimeout;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400194 }
Winson Chung6e1c0d32013-10-25 15:24:24 -0700195
Michael Jurkaa6a05472013-11-13 17:59:46 +0100196 public void onWallpaperChanged() {
197 mWallpaperChangedSinceLastCheck = true;
198 }
199
200 public boolean hasWallpaperChangedSinceLastCheck() {
201 boolean result = mWallpaperChangedSinceLastCheck;
202 mWallpaperChangedSinceLastCheck = false;
203 return result;
204 }
205
Adam Cohen2e6da152015-05-06 11:42:25 -0700206 public InvariantDeviceProfile getInvariantDeviceProfile() {
207 return mInvariantDeviceProfile;
208 }
209
Jorim Jaggieedb00a2014-01-13 13:45:07 -0800210 public static boolean isDogfoodBuild() {
211 return getInstance().mBuildInfo.isDogfoodBuild();
212 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400213}