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