blob: fe65b31fbe8a17377fad4cde0e66ee1c93076196 [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;
Sunny Goyalae502842016-06-17 08:43:56 -070031import com.android.launcher3.util.ConfigMonitor;
Sunny Goyal322d5562015-06-25 19:35:49 -070032import com.android.launcher3.util.TestingUtils;
Adam Cohen091440a2015-03-18 14:16:05 -070033import com.android.launcher3.util.Thunk;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080034
Daniel Sandlercc8befa2013-06-11 14:45:48 -040035import java.lang.ref.WeakReference;
36
Sunny Goyalc6205602015-05-21 20:46:33 -070037public class LauncherAppState {
Chris Wrenaeff7ea2014-02-14 16:59:24 -050038
Sunny Goyale26d1002016-06-20 14:52:14 -070039 public static final boolean PROFILE_STARTUP = ProviderConfig.IS_DOGFOOD_BUILD;
40
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -080041 private final AppFilter mAppFilter;
Adam Cohen091440a2015-03-18 14:16:05 -070042 @Thunk final LauncherModel mModel;
Sunny Goyale0f58d72014-11-10 18:05:31 -080043 private final IconCache mIconCache;
Sunny Goyal5b0e6692015-03-19 14:31:19 -070044 private final WidgetPreviewLoader mWidgetCache;
Sunny Goyale0f58d72014-11-10 18:05:31 -080045
Sunny Goyal4b171472015-08-19 12:43:27 -070046 @Thunk 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;
Sunny Goyal7779d622015-06-11 16:18:39 -070054
Daniel Sandlercc8befa2013-06-11 14:45:48 -040055 public static LauncherAppState getInstance() {
Daniel Sandlere4f98912013-06-25 15:13:26 -040056 if (INSTANCE == null) {
57 INSTANCE = new LauncherAppState();
58 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040059 return INSTANCE;
60 }
61
Chris Wrend8fe6de2013-10-04 10:42:14 -040062 public static LauncherAppState getInstanceNoCreate() {
63 return INSTANCE;
64 }
65
Daniel Sandlercc8befa2013-06-11 14:45:48 -040066 public Context getContext() {
Daniel Sandlere4f98912013-06-25 15:13:26 -040067 return sContext;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040068 }
69
Sunny Goyald3849d12015-10-29 10:28:32 -070070 static void setLauncherProvider(LauncherProvider provider) {
71 if (sLauncherProvider != null) {
72 Log.w(Launcher.TAG, "setLauncherProvider called twice! old=" +
73 sLauncherProvider.get() + " new=" + provider);
Daniel Sandlere4f98912013-06-25 15:13:26 -040074 }
Sunny Goyald3849d12015-10-29 10:28:32 -070075 sLauncherProvider = new WeakReference<>(provider);
76
77 // The content provider exists for the entire duration of the launcher main process and
78 // is the first component to get created. Initializing application context here ensures
79 // that LauncherAppState always exists in the main process.
80 sContext = provider.getContext().getApplicationContext();
Sunny Goyal713edfc2016-05-06 09:58:34 -070081 FileLog.setDir(sContext.getFilesDir());
Daniel Sandlere4f98912013-06-25 15:13:26 -040082 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040083
Daniel Sandlere4f98912013-06-25 15:13:26 -040084 private LauncherAppState() {
85 if (sContext == null) {
86 throw new IllegalStateException("LauncherAppState inited before app context set");
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040087 }
88
Daniel Sandlere4f98912013-06-25 15:13:26 -040089 Log.v(Launcher.TAG, "LauncherAppState inited");
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040090
Sunny Goyal322d5562015-06-25 19:35:49 -070091 if (TestingUtils.MEMORY_DUMP_ENABLED) {
92 TestingUtils.startTrackingMemory(sContext);
Daniel Sandlere4f98912013-06-25 15:13:26 -040093 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040094
Adam Cohen2e6da152015-05-06 11:42:25 -070095 mInvariantDeviceProfile = new InvariantDeviceProfile(sContext);
96 mIconCache = new IconCache(sContext, mInvariantDeviceProfile);
Sunny Goyal383c5072015-06-12 21:18:53 -070097 mWidgetCache = new WidgetPreviewLoader(sContext, mIconCache);
Bjorn Bringert1307f632013-10-03 22:31:03 +010098
99 mAppFilter = AppFilter.loadByName(sContext.getString(R.string.app_filter_class));
100 mModel = new LauncherModel(this, mIconCache, mAppFilter);
Sunny Goyal27595792015-03-19 13:18:44 -0700101
102 LauncherAppsCompat.getInstance(sContext).addOnAppsChangedCallback(mModel);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400103
104 // Register intent receivers
Kenny Guyed131872014-04-30 03:02:21 +0100105 IntentFilter filter = new IntentFilter();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400106 filter.addAction(Intent.ACTION_LOCALE_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);
Rubin Xuac6e5d72016-04-04 16:13:35 +0100110 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_AVAILABLE);
111 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_UNAVAILABLE);
Tony Wickham827cef22016-03-17 15:39:39 -0700112 // For extracting colors from the wallpaper
113 if (Utilities.isNycOrAbove()) {
114 // TODO: add a broadcast entry to the manifest for pre-N.
115 filter.addAction(Intent.ACTION_WALLPAPER_CHANGED);
116 }
Sunny Goyal957c13f2015-05-01 13:02:20 -0700117
Daniel Sandlere4f98912013-06-25 15:13:26 -0400118 sContext.registerReceiver(mModel, filter);
Sunny Goyal823fd502015-08-04 11:40:13 -0700119 UserManagerCompat.getInstance(sContext).enableAndResetCache();
Sunny Goyal4b171472015-08-19 12:43:27 -0700120 if (!Utilities.ATLEAST_KITKAT) {
121 sContext.registerReceiver(new BroadcastReceiver() {
122
123 @Override
124 public void onReceive(Context context, Intent intent) {
125 mWallpaperChangedSinceLastCheck = true;
126 }
127 }, new IntentFilter(Intent.ACTION_WALLPAPER_CHANGED));
128 }
Sunny Goyala50a4192015-12-11 09:50:49 -0800129 new ConfigMonitor(sContext).register();
Tony Wickham827cef22016-03-17 15:39:39 -0700130
131 ExtractionUtils.startColorExtractionServiceIfNecessary(sContext);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400132 }
Kenny Guy1317e2d2014-05-08 18:52:50 +0100133
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400134 /**
135 * Call from Application.onTerminate(), which is not guaranteed to ever be called.
136 */
137 public void onTerminate() {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400138 sContext.unregisterReceiver(mModel);
Kenny Guy1317e2d2014-05-08 18:52:50 +0100139 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(sContext);
Kenny Guyc2bd8102014-06-30 12:30:31 +0100140 launcherApps.removeOnAppsChangedCallback(mModel);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700141 PackageInstallerCompat.getInstance(sContext).onStop();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400142 }
143
144 /**
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700145 * Reloads the workspace items from the DB and re-binds the workspace. This should generally
146 * not be called as DB updates are automatically followed by UI update
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400147 */
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700148 public void reloadWorkspace() {
149 mModel.resetLoadedState(false, true);
150 mModel.startLoaderFromBackground();
151 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400152
153 LauncherModel setLauncher(Launcher launcher) {
Sunny Goyald2497482015-09-22 18:24:19 -0700154 sLauncherProvider.get().setLauncherProviderChangeListener(launcher);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400155 mModel.initialize(launcher);
156 return mModel;
157 }
158
Sunny Goyal34942622014-08-29 17:20:55 -0700159 public IconCache getIconCache() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400160 return mIconCache;
161 }
162
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700163 public LauncherModel getModel() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400164 return mModel;
165 }
166
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700167 public WidgetPreviewLoader getWidgetCache() {
168 return mWidgetCache;
169 }
Michael Jurkaa6a05472013-11-13 17:59:46 +0100170
171 public boolean hasWallpaperChangedSinceLastCheck() {
172 boolean result = mWallpaperChangedSinceLastCheck;
173 mWallpaperChangedSinceLastCheck = false;
174 return result;
175 }
176
Adam Cohen2e6da152015-05-06 11:42:25 -0700177 public InvariantDeviceProfile getInvariantDeviceProfile() {
178 return mInvariantDeviceProfile;
179 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400180}