blob: 4ac5ef3d5da73548d1723333fc2af6fddf3135ef [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;
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040023import android.util.Log;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080024
Sunny Goyal83a8f042015-05-19 12:52:12 -070025import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Kenny Guyed131872014-04-30 03:02:21 +010026import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal4390ace2014-10-13 11:33:11 -070027import com.android.launcher3.compat.PackageInstallerCompat;
Sunny Goyal823fd502015-08-04 11:40:13 -070028import com.android.launcher3.compat.UserManagerCompat;
Sunny Goyal20806032016-01-07 12:17:38 -080029import com.android.launcher3.config.FeatureFlags;
Sunny Goyala50a4192015-12-11 09:50:49 -080030import com.android.launcher3.util.ConfigMonitor;
Sunny Goyald3060552015-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
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -080038 private final AppFilter mAppFilter;
Adam Cohen091440a2015-03-18 14:16:05 -070039 @Thunk final LauncherModel mModel;
Sunny Goyale0f58d72014-11-10 18:05:31 -080040 private final IconCache mIconCache;
Sunny Goyal5b0e6692015-03-19 14:31:19 -070041 private final WidgetPreviewLoader mWidgetCache;
Sunny Goyale0f58d72014-11-10 18:05:31 -080042
Michael Jurkaa6a05472013-11-13 17:59:46 +010043 private boolean mWallpaperChangedSinceLastCheck;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040044
Daniel Sandlere4f98912013-06-25 15:13:26 -040045 private static WeakReference<LauncherProvider> sLauncherProvider;
46 private static Context sContext;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040047
Daniel Sandlere4f98912013-06-25 15:13:26 -040048 private static LauncherAppState INSTANCE;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040049
Adam Cohen2e6da152015-05-06 11:42:25 -070050 private InvariantDeviceProfile mInvariantDeviceProfile;
Sunny Goyal7779d622015-06-11 16:18:39 -070051
Adam Cohenc9735cf2015-01-23 16:11:55 -080052 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Winson Chung5f8afe62013-08-12 16:19:28 -070053
Daniel Sandlercc8befa2013-06-11 14:45:48 -040054 public static LauncherAppState getInstance() {
Daniel Sandlere4f98912013-06-25 15:13:26 -040055 if (INSTANCE == null) {
56 INSTANCE = new LauncherAppState();
57 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040058 return INSTANCE;
59 }
60
Chris Wrend8fe6de2013-10-04 10:42:14 -040061 public static LauncherAppState getInstanceNoCreate() {
62 return INSTANCE;
63 }
64
Daniel Sandlercc8befa2013-06-11 14:45:48 -040065 public Context getContext() {
Daniel Sandlere4f98912013-06-25 15:13:26 -040066 return sContext;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040067 }
68
Daniel Sandlere4f98912013-06-25 15:13:26 -040069 public static void setApplicationContext(Context context) {
70 if (sContext != null) {
Daniel Sandlere060b0b2013-06-27 21:47:55 -040071 Log.w(Launcher.TAG, "setApplicationContext called twice! old=" + sContext + " new=" + context);
Daniel Sandlere4f98912013-06-25 15:13:26 -040072 }
73 sContext = context.getApplicationContext();
74 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040075
Daniel Sandlere4f98912013-06-25 15:13:26 -040076 private LauncherAppState() {
77 if (sContext == null) {
78 throw new IllegalStateException("LauncherAppState inited before app context set");
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040079 }
80
Daniel Sandlere4f98912013-06-25 15:13:26 -040081 Log.v(Launcher.TAG, "LauncherAppState inited");
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040082
Sunny Goyald3060552015-06-25 19:35:49 -070083 if (TestingUtils.MEMORY_DUMP_ENABLED) {
84 TestingUtils.startTrackingMemory(sContext);
Daniel Sandlere4f98912013-06-25 15:13:26 -040085 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040086
Adam Cohen2e6da152015-05-06 11:42:25 -070087 mInvariantDeviceProfile = new InvariantDeviceProfile(sContext);
88 mIconCache = new IconCache(sContext, mInvariantDeviceProfile);
Sunny Goyal383c5072015-06-12 21:18:53 -070089 mWidgetCache = new WidgetPreviewLoader(sContext, mIconCache);
Bjorn Bringert1307f632013-10-03 22:31:03 +010090
91 mAppFilter = AppFilter.loadByName(sContext.getString(R.string.app_filter_class));
92 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();
Sunny Goyala50a4192015-12-11 09:50:49 -0800106 new ConfigMonitor(sContext).register();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400107 }
Kenny Guy1317e2d2014-05-08 18:52:50 +0100108
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400109 /**
110 * Call from Application.onTerminate(), which is not guaranteed to ever be called.
111 */
112 public void onTerminate() {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400113 sContext.unregisterReceiver(mModel);
Kenny Guy1317e2d2014-05-08 18:52:50 +0100114 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(sContext);
Kenny Guyc2bd8102014-06-30 12:30:31 +0100115 launcherApps.removeOnAppsChangedCallback(mModel);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700116 PackageInstallerCompat.getInstance(sContext).onStop();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400117 }
118
119 /**
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700120 * Reloads the workspace items from the DB and re-binds the workspace. This should generally
121 * not be called as DB updates are automatically followed by UI update
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400122 */
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700123 public void reloadWorkspace() {
124 mModel.resetLoadedState(false, true);
125 mModel.startLoaderFromBackground();
126 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400127
128 LauncherModel setLauncher(Launcher launcher) {
Sunny Goyal383c5072015-06-12 21:18:53 -0700129 getLauncherProvider().setLauncherProviderChangeListener(launcher);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400130 mModel.initialize(launcher);
Sunny Goyal9fc953b2015-08-17 12:24:25 -0700131 mAccessibilityDelegate = ((launcher != null) && Utilities.ATLEAST_LOLLIPOP) ?
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800132 new LauncherAccessibilityDelegate(launcher) : null;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400133 return mModel;
134 }
135
Hyunyoung Song3f471442015-04-08 19:01:34 -0700136 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800137 return mAccessibilityDelegate;
138 }
139
Sunny Goyal34942622014-08-29 17:20:55 -0700140 public IconCache getIconCache() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400141 return mIconCache;
142 }
143
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700144 public LauncherModel getModel() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400145 return mModel;
146 }
147
Daniel Sandlere4f98912013-06-25 15:13:26 -0400148 static void setLauncherProvider(LauncherProvider provider) {
149 sLauncherProvider = new WeakReference<LauncherProvider>(provider);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400150 }
151
Sunny Goyale5bb7052015-07-27 14:36:07 -0700152 public static LauncherProvider getLauncherProvider() {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400153 return sLauncherProvider.get();
Daniel Sandler924b9932013-06-12 00:38:25 -0400154 }
155
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700156 public WidgetPreviewLoader getWidgetCache() {
157 return mWidgetCache;
158 }
Sunny Goyalf7258242015-10-19 16:59:07 -0700159
Michael Jurkaa6a05472013-11-13 17:59:46 +0100160 public void onWallpaperChanged() {
161 mWallpaperChangedSinceLastCheck = true;
162 }
163
164 public boolean hasWallpaperChangedSinceLastCheck() {
165 boolean result = mWallpaperChangedSinceLastCheck;
166 mWallpaperChangedSinceLastCheck = false;
167 return result;
168 }
169
Adam Cohen2e6da152015-05-06 11:42:25 -0700170 public InvariantDeviceProfile getInvariantDeviceProfile() {
171 return mInvariantDeviceProfile;
172 }
173
Jorim Jaggieedb00a2014-01-13 13:45:07 -0800174 public static boolean isDogfoodBuild() {
Sunny Goyal20806032016-01-07 12:17:38 -0800175 return FeatureFlags.IS_ALPHA_BUILD || FeatureFlags.IS_DEV_BUILD;
Jorim Jaggieedb00a2014-01-13 13:45:07 -0800176 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400177}