1 /* 2 * Copyright (C) 2008 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.launcher2; 18 19 import android.app.Application; 20 import android.app.SearchManager; 21 import android.content.ContentResolver; 22 import android.content.Context; 23 import android.content.Intent; 24 import android.content.IntentFilter; 25 import android.content.res.Configuration; 26 import android.database.ContentObserver; 27 import android.os.Handler; 28 29 import com.android.launcher.R; 30 31 import java.lang.ref.WeakReference; 32 33 public class LauncherApplication extends Application { 34 public LauncherModel mModel; 35 public IconCache mIconCache; 36 private static boolean sIsScreenLarge; 37 private static float sScreenDensity; 38 private static int sLongPressTimeout = 300; 39 private static final String sSharedPreferencesKey = "com.android.launcher2.prefs"; 40 WeakReference<LauncherProvider> mLauncherProvider; 41 42 @Override onCreate()43 public void onCreate() { 44 super.onCreate(); 45 46 // set sIsScreenXLarge and sScreenDensity *before* creating icon cache 47 sIsScreenLarge = getResources().getBoolean(R.bool.is_large_screen); 48 sScreenDensity = getResources().getDisplayMetrics().density; 49 50 mIconCache = new IconCache(this); 51 mModel = new LauncherModel(this, mIconCache); 52 53 // Register intent receivers 54 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED); 55 filter.addAction(Intent.ACTION_PACKAGE_REMOVED); 56 filter.addAction(Intent.ACTION_PACKAGE_CHANGED); 57 filter.addDataScheme("package"); 58 registerReceiver(mModel, filter); 59 filter = new IntentFilter(); 60 filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE); 61 filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE); 62 filter.addAction(Intent.ACTION_LOCALE_CHANGED); 63 filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED); 64 registerReceiver(mModel, filter); 65 filter = new IntentFilter(); 66 filter.addAction(SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED); 67 registerReceiver(mModel, filter); 68 filter = new IntentFilter(); 69 filter.addAction(SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED); 70 registerReceiver(mModel, filter); 71 72 // Register for changes to the favorites 73 ContentResolver resolver = getContentResolver(); 74 resolver.registerContentObserver(LauncherSettings.Favorites.CONTENT_URI, true, 75 mFavoritesObserver); 76 } 77 78 /** 79 * There's no guarantee that this function is ever called. 80 */ 81 @Override onTerminate()82 public void onTerminate() { 83 super.onTerminate(); 84 85 unregisterReceiver(mModel); 86 87 ContentResolver resolver = getContentResolver(); 88 resolver.unregisterContentObserver(mFavoritesObserver); 89 } 90 91 /** 92 * Receives notifications whenever the user favorites have changed. 93 */ 94 private final ContentObserver mFavoritesObserver = new ContentObserver(new Handler()) { 95 @Override 96 public void onChange(boolean selfChange) { 97 // If the database has ever changed, then we really need to force a reload of the 98 // workspace on the next load 99 mModel.resetLoadedState(false, true); 100 mModel.startLoaderFromBackground(); 101 } 102 }; 103 setLauncher(Launcher launcher)104 LauncherModel setLauncher(Launcher launcher) { 105 mModel.initialize(launcher); 106 return mModel; 107 } 108 getIconCache()109 IconCache getIconCache() { 110 return mIconCache; 111 } 112 getModel()113 LauncherModel getModel() { 114 return mModel; 115 } 116 setLauncherProvider(LauncherProvider provider)117 void setLauncherProvider(LauncherProvider provider) { 118 mLauncherProvider = new WeakReference<LauncherProvider>(provider); 119 } 120 getLauncherProvider()121 LauncherProvider getLauncherProvider() { 122 return mLauncherProvider.get(); 123 } 124 getSharedPreferencesKey()125 public static String getSharedPreferencesKey() { 126 return sSharedPreferencesKey; 127 } 128 isScreenLarge()129 public static boolean isScreenLarge() { 130 return sIsScreenLarge; 131 } 132 isScreenLandscape(Context context)133 public static boolean isScreenLandscape(Context context) { 134 return context.getResources().getConfiguration().orientation == 135 Configuration.ORIENTATION_LANDSCAPE; 136 } 137 getScreenDensity()138 public static float getScreenDensity() { 139 return sScreenDensity; 140 } 141 getLongPressTimeout()142 public static int getLongPressTimeout() { 143 return sLongPressTimeout; 144 } 145 } 146