blob: afa09ec83cd7855af5ea644e87512a8de09486f4 [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;
Sunny Goyale0f58d72014-11-10 18:05:31 -080044 private final int mLongPressTimeout = 300;
45
Michael Jurkaa6a05472013-11-13 17:59:46 +010046 private boolean mWallpaperChangedSinceLastCheck;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040047
Daniel Sandlere4f98912013-06-25 15:13:26 -040048 private static WeakReference<LauncherProvider> sLauncherProvider;
49 private static Context sContext;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040050
Daniel Sandlere4f98912013-06-25 15:13:26 -040051 private static LauncherAppState INSTANCE;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040052
Adam Cohen2e6da152015-05-06 11:42:25 -070053 private InvariantDeviceProfile mInvariantDeviceProfile;
Adam Cohenc9735cf2015-01-23 16:11:55 -080054 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Winson Chung5f8afe62013-08-12 16:19:28 -070055
Daniel Sandlercc8befa2013-06-11 14:45:48 -040056 public static LauncherAppState getInstance() {
Daniel Sandlere4f98912013-06-25 15:13:26 -040057 if (INSTANCE == null) {
58 INSTANCE = new LauncherAppState();
59 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040060 return INSTANCE;
61 }
62
Chris Wrend8fe6de2013-10-04 10:42:14 -040063 public static LauncherAppState getInstanceNoCreate() {
64 return INSTANCE;
65 }
66
Daniel Sandlercc8befa2013-06-11 14:45:48 -040067 public Context getContext() {
Daniel Sandlere4f98912013-06-25 15:13:26 -040068 return sContext;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040069 }
70
Daniel Sandlere4f98912013-06-25 15:13:26 -040071 public static void setApplicationContext(Context context) {
72 if (sContext != null) {
Daniel Sandlere060b0b2013-06-27 21:47:55 -040073 Log.w(Launcher.TAG, "setApplicationContext called twice! old=" + sContext + " new=" + context);
Daniel Sandlere4f98912013-06-25 15:13:26 -040074 }
75 sContext = context.getApplicationContext();
76 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040077
Daniel Sandlere4f98912013-06-25 15:13:26 -040078 private LauncherAppState() {
79 if (sContext == null) {
80 throw new IllegalStateException("LauncherAppState inited before app context set");
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040081 }
82
Daniel Sandlere4f98912013-06-25 15:13:26 -040083 Log.v(Launcher.TAG, "LauncherAppState inited");
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040084
Daniel Sandlere4f98912013-06-25 15:13:26 -040085 if (sContext.getResources().getBoolean(R.bool.debug_memory_enabled)) {
86 MemoryTracker.startTrackingMe(sContext, "L");
87 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040088
Daniel Sandlere4f98912013-06-25 15:13:26 -040089 // set sIsScreenXLarge and mScreenDensity *before* creating icon cache
Michael Jurka104c4562013-07-08 18:03:46 -070090 mIsScreenLarge = isScreenLarge(sContext.getResources());
Adam Cohen2e6da152015-05-06 11:42:25 -070091 mInvariantDeviceProfile = new InvariantDeviceProfile(sContext);
92 mIconCache = new IconCache(sContext, mInvariantDeviceProfile);
93 mWidgetCache = new WidgetPreviewLoader(sContext, mInvariantDeviceProfile, mIconCache);
Bjorn Bringert1307f632013-10-03 22:31:03 +010094
95 mAppFilter = AppFilter.loadByName(sContext.getString(R.string.app_filter_class));
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -080096 mBuildInfo = BuildInfo.loadByName(sContext.getString(R.string.build_info_class));
Bjorn Bringert1307f632013-10-03 22:31:03 +010097 mModel = new LauncherModel(this, mIconCache, mAppFilter);
Sunny Goyal27595792015-03-19 13:18:44 -070098
99 LauncherAppsCompat.getInstance(sContext).addOnAppsChangedCallback(mModel);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400100
101 // Register intent receivers
Kenny Guyed131872014-04-30 03:02:21 +0100102 IntentFilter filter = new IntentFilter();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400103 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
104 filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400105 filter.addAction(SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400106 filter.addAction(SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED);
Sunny Goyal957c13f2015-05-01 13:02:20 -0700107 // For handling managed profiles
108 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED);
109 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED);
110
Daniel Sandlere4f98912013-06-25 15:13:26 -0400111 sContext.registerReceiver(mModel, filter);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400112 }
Kenny Guy1317e2d2014-05-08 18:52:50 +0100113
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400114 /**
115 * Call from Application.onTerminate(), which is not guaranteed to ever be called.
116 */
117 public void onTerminate() {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400118 sContext.unregisterReceiver(mModel);
Kenny Guy1317e2d2014-05-08 18:52:50 +0100119 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(sContext);
Kenny Guyc2bd8102014-06-30 12:30:31 +0100120 launcherApps.removeOnAppsChangedCallback(mModel);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700121 PackageInstallerCompat.getInstance(sContext).onStop();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400122 }
123
124 /**
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700125 * Reloads the workspace items from the DB and re-binds the workspace. This should generally
126 * not be called as DB updates are automatically followed by UI update
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400127 */
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700128 public void reloadWorkspace() {
129 mModel.resetLoadedState(false, true);
130 mModel.startLoaderFromBackground();
131 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400132
133 LauncherModel setLauncher(Launcher launcher) {
134 mModel.initialize(launcher);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800135 mAccessibilityDelegate = ((launcher != null) && Utilities.isLmpOrAbove()) ?
136 new LauncherAccessibilityDelegate(launcher) : null;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400137 return mModel;
138 }
139
Hyunyoung Song3f471442015-04-08 19:01:34 -0700140 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800141 return mAccessibilityDelegate;
142 }
143
Sunny Goyal34942622014-08-29 17:20:55 -0700144 public IconCache getIconCache() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400145 return mIconCache;
146 }
147
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700148 public LauncherModel getModel() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400149 return mModel;
150 }
151
Bjorn Bringert1307f632013-10-03 22:31:03 +0100152 boolean shouldShowAppOrWidgetProvider(ComponentName componentName) {
153 return mAppFilter == null || mAppFilter.shouldShowApp(componentName);
154 }
155
Daniel Sandlere4f98912013-06-25 15:13:26 -0400156 static void setLauncherProvider(LauncherProvider provider) {
157 sLauncherProvider = new WeakReference<LauncherProvider>(provider);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400158 }
159
Daniel Sandlere4f98912013-06-25 15:13:26 -0400160 static LauncherProvider getLauncherProvider() {
161 return sLauncherProvider.get();
Daniel Sandler924b9932013-06-12 00:38:25 -0400162 }
163
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400164 public static String getSharedPreferencesKey() {
Helena Josol28db2802014-10-09 17:04:09 +0100165 return LauncherFiles.SHARED_PREFERENCES_KEY;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400166 }
167
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700168 public WidgetPreviewLoader getWidgetCache() {
169 return mWidgetCache;
170 }
171
Daniel Sandlere4f98912013-06-25 15:13:26 -0400172 public boolean isScreenLarge() {
173 return mIsScreenLarge;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400174 }
175
Michael Jurka104c4562013-07-08 18:03:46 -0700176 // Need a version that doesn't require an instance of LauncherAppState for the wallpaper picker
177 public static boolean isScreenLarge(Resources res) {
178 return res.getBoolean(R.bool.is_large_tablet);
179 }
180
Daniel Sandlere4f98912013-06-25 15:13:26 -0400181 public int getLongPressTimeout() {
182 return mLongPressTimeout;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400183 }
Winson Chung6e1c0d32013-10-25 15:24:24 -0700184
Michael Jurkaa6a05472013-11-13 17:59:46 +0100185 public void onWallpaperChanged() {
186 mWallpaperChangedSinceLastCheck = true;
187 }
188
189 public boolean hasWallpaperChangedSinceLastCheck() {
190 boolean result = mWallpaperChangedSinceLastCheck;
191 mWallpaperChangedSinceLastCheck = false;
192 return result;
193 }
194
Adam Cohen2e6da152015-05-06 11:42:25 -0700195 public InvariantDeviceProfile getInvariantDeviceProfile() {
196 return mInvariantDeviceProfile;
197 }
198
Jorim Jaggieedb00a2014-01-13 13:45:07 -0800199 public static boolean isDogfoodBuild() {
200 return getInstance().mBuildInfo.isDogfoodBuild();
201 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400202}