LauncherAppState.java revision bfbf7f9f4a0b300613f0ff27a4eb592d88c08325
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 17package com.android.launcher3; 18 19import android.content.BroadcastReceiver; 20import android.content.Context; 21import android.content.Intent; 22import android.content.IntentFilter; 23import android.util.Log; 24 25import com.android.launcher3.compat.LauncherAppsCompat; 26import com.android.launcher3.compat.PackageInstallerCompat; 27import com.android.launcher3.compat.UserManagerCompat; 28import com.android.launcher3.dynamicui.ExtractionUtils; 29import com.android.launcher3.logging.FileLog; 30import com.android.launcher3.shortcuts.DeepShortcutManager; 31import com.android.launcher3.shortcuts.ShortcutCache; 32import com.android.launcher3.util.ConfigMonitor; 33import com.android.launcher3.util.TestingUtils; 34import com.android.launcher3.util.Thunk; 35 36import java.lang.ref.WeakReference; 37 38public class LauncherAppState { 39 40 private final AppFilter mAppFilter; 41 @Thunk final LauncherModel mModel; 42 private final IconCache mIconCache; 43 private final WidgetPreviewLoader mWidgetCache; 44 private final DeepShortcutManager mDeepShortcutManager; 45 46 @Thunk boolean mWallpaperChangedSinceLastCheck; 47 48 private static WeakReference<LauncherProvider> sLauncherProvider; 49 private static Context sContext; 50 51 private static LauncherAppState INSTANCE; 52 53 private InvariantDeviceProfile mInvariantDeviceProfile; 54 55 public static LauncherAppState getInstance() { 56 if (INSTANCE == null) { 57 INSTANCE = new LauncherAppState(); 58 } 59 return INSTANCE; 60 } 61 62 public static LauncherAppState getInstanceNoCreate() { 63 return INSTANCE; 64 } 65 66 public Context getContext() { 67 return sContext; 68 } 69 70 static void setLauncherProvider(LauncherProvider provider) { 71 if (sLauncherProvider != null) { 72 Log.w(Launcher.TAG, "setLauncherProvider called twice! old=" + 73 sLauncherProvider.get() + " new=" + provider); 74 } 75 sLauncherProvider = new WeakReference<>(provider); 76 77 // The content provider exists for the entire duration of the launcher main process and 78 // is the first component to get created. Initializing application context here ensures 79 // that LauncherAppState always exists in the main process. 80 sContext = provider.getContext().getApplicationContext(); 81 FileLog.setDir(sContext.getFilesDir()); 82 } 83 84 private LauncherAppState() { 85 if (sContext == null) { 86 throw new IllegalStateException("LauncherAppState inited before app context set"); 87 } 88 89 Log.v(Launcher.TAG, "LauncherAppState inited"); 90 91 if (TestingUtils.MEMORY_DUMP_ENABLED) { 92 TestingUtils.startTrackingMemory(sContext); 93 } 94 95 mInvariantDeviceProfile = new InvariantDeviceProfile(sContext); 96 mIconCache = new IconCache(sContext, mInvariantDeviceProfile); 97 mWidgetCache = new WidgetPreviewLoader(sContext, mIconCache); 98 mDeepShortcutManager = new DeepShortcutManager(sContext, new ShortcutCache()); 99 100 mAppFilter = AppFilter.loadByName(sContext.getString(R.string.app_filter_class)); 101 mModel = new LauncherModel(this, mIconCache, mAppFilter, mDeepShortcutManager); 102 103 LauncherAppsCompat.getInstance(sContext).addOnAppsChangedCallback(mModel); 104 105 // Register intent receivers 106 IntentFilter filter = new IntentFilter(); 107 filter.addAction(Intent.ACTION_LOCALE_CHANGED); 108 // For handling managed profiles 109 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED); 110 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED); 111 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_AVAILABLE); 112 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_UNAVAILABLE); 113 // For extracting colors from the wallpaper 114 if (Utilities.isNycOrAbove()) { 115 // TODO: add a broadcast entry to the manifest for pre-N. 116 filter.addAction(Intent.ACTION_WALLPAPER_CHANGED); 117 } 118 119 sContext.registerReceiver(mModel, filter); 120 UserManagerCompat.getInstance(sContext).enableAndResetCache(); 121 if (!Utilities.ATLEAST_KITKAT) { 122 sContext.registerReceiver(new BroadcastReceiver() { 123 124 @Override 125 public void onReceive(Context context, Intent intent) { 126 mWallpaperChangedSinceLastCheck = true; 127 } 128 }, new IntentFilter(Intent.ACTION_WALLPAPER_CHANGED)); 129 } 130 new ConfigMonitor(sContext).register(); 131 132 ExtractionUtils.startColorExtractionServiceIfNecessary(sContext); 133 } 134 135 /** 136 * Call from Application.onTerminate(), which is not guaranteed to ever be called. 137 */ 138 public void onTerminate() { 139 sContext.unregisterReceiver(mModel); 140 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(sContext); 141 launcherApps.removeOnAppsChangedCallback(mModel); 142 PackageInstallerCompat.getInstance(sContext).onStop(); 143 } 144 145 /** 146 * Reloads the workspace items from the DB and re-binds the workspace. This should generally 147 * not be called as DB updates are automatically followed by UI update 148 */ 149 public void reloadWorkspace() { 150 mModel.resetLoadedState(false, true); 151 mModel.startLoaderFromBackground(); 152 } 153 154 LauncherModel setLauncher(Launcher launcher) { 155 sLauncherProvider.get().setLauncherProviderChangeListener(launcher); 156 mModel.initialize(launcher); 157 return mModel; 158 } 159 160 public IconCache getIconCache() { 161 return mIconCache; 162 } 163 164 public LauncherModel getModel() { 165 return mModel; 166 } 167 168 public WidgetPreviewLoader getWidgetCache() { 169 return mWidgetCache; 170 } 171 172 public DeepShortcutManager getShortcutManager() { 173 return mDeepShortcutManager; 174 } 175 176 public boolean hasWallpaperChangedSinceLastCheck() { 177 boolean result = mWallpaperChangedSinceLastCheck; 178 mWallpaperChangedSinceLastCheck = false; 179 return result; 180 } 181 182 public InvariantDeviceProfile getInvariantDeviceProfile() { 183 return mInvariantDeviceProfile; 184 } 185} 186