LauncherAppState.java revision a50a419b824c10f11d12097215982c978f740e00
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.app.SearchManager; 20import android.content.Context; 21import android.content.Intent; 22import android.content.IntentFilter; 23import android.os.UserManager; 24import android.util.Log; 25 26import com.android.launcher3.accessibility.LauncherAccessibilityDelegate; 27import com.android.launcher3.compat.LauncherAppsCompat; 28import com.android.launcher3.compat.PackageInstallerCompat; 29import com.android.launcher3.compat.UserManagerCompat; 30import com.android.launcher3.util.ConfigMonitor; 31import com.android.launcher3.util.TestingUtils; 32import com.android.launcher3.util.Thunk; 33 34import java.lang.ref.WeakReference; 35 36public class LauncherAppState { 37 38 private final AppFilter mAppFilter; 39 private final BuildInfo mBuildInfo; 40 @Thunk final LauncherModel mModel; 41 private final IconCache mIconCache; 42 private final WidgetPreviewLoader mWidgetCache; 43 44 private boolean mWallpaperChangedSinceLastCheck; 45 46 private static WeakReference<LauncherProvider> sLauncherProvider; 47 private static Context sContext; 48 49 private static LauncherAppState INSTANCE; 50 51 private InvariantDeviceProfile mInvariantDeviceProfile; 52 53 private LauncherAccessibilityDelegate mAccessibilityDelegate; 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 public static void setApplicationContext(Context context) { 71 if (sContext != null) { 72 Log.w(Launcher.TAG, "setApplicationContext called twice! old=" + sContext + " new=" + context); 73 } 74 sContext = context.getApplicationContext(); 75 } 76 77 private LauncherAppState() { 78 if (sContext == null) { 79 throw new IllegalStateException("LauncherAppState inited before app context set"); 80 } 81 82 Log.v(Launcher.TAG, "LauncherAppState inited"); 83 84 if (TestingUtils.MEMORY_DUMP_ENABLED) { 85 TestingUtils.startTrackingMemory(sContext); 86 } 87 88 mInvariantDeviceProfile = new InvariantDeviceProfile(sContext); 89 mIconCache = new IconCache(sContext, mInvariantDeviceProfile); 90 mWidgetCache = new WidgetPreviewLoader(sContext, mIconCache); 91 92 mAppFilter = AppFilter.loadByName(sContext.getString(R.string.app_filter_class)); 93 mBuildInfo = BuildInfo.loadByName(sContext.getString(R.string.build_info_class)); 94 mModel = new LauncherModel(this, mIconCache, mAppFilter); 95 96 LauncherAppsCompat.getInstance(sContext).addOnAppsChangedCallback(mModel); 97 98 // Register intent receivers 99 IntentFilter filter = new IntentFilter(); 100 filter.addAction(Intent.ACTION_LOCALE_CHANGED); 101 filter.addAction(SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED); 102 // For handling managed profiles 103 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED); 104 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED); 105 106 sContext.registerReceiver(mModel, filter); 107 UserManagerCompat.getInstance(sContext).enableAndResetCache(); 108 new ConfigMonitor(sContext).register(); 109 } 110 111 /** 112 * Call from Application.onTerminate(), which is not guaranteed to ever be called. 113 */ 114 public void onTerminate() { 115 sContext.unregisterReceiver(mModel); 116 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(sContext); 117 launcherApps.removeOnAppsChangedCallback(mModel); 118 PackageInstallerCompat.getInstance(sContext).onStop(); 119 } 120 121 /** 122 * Reloads the workspace items from the DB and re-binds the workspace. This should generally 123 * not be called as DB updates are automatically followed by UI update 124 */ 125 public void reloadWorkspace() { 126 mModel.resetLoadedState(false, true); 127 mModel.startLoaderFromBackground(); 128 } 129 130 LauncherModel setLauncher(Launcher launcher) { 131 getLauncherProvider().setLauncherProviderChangeListener(launcher); 132 mModel.initialize(launcher); 133 mAccessibilityDelegate = ((launcher != null) && Utilities.ATLEAST_LOLLIPOP) ? 134 new LauncherAccessibilityDelegate(launcher) : null; 135 return mModel; 136 } 137 138 public LauncherAccessibilityDelegate getAccessibilityDelegate() { 139 return mAccessibilityDelegate; 140 } 141 142 public IconCache getIconCache() { 143 return mIconCache; 144 } 145 146 public LauncherModel getModel() { 147 return mModel; 148 } 149 150 static void setLauncherProvider(LauncherProvider provider) { 151 sLauncherProvider = new WeakReference<LauncherProvider>(provider); 152 } 153 154 public static LauncherProvider getLauncherProvider() { 155 return sLauncherProvider.get(); 156 } 157 158 public WidgetPreviewLoader getWidgetCache() { 159 return mWidgetCache; 160 } 161 162 public void onWallpaperChanged() { 163 mWallpaperChangedSinceLastCheck = true; 164 } 165 166 public boolean hasWallpaperChangedSinceLastCheck() { 167 boolean result = mWallpaperChangedSinceLastCheck; 168 mWallpaperChangedSinceLastCheck = false; 169 return result; 170 } 171 172 public InvariantDeviceProfile getInvariantDeviceProfile() { 173 return mInvariantDeviceProfile; 174 } 175 176 public static boolean isDogfoodBuild() { 177 return getInstance().mBuildInfo.isDogfoodBuild(); 178 } 179} 180