blob: 94163acc6b4578b0662fb5853ee9833d8380fc97 [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;
Narayan Kamathcb1a4772011-06-28 13:46:59 +010020import android.app.SearchManager;
Joe Onorato9c1289c2009-08-17 11:03:03 -040021import android.content.ContentResolver;
Joe Onorato9c1289c2009-08-17 11:03:03 -040022import android.content.Intent;
23import android.content.IntentFilter;
Winson Chungaafa03c2010-06-11 17:34:16 -070024import android.content.res.Configuration;
Joe Onorato9c1289c2009-08-17 11:03:03 -040025import android.database.ContentObserver;
26import android.os.Handler;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080027
Michael Jurkaa8c760d2011-04-28 14:59:33 -070028import java.lang.ref.WeakReference;
29
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030public class LauncherApplication extends Application {
Joe Onorato0589f0f2010-02-08 13:44:00 -080031 public LauncherModel mModel;
32 public IconCache mIconCache;
Michael Jurkaa2eb1702011-05-12 14:57:05 -070033 private static boolean sIsScreenLarge;
Patrick Dubroy8e58e912010-10-14 13:21:48 -070034 private static float sScreenDensity;
Michael Jurkaa8c760d2011-04-28 14:59:33 -070035 WeakReference<LauncherProvider> mLauncherProvider;
Joe Onorato9c1289c2009-08-17 11:03:03 -040036
The Android Open Source Project31dd5032009-03-03 19:32:27 -080037 @Override
38 public void onCreate() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039 super.onCreate();
Joe Onorato9c1289c2009-08-17 11:03:03 -040040
Michael Jurkac9a96192010-11-01 11:52:08 -070041 // set sIsScreenXLarge and sScreenDensity *before* creating icon cache
Michael Jurkaa2eb1702011-05-12 14:57:05 -070042 final int screenSize = getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK;
43 sIsScreenLarge = screenSize == Configuration.SCREENLAYOUT_SIZE_LARGE ||
44 screenSize == Configuration.SCREENLAYOUT_SIZE_XLARGE;
Patrick Dubroy8e58e912010-10-14 13:21:48 -070045 sScreenDensity = getResources().getDisplayMetrics().density;
Joe Onorato0589f0f2010-02-08 13:44:00 -080046
Michael Jurkac9a96192010-11-01 11:52:08 -070047 mIconCache = new IconCache(this);
48 mModel = new LauncherModel(this, mIconCache);
49
Joe Onorato9c1289c2009-08-17 11:03:03 -040050 // Register intent receivers
51 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
52 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
53 filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
54 filter.addDataScheme("package");
Joe Onoratof99f8c12009-10-31 17:27:36 -040055 registerReceiver(mModel, filter);
Joe Onorato64e6be72010-03-05 15:05:52 -050056 filter = new IntentFilter();
57 filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);
58 filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
Joe Onoratoe9ad59e2010-10-29 17:35:36 -070059 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
Joe Onorato64e6be72010-03-05 15:05:52 -050060 registerReceiver(mModel, filter);
Narayan Kamathcb1a4772011-06-28 13:46:59 +010061 filter = new IntentFilter();
62 filter.addAction(SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED);
63 registerReceiver(mModel, filter);
Joe Onorato9c1289c2009-08-17 11:03:03 -040064
65 // Register for changes to the favorites
66 ContentResolver resolver = getContentResolver();
67 resolver.registerContentObserver(LauncherSettings.Favorites.CONTENT_URI, true,
68 mFavoritesObserver);
69 }
70
71 /**
72 * There's no guarantee that this function is ever called.
73 */
74 @Override
75 public void onTerminate() {
76 super.onTerminate();
77
Joe Onoratof99f8c12009-10-31 17:27:36 -040078 unregisterReceiver(mModel);
Joe Onorato9c1289c2009-08-17 11:03:03 -040079
80 ContentResolver resolver = getContentResolver();
81 resolver.unregisterContentObserver(mFavoritesObserver);
82 }
83
84 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -040085 * Receives notifications whenever the user favorites have changed.
86 */
87 private final ContentObserver mFavoritesObserver = new ContentObserver(new Handler()) {
88 @Override
89 public void onChange(boolean selfChange) {
Joe Onoratof99f8c12009-10-31 17:27:36 -040090 mModel.startLoader(LauncherApplication.this, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -040091 }
92 };
93
94 LauncherModel setLauncher(Launcher launcher) {
Joe Onoratof99f8c12009-10-31 17:27:36 -040095 mModel.initialize(launcher);
96 return mModel;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080097 }
Joe Onorato0589f0f2010-02-08 13:44:00 -080098
99 IconCache getIconCache() {
100 return mIconCache;
101 }
102
103 LauncherModel getModel() {
104 return mModel;
105 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700106
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700107 void setLauncherProvider(LauncherProvider provider) {
108 mLauncherProvider = new WeakReference<LauncherProvider>(provider);
109 }
110
111 LauncherProvider getLauncherProvider() {
112 return mLauncherProvider.get();
113 }
114
Michael Jurkaa2eb1702011-05-12 14:57:05 -0700115 public static boolean isScreenLarge() {
116 return sIsScreenLarge;
Winson Chungaafa03c2010-06-11 17:34:16 -0700117 }
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700118
119 public static float getScreenDensity() {
120 return sScreenDensity;
121 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800122}