blob: ba028efea4c6edd04e250510545e460bae733b42 [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
Michael Jurkaa8c760d2011-04-28 14:59:33 -070029import java.lang.ref.WeakReference;
30
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031public class LauncherApplication extends Application {
Joe Onorato0589f0f2010-02-08 13:44:00 -080032 public LauncherModel mModel;
33 public IconCache mIconCache;
Michael Jurkaa2eb1702011-05-12 14:57:05 -070034 private static boolean sIsScreenLarge;
Patrick Dubroy8e58e912010-10-14 13:21:48 -070035 private static float sScreenDensity;
Michael Jurkaa8c760d2011-04-28 14:59:33 -070036 WeakReference<LauncherProvider> mLauncherProvider;
Joe Onorato9c1289c2009-08-17 11:03:03 -040037
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038 @Override
39 public void onCreate() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040 super.onCreate();
Joe Onorato9c1289c2009-08-17 11:03:03 -040041
Michael Jurkac9a96192010-11-01 11:52:08 -070042 // set sIsScreenXLarge and sScreenDensity *before* creating icon cache
Michael Jurkaa2eb1702011-05-12 14:57:05 -070043 final int screenSize = getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK;
44 sIsScreenLarge = screenSize == Configuration.SCREENLAYOUT_SIZE_LARGE ||
45 screenSize == Configuration.SCREENLAYOUT_SIZE_XLARGE;
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);
Joe Onorato64e6be72010-03-05 15:05:52 -050061 registerReceiver(mModel, filter);
Narayan Kamathcb1a4772011-06-28 13:46:59 +010062 filter = new IntentFilter();
63 filter.addAction(SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED);
64 registerReceiver(mModel, filter);
Winson Chungcbf7c4d2011-08-23 11:58:54 -070065 filter = new IntentFilter();
66 filter.addAction(SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED);
67 registerReceiver(mModel, filter);
Joe Onorato9c1289c2009-08-17 11:03:03 -040068
69 // Register for changes to the favorites
70 ContentResolver resolver = getContentResolver();
71 resolver.registerContentObserver(LauncherSettings.Favorites.CONTENT_URI, true,
72 mFavoritesObserver);
73 }
74
75 /**
76 * There's no guarantee that this function is ever called.
77 */
78 @Override
79 public void onTerminate() {
80 super.onTerminate();
81
Joe Onoratof99f8c12009-10-31 17:27:36 -040082 unregisterReceiver(mModel);
Joe Onorato9c1289c2009-08-17 11:03:03 -040083
84 ContentResolver resolver = getContentResolver();
85 resolver.unregisterContentObserver(mFavoritesObserver);
86 }
87
88 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -040089 * Receives notifications whenever the user favorites have changed.
90 */
91 private final ContentObserver mFavoritesObserver = new ContentObserver(new Handler()) {
92 @Override
93 public void onChange(boolean selfChange) {
Joe Onoratof99f8c12009-10-31 17:27:36 -040094 mModel.startLoader(LauncherApplication.this, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -040095 }
96 };
97
98 LauncherModel setLauncher(Launcher launcher) {
Joe Onoratof99f8c12009-10-31 17:27:36 -040099 mModel.initialize(launcher);
100 return mModel;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800101 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800102
103 IconCache getIconCache() {
104 return mIconCache;
105 }
106
107 LauncherModel getModel() {
108 return mModel;
109 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700110
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700111 void setLauncherProvider(LauncherProvider provider) {
112 mLauncherProvider = new WeakReference<LauncherProvider>(provider);
113 }
114
115 LauncherProvider getLauncherProvider() {
116 return mLauncherProvider.get();
117 }
118
Michael Jurkaa2eb1702011-05-12 14:57:05 -0700119 public static boolean isScreenLarge() {
120 return sIsScreenLarge;
Winson Chungaafa03c2010-06-11 17:34:16 -0700121 }
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700122
Winson Chung4afe9b32011-07-27 17:46:20 -0700123 public static boolean isScreenLandscape(Context context) {
124 return context.getResources().getConfiguration().orientation ==
125 Configuration.ORIENTATION_LANDSCAPE;
126 }
127
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700128 public static float getScreenDensity() {
129 return sScreenDensity;
130 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131}