Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.launcher3; |
| 18 | |
| 19 | import android.app.SearchManager; |
Sunny Goyal | e755d46 | 2014-07-22 13:48:29 -0700 | [diff] [blame] | 20 | import android.content.ComponentName; |
Sunny Goyal | e755d46 | 2014-07-22 13:48:29 -0700 | [diff] [blame] | 21 | import android.content.Context; |
| 22 | import android.content.Intent; |
| 23 | import android.content.IntentFilter; |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 24 | import android.content.res.Configuration; |
Michael Jurka | 104c456 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 25 | import android.content.res.Resources; |
Daniel Sandler | b9eb286 | 2013-06-14 20:17:30 -0400 | [diff] [blame] | 26 | import android.util.Log; |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 27 | |
Sunny Goyal | 83a8f04 | 2015-05-19 12:52:12 -0700 | [diff] [blame] | 28 | import com.android.launcher3.accessibility.LauncherAccessibilityDelegate; |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 29 | import com.android.launcher3.compat.LauncherAppsCompat; |
Sunny Goyal | 4390ace | 2014-10-13 11:33:11 -0700 | [diff] [blame] | 30 | import com.android.launcher3.compat.PackageInstallerCompat; |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 31 | import com.android.launcher3.util.Thunk; |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 32 | |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 33 | import java.lang.ref.WeakReference; |
| 34 | |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame] | 35 | public class LauncherAppState { |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 36 | |
Nilesh Agrawal | 16f3ea8 | 2014-01-09 17:14:01 -0800 | [diff] [blame] | 37 | private final AppFilter mAppFilter; |
| 38 | private final BuildInfo mBuildInfo; |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 39 | @Thunk final LauncherModel mModel; |
Sunny Goyal | e0f58d7 | 2014-11-10 18:05:31 -0800 | [diff] [blame] | 40 | private final IconCache mIconCache; |
Sunny Goyal | 5b0e669 | 2015-03-19 14:31:19 -0700 | [diff] [blame] | 41 | private final WidgetPreviewLoader mWidgetCache; |
Sunny Goyal | e0f58d7 | 2014-11-10 18:05:31 -0800 | [diff] [blame] | 42 | |
| 43 | private final boolean mIsScreenLarge; |
Sunny Goyal | e0f58d7 | 2014-11-10 18:05:31 -0800 | [diff] [blame] | 44 | private final int mLongPressTimeout = 300; |
| 45 | |
Michael Jurka | a6a0547 | 2013-11-13 17:59:46 +0100 | [diff] [blame] | 46 | private boolean mWallpaperChangedSinceLastCheck; |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 47 | |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 48 | private static WeakReference<LauncherProvider> sLauncherProvider; |
| 49 | private static Context sContext; |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 50 | |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 51 | private static LauncherAppState INSTANCE; |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 52 | |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 53 | private InvariantDeviceProfile mInvariantDeviceProfile; |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 54 | private LauncherAccessibilityDelegate mAccessibilityDelegate; |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 55 | |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 56 | public static LauncherAppState getInstance() { |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 57 | if (INSTANCE == null) { |
| 58 | INSTANCE = new LauncherAppState(); |
| 59 | } |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 60 | return INSTANCE; |
| 61 | } |
| 62 | |
Chris Wren | d8fe6de | 2013-10-04 10:42:14 -0400 | [diff] [blame] | 63 | public static LauncherAppState getInstanceNoCreate() { |
| 64 | return INSTANCE; |
| 65 | } |
| 66 | |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 67 | public Context getContext() { |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 68 | return sContext; |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 69 | } |
| 70 | |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 71 | public static void setApplicationContext(Context context) { |
| 72 | if (sContext != null) { |
Daniel Sandler | e060b0b | 2013-06-27 21:47:55 -0400 | [diff] [blame] | 73 | Log.w(Launcher.TAG, "setApplicationContext called twice! old=" + sContext + " new=" + context); |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 74 | } |
| 75 | sContext = context.getApplicationContext(); |
| 76 | } |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 77 | |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 78 | private LauncherAppState() { |
| 79 | if (sContext == null) { |
| 80 | throw new IllegalStateException("LauncherAppState inited before app context set"); |
Daniel Sandler | b9eb286 | 2013-06-14 20:17:30 -0400 | [diff] [blame] | 81 | } |
| 82 | |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 83 | Log.v(Launcher.TAG, "LauncherAppState inited"); |
Daniel Sandler | b9eb286 | 2013-06-14 20:17:30 -0400 | [diff] [blame] | 84 | |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 85 | if (sContext.getResources().getBoolean(R.bool.debug_memory_enabled)) { |
| 86 | MemoryTracker.startTrackingMe(sContext, "L"); |
| 87 | } |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 88 | |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 89 | // set sIsScreenXLarge and mScreenDensity *before* creating icon cache |
Michael Jurka | 104c456 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 90 | mIsScreenLarge = isScreenLarge(sContext.getResources()); |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 91 | mInvariantDeviceProfile = new InvariantDeviceProfile(sContext); |
| 92 | mIconCache = new IconCache(sContext, mInvariantDeviceProfile); |
| 93 | mWidgetCache = new WidgetPreviewLoader(sContext, mInvariantDeviceProfile, mIconCache); |
Bjorn Bringert | 1307f63 | 2013-10-03 22:31:03 +0100 | [diff] [blame] | 94 | |
| 95 | mAppFilter = AppFilter.loadByName(sContext.getString(R.string.app_filter_class)); |
Nilesh Agrawal | 16f3ea8 | 2014-01-09 17:14:01 -0800 | [diff] [blame] | 96 | mBuildInfo = BuildInfo.loadByName(sContext.getString(R.string.build_info_class)); |
Bjorn Bringert | 1307f63 | 2013-10-03 22:31:03 +0100 | [diff] [blame] | 97 | mModel = new LauncherModel(this, mIconCache, mAppFilter); |
Sunny Goyal | 2759579 | 2015-03-19 13:18:44 -0700 | [diff] [blame] | 98 | |
| 99 | LauncherAppsCompat.getInstance(sContext).addOnAppsChangedCallback(mModel); |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 100 | |
| 101 | // Register intent receivers |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 102 | IntentFilter filter = new IntentFilter(); |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 103 | filter.addAction(Intent.ACTION_LOCALE_CHANGED); |
| 104 | filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED); |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 105 | filter.addAction(SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED); |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 106 | filter.addAction(SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED); |
Sunny Goyal | 957c13f | 2015-05-01 13:02:20 -0700 | [diff] [blame] | 107 | // For handling managed profiles |
| 108 | filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED); |
| 109 | filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED); |
| 110 | |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 111 | sContext.registerReceiver(mModel, filter); |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 112 | } |
Kenny Guy | 1317e2d | 2014-05-08 18:52:50 +0100 | [diff] [blame] | 113 | |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 114 | /** |
| 115 | * Call from Application.onTerminate(), which is not guaranteed to ever be called. |
| 116 | */ |
| 117 | public void onTerminate() { |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 118 | sContext.unregisterReceiver(mModel); |
Kenny Guy | 1317e2d | 2014-05-08 18:52:50 +0100 | [diff] [blame] | 119 | final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(sContext); |
Kenny Guy | c2bd810 | 2014-06-30 12:30:31 +0100 | [diff] [blame] | 120 | launcherApps.removeOnAppsChangedCallback(mModel); |
Sunny Goyal | 4390ace | 2014-10-13 11:33:11 -0700 | [diff] [blame] | 121 | PackageInstallerCompat.getInstance(sContext).onStop(); |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | /** |
Sunny Goyal | 1d4a2df | 2015-03-30 11:11:46 -0700 | [diff] [blame] | 125 | * Reloads the workspace items from the DB and re-binds the workspace. This should generally |
| 126 | * not be called as DB updates are automatically followed by UI update |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 127 | */ |
Sunny Goyal | 1d4a2df | 2015-03-30 11:11:46 -0700 | [diff] [blame] | 128 | public void reloadWorkspace() { |
| 129 | mModel.resetLoadedState(false, true); |
| 130 | mModel.startLoaderFromBackground(); |
| 131 | } |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 132 | |
| 133 | LauncherModel setLauncher(Launcher launcher) { |
| 134 | mModel.initialize(launcher); |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 135 | mAccessibilityDelegate = ((launcher != null) && Utilities.isLmpOrAbove()) ? |
| 136 | new LauncherAccessibilityDelegate(launcher) : null; |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 137 | return mModel; |
| 138 | } |
| 139 | |
Hyunyoung Song | 3f47144 | 2015-04-08 19:01:34 -0700 | [diff] [blame] | 140 | public LauncherAccessibilityDelegate getAccessibilityDelegate() { |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 141 | return mAccessibilityDelegate; |
| 142 | } |
| 143 | |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 144 | public IconCache getIconCache() { |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 145 | return mIconCache; |
| 146 | } |
| 147 | |
Sunny Goyal | 18bf8e2 | 2015-04-08 18:13:46 -0700 | [diff] [blame] | 148 | public LauncherModel getModel() { |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 149 | return mModel; |
| 150 | } |
| 151 | |
Bjorn Bringert | 1307f63 | 2013-10-03 22:31:03 +0100 | [diff] [blame] | 152 | boolean shouldShowAppOrWidgetProvider(ComponentName componentName) { |
| 153 | return mAppFilter == null || mAppFilter.shouldShowApp(componentName); |
| 154 | } |
| 155 | |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 156 | static void setLauncherProvider(LauncherProvider provider) { |
| 157 | sLauncherProvider = new WeakReference<LauncherProvider>(provider); |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 158 | } |
| 159 | |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 160 | static LauncherProvider getLauncherProvider() { |
| 161 | return sLauncherProvider.get(); |
Daniel Sandler | 924b993 | 2013-06-12 00:38:25 -0400 | [diff] [blame] | 162 | } |
| 163 | |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 164 | public static String getSharedPreferencesKey() { |
Helena Josol | 28db280 | 2014-10-09 17:04:09 +0100 | [diff] [blame] | 165 | return LauncherFiles.SHARED_PREFERENCES_KEY; |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 166 | } |
| 167 | |
Sunny Goyal | 5b0e669 | 2015-03-19 14:31:19 -0700 | [diff] [blame] | 168 | public WidgetPreviewLoader getWidgetCache() { |
| 169 | return mWidgetCache; |
| 170 | } |
| 171 | |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 172 | public boolean isScreenLarge() { |
| 173 | return mIsScreenLarge; |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 174 | } |
| 175 | |
Michael Jurka | 104c456 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 176 | // Need a version that doesn't require an instance of LauncherAppState for the wallpaper picker |
| 177 | public static boolean isScreenLarge(Resources res) { |
| 178 | return res.getBoolean(R.bool.is_large_tablet); |
| 179 | } |
| 180 | |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 181 | public int getLongPressTimeout() { |
| 182 | return mLongPressTimeout; |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 183 | } |
Winson Chung | 6e1c0d3 | 2013-10-25 15:24:24 -0700 | [diff] [blame] | 184 | |
Michael Jurka | a6a0547 | 2013-11-13 17:59:46 +0100 | [diff] [blame] | 185 | public void onWallpaperChanged() { |
| 186 | mWallpaperChangedSinceLastCheck = true; |
| 187 | } |
| 188 | |
| 189 | public boolean hasWallpaperChangedSinceLastCheck() { |
| 190 | boolean result = mWallpaperChangedSinceLastCheck; |
| 191 | mWallpaperChangedSinceLastCheck = false; |
| 192 | return result; |
| 193 | } |
| 194 | |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 195 | public InvariantDeviceProfile getInvariantDeviceProfile() { |
| 196 | return mInvariantDeviceProfile; |
| 197 | } |
| 198 | |
Jorim Jaggi | eedb00a | 2014-01-13 13:45:07 -0800 | [diff] [blame] | 199 | public static boolean isDogfoodBuild() { |
| 200 | return getInstance().mBuildInfo.isDogfoodBuild(); |
| 201 | } |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 202 | } |