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