blob: 7861a106fd2e6b35a405d991a69eef79515c5a65 [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 Goyal4b171472015-08-19 12:43:27 -070019import android.content.BroadcastReceiver;
Sunny Goyale755d462014-07-22 13:48:29 -070020import android.content.Context;
21import android.content.Intent;
22import android.content.IntentFilter;
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040023import android.util.Log;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080024
Kenny Guyed131872014-04-30 03:02:21 +010025import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal4390ace2014-10-13 11:33:11 -070026import com.android.launcher3.compat.PackageInstallerCompat;
Sunny Goyal823fd502015-08-04 11:40:13 -070027import com.android.launcher3.compat.UserManagerCompat;
Sunny Goyale26d1002016-06-20 14:52:14 -070028import com.android.launcher3.config.ProviderConfig;
Tony Wickham827cef22016-03-17 15:39:39 -070029import com.android.launcher3.dynamicui.ExtractionUtils;
Sunny Goyal713edfc2016-05-06 09:58:34 -070030import com.android.launcher3.logging.FileLog;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070031import com.android.launcher3.shortcuts.DeepShortcutManager;
32import com.android.launcher3.shortcuts.ShortcutCache;
Sunny Goyalae502842016-06-17 08:43:56 -070033import com.android.launcher3.util.ConfigMonitor;
Sunny Goyal322d5562015-06-25 19:35:49 -070034import com.android.launcher3.util.TestingUtils;
Adam Cohen091440a2015-03-18 14:16:05 -070035import com.android.launcher3.util.Thunk;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080036
Daniel Sandlercc8befa2013-06-11 14:45:48 -040037import java.lang.ref.WeakReference;
38
Sunny Goyalc6205602015-05-21 20:46:33 -070039public class LauncherAppState {
Chris Wrenaeff7ea2014-02-14 16:59:24 -050040
Sunny Goyale26d1002016-06-20 14:52:14 -070041 public static final boolean PROFILE_STARTUP = ProviderConfig.IS_DOGFOOD_BUILD;
42
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -080043 private final AppFilter mAppFilter;
Adam Cohen091440a2015-03-18 14:16:05 -070044 @Thunk final LauncherModel mModel;
Sunny Goyale0f58d72014-11-10 18:05:31 -080045 private final IconCache mIconCache;
Sunny Goyal5b0e6692015-03-19 14:31:19 -070046 private final WidgetPreviewLoader mWidgetCache;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070047 private final DeepShortcutManager mDeepShortcutManager;
Sunny Goyale0f58d72014-11-10 18:05:31 -080048
Sunny Goyal4b171472015-08-19 12:43:27 -070049 @Thunk boolean mWallpaperChangedSinceLastCheck;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040050
Daniel Sandlere4f98912013-06-25 15:13:26 -040051 private static WeakReference<LauncherProvider> sLauncherProvider;
52 private static Context sContext;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040053
Daniel Sandlere4f98912013-06-25 15:13:26 -040054 private static LauncherAppState INSTANCE;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040055
Adam Cohen2e6da152015-05-06 11:42:25 -070056 private InvariantDeviceProfile mInvariantDeviceProfile;
Sunny Goyal7779d622015-06-11 16:18:39 -070057
Daniel Sandlercc8befa2013-06-11 14:45:48 -040058 public static LauncherAppState getInstance() {
Daniel Sandlere4f98912013-06-25 15:13:26 -040059 if (INSTANCE == null) {
60 INSTANCE = new LauncherAppState();
61 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040062 return INSTANCE;
63 }
64
Chris Wrend8fe6de2013-10-04 10:42:14 -040065 public static LauncherAppState getInstanceNoCreate() {
66 return INSTANCE;
67 }
68
Daniel Sandlercc8befa2013-06-11 14:45:48 -040069 public Context getContext() {
Daniel Sandlere4f98912013-06-25 15:13:26 -040070 return sContext;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040071 }
72
Sunny Goyald3849d12015-10-29 10:28:32 -070073 static void setLauncherProvider(LauncherProvider provider) {
74 if (sLauncherProvider != null) {
75 Log.w(Launcher.TAG, "setLauncherProvider called twice! old=" +
76 sLauncherProvider.get() + " new=" + provider);
Daniel Sandlere4f98912013-06-25 15:13:26 -040077 }
Sunny Goyald3849d12015-10-29 10:28:32 -070078 sLauncherProvider = new WeakReference<>(provider);
79
80 // The content provider exists for the entire duration of the launcher main process and
81 // is the first component to get created. Initializing application context here ensures
82 // that LauncherAppState always exists in the main process.
83 sContext = provider.getContext().getApplicationContext();
Sunny Goyal713edfc2016-05-06 09:58:34 -070084 FileLog.setDir(sContext.getFilesDir());
Daniel Sandlere4f98912013-06-25 15:13:26 -040085 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040086
Daniel Sandlere4f98912013-06-25 15:13:26 -040087 private LauncherAppState() {
88 if (sContext == null) {
89 throw new IllegalStateException("LauncherAppState inited before app context set");
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040090 }
91
Daniel Sandlere4f98912013-06-25 15:13:26 -040092 Log.v(Launcher.TAG, "LauncherAppState inited");
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040093
Sunny Goyal322d5562015-06-25 19:35:49 -070094 if (TestingUtils.MEMORY_DUMP_ENABLED) {
95 TestingUtils.startTrackingMemory(sContext);
Daniel Sandlere4f98912013-06-25 15:13:26 -040096 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040097
Adam Cohen2e6da152015-05-06 11:42:25 -070098 mInvariantDeviceProfile = new InvariantDeviceProfile(sContext);
99 mIconCache = new IconCache(sContext, mInvariantDeviceProfile);
Sunny Goyal383c5072015-06-12 21:18:53 -0700100 mWidgetCache = new WidgetPreviewLoader(sContext, mIconCache);
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700101 mDeepShortcutManager = new DeepShortcutManager(sContext, new ShortcutCache());
Bjorn Bringert1307f632013-10-03 22:31:03 +0100102
103 mAppFilter = AppFilter.loadByName(sContext.getString(R.string.app_filter_class));
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700104 mModel = new LauncherModel(this, mIconCache, mAppFilter, mDeepShortcutManager);
Sunny Goyal27595792015-03-19 13:18:44 -0700105
106 LauncherAppsCompat.getInstance(sContext).addOnAppsChangedCallback(mModel);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400107
108 // Register intent receivers
Kenny Guyed131872014-04-30 03:02:21 +0100109 IntentFilter filter = new IntentFilter();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400110 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
Sunny Goyal957c13f2015-05-01 13:02:20 -0700111 // For handling managed profiles
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700112 filter.addAction(Intent.ACTION_MANAGED_PROFILE_ADDED);
113 filter.addAction(Intent.ACTION_MANAGED_PROFILE_REMOVED);
114 filter.addAction(Intent.ACTION_MANAGED_PROFILE_AVAILABLE);
115 filter.addAction(Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE);
116 filter.addAction(Intent.ACTION_MANAGED_PROFILE_UNLOCKED);
Tony Wickham827cef22016-03-17 15:39:39 -0700117 // For extracting colors from the wallpaper
118 if (Utilities.isNycOrAbove()) {
119 // TODO: add a broadcast entry to the manifest for pre-N.
120 filter.addAction(Intent.ACTION_WALLPAPER_CHANGED);
121 }
Sunny Goyal957c13f2015-05-01 13:02:20 -0700122
Daniel Sandlere4f98912013-06-25 15:13:26 -0400123 sContext.registerReceiver(mModel, filter);
Sunny Goyal823fd502015-08-04 11:40:13 -0700124 UserManagerCompat.getInstance(sContext).enableAndResetCache();
Sunny Goyal4b171472015-08-19 12:43:27 -0700125 if (!Utilities.ATLEAST_KITKAT) {
126 sContext.registerReceiver(new BroadcastReceiver() {
127
128 @Override
129 public void onReceive(Context context, Intent intent) {
130 mWallpaperChangedSinceLastCheck = true;
131 }
132 }, new IntentFilter(Intent.ACTION_WALLPAPER_CHANGED));
133 }
Sunny Goyala50a4192015-12-11 09:50:49 -0800134 new ConfigMonitor(sContext).register();
Tony Wickham827cef22016-03-17 15:39:39 -0700135
136 ExtractionUtils.startColorExtractionServiceIfNecessary(sContext);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400137 }
Kenny Guy1317e2d2014-05-08 18:52:50 +0100138
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400139 /**
140 * Call from Application.onTerminate(), which is not guaranteed to ever be called.
141 */
142 public void onTerminate() {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400143 sContext.unregisterReceiver(mModel);
Kenny Guy1317e2d2014-05-08 18:52:50 +0100144 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(sContext);
Kenny Guyc2bd8102014-06-30 12:30:31 +0100145 launcherApps.removeOnAppsChangedCallback(mModel);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700146 PackageInstallerCompat.getInstance(sContext).onStop();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400147 }
148
149 /**
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700150 * Reloads the workspace items from the DB and re-binds the workspace. This should generally
151 * not be called as DB updates are automatically followed by UI update
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400152 */
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700153 public void reloadWorkspace() {
154 mModel.resetLoadedState(false, true);
155 mModel.startLoaderFromBackground();
156 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400157
158 LauncherModel setLauncher(Launcher launcher) {
Sunny Goyald2497482015-09-22 18:24:19 -0700159 sLauncherProvider.get().setLauncherProviderChangeListener(launcher);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400160 mModel.initialize(launcher);
161 return mModel;
162 }
163
Sunny Goyal34942622014-08-29 17:20:55 -0700164 public IconCache getIconCache() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400165 return mIconCache;
166 }
167
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700168 public LauncherModel getModel() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400169 return mModel;
170 }
171
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700172 public WidgetPreviewLoader getWidgetCache() {
173 return mWidgetCache;
174 }
Michael Jurkaa6a05472013-11-13 17:59:46 +0100175
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700176 public DeepShortcutManager getShortcutManager() {
177 return mDeepShortcutManager;
178 }
179
Michael Jurkaa6a05472013-11-13 17:59:46 +0100180 public boolean hasWallpaperChangedSinceLastCheck() {
181 boolean result = mWallpaperChangedSinceLastCheck;
182 mWallpaperChangedSinceLastCheck = false;
183 return result;
184 }
185
Adam Cohen2e6da152015-05-06 11:42:25 -0700186 public InvariantDeviceProfile getInvariantDeviceProfile() {
187 return mInvariantDeviceProfile;
188 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400189}