blob: fb49d93986d3ae9616a33406574d1e9195f32d90 [file] [log] [blame]
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001/*
2 * Copyright (C) 2013 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
17package com.android.launcher3;
18
19import android.app.SearchManager;
20import android.content.ContentResolver;
21import android.content.Context;
22import android.content.Intent;
23import android.content.IntentFilter;
24import android.content.res.Configuration;
25import android.database.ContentObserver;
26import android.os.Handler;
27
28import java.lang.ref.WeakReference;
29
30public class LauncherAppState {
31 private Context mContext;
32 private LauncherModel mModel;
33 private IconCache mIconCache;
34 private WidgetPreviewLoader.CacheDb mWidgetPreviewCacheDb;
35 private static boolean sIsScreenLarge;
36 private static float sScreenDensity;
37 private static int sLongPressTimeout = 300;
38 private static final String sSharedPreferencesKey = "com.android.launcher3.prefs";
39 WeakReference<LauncherProvider> mLauncherProvider;
40
41 private static LauncherAppState INSTANCE;
42
43 private static final LauncherAppState INSTANCE = new LauncherAppState();
44
45 public static LauncherAppState getInstance() {
46 return INSTANCE;
47 }
48
49 public static void init(Context context) {
50 INSTANCE.initialize(context);
51 }
52
53 public Context getContext() {
54 return mContext;
55 }
56
57 private LauncherAppState() { }
58
59 private void initialize(Context context) {
60 mContext = context;
61
62 // set sIsScreenXLarge and sScreenDensity *before* creating icon cache
63 sIsScreenLarge = context.getResources().getBoolean(R.bool.is_large_screen);
64 sScreenDensity = context.getResources().getDisplayMetrics().density;
65
66 mWidgetPreviewCacheDb = new WidgetPreviewLoader.CacheDb(context);
67 mIconCache = new IconCache(context);
68 mModel = new LauncherModel(context, mIconCache);
69
70 // Register intent receivers
71 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
72 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
73 filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
74 filter.addDataScheme("package");
75 context.registerReceiver(mModel, filter);
76 filter = new IntentFilter();
77 filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);
78 filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
79 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
80 filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
81 context.registerReceiver(mModel, filter);
82 filter = new IntentFilter();
83 filter.addAction(SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED);
84 context.registerReceiver(mModel, filter);
85 filter = new IntentFilter();
86 filter.addAction(SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED);
87 context.registerReceiver(mModel, filter);
88
89 // Register for changes to the favorites
90 ContentResolver resolver = context.getContentResolver();
91 resolver.registerContentObserver(LauncherSettings.Favorites.CONTENT_URI, true,
92 mFavoritesObserver);
93 }
94
95 /**
96 * Call from Application.onTerminate(), which is not guaranteed to ever be called.
97 */
98 public void onTerminate() {
99 mContext.unregisterReceiver(mModel);
100
101 ContentResolver resolver = mContext.getContentResolver();
102 resolver.unregisterContentObserver(mFavoritesObserver);
103 }
104
105 /**
106 * Receives notifications whenever the user favorites have changed.
107 */
108 private final ContentObserver mFavoritesObserver = new ContentObserver(new Handler()) {
109 @Override
110 public void onChange(boolean selfChange) {
111 // If the database has ever changed, then we really need to force a reload of the
112 // workspace on the next load
113 mModel.resetLoadedState(false, true);
114 mModel.startLoaderFromBackground();
115 }
116 };
117
118 LauncherModel setLauncher(Launcher launcher) {
119 mModel.initialize(launcher);
120 return mModel;
121 }
122
123 IconCache getIconCache() {
124 return mIconCache;
125 }
126
127 LauncherModel getModel() {
128 return mModel;
129 }
130
131 WidgetPreviewLoader.CacheDb getWidgetPreviewCacheDb() {
132 return mWidgetPreviewCacheDb;
133 }
134
135 void setLauncherProvider(LauncherProvider provider) {
136 mLauncherProvider = new WeakReference<LauncherProvider>(provider);
137 }
138
139 LauncherProvider getLauncherProvider() {
140 return mLauncherProvider.get();
141 }
142
143 public static String getSharedPreferencesKey() {
144 return sSharedPreferencesKey;
145 }
146
147 public static boolean isScreenLarge() {
148 return sIsScreenLarge;
149 }
150
151 public static boolean isScreenLandscape(Context context) {
152 return context.getResources().getConfiguration().orientation ==
153 Configuration.ORIENTATION_LANDSCAPE;
154 }
155
156 public static float getScreenDensity() {
157 return sScreenDensity;
158 }
159
160 public static int getLongPressTimeout() {
161 return sLongPressTimeout;
162 }
163}