blob: 29c93bef2815012c57271330d75d84035f060912 [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;
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
Andrew Flynn0dca1ec2012-02-29 13:33:22 -080045 sIsScreenLarge = getResources().getBoolean(R.bool.is_large_screen);
Patrick Dubroy8e58e912010-10-14 13:21:48 -070046 sScreenDensity = getResources().getDisplayMetrics().density;
Joe Onorato0589f0f2010-02-08 13:44:00 -080047
Michael Jurkac9a96192010-11-01 11:52:08 -070048 mIconCache = new IconCache(this);
49 mModel = new LauncherModel(this, mIconCache);
50
Joe Onorato9c1289c2009-08-17 11:03:03 -040051 // Register intent receivers
52 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
53 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
54 filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
55 filter.addDataScheme("package");
Joe Onoratof99f8c12009-10-31 17:27:36 -040056 registerReceiver(mModel, filter);
Joe Onorato64e6be72010-03-05 15:05:52 -050057 filter = new IntentFilter();
58 filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);
59 filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
Joe Onoratoe9ad59e2010-10-29 17:35:36 -070060 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
Reena Lee93f824a2011-09-23 17:20:28 -070061 filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
Joe Onorato64e6be72010-03-05 15:05:52 -050062 registerReceiver(mModel, filter);
Narayan Kamathcb1a4772011-06-28 13:46:59 +010063 filter = new IntentFilter();
64 filter.addAction(SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED);
65 registerReceiver(mModel, filter);
Winson Chungcbf7c4d2011-08-23 11:58:54 -070066 filter = new IntentFilter();
67 filter.addAction(SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED);
68 registerReceiver(mModel, filter);
Joe Onorato9c1289c2009-08-17 11:03:03 -040069
70 // Register for changes to the favorites
71 ContentResolver resolver = getContentResolver();
72 resolver.registerContentObserver(LauncherSettings.Favorites.CONTENT_URI, true,
73 mFavoritesObserver);
74 }
75
76 /**
77 * There's no guarantee that this function is ever called.
78 */
79 @Override
80 public void onTerminate() {
81 super.onTerminate();
82
Joe Onoratof99f8c12009-10-31 17:27:36 -040083 unregisterReceiver(mModel);
Joe Onorato9c1289c2009-08-17 11:03:03 -040084
85 ContentResolver resolver = getContentResolver();
86 resolver.unregisterContentObserver(mFavoritesObserver);
87 }
88
89 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -040090 * Receives notifications whenever the user favorites have changed.
91 */
92 private final ContentObserver mFavoritesObserver = new ContentObserver(new Handler()) {
93 @Override
94 public void onChange(boolean selfChange) {
Joe Onoratof99f8c12009-10-31 17:27:36 -040095 mModel.startLoader(LauncherApplication.this, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -040096 }
97 };
98
99 LauncherModel setLauncher(Launcher launcher) {
Joe Onoratof99f8c12009-10-31 17:27:36 -0400100 mModel.initialize(launcher);
101 return mModel;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800102 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800103
104 IconCache getIconCache() {
105 return mIconCache;
106 }
107
108 LauncherModel getModel() {
109 return mModel;
110 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700111
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700112 void setLauncherProvider(LauncherProvider provider) {
113 mLauncherProvider = new WeakReference<LauncherProvider>(provider);
114 }
115
116 LauncherProvider getLauncherProvider() {
117 return mLauncherProvider.get();
118 }
119
Michael Jurkaa2eb1702011-05-12 14:57:05 -0700120 public static boolean isScreenLarge() {
121 return sIsScreenLarge;
Winson Chungaafa03c2010-06-11 17:34:16 -0700122 }
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700123
Winson Chung4afe9b32011-07-27 17:46:20 -0700124 public static boolean isScreenLandscape(Context context) {
125 return context.getResources().getConfiguration().orientation ==
126 Configuration.ORIENTATION_LANDSCAPE;
127 }
128
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700129 public static float getScreenDensity() {
130 return sScreenDensity;
131 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132}