blob: 52f85ea42892bc582947bdcaa2575fad2b59ecaa [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.Context;
21import android.content.Intent;
22import android.content.IntentFilter;
Sunny Goyal823fd502015-08-04 11:40:13 -070023import android.os.UserManager;
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;
Adam Cohen091440a2015-03-18 14:16:05 -070030import com.android.launcher3.util.Thunk;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080031
Daniel Sandlercc8befa2013-06-11 14:45:48 -040032import java.lang.ref.WeakReference;
33
Sunny Goyalc6205602015-05-21 20:46:33 -070034public class LauncherAppState {
Chris Wrenaeff7ea2014-02-14 16:59:24 -050035
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -080036 private final AppFilter mAppFilter;
37 private final BuildInfo mBuildInfo;
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
Michael Jurkaa6a05472013-11-13 17:59:46 +010042 private 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
Daniel Sandlere4f98912013-06-25 15:13:26 -040068 public static void setApplicationContext(Context context) {
69 if (sContext != null) {
Daniel Sandlere060b0b2013-06-27 21:47:55 -040070 Log.w(Launcher.TAG, "setApplicationContext called twice! old=" + sContext + " new=" + context);
Daniel Sandlere4f98912013-06-25 15:13:26 -040071 }
72 sContext = context.getApplicationContext();
73 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040074
Daniel Sandlere4f98912013-06-25 15:13:26 -040075 private LauncherAppState() {
76 if (sContext == null) {
77 throw new IllegalStateException("LauncherAppState inited before app context set");
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040078 }
79
Daniel Sandlere4f98912013-06-25 15:13:26 -040080 Log.v(Launcher.TAG, "LauncherAppState inited");
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040081
Daniel Sandlere4f98912013-06-25 15:13:26 -040082 if (sContext.getResources().getBoolean(R.bool.debug_memory_enabled)) {
83 MemoryTracker.startTrackingMe(sContext, "L");
84 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040085
Adam Cohen2e6da152015-05-06 11:42:25 -070086 mInvariantDeviceProfile = new InvariantDeviceProfile(sContext);
87 mIconCache = new IconCache(sContext, mInvariantDeviceProfile);
Sunny Goyal383c5072015-06-12 21:18:53 -070088 mWidgetCache = new WidgetPreviewLoader(sContext, mIconCache);
Bjorn Bringert1307f632013-10-03 22:31:03 +010089
90 mAppFilter = AppFilter.loadByName(sContext.getString(R.string.app_filter_class));
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -080091 mBuildInfo = BuildInfo.loadByName(sContext.getString(R.string.build_info_class));
Bjorn Bringert1307f632013-10-03 22:31:03 +010092 mModel = new LauncherModel(this, mIconCache, mAppFilter);
Sunny Goyal27595792015-03-19 13:18:44 -070093
94 LauncherAppsCompat.getInstance(sContext).addOnAppsChangedCallback(mModel);
Daniel Sandlercc8befa2013-06-11 14:45:48 -040095
96 // Register intent receivers
Kenny Guyed131872014-04-30 03:02:21 +010097 IntentFilter filter = new IntentFilter();
Daniel Sandlercc8befa2013-06-11 14:45:48 -040098 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
Daniel Sandlercc8befa2013-06-11 14:45:48 -040099 filter.addAction(SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED);
Sunny Goyal957c13f2015-05-01 13:02:20 -0700100 // For handling managed profiles
101 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED);
102 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED);
103
Daniel Sandlere4f98912013-06-25 15:13:26 -0400104 sContext.registerReceiver(mModel, filter);
Sunny Goyal823fd502015-08-04 11:40:13 -0700105 UserManagerCompat.getInstance(sContext).enableAndResetCache();
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) {
Sunny Goyal383c5072015-06-12 21:18:53 -0700128 getLauncherProvider().setLauncherProviderChangeListener(launcher);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400129 mModel.initialize(launcher);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800130 mAccessibilityDelegate = ((launcher != null) && Utilities.isLmpOrAbove()) ?
131 new LauncherAccessibilityDelegate(launcher) : null;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400132 return mModel;
133 }
134
Hyunyoung Song3f471442015-04-08 19:01:34 -0700135 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800136 return mAccessibilityDelegate;
137 }
138
Sunny Goyal34942622014-08-29 17:20:55 -0700139 public IconCache getIconCache() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400140 return mIconCache;
141 }
142
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700143 public LauncherModel getModel() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400144 return mModel;
145 }
146
Daniel Sandlere4f98912013-06-25 15:13:26 -0400147 static void setLauncherProvider(LauncherProvider provider) {
148 sLauncherProvider = new WeakReference<LauncherProvider>(provider);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400149 }
150
Sunny Goyale5bb7052015-07-27 14:36:07 -0700151 public static LauncherProvider getLauncherProvider() {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400152 return sLauncherProvider.get();
Daniel Sandler924b9932013-06-12 00:38:25 -0400153 }
154
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400155 public static String getSharedPreferencesKey() {
Helena Josol28db2802014-10-09 17:04:09 +0100156 return LauncherFiles.SHARED_PREFERENCES_KEY;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400157 }
158
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700159 public WidgetPreviewLoader getWidgetCache() {
160 return mWidgetCache;
161 }
Winson Chung7501adf2015-06-02 11:24:28 -0700162
Michael Jurkaa6a05472013-11-13 17:59:46 +0100163 public void onWallpaperChanged() {
164 mWallpaperChangedSinceLastCheck = true;
165 }
166
167 public boolean hasWallpaperChangedSinceLastCheck() {
168 boolean result = mWallpaperChangedSinceLastCheck;
169 mWallpaperChangedSinceLastCheck = false;
170 return result;
171 }
172
Adam Cohen2e6da152015-05-06 11:42:25 -0700173 public InvariantDeviceProfile getInvariantDeviceProfile() {
174 return mInvariantDeviceProfile;
175 }
176
Jorim Jaggieedb00a2014-01-13 13:45:07 -0800177 public static boolean isDogfoodBuild() {
178 return getInstance().mBuildInfo.isDogfoodBuild();
179 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400180}