blob: e5f325021913f0928173e0b263e040f76766e8d1 [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
Andrew Flynn0dca1ec2012-02-29 13:33:22 -080029import com.android.launcher.R;
30
Michael Jurkaa8c760d2011-04-28 14:59:33 -070031import java.lang.ref.WeakReference;
32
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033public class LauncherApplication extends Application {
Michael Jurkad9cb4a12013-03-19 12:01:06 +010034 private LauncherModel mModel;
35 private IconCache mIconCache;
36 private WidgetPreviewLoader.CacheDb mWidgetPreviewCacheDb;
Michael Jurkaa2eb1702011-05-12 14:57:05 -070037 private static boolean sIsScreenLarge;
Patrick Dubroy8e58e912010-10-14 13:21:48 -070038 private static float sScreenDensity;
Winson Chung88f33452012-02-23 15:23:44 -080039 private static int sLongPressTimeout = 300;
Winson Chungf0c6ae02012-03-21 16:10:31 -070040 private static final String sSharedPreferencesKey = "com.android.launcher2.prefs";
Michael Jurkaa8c760d2011-04-28 14:59:33 -070041 WeakReference<LauncherProvider> mLauncherProvider;
Joe Onorato9c1289c2009-08-17 11:03:03 -040042
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043 @Override
44 public void onCreate() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045 super.onCreate();
Joe Onorato9c1289c2009-08-17 11:03:03 -040046
Michael Jurkac9a96192010-11-01 11:52:08 -070047 // set sIsScreenXLarge and sScreenDensity *before* creating icon cache
Andrew Flynn0dca1ec2012-02-29 13:33:22 -080048 sIsScreenLarge = getResources().getBoolean(R.bool.is_large_screen);
Patrick Dubroy8e58e912010-10-14 13:21:48 -070049 sScreenDensity = getResources().getDisplayMetrics().density;
Joe Onorato0589f0f2010-02-08 13:44:00 -080050
Michael Jurkad9cb4a12013-03-19 12:01:06 +010051 mWidgetPreviewCacheDb = new WidgetPreviewLoader.CacheDb(this);
Michael Jurkac9a96192010-11-01 11:52:08 -070052 mIconCache = new IconCache(this);
53 mModel = new LauncherModel(this, mIconCache);
54
Joe Onorato9c1289c2009-08-17 11:03:03 -040055 // Register intent receivers
56 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
57 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
58 filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
59 filter.addDataScheme("package");
Joe Onoratof99f8c12009-10-31 17:27:36 -040060 registerReceiver(mModel, filter);
Joe Onorato64e6be72010-03-05 15:05:52 -050061 filter = new IntentFilter();
62 filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);
63 filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
Joe Onoratoe9ad59e2010-10-29 17:35:36 -070064 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
Reena Lee93f824a2011-09-23 17:20:28 -070065 filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
Joe Onorato64e6be72010-03-05 15:05:52 -050066 registerReceiver(mModel, filter);
Narayan Kamathcb1a4772011-06-28 13:46:59 +010067 filter = new IntentFilter();
68 filter.addAction(SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED);
69 registerReceiver(mModel, filter);
Winson Chungcbf7c4d2011-08-23 11:58:54 -070070 filter = new IntentFilter();
71 filter.addAction(SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED);
72 registerReceiver(mModel, filter);
Joe Onorato9c1289c2009-08-17 11:03:03 -040073
74 // Register for changes to the favorites
75 ContentResolver resolver = getContentResolver();
76 resolver.registerContentObserver(LauncherSettings.Favorites.CONTENT_URI, true,
77 mFavoritesObserver);
78 }
79
80 /**
81 * There's no guarantee that this function is ever called.
82 */
83 @Override
84 public void onTerminate() {
85 super.onTerminate();
86
Joe Onoratof99f8c12009-10-31 17:27:36 -040087 unregisterReceiver(mModel);
Joe Onorato9c1289c2009-08-17 11:03:03 -040088
89 ContentResolver resolver = getContentResolver();
90 resolver.unregisterContentObserver(mFavoritesObserver);
91 }
92
93 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -040094 * Receives notifications whenever the user favorites have changed.
95 */
96 private final ContentObserver mFavoritesObserver = new ContentObserver(new Handler()) {
97 @Override
98 public void onChange(boolean selfChange) {
Winson Chungf0c6ae02012-03-21 16:10:31 -070099 // If the database has ever changed, then we really need to force a reload of the
100 // workspace on the next load
101 mModel.resetLoadedState(false, true);
102 mModel.startLoaderFromBackground();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400103 }
104 };
105
106 LauncherModel setLauncher(Launcher launcher) {
Joe Onoratof99f8c12009-10-31 17:27:36 -0400107 mModel.initialize(launcher);
108 return mModel;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800109 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800110
111 IconCache getIconCache() {
112 return mIconCache;
113 }
114
115 LauncherModel getModel() {
116 return mModel;
117 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700118
Michael Jurkad9cb4a12013-03-19 12:01:06 +0100119 WidgetPreviewLoader.CacheDb getWidgetPreviewCacheDb() {
120 return mWidgetPreviewCacheDb;
121 }
122
123 void setLauncherProvider(LauncherProvider provider) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700124 mLauncherProvider = new WeakReference<LauncherProvider>(provider);
125 }
126
127 LauncherProvider getLauncherProvider() {
128 return mLauncherProvider.get();
129 }
130
Winson Chungf0c6ae02012-03-21 16:10:31 -0700131 public static String getSharedPreferencesKey() {
132 return sSharedPreferencesKey;
133 }
134
Michael Jurkaa2eb1702011-05-12 14:57:05 -0700135 public static boolean isScreenLarge() {
136 return sIsScreenLarge;
Winson Chungaafa03c2010-06-11 17:34:16 -0700137 }
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700138
Winson Chung4afe9b32011-07-27 17:46:20 -0700139 public static boolean isScreenLandscape(Context context) {
140 return context.getResources().getConfiguration().orientation ==
141 Configuration.ORIENTATION_LANDSCAPE;
142 }
143
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700144 public static float getScreenDensity() {
145 return sScreenDensity;
146 }
Winson Chung88f33452012-02-23 15:23:44 -0800147
148 public static int getLongPressTimeout() {
149 return sLongPressTimeout;
150 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151}