LauncherAppState.java revision 924b993c750e6de8537afb750c2d2b9932efbdac
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.ContentResolver; 21import android.content.Context; 22import android.content.Intent; 23import android.content.IntentFilter; 24import android.content.res.Configuration; 25import android.database.ContentObserver; 26import android.os.Handler; 27 28import java.lang.ref.WeakReference; 29 30public class LauncherAppState { 31 private Context mContext; 32 private LauncherModel mModel; 33 private IconCache mIconCache; 34 private WidgetPreviewLoader.CacheDb mWidgetPreviewCacheDb; 35 private static boolean sIsScreenLarge; 36 private static float sScreenDensity; 37 private static int sLongPressTimeout = 300; 38 private static final String sSharedPreferencesKey = "com.android.launcher3.prefs"; 39 private long mUptime; 40 41 WeakReference<LauncherProvider> mLauncherProvider; 42 43 private static final LauncherAppState INSTANCE = new LauncherAppState(); 44 45 public static LauncherAppState getInstance() { 46 return INSTANCE; 47 } 48 49 public static void init(Context context) { 50 INSTANCE.initialize(context); 51 } 52 53 public Context getContext() { 54 return mContext; 55 } 56 57 private LauncherAppState() { } 58 59 private void initialize(Context context) { 60 mContext = context; 61 62 mUptime = System.currentTimeMillis(); 63 64 // set sIsScreenXLarge and sScreenDensity *before* creating icon cache 65 sIsScreenLarge = context.getResources().getBoolean(R.bool.is_large_screen); 66 sScreenDensity = context.getResources().getDisplayMetrics().density; 67 68 mWidgetPreviewCacheDb = new WidgetPreviewLoader.CacheDb(context); 69 mIconCache = new IconCache(context); 70 mModel = new LauncherModel(context, mIconCache); 71 72 // Register intent receivers 73 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED); 74 filter.addAction(Intent.ACTION_PACKAGE_REMOVED); 75 filter.addAction(Intent.ACTION_PACKAGE_CHANGED); 76 filter.addDataScheme("package"); 77 context.registerReceiver(mModel, filter); 78 filter = new IntentFilter(); 79 filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE); 80 filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE); 81 filter.addAction(Intent.ACTION_LOCALE_CHANGED); 82 filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED); 83 context.registerReceiver(mModel, filter); 84 filter = new IntentFilter(); 85 filter.addAction(SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED); 86 context.registerReceiver(mModel, filter); 87 filter = new IntentFilter(); 88 filter.addAction(SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED); 89 context.registerReceiver(mModel, filter); 90 91 // Register for changes to the favorites 92 ContentResolver resolver = context.getContentResolver(); 93 resolver.registerContentObserver(LauncherSettings.Favorites.CONTENT_URI, true, 94 mFavoritesObserver); 95 } 96 97 /** 98 * Call from Application.onTerminate(), which is not guaranteed to ever be called. 99 */ 100 public void onTerminate() { 101 mContext.unregisterReceiver(mModel); 102 103 ContentResolver resolver = mContext.getContentResolver(); 104 resolver.unregisterContentObserver(mFavoritesObserver); 105 } 106 107 /** 108 * Receives notifications whenever the user favorites have changed. 109 */ 110 private final ContentObserver mFavoritesObserver = new ContentObserver(new Handler()) { 111 @Override 112 public void onChange(boolean selfChange) { 113 // If the database has ever changed, then we really need to force a reload of the 114 // workspace on the next load 115 mModel.resetLoadedState(false, true); 116 mModel.startLoaderFromBackground(); 117 } 118 }; 119 120 LauncherModel setLauncher(Launcher launcher) { 121 mModel.initialize(launcher); 122 return mModel; 123 } 124 125 IconCache getIconCache() { 126 return mIconCache; 127 } 128 129 LauncherModel getModel() { 130 return mModel; 131 } 132 133 WidgetPreviewLoader.CacheDb getWidgetPreviewCacheDb() { 134 return mWidgetPreviewCacheDb; 135 } 136 137 void setLauncherProvider(LauncherProvider provider) { 138 mLauncherProvider = new WeakReference<LauncherProvider>(provider); 139 } 140 141 LauncherProvider getLauncherProvider() { 142 return mLauncherProvider.get(); 143 } 144 145 /** 146 * @return Milliseconds since the application state was created. 147 */ 148 public long getUptime() { 149 return System.currentTimeMillis() - mUptime; 150 } 151 152 public static String getSharedPreferencesKey() { 153 return sSharedPreferencesKey; 154 } 155 156 public static boolean isScreenLarge() { 157 return sIsScreenLarge; 158 } 159 160 public static boolean isScreenLandscape(Context context) { 161 return context.getResources().getConfiguration().orientation == 162 Configuration.ORIENTATION_LANDSCAPE; 163 } 164 165 public static float getScreenDensity() { 166 return sScreenDensity; 167 } 168 169 public static int getLongPressTimeout() { 170 return sLongPressTimeout; 171 } 172} 173