blob: 0a26109c96b420224c423e91188989ea933c9ef1 [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 Goyal4b171472015-08-19 12:43:27 -070020import android.content.BroadcastReceiver;
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;
Sunny Goyal823fd502015-08-04 11:40:13 -070029import com.android.launcher3.compat.UserManagerCompat;
Sunny Goyal322d5562015-06-25 19:35:49 -070030import com.android.launcher3.util.TestingUtils;
Adam Cohen091440a2015-03-18 14:16:05 -070031import com.android.launcher3.util.Thunk;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080032
Daniel Sandlercc8befa2013-06-11 14:45:48 -040033import java.lang.ref.WeakReference;
34
Sunny Goyalc6205602015-05-21 20:46:33 -070035public class LauncherAppState {
Chris Wrenaeff7ea2014-02-14 16:59:24 -050036
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -080037 private final AppFilter mAppFilter;
Adam Cohen091440a2015-03-18 14:16:05 -070038 @Thunk final LauncherModel mModel;
Sunny Goyale0f58d72014-11-10 18:05:31 -080039 private final IconCache mIconCache;
Sunny Goyal5b0e6692015-03-19 14:31:19 -070040 private final WidgetPreviewLoader mWidgetCache;
Sunny Goyale0f58d72014-11-10 18:05:31 -080041
Sunny Goyal4b171472015-08-19 12:43:27 -070042 @Thunk boolean mWallpaperChangedSinceLastCheck;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040043
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
Adam Cohenc9735cf2015-01-23 16:11:55 -080051 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Winson Chung5f8afe62013-08-12 16:19:28 -070052
Daniel Sandlercc8befa2013-06-11 14:45:48 -040053 public static LauncherAppState getInstance() {
Daniel Sandlere4f98912013-06-25 15:13:26 -040054 if (INSTANCE == null) {
55 INSTANCE = new LauncherAppState();
56 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040057 return INSTANCE;
58 }
59
Chris Wrend8fe6de2013-10-04 10:42:14 -040060 public static LauncherAppState getInstanceNoCreate() {
61 return INSTANCE;
62 }
63
Daniel Sandlercc8befa2013-06-11 14:45:48 -040064 public Context getContext() {
Daniel Sandlere4f98912013-06-25 15:13:26 -040065 return sContext;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040066 }
67
Sunny Goyald3849d12015-10-29 10:28:32 -070068 static void setLauncherProvider(LauncherProvider provider) {
69 if (sLauncherProvider != null) {
70 Log.w(Launcher.TAG, "setLauncherProvider called twice! old=" +
71 sLauncherProvider.get() + " new=" + provider);
Daniel Sandlere4f98912013-06-25 15:13:26 -040072 }
Sunny Goyald3849d12015-10-29 10:28:32 -070073 sLauncherProvider = new WeakReference<>(provider);
74
75 // The content provider exists for the entire duration of the launcher main process and
76 // is the first component to get created. Initializing application context here ensures
77 // that LauncherAppState always exists in the main process.
78 sContext = provider.getContext().getApplicationContext();
Daniel Sandlere4f98912013-06-25 15:13:26 -040079 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040080
Daniel Sandlere4f98912013-06-25 15:13:26 -040081 private LauncherAppState() {
82 if (sContext == null) {
83 throw new IllegalStateException("LauncherAppState inited before app context set");
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040084 }
85
Daniel Sandlere4f98912013-06-25 15:13:26 -040086 Log.v(Launcher.TAG, "LauncherAppState inited");
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040087
Sunny Goyal322d5562015-06-25 19:35:49 -070088 if (TestingUtils.MEMORY_DUMP_ENABLED) {
89 TestingUtils.startTrackingMemory(sContext);
Daniel Sandlere4f98912013-06-25 15:13:26 -040090 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040091
Adam Cohen2e6da152015-05-06 11:42:25 -070092 mInvariantDeviceProfile = new InvariantDeviceProfile(sContext);
93 mIconCache = new IconCache(sContext, mInvariantDeviceProfile);
Sunny Goyal383c5072015-06-12 21:18:53 -070094 mWidgetCache = new WidgetPreviewLoader(sContext, mIconCache);
Bjorn Bringert1307f632013-10-03 22:31:03 +010095
96 mAppFilter = AppFilter.loadByName(sContext.getString(R.string.app_filter_class));
97 mModel = new LauncherModel(this, mIconCache, mAppFilter);
Sunny Goyal27595792015-03-19 13:18:44 -070098
99 LauncherAppsCompat.getInstance(sContext).addOnAppsChangedCallback(mModel);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400100
101 // Register intent receivers
Kenny Guyed131872014-04-30 03:02:21 +0100102 IntentFilter filter = new IntentFilter();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400103 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400104 filter.addAction(SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED);
Sunny Goyal957c13f2015-05-01 13:02:20 -0700105 // For handling managed profiles
106 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED);
107 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED);
108
Daniel Sandlere4f98912013-06-25 15:13:26 -0400109 sContext.registerReceiver(mModel, filter);
Sunny Goyal823fd502015-08-04 11:40:13 -0700110 UserManagerCompat.getInstance(sContext).enableAndResetCache();
Sunny Goyal4b171472015-08-19 12:43:27 -0700111
112 if (!Utilities.ATLEAST_KITKAT) {
113 sContext.registerReceiver(new BroadcastReceiver() {
114
115 @Override
116 public void onReceive(Context context, Intent intent) {
117 mWallpaperChangedSinceLastCheck = true;
118 }
119 }, new IntentFilter(Intent.ACTION_WALLPAPER_CHANGED));
120 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400121 }
Kenny Guy1317e2d2014-05-08 18:52:50 +0100122
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400123 /**
124 * Call from Application.onTerminate(), which is not guaranteed to ever be called.
125 */
126 public void onTerminate() {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400127 sContext.unregisterReceiver(mModel);
Kenny Guy1317e2d2014-05-08 18:52:50 +0100128 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(sContext);
Kenny Guyc2bd8102014-06-30 12:30:31 +0100129 launcherApps.removeOnAppsChangedCallback(mModel);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700130 PackageInstallerCompat.getInstance(sContext).onStop();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400131 }
132
133 /**
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700134 * Reloads the workspace items from the DB and re-binds the workspace. This should generally
135 * not be called as DB updates are automatically followed by UI update
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400136 */
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700137 public void reloadWorkspace() {
138 mModel.resetLoadedState(false, true);
139 mModel.startLoaderFromBackground();
140 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400141
142 LauncherModel setLauncher(Launcher launcher) {
Sunny Goyald2497482015-09-22 18:24:19 -0700143 sLauncherProvider.get().setLauncherProviderChangeListener(launcher);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400144 mModel.initialize(launcher);
Sunny Goyal9fc953b2015-08-17 12:24:25 -0700145 mAccessibilityDelegate = ((launcher != null) && Utilities.ATLEAST_LOLLIPOP) ?
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800146 new LauncherAccessibilityDelegate(launcher) : null;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400147 return mModel;
148 }
149
Hyunyoung Song3f471442015-04-08 19:01:34 -0700150 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800151 return mAccessibilityDelegate;
152 }
153
Sunny Goyal34942622014-08-29 17:20:55 -0700154 public IconCache getIconCache() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400155 return mIconCache;
156 }
157
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700158 public LauncherModel getModel() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400159 return mModel;
160 }
161
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700162 public WidgetPreviewLoader getWidgetCache() {
163 return mWidgetCache;
164 }
Michael Jurkaa6a05472013-11-13 17:59:46 +0100165
166 public boolean hasWallpaperChangedSinceLastCheck() {
167 boolean result = mWallpaperChangedSinceLastCheck;
168 mWallpaperChangedSinceLastCheck = false;
169 return result;
170 }
171
Adam Cohen2e6da152015-05-06 11:42:25 -0700172 public InvariantDeviceProfile getInvariantDeviceProfile() {
173 return mInvariantDeviceProfile;
174 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400175}