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 java.lang.ref.WeakReference; 30 31 public class LauncherApplication extends Application { 32 public LauncherModel mModel; 33 public IconCache mIconCache; 34 private static boolean sIsScreenLarge; 35 private static float sScreenDensity; 36 WeakReference<LauncherProvider> mLauncherProvider; 37 38 @Override 39 public void onCreate() { 40 super.onCreate(); 41 42 // set sIsScreenXLarge and sScreenDensity *before* creating icon cache 43 final int screenSize = getResources().getConfiguration().screenLayout & 44 Configuration.SCREENLAYOUT_SIZE_MASK; 45 sIsScreenLarge = screenSize == Configuration.SCREENLAYOUT_SIZE_LARGE || 46 screenSize == Configuration.SCREENLAYOUT_SIZE_XLARGE; 47 sScreenDensity = getResources().getDisplayMetrics().density; 48 49 mIconCache = new IconCache(this); 50 mModel = new LauncherModel(this, mIconCache); 51 52 // Register intent receivers 53 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED); 54 filter.addAction(Intent.ACTION_PACKAGE_REMOVED); 55 filter.addAction(Intent.ACTION_PACKAGE_CHANGED); 56 filter.addDataScheme("package"); 57 registerReceiver(mModel, filter); 58 filter = new IntentFilter(); 59 filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE); 60 filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE); 61 filter.addAction(Intent.ACTION_LOCALE_CHANGED); 62 filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED); 63 registerReceiver(mModel, filter); 64 filter = new IntentFilter(); 65 filter.addAction(SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED); 66 registerReceiver(mModel, filter); 67 filter = new IntentFilter(); 68 filter.addAction(SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED); 69 registerReceiver(mModel, filter); 70 71 // Register for changes to the favorites 72 ContentResolver resolver = getContentResolver(); 73 resolver.registerContentObserver(LauncherSettings.Favorites.CONTENT_URI, true, 74 mFavoritesObserver); 75 } 76 77 /** 78 * There's no guarantee that this function is ever called. 79 */ 80 @Override 81 public void onTerminate() { 82 super.onTerminate(); 83 84 unregisterReceiver(mModel); 85 86 ContentResolver resolver = getContentResolver(); 87 resolver.unregisterContentObserver(mFavoritesObserver); 88 } 89 90 /** 91 * Receives notifications whenever the user favorites have changed. 92 */ 93 private final ContentObserver mFavoritesObserver = new ContentObserver(new Handler()) { 94 @Override 95 public void onChange(boolean selfChange) { 96 mModel.startLoader(LauncherApplication.this, false); 97 } 98 }; 99 100 LauncherModel setLauncher(Launcher launcher) { 101 mModel.initialize(launcher); 102 return mModel; 103 } 104 105 IconCache getIconCache() { 106 return mIconCache; 107 } 108 109 LauncherModel getModel() { 110 return mModel; 111 } 112 113 void setLauncherProvider(LauncherProvider provider) { 114 mLauncherProvider = new WeakReference<LauncherProvider>(provider); 115 } 116 117 LauncherProvider getLauncherProvider() { 118 return mLauncherProvider.get(); 119 } 120 121 public static boolean isScreenLarge() { 122 return sIsScreenLarge; 123 } 124 125 public static boolean isScreenLandscape(Context context) { 126 return context.getResources().getConfiguration().orientation == 127 Configuration.ORIENTATION_LANDSCAPE; 128 } 129 130 public static float getScreenDensity() { 131 return sScreenDensity; 132 } 133 } 134