blob: ef1eb5fcdd5a5a3881c95b9641528b128105a995 [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;
Winson Chung88f33452012-02-23 15:23:44 -080028import android.view.MotionEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029
Andrew Flynn0dca1ec2012-02-29 13:33:22 -080030import com.android.launcher.R;
31
Michael Jurkaa8c760d2011-04-28 14:59:33 -070032import java.lang.ref.WeakReference;
33
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034public class LauncherApplication extends Application {
Joe Onorato0589f0f2010-02-08 13:44:00 -080035 public LauncherModel mModel;
36 public IconCache mIconCache;
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 Jurkac9a96192010-11-01 11:52:08 -070051 mIconCache = new IconCache(this);
52 mModel = new LauncherModel(this, mIconCache);
53
Joe Onorato9c1289c2009-08-17 11:03:03 -040054 // Register intent receivers
55 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
56 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
57 filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
58 filter.addDataScheme("package");
Joe Onoratof99f8c12009-10-31 17:27:36 -040059 registerReceiver(mModel, filter);
Joe Onorato64e6be72010-03-05 15:05:52 -050060 filter = new IntentFilter();
61 filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);
62 filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
Joe Onoratoe9ad59e2010-10-29 17:35:36 -070063 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
Reena Lee93f824a2011-09-23 17:20:28 -070064 filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
Joe Onorato64e6be72010-03-05 15:05:52 -050065 registerReceiver(mModel, filter);
Narayan Kamathcb1a4772011-06-28 13:46:59 +010066 filter = new IntentFilter();
67 filter.addAction(SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED);
68 registerReceiver(mModel, filter);
Winson Chungcbf7c4d2011-08-23 11:58:54 -070069 filter = new IntentFilter();
70 filter.addAction(SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED);
71 registerReceiver(mModel, filter);
Joe Onorato9c1289c2009-08-17 11:03:03 -040072
73 // Register for changes to the favorites
74 ContentResolver resolver = getContentResolver();
75 resolver.registerContentObserver(LauncherSettings.Favorites.CONTENT_URI, true,
76 mFavoritesObserver);
77 }
78
79 /**
80 * There's no guarantee that this function is ever called.
81 */
82 @Override
83 public void onTerminate() {
84 super.onTerminate();
85
Joe Onoratof99f8c12009-10-31 17:27:36 -040086 unregisterReceiver(mModel);
Joe Onorato9c1289c2009-08-17 11:03:03 -040087
88 ContentResolver resolver = getContentResolver();
89 resolver.unregisterContentObserver(mFavoritesObserver);
90 }
91
92 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -040093 * Receives notifications whenever the user favorites have changed.
94 */
95 private final ContentObserver mFavoritesObserver = new ContentObserver(new Handler()) {
96 @Override
97 public void onChange(boolean selfChange) {
Winson Chungf0c6ae02012-03-21 16:10:31 -070098 // If the database has ever changed, then we really need to force a reload of the
99 // workspace on the next load
100 mModel.resetLoadedState(false, true);
101 mModel.startLoaderFromBackground();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400102 }
103 };
104
105 LauncherModel setLauncher(Launcher launcher) {
Joe Onoratof99f8c12009-10-31 17:27:36 -0400106 mModel.initialize(launcher);
107 return mModel;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800108 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800109
110 IconCache getIconCache() {
111 return mIconCache;
112 }
113
114 LauncherModel getModel() {
115 return mModel;
116 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700117
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700118 void setLauncherProvider(LauncherProvider provider) {
119 mLauncherProvider = new WeakReference<LauncherProvider>(provider);
120 }
121
122 LauncherProvider getLauncherProvider() {
123 return mLauncherProvider.get();
124 }
125
Winson Chungf0c6ae02012-03-21 16:10:31 -0700126 public static String getSharedPreferencesKey() {
127 return sSharedPreferencesKey;
128 }
129
Michael Jurkaa2eb1702011-05-12 14:57:05 -0700130 public static boolean isScreenLarge() {
131 return sIsScreenLarge;
Winson Chungaafa03c2010-06-11 17:34:16 -0700132 }
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700133
Winson Chung4afe9b32011-07-27 17:46:20 -0700134 public static boolean isScreenLandscape(Context context) {
135 return context.getResources().getConfiguration().orientation ==
136 Configuration.ORIENTATION_LANDSCAPE;
137 }
138
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700139 public static float getScreenDensity() {
140 return sScreenDensity;
141 }
Winson Chung88f33452012-02-23 15:23:44 -0800142
143 public static int getLongPressTimeout() {
144 return sLongPressTimeout;
145 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800146}