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