The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1 | /* |
| 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 Onorato | a590252 | 2009-07-30 13:37:37 -0700 | [diff] [blame] | 17 | package com.android.launcher2; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 18 | |
| 19 | import android.app.Application; |
Narayan Kamath | cb1a477 | 2011-06-28 13:46:59 +0100 | [diff] [blame] | 20 | import android.app.SearchManager; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 21 | import android.content.ContentResolver; |
Winson Chung | 4afe9b3 | 2011-07-27 17:46:20 -0700 | [diff] [blame] | 22 | import android.content.Context; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 23 | import android.content.Intent; |
| 24 | import android.content.IntentFilter; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 25 | import android.content.res.Configuration; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 26 | import android.database.ContentObserver; |
| 27 | import android.os.Handler; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 28 | |
Andrew Flynn | 0dca1ec | 2012-02-29 13:33:22 -0800 | [diff] [blame] | 29 | import com.android.launcher.R; |
| 30 | |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 31 | import java.lang.ref.WeakReference; |
| 32 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 33 | public class LauncherApplication extends Application { |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 34 | public LauncherModel mModel; |
| 35 | public IconCache mIconCache; |
Michael Jurka | a2eb170 | 2011-05-12 14:57:05 -0700 | [diff] [blame] | 36 | private static boolean sIsScreenLarge; |
Patrick Dubroy | 8e58e91 | 2010-10-14 13:21:48 -0700 | [diff] [blame] | 37 | private static float sScreenDensity; |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 38 | private static int sLongPressTimeout = 300; |
Winson Chung | f0c6ae0 | 2012-03-21 16:10:31 -0700 | [diff] [blame] | 39 | private static final String sSharedPreferencesKey = "com.android.launcher2.prefs"; |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 40 | WeakReference<LauncherProvider> mLauncherProvider; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 41 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 42 | @Override |
| 43 | public void onCreate() { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 44 | super.onCreate(); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 45 | |
Michael Jurka | c9a9619 | 2010-11-01 11:52:08 -0700 | [diff] [blame] | 46 | // set sIsScreenXLarge and sScreenDensity *before* creating icon cache |
Andrew Flynn | 0dca1ec | 2012-02-29 13:33:22 -0800 | [diff] [blame] | 47 | sIsScreenLarge = getResources().getBoolean(R.bool.is_large_screen); |
Patrick Dubroy | 8e58e91 | 2010-10-14 13:21:48 -0700 | [diff] [blame] | 48 | sScreenDensity = getResources().getDisplayMetrics().density; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 49 | |
Michael Jurka | c9a9619 | 2010-11-01 11:52:08 -0700 | [diff] [blame] | 50 | mIconCache = new IconCache(this); |
| 51 | mModel = new LauncherModel(this, mIconCache); |
| 52 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 53 | // Register intent receivers |
| 54 | IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED); |
| 55 | filter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
| 56 | filter.addAction(Intent.ACTION_PACKAGE_CHANGED); |
| 57 | filter.addDataScheme("package"); |
Joe Onorato | f99f8c1 | 2009-10-31 17:27:36 -0400 | [diff] [blame] | 58 | registerReceiver(mModel, filter); |
Joe Onorato | 64e6be7 | 2010-03-05 15:05:52 -0500 | [diff] [blame] | 59 | filter = new IntentFilter(); |
| 60 | filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE); |
| 61 | filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE); |
Joe Onorato | e9ad59e | 2010-10-29 17:35:36 -0700 | [diff] [blame] | 62 | filter.addAction(Intent.ACTION_LOCALE_CHANGED); |
Reena Lee | 93f824a | 2011-09-23 17:20:28 -0700 | [diff] [blame] | 63 | filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED); |
Joe Onorato | 64e6be7 | 2010-03-05 15:05:52 -0500 | [diff] [blame] | 64 | registerReceiver(mModel, filter); |
Narayan Kamath | cb1a477 | 2011-06-28 13:46:59 +0100 | [diff] [blame] | 65 | filter = new IntentFilter(); |
| 66 | filter.addAction(SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED); |
| 67 | registerReceiver(mModel, filter); |
Winson Chung | cbf7c4d | 2011-08-23 11:58:54 -0700 | [diff] [blame] | 68 | filter = new IntentFilter(); |
| 69 | filter.addAction(SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED); |
| 70 | registerReceiver(mModel, filter); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 71 | |
| 72 | // Register for changes to the favorites |
| 73 | ContentResolver resolver = getContentResolver(); |
| 74 | resolver.registerContentObserver(LauncherSettings.Favorites.CONTENT_URI, true, |
| 75 | mFavoritesObserver); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * There's no guarantee that this function is ever called. |
| 80 | */ |
| 81 | @Override |
| 82 | public void onTerminate() { |
| 83 | super.onTerminate(); |
| 84 | |
Joe Onorato | f99f8c1 | 2009-10-31 17:27:36 -0400 | [diff] [blame] | 85 | unregisterReceiver(mModel); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 86 | |
| 87 | ContentResolver resolver = getContentResolver(); |
| 88 | resolver.unregisterContentObserver(mFavoritesObserver); |
| 89 | } |
| 90 | |
| 91 | /** |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 92 | * Receives notifications whenever the user favorites have changed. |
| 93 | */ |
| 94 | private final ContentObserver mFavoritesObserver = new ContentObserver(new Handler()) { |
| 95 | @Override |
| 96 | public void onChange(boolean selfChange) { |
Winson Chung | f0c6ae0 | 2012-03-21 16:10:31 -0700 | [diff] [blame] | 97 | // If the database has ever changed, then we really need to force a reload of the |
| 98 | // workspace on the next load |
| 99 | mModel.resetLoadedState(false, true); |
| 100 | mModel.startLoaderFromBackground(); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 101 | } |
| 102 | }; |
| 103 | |
| 104 | LauncherModel setLauncher(Launcher launcher) { |
Joe Onorato | f99f8c1 | 2009-10-31 17:27:36 -0400 | [diff] [blame] | 105 | mModel.initialize(launcher); |
| 106 | return mModel; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 107 | } |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 108 | |
| 109 | IconCache getIconCache() { |
| 110 | return mIconCache; |
| 111 | } |
| 112 | |
| 113 | LauncherModel getModel() { |
| 114 | return mModel; |
| 115 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 116 | |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 117 | void setLauncherProvider(LauncherProvider provider) { |
| 118 | mLauncherProvider = new WeakReference<LauncherProvider>(provider); |
| 119 | } |
| 120 | |
| 121 | LauncherProvider getLauncherProvider() { |
| 122 | return mLauncherProvider.get(); |
| 123 | } |
| 124 | |
Winson Chung | f0c6ae0 | 2012-03-21 16:10:31 -0700 | [diff] [blame] | 125 | public static String getSharedPreferencesKey() { |
| 126 | return sSharedPreferencesKey; |
| 127 | } |
| 128 | |
Michael Jurka | a2eb170 | 2011-05-12 14:57:05 -0700 | [diff] [blame] | 129 | public static boolean isScreenLarge() { |
| 130 | return sIsScreenLarge; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 131 | } |
Patrick Dubroy | 8e58e91 | 2010-10-14 13:21:48 -0700 | [diff] [blame] | 132 | |
Winson Chung | 4afe9b3 | 2011-07-27 17:46:20 -0700 | [diff] [blame] | 133 | public static boolean isScreenLandscape(Context context) { |
| 134 | return context.getResources().getConfiguration().orientation == |
| 135 | Configuration.ORIENTATION_LANDSCAPE; |
| 136 | } |
| 137 | |
Patrick Dubroy | 8e58e91 | 2010-10-14 13:21:48 -0700 | [diff] [blame] | 138 | public static float getScreenDensity() { |
| 139 | return sScreenDensity; |
| 140 | } |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 141 | |
| 142 | public static int getLongPressTimeout() { |
| 143 | return sLongPressTimeout; |
| 144 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 145 | } |