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