blob: 81d595aaf846da70e3cfe72c2356cff79b752a76 [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
Winson Chung7501adf2015-06-02 11:24:28 -070028import android.view.ViewConfiguration;
Sunny Goyal83a8f042015-05-19 12:52:12 -070029import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Kenny Guyed131872014-04-30 03:02:21 +010030import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal4390ace2014-10-13 11:33:11 -070031import com.android.launcher3.compat.PackageInstallerCompat;
Adam Cohen091440a2015-03-18 14:16:05 -070032import com.android.launcher3.util.Thunk;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080033
Daniel Sandlercc8befa2013-06-11 14:45:48 -040034import java.lang.ref.WeakReference;
35
Sunny Goyalc6205602015-05-21 20:46:33 -070036public class LauncherAppState {
Chris Wrenaeff7ea2014-02-14 16:59:24 -050037
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -080038 private final AppFilter mAppFilter;
39 private final BuildInfo mBuildInfo;
Adam Cohen091440a2015-03-18 14:16:05 -070040 @Thunk final LauncherModel mModel;
Sunny Goyale0f58d72014-11-10 18:05:31 -080041 private final IconCache mIconCache;
Sunny Goyal5b0e6692015-03-19 14:31:19 -070042 private final WidgetPreviewLoader mWidgetCache;
Sunny Goyale0f58d72014-11-10 18:05:31 -080043
Michael Jurkaa6a05472013-11-13 17:59:46 +010044 private boolean mWallpaperChangedSinceLastCheck;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040045
Daniel Sandlere4f98912013-06-25 15:13:26 -040046 private static WeakReference<LauncherProvider> sLauncherProvider;
47 private static Context sContext;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040048
Daniel Sandlere4f98912013-06-25 15:13:26 -040049 private static LauncherAppState INSTANCE;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040050
Adam Cohen2e6da152015-05-06 11:42:25 -070051 private InvariantDeviceProfile mInvariantDeviceProfile;
Adam Cohenc9735cf2015-01-23 16:11:55 -080052 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Winson Chung5f8afe62013-08-12 16:19:28 -070053
Daniel Sandlercc8befa2013-06-11 14:45:48 -040054 public static LauncherAppState getInstance() {
Daniel Sandlere4f98912013-06-25 15:13:26 -040055 if (INSTANCE == null) {
56 INSTANCE = new LauncherAppState();
57 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040058 return INSTANCE;
59 }
60
Chris Wrend8fe6de2013-10-04 10:42:14 -040061 public static LauncherAppState getInstanceNoCreate() {
62 return INSTANCE;
63 }
64
Daniel Sandlercc8befa2013-06-11 14:45:48 -040065 public Context getContext() {
Daniel Sandlere4f98912013-06-25 15:13:26 -040066 return sContext;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040067 }
68
Daniel Sandlere4f98912013-06-25 15:13:26 -040069 public static void setApplicationContext(Context context) {
70 if (sContext != null) {
Daniel Sandlere060b0b2013-06-27 21:47:55 -040071 Log.w(Launcher.TAG, "setApplicationContext called twice! old=" + sContext + " new=" + context);
Daniel Sandlere4f98912013-06-25 15:13:26 -040072 }
73 sContext = context.getApplicationContext();
74 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040075
Daniel Sandlere4f98912013-06-25 15:13:26 -040076 private LauncherAppState() {
77 if (sContext == null) {
78 throw new IllegalStateException("LauncherAppState inited before app context set");
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040079 }
80
Daniel Sandlere4f98912013-06-25 15:13:26 -040081 Log.v(Launcher.TAG, "LauncherAppState inited");
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040082
Daniel Sandlere4f98912013-06-25 15:13:26 -040083 if (sContext.getResources().getBoolean(R.bool.debug_memory_enabled)) {
84 MemoryTracker.startTrackingMe(sContext, "L");
85 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040086
Adam Cohen2e6da152015-05-06 11:42:25 -070087 mInvariantDeviceProfile = new InvariantDeviceProfile(sContext);
88 mIconCache = new IconCache(sContext, mInvariantDeviceProfile);
89 mWidgetCache = new WidgetPreviewLoader(sContext, mInvariantDeviceProfile, mIconCache);
Bjorn Bringert1307f632013-10-03 22:31:03 +010090
91 mAppFilter = AppFilter.loadByName(sContext.getString(R.string.app_filter_class));
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -080092 mBuildInfo = BuildInfo.loadByName(sContext.getString(R.string.build_info_class));
Bjorn Bringert1307f632013-10-03 22:31:03 +010093 mModel = new LauncherModel(this, mIconCache, mAppFilter);
Sunny Goyal27595792015-03-19 13:18:44 -070094
95 LauncherAppsCompat.getInstance(sContext).addOnAppsChangedCallback(mModel);
Daniel Sandlercc8befa2013-06-11 14:45:48 -040096
97 // Register intent receivers
Kenny Guyed131872014-04-30 03:02:21 +010098 IntentFilter filter = new IntentFilter();
Daniel Sandlercc8befa2013-06-11 14:45:48 -040099 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
100 filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400101 filter.addAction(SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400102 filter.addAction(SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED);
Sunny Goyal957c13f2015-05-01 13:02:20 -0700103 // For handling managed profiles
104 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED);
105 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED);
106
Daniel Sandlere4f98912013-06-25 15:13:26 -0400107 sContext.registerReceiver(mModel, filter);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400108 }
Kenny Guy1317e2d2014-05-08 18:52:50 +0100109
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400110 /**
111 * Call from Application.onTerminate(), which is not guaranteed to ever be called.
112 */
113 public void onTerminate() {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400114 sContext.unregisterReceiver(mModel);
Kenny Guy1317e2d2014-05-08 18:52:50 +0100115 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(sContext);
Kenny Guyc2bd8102014-06-30 12:30:31 +0100116 launcherApps.removeOnAppsChangedCallback(mModel);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700117 PackageInstallerCompat.getInstance(sContext).onStop();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400118 }
119
120 /**
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700121 * Reloads the workspace items from the DB and re-binds the workspace. This should generally
122 * not be called as DB updates are automatically followed by UI update
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400123 */
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700124 public void reloadWorkspace() {
125 mModel.resetLoadedState(false, true);
126 mModel.startLoaderFromBackground();
127 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400128
129 LauncherModel setLauncher(Launcher launcher) {
130 mModel.initialize(launcher);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800131 mAccessibilityDelegate = ((launcher != null) && Utilities.isLmpOrAbove()) ?
132 new LauncherAccessibilityDelegate(launcher) : null;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400133 return mModel;
134 }
135
Hyunyoung Song3f471442015-04-08 19:01:34 -0700136 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800137 return mAccessibilityDelegate;
138 }
139
Sunny Goyal34942622014-08-29 17:20:55 -0700140 public IconCache getIconCache() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400141 return mIconCache;
142 }
143
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700144 public LauncherModel getModel() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400145 return mModel;
146 }
147
Winson Chung7501adf2015-06-02 11:24:28 -0700148 /**
149 * TODO(winsonc, hyunyoungs): We need to respect this
150 */
Bjorn Bringert1307f632013-10-03 22:31:03 +0100151 boolean shouldShowAppOrWidgetProvider(ComponentName componentName) {
152 return mAppFilter == null || mAppFilter.shouldShowApp(componentName);
153 }
154
Daniel Sandlere4f98912013-06-25 15:13:26 -0400155 static void setLauncherProvider(LauncherProvider provider) {
156 sLauncherProvider = new WeakReference<LauncherProvider>(provider);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400157 }
158
Daniel Sandlere4f98912013-06-25 15:13:26 -0400159 static LauncherProvider getLauncherProvider() {
160 return sLauncherProvider.get();
Daniel Sandler924b9932013-06-12 00:38:25 -0400161 }
162
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400163 public static String getSharedPreferencesKey() {
Helena Josol28db2802014-10-09 17:04:09 +0100164 return LauncherFiles.SHARED_PREFERENCES_KEY;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400165 }
166
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700167 public WidgetPreviewLoader getWidgetCache() {
168 return mWidgetCache;
169 }
Winson Chung7501adf2015-06-02 11:24:28 -0700170
Michael Jurkaa6a05472013-11-13 17:59:46 +0100171 public void onWallpaperChanged() {
172 mWallpaperChangedSinceLastCheck = true;
173 }
174
175 public boolean hasWallpaperChangedSinceLastCheck() {
176 boolean result = mWallpaperChangedSinceLastCheck;
177 mWallpaperChangedSinceLastCheck = false;
178 return result;
179 }
180
Adam Cohen2e6da152015-05-06 11:42:25 -0700181 public InvariantDeviceProfile getInvariantDeviceProfile() {
182 return mInvariantDeviceProfile;
183 }
184
Jorim Jaggieedb00a2014-01-13 13:45:07 -0800185 public static boolean isDogfoodBuild() {
186 return getInstance().mBuildInfo.isDogfoodBuild();
187 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400188}