blob: 28362fd0b426f89425c234836ca1dc1c50636243 [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 {
Joe Onorato0589f0f2010-02-08 13:44:00 -080034 public LauncherModel mModel;
35 public IconCache mIconCache;
Michael Jurkaa2eb1702011-05-12 14:57:05 -070036 private static boolean sIsScreenLarge;
Patrick Dubroy8e58e912010-10-14 13:21:48 -070037 private static float sScreenDensity;
Winson Chung88f33452012-02-23 15:23:44 -080038 private static int sLongPressTimeout = 300;
Winson Chungf0c6ae02012-03-21 16:10:31 -070039 private static final String sSharedPreferencesKey = "com.android.launcher2.prefs";
Michael Jurkaa8c760d2011-04-28 14:59:33 -070040 WeakReference<LauncherProvider> mLauncherProvider;
Joe Onorato9c1289c2009-08-17 11:03:03 -040041
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042 @Override
43 public void onCreate() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080044 super.onCreate();
Joe Onorato9c1289c2009-08-17 11:03:03 -040045
Michael Jurkac9a96192010-11-01 11:52:08 -070046 // set sIsScreenXLarge and sScreenDensity *before* creating icon cache
Andrew Flynn0dca1ec2012-02-29 13:33:22 -080047 sIsScreenLarge = getResources().getBoolean(R.bool.is_large_screen);
Patrick Dubroy8e58e912010-10-14 13:21:48 -070048 sScreenDensity = getResources().getDisplayMetrics().density;
Joe Onorato0589f0f2010-02-08 13:44:00 -080049
Michael Jurkac9a96192010-11-01 11:52:08 -070050 mIconCache = new IconCache(this);
51 mModel = new LauncherModel(this, mIconCache);
52
Joe Onorato9c1289c2009-08-17 11:03:03 -040053 // 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 Onoratof99f8c12009-10-31 17:27:36 -040058 registerReceiver(mModel, filter);
Joe Onorato64e6be72010-03-05 15:05:52 -050059 filter = new IntentFilter();
60 filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);
61 filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
Joe Onoratoe9ad59e2010-10-29 17:35:36 -070062 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
Reena Lee93f824a2011-09-23 17:20:28 -070063 filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
Joe Onorato64e6be72010-03-05 15:05:52 -050064 registerReceiver(mModel, filter);
Narayan Kamathcb1a4772011-06-28 13:46:59 +010065 filter = new IntentFilter();
66 filter.addAction(SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED);
67 registerReceiver(mModel, filter);
Winson Chungcbf7c4d2011-08-23 11:58:54 -070068 filter = new IntentFilter();
69 filter.addAction(SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED);
70 registerReceiver(mModel, filter);
Joe Onorato9c1289c2009-08-17 11:03:03 -040071
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 Onoratof99f8c12009-10-31 17:27:36 -040085 unregisterReceiver(mModel);
Joe Onorato9c1289c2009-08-17 11:03:03 -040086
87 ContentResolver resolver = getContentResolver();
88 resolver.unregisterContentObserver(mFavoritesObserver);
89 }
90
91 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -040092 * 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 Chungf0c6ae02012-03-21 16:10:31 -070097 // 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 Onorato9c1289c2009-08-17 11:03:03 -0400101 }
102 };
103
104 LauncherModel setLauncher(Launcher launcher) {
Joe Onoratof99f8c12009-10-31 17:27:36 -0400105 mModel.initialize(launcher);
106 return mModel;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800107 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800108
109 IconCache getIconCache() {
110 return mIconCache;
111 }
112
113 LauncherModel getModel() {
114 return mModel;
115 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700116
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700117 void setLauncherProvider(LauncherProvider provider) {
118 mLauncherProvider = new WeakReference<LauncherProvider>(provider);
119 }
120
121 LauncherProvider getLauncherProvider() {
122 return mLauncherProvider.get();
123 }
124
Winson Chungf0c6ae02012-03-21 16:10:31 -0700125 public static String getSharedPreferencesKey() {
126 return sSharedPreferencesKey;
127 }
128
Michael Jurkaa2eb1702011-05-12 14:57:05 -0700129 public static boolean isScreenLarge() {
130 return sIsScreenLarge;
Winson Chungaafa03c2010-06-11 17:34:16 -0700131 }
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700132
Winson Chung4afe9b32011-07-27 17:46:20 -0700133 public static boolean isScreenLandscape(Context context) {
134 return context.getResources().getConfiguration().orientation ==
135 Configuration.ORIENTATION_LANDSCAPE;
136 }
137
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700138 public static float getScreenDensity() {
139 return sScreenDensity;
140 }
Winson Chung88f33452012-02-23 15:23:44 -0800141
142 public static int getLongPressTimeout() {
143 return sLongPressTimeout;
144 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800145}