blob: 9b2458758c2548958e8d61adbd7379ba8cb72c8c [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;
Winson Chung4afe9b32011-07-27 17:46:20 -070022import android.content.Context;
Joe Onorato9c1289c2009-08-17 11:03:03 -040023import android.content.Intent;
24import android.content.IntentFilter;
Winson Chungaafa03c2010-06-11 17:34:16 -070025import android.content.res.Configuration;
Joe Onorato9c1289c2009-08-17 11:03:03 -040026import android.database.ContentObserver;
27import android.os.Handler;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028
Michael Jurkaa8c760d2011-04-28 14:59:33 -070029import java.lang.ref.WeakReference;
30
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031public class LauncherApplication extends Application {
Joe Onorato0589f0f2010-02-08 13:44:00 -080032 public LauncherModel mModel;
33 public IconCache mIconCache;
Michael Jurkaa2eb1702011-05-12 14:57:05 -070034 private static boolean sIsScreenLarge;
Patrick Dubroy8e58e912010-10-14 13:21:48 -070035 private static float sScreenDensity;
Michael Jurkaa8c760d2011-04-28 14:59:33 -070036 WeakReference<LauncherProvider> mLauncherProvider;
Joe Onorato9c1289c2009-08-17 11:03:03 -040037
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038 @Override
39 public void onCreate() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040 super.onCreate();
Joe Onorato9c1289c2009-08-17 11:03:03 -040041
Michael Jurkac9a96192010-11-01 11:52:08 -070042 // set sIsScreenXLarge and sScreenDensity *before* creating icon cache
Michael Jurkaa2eb1702011-05-12 14:57:05 -070043 final int screenSize = getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK;
44 sIsScreenLarge = screenSize == Configuration.SCREENLAYOUT_SIZE_LARGE ||
45 screenSize == Configuration.SCREENLAYOUT_SIZE_XLARGE;
Patrick Dubroy8e58e912010-10-14 13:21:48 -070046 sScreenDensity = getResources().getDisplayMetrics().density;
Joe Onorato0589f0f2010-02-08 13:44:00 -080047
Michael Jurkac9a96192010-11-01 11:52:08 -070048 mIconCache = new IconCache(this);
49 mModel = new LauncherModel(this, mIconCache);
50
Joe Onorato9c1289c2009-08-17 11:03:03 -040051 // 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");
Joe Onoratof99f8c12009-10-31 17:27:36 -040056 registerReceiver(mModel, filter);
Joe Onorato64e6be72010-03-05 15:05:52 -050057 filter = new IntentFilter();
58 filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);
59 filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
Joe Onoratoe9ad59e2010-10-29 17:35:36 -070060 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
Joe Onorato64e6be72010-03-05 15:05:52 -050061 registerReceiver(mModel, filter);
Narayan Kamathcb1a4772011-06-28 13:46:59 +010062 filter = new IntentFilter();
63 filter.addAction(SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED);
64 registerReceiver(mModel, filter);
Joe Onorato9c1289c2009-08-17 11:03:03 -040065
66 // Register for changes to the favorites
67 ContentResolver resolver = getContentResolver();
68 resolver.registerContentObserver(LauncherSettings.Favorites.CONTENT_URI, true,
69 mFavoritesObserver);
70 }
71
72 /**
73 * There's no guarantee that this function is ever called.
74 */
75 @Override
76 public void onTerminate() {
77 super.onTerminate();
78
Joe Onoratof99f8c12009-10-31 17:27:36 -040079 unregisterReceiver(mModel);
Joe Onorato9c1289c2009-08-17 11:03:03 -040080
81 ContentResolver resolver = getContentResolver();
82 resolver.unregisterContentObserver(mFavoritesObserver);
83 }
84
85 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -040086 * Receives notifications whenever the user favorites have changed.
87 */
88 private final ContentObserver mFavoritesObserver = new ContentObserver(new Handler()) {
89 @Override
90 public void onChange(boolean selfChange) {
Joe Onoratof99f8c12009-10-31 17:27:36 -040091 mModel.startLoader(LauncherApplication.this, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -040092 }
93 };
94
95 LauncherModel setLauncher(Launcher launcher) {
Joe Onoratof99f8c12009-10-31 17:27:36 -040096 mModel.initialize(launcher);
97 return mModel;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080098 }
Joe Onorato0589f0f2010-02-08 13:44:00 -080099
100 IconCache getIconCache() {
101 return mIconCache;
102 }
103
104 LauncherModel getModel() {
105 return mModel;
106 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700107
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700108 void setLauncherProvider(LauncherProvider provider) {
109 mLauncherProvider = new WeakReference<LauncherProvider>(provider);
110 }
111
112 LauncherProvider getLauncherProvider() {
113 return mLauncherProvider.get();
114 }
115
Michael Jurkaa2eb1702011-05-12 14:57:05 -0700116 public static boolean isScreenLarge() {
117 return sIsScreenLarge;
Winson Chungaafa03c2010-06-11 17:34:16 -0700118 }
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700119
Winson Chung4afe9b32011-07-27 17:46:20 -0700120 public static boolean isScreenLandscape(Context context) {
121 return context.getResources().getConfiguration().orientation ==
122 Configuration.ORIENTATION_LANDSCAPE;
123 }
124
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700125 public static float getScreenDensity() {
126 return sScreenDensity;
127 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800128}