blob: 540bdf81493629a8657087a9c5d91aca13ca4c4a [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 Sandlerb9eb2862013-06-14 20:17:30 -040024import android.util.Log;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080025
Sunny Goyal83a8f042015-05-19 12:52:12 -070026import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Kenny Guyed131872014-04-30 03:02:21 +010027import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal4390ace2014-10-13 11:33:11 -070028import com.android.launcher3.compat.PackageInstallerCompat;
Adam Cohen091440a2015-03-18 14:16:05 -070029import com.android.launcher3.util.Thunk;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080030
Daniel Sandlercc8befa2013-06-11 14:45:48 -040031import java.lang.ref.WeakReference;
32
Sunny Goyalc6205602015-05-21 20:46:33 -070033public class LauncherAppState {
Chris Wrenaeff7ea2014-02-14 16:59:24 -050034
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -080035 private final AppFilter mAppFilter;
36 private final BuildInfo mBuildInfo;
Adam Cohen091440a2015-03-18 14:16:05 -070037 @Thunk final LauncherModel mModel;
Sunny Goyale0f58d72014-11-10 18:05:31 -080038 private final IconCache mIconCache;
Sunny Goyal5b0e6692015-03-19 14:31:19 -070039 private final WidgetPreviewLoader mWidgetCache;
Sunny Goyale0f58d72014-11-10 18:05:31 -080040
Michael Jurkaa6a05472013-11-13 17:59:46 +010041 private boolean mWallpaperChangedSinceLastCheck;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040042
Daniel Sandlere4f98912013-06-25 15:13:26 -040043 private static WeakReference<LauncherProvider> sLauncherProvider;
44 private static Context sContext;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040045
Daniel Sandlere4f98912013-06-25 15:13:26 -040046 private static LauncherAppState INSTANCE;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040047
Adam Cohen2e6da152015-05-06 11:42:25 -070048 private InvariantDeviceProfile mInvariantDeviceProfile;
Sunny Goyal7779d622015-06-11 16:18:39 -070049
Adam Cohenc9735cf2015-01-23 16:11:55 -080050 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Winson Chung5f8afe62013-08-12 16:19:28 -070051
Daniel Sandlercc8befa2013-06-11 14:45:48 -040052 public static LauncherAppState getInstance() {
Daniel Sandlere4f98912013-06-25 15:13:26 -040053 if (INSTANCE == null) {
54 INSTANCE = new LauncherAppState();
55 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040056 return INSTANCE;
57 }
58
Chris Wrend8fe6de2013-10-04 10:42:14 -040059 public static LauncherAppState getInstanceNoCreate() {
60 return INSTANCE;
61 }
62
Daniel Sandlercc8befa2013-06-11 14:45:48 -040063 public Context getContext() {
Daniel Sandlere4f98912013-06-25 15:13:26 -040064 return sContext;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040065 }
66
Daniel Sandlere4f98912013-06-25 15:13:26 -040067 public static void setApplicationContext(Context context) {
68 if (sContext != null) {
Daniel Sandlere060b0b2013-06-27 21:47:55 -040069 Log.w(Launcher.TAG, "setApplicationContext called twice! old=" + sContext + " new=" + context);
Daniel Sandlere4f98912013-06-25 15:13:26 -040070 }
71 sContext = context.getApplicationContext();
72 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040073
Daniel Sandlere4f98912013-06-25 15:13:26 -040074 private LauncherAppState() {
75 if (sContext == null) {
76 throw new IllegalStateException("LauncherAppState inited before app context set");
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040077 }
78
Daniel Sandlere4f98912013-06-25 15:13:26 -040079 Log.v(Launcher.TAG, "LauncherAppState inited");
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040080
Daniel Sandlere4f98912013-06-25 15:13:26 -040081 if (sContext.getResources().getBoolean(R.bool.debug_memory_enabled)) {
82 MemoryTracker.startTrackingMe(sContext, "L");
83 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040084
Adam Cohen2e6da152015-05-06 11:42:25 -070085 mInvariantDeviceProfile = new InvariantDeviceProfile(sContext);
86 mIconCache = new IconCache(sContext, mInvariantDeviceProfile);
87 mWidgetCache = new WidgetPreviewLoader(sContext, mInvariantDeviceProfile, mIconCache);
Bjorn Bringert1307f632013-10-03 22:31:03 +010088
89 mAppFilter = AppFilter.loadByName(sContext.getString(R.string.app_filter_class));
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -080090 mBuildInfo = BuildInfo.loadByName(sContext.getString(R.string.build_info_class));
Bjorn Bringert1307f632013-10-03 22:31:03 +010091 mModel = new LauncherModel(this, mIconCache, mAppFilter);
Sunny Goyal27595792015-03-19 13:18:44 -070092
93 LauncherAppsCompat.getInstance(sContext).addOnAppsChangedCallback(mModel);
Daniel Sandlercc8befa2013-06-11 14:45:48 -040094
95 // Register intent receivers
Kenny Guyed131872014-04-30 03:02:21 +010096 IntentFilter filter = new IntentFilter();
Daniel Sandlercc8befa2013-06-11 14:45:48 -040097 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
98 filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
Daniel Sandlercc8befa2013-06-11 14:45:48 -040099 filter.addAction(SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400100 filter.addAction(SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED);
Sunny Goyal957c13f2015-05-01 13:02:20 -0700101 // For handling managed profiles
102 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED);
103 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED);
104
Daniel Sandlere4f98912013-06-25 15:13:26 -0400105 sContext.registerReceiver(mModel, filter);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400106 }
Kenny Guy1317e2d2014-05-08 18:52:50 +0100107
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400108 /**
109 * Call from Application.onTerminate(), which is not guaranteed to ever be called.
110 */
111 public void onTerminate() {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400112 sContext.unregisterReceiver(mModel);
Kenny Guy1317e2d2014-05-08 18:52:50 +0100113 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(sContext);
Kenny Guyc2bd8102014-06-30 12:30:31 +0100114 launcherApps.removeOnAppsChangedCallback(mModel);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700115 PackageInstallerCompat.getInstance(sContext).onStop();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400116 }
117
118 /**
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700119 * Reloads the workspace items from the DB and re-binds the workspace. This should generally
120 * not be called as DB updates are automatically followed by UI update
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400121 */
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700122 public void reloadWorkspace() {
123 mModel.resetLoadedState(false, true);
124 mModel.startLoaderFromBackground();
125 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400126
127 LauncherModel setLauncher(Launcher launcher) {
128 mModel.initialize(launcher);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800129 mAccessibilityDelegate = ((launcher != null) && Utilities.isLmpOrAbove()) ?
130 new LauncherAccessibilityDelegate(launcher) : null;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400131 return mModel;
132 }
133
Hyunyoung Song3f471442015-04-08 19:01:34 -0700134 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800135 return mAccessibilityDelegate;
136 }
137
Sunny Goyal34942622014-08-29 17:20:55 -0700138 public IconCache getIconCache() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400139 return mIconCache;
140 }
141
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700142 public LauncherModel getModel() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400143 return mModel;
144 }
145
Winson Chung7501adf2015-06-02 11:24:28 -0700146 /**
147 * TODO(winsonc, hyunyoungs): We need to respect this
148 */
Bjorn Bringert1307f632013-10-03 22:31:03 +0100149 boolean shouldShowAppOrWidgetProvider(ComponentName componentName) {
150 return mAppFilter == null || mAppFilter.shouldShowApp(componentName);
151 }
152
Daniel Sandlere4f98912013-06-25 15:13:26 -0400153 static void setLauncherProvider(LauncherProvider provider) {
154 sLauncherProvider = new WeakReference<LauncherProvider>(provider);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400155 }
156
Daniel Sandlere4f98912013-06-25 15:13:26 -0400157 static LauncherProvider getLauncherProvider() {
158 return sLauncherProvider.get();
Daniel Sandler924b9932013-06-12 00:38:25 -0400159 }
160
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400161 public static String getSharedPreferencesKey() {
Helena Josol28db2802014-10-09 17:04:09 +0100162 return LauncherFiles.SHARED_PREFERENCES_KEY;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400163 }
164
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700165 public WidgetPreviewLoader getWidgetCache() {
166 return mWidgetCache;
167 }
Winson Chung7501adf2015-06-02 11:24:28 -0700168
Michael Jurkaa6a05472013-11-13 17:59:46 +0100169 public void onWallpaperChanged() {
170 mWallpaperChangedSinceLastCheck = true;
171 }
172
173 public boolean hasWallpaperChangedSinceLastCheck() {
174 boolean result = mWallpaperChangedSinceLastCheck;
175 mWallpaperChangedSinceLastCheck = false;
176 return result;
177 }
178
Adam Cohen2e6da152015-05-06 11:42:25 -0700179 public InvariantDeviceProfile getInvariantDeviceProfile() {
180 return mInvariantDeviceProfile;
181 }
182
Jorim Jaggieedb00a2014-01-13 13:45:07 -0800183 public static boolean isDogfoodBuild() {
184 return getInstance().mBuildInfo.isDogfoodBuild();
185 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400186}