blob: e69c5ab0b04b80fe6cb14d03bf471770de18fcf6 [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
Michael Jurkaa8c760d2011-04-28 14:59:33 -070030import java.lang.ref.WeakReference;
31
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032public class LauncherApplication extends Application {
Joe Onorato0589f0f2010-02-08 13:44:00 -080033 public LauncherModel mModel;
34 public IconCache mIconCache;
Michael Jurkaa2eb1702011-05-12 14:57:05 -070035 private static boolean sIsScreenLarge;
Patrick Dubroy8e58e912010-10-14 13:21:48 -070036 private static float sScreenDensity;
Winson Chung88f33452012-02-23 15:23:44 -080037 private static int sLongPressTimeout = 300;
Michael Jurkaa8c760d2011-04-28 14:59:33 -070038 WeakReference<LauncherProvider> mLauncherProvider;
Joe Onorato9c1289c2009-08-17 11:03:03 -040039
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040 @Override
41 public void onCreate() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042 super.onCreate();
Joe Onorato9c1289c2009-08-17 11:03:03 -040043
Michael Jurkac9a96192010-11-01 11:52:08 -070044 // set sIsScreenXLarge and sScreenDensity *before* creating icon cache
Adam Cohen3371da02011-10-25 21:38:29 -070045 final int screenSize = getResources().getConfiguration().screenLayout &
46 Configuration.SCREENLAYOUT_SIZE_MASK;
Michael Jurkaa2eb1702011-05-12 14:57:05 -070047 sIsScreenLarge = screenSize == Configuration.SCREENLAYOUT_SIZE_LARGE ||
48 screenSize == Configuration.SCREENLAYOUT_SIZE_XLARGE;
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) {
Joe Onoratof99f8c12009-10-31 17:27:36 -040098 mModel.startLoader(LauncherApplication.this, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -040099 }
100 };
101
102 LauncherModel setLauncher(Launcher launcher) {
Joe Onoratof99f8c12009-10-31 17:27:36 -0400103 mModel.initialize(launcher);
104 return mModel;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800105 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800106
107 IconCache getIconCache() {
108 return mIconCache;
109 }
110
111 LauncherModel getModel() {
112 return mModel;
113 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700114
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700115 void setLauncherProvider(LauncherProvider provider) {
116 mLauncherProvider = new WeakReference<LauncherProvider>(provider);
117 }
118
119 LauncherProvider getLauncherProvider() {
120 return mLauncherProvider.get();
121 }
122
Michael Jurkaa2eb1702011-05-12 14:57:05 -0700123 public static boolean isScreenLarge() {
124 return sIsScreenLarge;
Winson Chungaafa03c2010-06-11 17:34:16 -0700125 }
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700126
Winson Chung4afe9b32011-07-27 17:46:20 -0700127 public static boolean isScreenLandscape(Context context) {
128 return context.getResources().getConfiguration().orientation ==
129 Configuration.ORIENTATION_LANDSCAPE;
130 }
131
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700132 public static float getScreenDensity() {
133 return sScreenDensity;
134 }
Winson Chung88f33452012-02-23 15:23:44 -0800135
136 public static int getLongPressTimeout() {
137 return sLongPressTimeout;
138 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139}