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.content.ContentResolver;
     21 import android.content.Intent;
     22 import android.content.IntentFilter;
     23 import android.database.ContentObserver;
     24 import android.os.Handler;
     25 import dalvik.system.VMRuntime;
     26 
     27 public class LauncherApplication extends Application {
     28     public LauncherModel mModel;
     29     public IconCache mIconCache;
     30 
     31     @Override
     32     public void onCreate() {
     33         VMRuntime.getRuntime().setMinimumHeapSize(4 * 1024 * 1024);
     34 
     35         super.onCreate();
     36 
     37         mIconCache = new IconCache(this);
     38         mModel = new LauncherModel(this, mIconCache);
     39 
     40         // Register intent receivers
     41         IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
     42         filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
     43         filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
     44         filter.addDataScheme("package");
     45         registerReceiver(mModel, filter);
     46         filter = new IntentFilter();
     47         filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);
     48         filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
     49         registerReceiver(mModel, filter);
     50 
     51         // Register for changes to the favorites
     52         ContentResolver resolver = getContentResolver();
     53         resolver.registerContentObserver(LauncherSettings.Favorites.CONTENT_URI, true,
     54                 mFavoritesObserver);
     55     }
     56 
     57     /**
     58      * There's no guarantee that this function is ever called.
     59      */
     60     @Override
     61     public void onTerminate() {
     62         super.onTerminate();
     63 
     64         unregisterReceiver(mModel);
     65 
     66         ContentResolver resolver = getContentResolver();
     67         resolver.unregisterContentObserver(mFavoritesObserver);
     68     }
     69 
     70     /**
     71      * Receives notifications whenever the user favorites have changed.
     72      */
     73     private final ContentObserver mFavoritesObserver = new ContentObserver(new Handler()) {
     74         @Override
     75         public void onChange(boolean selfChange) {
     76             mModel.startLoader(LauncherApplication.this, false);
     77         }
     78     };
     79 
     80     LauncherModel setLauncher(Launcher launcher) {
     81         mModel.initialize(launcher);
     82         return mModel;
     83     }
     84 
     85     IconCache getIconCache() {
     86         return mIconCache;
     87     }
     88 
     89     LauncherModel getModel() {
     90         return mModel;
     91     }
     92 }
     93