blob: 9936ca68257c7702f87137638694bd85f03479d2 [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
Adam Cohen3371da02011-10-25 21:38:29 -070043 final int screenSize = getResources().getConfiguration().screenLayout &
44 Configuration.SCREENLAYOUT_SIZE_MASK;
Michael Jurkaa2eb1702011-05-12 14:57:05 -070045 sIsScreenLarge = screenSize == Configuration.SCREENLAYOUT_SIZE_LARGE ||
46 screenSize == Configuration.SCREENLAYOUT_SIZE_XLARGE;
Patrick Dubroy8e58e912010-10-14 13:21:48 -070047 sScreenDensity = getResources().getDisplayMetrics().density;
Joe Onorato0589f0f2010-02-08 13:44:00 -080048
Michael Jurkac9a96192010-11-01 11:52:08 -070049 mIconCache = new IconCache(this);
50 mModel = new LauncherModel(this, mIconCache);
51
Joe Onorato9c1289c2009-08-17 11:03:03 -040052 // Register intent receivers
53 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
54 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
55 filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
56 filter.addDataScheme("package");
Joe Onoratof99f8c12009-10-31 17:27:36 -040057 registerReceiver(mModel, filter);
Joe Onorato64e6be72010-03-05 15:05:52 -050058 filter = new IntentFilter();
59 filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);
60 filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
Joe Onoratoe9ad59e2010-10-29 17:35:36 -070061 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
Reena Lee93f824a2011-09-23 17:20:28 -070062 filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
Joe Onorato64e6be72010-03-05 15:05:52 -050063 registerReceiver(mModel, filter);
Narayan Kamathcb1a4772011-06-28 13:46:59 +010064 filter = new IntentFilter();
65 filter.addAction(SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED);
66 registerReceiver(mModel, filter);
Winson Chungcbf7c4d2011-08-23 11:58:54 -070067 filter = new IntentFilter();
68 filter.addAction(SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED);
69 registerReceiver(mModel, filter);
Joe Onorato9c1289c2009-08-17 11:03:03 -040070
71 // Register for changes to the favorites
72 ContentResolver resolver = getContentResolver();
73 resolver.registerContentObserver(LauncherSettings.Favorites.CONTENT_URI, true,
74 mFavoritesObserver);
75 }
76
77 /**
78 * There's no guarantee that this function is ever called.
79 */
80 @Override
81 public void onTerminate() {
82 super.onTerminate();
83
Joe Onoratof99f8c12009-10-31 17:27:36 -040084 unregisterReceiver(mModel);
Joe Onorato9c1289c2009-08-17 11:03:03 -040085
86 ContentResolver resolver = getContentResolver();
87 resolver.unregisterContentObserver(mFavoritesObserver);
88 }
89
90 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -040091 * Receives notifications whenever the user favorites have changed.
92 */
93 private final ContentObserver mFavoritesObserver = new ContentObserver(new Handler()) {
94 @Override
95 public void onChange(boolean selfChange) {
Joe Onoratof99f8c12009-10-31 17:27:36 -040096 mModel.startLoader(LauncherApplication.this, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -040097 }
98 };
99
100 LauncherModel setLauncher(Launcher launcher) {
Joe Onoratof99f8c12009-10-31 17:27:36 -0400101 mModel.initialize(launcher);
102 return mModel;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800103 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800104
105 IconCache getIconCache() {
106 return mIconCache;
107 }
108
109 LauncherModel getModel() {
110 return mModel;
111 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700112
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700113 void setLauncherProvider(LauncherProvider provider) {
114 mLauncherProvider = new WeakReference<LauncherProvider>(provider);
115 }
116
117 LauncherProvider getLauncherProvider() {
118 return mLauncherProvider.get();
119 }
120
Michael Jurkaa2eb1702011-05-12 14:57:05 -0700121 public static boolean isScreenLarge() {
122 return sIsScreenLarge;
Winson Chungaafa03c2010-06-11 17:34:16 -0700123 }
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700124
Winson Chung4afe9b32011-07-27 17:46:20 -0700125 public static boolean isScreenLandscape(Context context) {
126 return context.getResources().getConfiguration().orientation ==
127 Configuration.ORIENTATION_LANDSCAPE;
128 }
129
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700130 public static float getScreenDensity() {
131 return sScreenDensity;
132 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800133}