blob: 00e4bf4dc96a6c37ae7f3acfda65ac880a4cef4f [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 Goyale755d462014-07-22 13:48:29 -070019import android.content.Context;
20import android.content.Intent;
21import android.content.IntentFilter;
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040022import android.util.Log;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080023
Kenny Guyed131872014-04-30 03:02:21 +010024import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal4390ace2014-10-13 11:33:11 -070025import com.android.launcher3.compat.PackageInstallerCompat;
Sunny Goyal823fd502015-08-04 11:40:13 -070026import com.android.launcher3.compat.UserManagerCompat;
Sunny Goyale26d1002016-06-20 14:52:14 -070027import com.android.launcher3.config.ProviderConfig;
Tony Wickham827cef22016-03-17 15:39:39 -070028import com.android.launcher3.dynamicui.ExtractionUtils;
Sunny Goyal713edfc2016-05-06 09:58:34 -070029import com.android.launcher3.logging.FileLog;
Sunny Goyalae502842016-06-17 08:43:56 -070030import com.android.launcher3.util.ConfigMonitor;
Sunny Goyal322d5562015-06-25 19:35:49 -070031import com.android.launcher3.util.TestingUtils;
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
Sunny Goyale26d1002016-06-20 14:52:14 -070038 public static final boolean PROFILE_STARTUP = ProviderConfig.IS_DOGFOOD_BUILD;
39
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
Daniel Sandlere4f98912013-06-25 15:13:26 -040044 private static WeakReference<LauncherProvider> sLauncherProvider;
45 private static Context sContext;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040046
Daniel Sandlere4f98912013-06-25 15:13:26 -040047 private static LauncherAppState INSTANCE;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040048
Adam Cohen2e6da152015-05-06 11:42:25 -070049 private InvariantDeviceProfile mInvariantDeviceProfile;
Sunny Goyal7779d622015-06-11 16:18:39 -070050
Daniel Sandlercc8befa2013-06-11 14:45:48 -040051 public static LauncherAppState getInstance() {
Daniel Sandlere4f98912013-06-25 15:13:26 -040052 if (INSTANCE == null) {
53 INSTANCE = new LauncherAppState();
54 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040055 return INSTANCE;
56 }
57
Chris Wrend8fe6de2013-10-04 10:42:14 -040058 public static LauncherAppState getInstanceNoCreate() {
59 return INSTANCE;
60 }
61
Daniel Sandlercc8befa2013-06-11 14:45:48 -040062 public Context getContext() {
Daniel Sandlere4f98912013-06-25 15:13:26 -040063 return sContext;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040064 }
65
Sunny Goyald3849d12015-10-29 10:28:32 -070066 static void setLauncherProvider(LauncherProvider provider) {
67 if (sLauncherProvider != null) {
68 Log.w(Launcher.TAG, "setLauncherProvider called twice! old=" +
69 sLauncherProvider.get() + " new=" + provider);
Daniel Sandlere4f98912013-06-25 15:13:26 -040070 }
Sunny Goyald3849d12015-10-29 10:28:32 -070071 sLauncherProvider = new WeakReference<>(provider);
72
73 // The content provider exists for the entire duration of the launcher main process and
74 // is the first component to get created. Initializing application context here ensures
75 // that LauncherAppState always exists in the main process.
76 sContext = provider.getContext().getApplicationContext();
Sunny Goyal713edfc2016-05-06 09:58:34 -070077 FileLog.setDir(sContext.getFilesDir());
Daniel Sandlere4f98912013-06-25 15:13:26 -040078 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040079
Daniel Sandlere4f98912013-06-25 15:13:26 -040080 private LauncherAppState() {
81 if (sContext == null) {
Hyunyoung Song0de01172016-10-05 16:27:48 -070082 throw new IllegalStateException("LauncherAppState initiated before app context set");
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040083 }
84
Hyunyoung Song0de01172016-10-05 16:27:48 -070085 Log.v(Launcher.TAG, "LauncherAppState initiated");
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040086
Sunny Goyal322d5562015-06-25 19:35:49 -070087 if (TestingUtils.MEMORY_DUMP_ENABLED) {
88 TestingUtils.startTrackingMemory(sContext);
Daniel Sandlere4f98912013-06-25 15:13:26 -040089 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040090
Adam Cohen2e6da152015-05-06 11:42:25 -070091 mInvariantDeviceProfile = new InvariantDeviceProfile(sContext);
92 mIconCache = new IconCache(sContext, mInvariantDeviceProfile);
Sunny Goyal383c5072015-06-12 21:18:53 -070093 mWidgetCache = new WidgetPreviewLoader(sContext, mIconCache);
Bjorn Bringert1307f632013-10-03 22:31:03 +010094
Sunny Goyalab121c12016-12-02 19:29:43 +053095 mModel = new LauncherModel(this, mIconCache,
96 Utilities.getOverrideObject(AppFilter.class, sContext, R.string.app_filter_class));
Sunny Goyal27595792015-03-19 13:18:44 -070097
98 LauncherAppsCompat.getInstance(sContext).addOnAppsChangedCallback(mModel);
Daniel Sandlercc8befa2013-06-11 14:45:48 -040099
100 // Register intent receivers
Kenny Guyed131872014-04-30 03:02:21 +0100101 IntentFilter filter = new IntentFilter();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400102 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
Sunny Goyal957c13f2015-05-01 13:02:20 -0700103 // For handling managed profiles
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700104 filter.addAction(Intent.ACTION_MANAGED_PROFILE_ADDED);
105 filter.addAction(Intent.ACTION_MANAGED_PROFILE_REMOVED);
106 filter.addAction(Intent.ACTION_MANAGED_PROFILE_AVAILABLE);
107 filter.addAction(Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE);
108 filter.addAction(Intent.ACTION_MANAGED_PROFILE_UNLOCKED);
Tony Wickham827cef22016-03-17 15:39:39 -0700109 // For extracting colors from the wallpaper
Sunny Goyalf5e37442016-11-02 10:31:24 -0700110 if (Utilities.ATLEAST_NOUGAT) {
Tony Wickham827cef22016-03-17 15:39:39 -0700111 // TODO: add a broadcast entry to the manifest for pre-N.
112 filter.addAction(Intent.ACTION_WALLPAPER_CHANGED);
113 }
Sunny Goyal957c13f2015-05-01 13:02:20 -0700114
Daniel Sandlere4f98912013-06-25 15:13:26 -0400115 sContext.registerReceiver(mModel, filter);
Sunny Goyal823fd502015-08-04 11:40:13 -0700116 UserManagerCompat.getInstance(sContext).enableAndResetCache();
Sunny Goyala50a4192015-12-11 09:50:49 -0800117 new ConfigMonitor(sContext).register();
Tony Wickham827cef22016-03-17 15:39:39 -0700118
119 ExtractionUtils.startColorExtractionServiceIfNecessary(sContext);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400120 }
Kenny Guy1317e2d2014-05-08 18:52:50 +0100121
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400122 /**
123 * Call from Application.onTerminate(), which is not guaranteed to ever be called.
124 */
125 public void onTerminate() {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400126 sContext.unregisterReceiver(mModel);
Kenny Guy1317e2d2014-05-08 18:52:50 +0100127 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(sContext);
Kenny Guyc2bd8102014-06-30 12:30:31 +0100128 launcherApps.removeOnAppsChangedCallback(mModel);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700129 PackageInstallerCompat.getInstance(sContext).onStop();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400130 }
131
132 /**
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700133 * Reloads the workspace items from the DB and re-binds the workspace. This should generally
134 * not be called as DB updates are automatically followed by UI update
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400135 */
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700136 public void reloadWorkspace() {
137 mModel.resetLoadedState(false, true);
138 mModel.startLoaderFromBackground();
139 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400140
141 LauncherModel setLauncher(Launcher launcher) {
Sunny Goyald2497482015-09-22 18:24:19 -0700142 sLauncherProvider.get().setLauncherProviderChangeListener(launcher);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400143 mModel.initialize(launcher);
144 return mModel;
145 }
146
Sunny Goyal34942622014-08-29 17:20:55 -0700147 public IconCache getIconCache() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400148 return mIconCache;
149 }
150
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700151 public LauncherModel getModel() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400152 return mModel;
153 }
154
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700155 public WidgetPreviewLoader getWidgetCache() {
156 return mWidgetCache;
157 }
Michael Jurkaa6a05472013-11-13 17:59:46 +0100158
Adam Cohen2e6da152015-05-06 11:42:25 -0700159 public InvariantDeviceProfile getInvariantDeviceProfile() {
160 return mInvariantDeviceProfile;
161 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400162}