blob: db3a4cbe4f00b2baaecf62394e937958023e373e [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
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
Joe Onoratoa5902522009-07-30 13:37:37 -070017package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
19import android.app.Application;
Joe Onorato9c1289c2009-08-17 11:03:03 -040020import android.content.ContentResolver;
Joe Onorato9c1289c2009-08-17 11:03:03 -040021import android.content.Intent;
22import android.content.IntentFilter;
Winson Chungaafa03c2010-06-11 17:34:16 -070023import android.content.res.Configuration;
Joe Onorato9c1289c2009-08-17 11:03:03 -040024import android.database.ContentObserver;
25import android.os.Handler;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080026
Michael Jurkaa8c760d2011-04-28 14:59:33 -070027import java.lang.ref.WeakReference;
28
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029public class LauncherApplication extends Application {
Joe Onorato0589f0f2010-02-08 13:44:00 -080030 public LauncherModel mModel;
31 public IconCache mIconCache;
Michael Jurkaa2eb1702011-05-12 14:57:05 -070032 private static boolean sIsScreenLarge;
Patrick Dubroy8e58e912010-10-14 13:21:48 -070033 private static float sScreenDensity;
Michael Jurkaa8c760d2011-04-28 14:59:33 -070034 WeakReference<LauncherProvider> mLauncherProvider;
Joe Onorato9c1289c2009-08-17 11:03:03 -040035
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036 @Override
37 public void onCreate() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038 super.onCreate();
Joe Onorato9c1289c2009-08-17 11:03:03 -040039
Michael Jurkac9a96192010-11-01 11:52:08 -070040 // set sIsScreenXLarge and sScreenDensity *before* creating icon cache
Michael Jurkaa2eb1702011-05-12 14:57:05 -070041 final int screenSize = getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK;
42 sIsScreenLarge = screenSize == Configuration.SCREENLAYOUT_SIZE_LARGE ||
43 screenSize == Configuration.SCREENLAYOUT_SIZE_XLARGE;
Patrick Dubroy8e58e912010-10-14 13:21:48 -070044 sScreenDensity = getResources().getDisplayMetrics().density;
Joe Onorato0589f0f2010-02-08 13:44:00 -080045
Michael Jurkac9a96192010-11-01 11:52:08 -070046 mIconCache = new IconCache(this);
47 mModel = new LauncherModel(this, mIconCache);
48
Joe Onorato9c1289c2009-08-17 11:03:03 -040049 // Register intent receivers
50 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
51 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
52 filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
53 filter.addDataScheme("package");
Joe Onoratof99f8c12009-10-31 17:27:36 -040054 registerReceiver(mModel, filter);
Joe Onorato64e6be72010-03-05 15:05:52 -050055 filter = new IntentFilter();
56 filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);
57 filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
Joe Onoratoe9ad59e2010-10-29 17:35:36 -070058 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
Joe Onorato64e6be72010-03-05 15:05:52 -050059 registerReceiver(mModel, filter);
Joe Onorato9c1289c2009-08-17 11:03:03 -040060
61 // Register for changes to the favorites
62 ContentResolver resolver = getContentResolver();
63 resolver.registerContentObserver(LauncherSettings.Favorites.CONTENT_URI, true,
64 mFavoritesObserver);
65 }
66
67 /**
68 * There's no guarantee that this function is ever called.
69 */
70 @Override
71 public void onTerminate() {
72 super.onTerminate();
73
Joe Onoratof99f8c12009-10-31 17:27:36 -040074 unregisterReceiver(mModel);
Joe Onorato9c1289c2009-08-17 11:03:03 -040075
76 ContentResolver resolver = getContentResolver();
77 resolver.unregisterContentObserver(mFavoritesObserver);
78 }
79
80 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -040081 * Receives notifications whenever the user favorites have changed.
82 */
83 private final ContentObserver mFavoritesObserver = new ContentObserver(new Handler()) {
84 @Override
85 public void onChange(boolean selfChange) {
Joe Onoratof99f8c12009-10-31 17:27:36 -040086 mModel.startLoader(LauncherApplication.this, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -040087 }
88 };
89
90 LauncherModel setLauncher(Launcher launcher) {
Joe Onoratof99f8c12009-10-31 17:27:36 -040091 mModel.initialize(launcher);
92 return mModel;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080093 }
Joe Onorato0589f0f2010-02-08 13:44:00 -080094
95 IconCache getIconCache() {
96 return mIconCache;
97 }
98
99 LauncherModel getModel() {
100 return mModel;
101 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700102
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700103 void setLauncherProvider(LauncherProvider provider) {
104 mLauncherProvider = new WeakReference<LauncherProvider>(provider);
105 }
106
107 LauncherProvider getLauncherProvider() {
108 return mLauncherProvider.get();
109 }
110
Michael Jurkaa2eb1702011-05-12 14:57:05 -0700111 public static boolean isScreenLarge() {
112 return sIsScreenLarge;
Winson Chungaafa03c2010-06-11 17:34:16 -0700113 }
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700114
115 public static float getScreenDensity() {
116 return sScreenDensity;
117 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800118}