blob: ea8904f1fc5d51c7db22c9539263a8d4bc072f41 [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;
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040020import android.content.*;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040021import android.content.res.Configuration;
22import android.database.ContentObserver;
23import android.os.Handler;
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040024import android.util.Log;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040025
26import java.lang.ref.WeakReference;
27
28public class LauncherAppState {
Daniel Sandlere4f98912013-06-25 15:13:26 -040029 private static final String SHARED_PREFERENCES_KEY = "com.android.launcher3.prefs";
30
Daniel Sandlercc8befa2013-06-11 14:45:48 -040031 private LauncherModel mModel;
32 private IconCache mIconCache;
33 private WidgetPreviewLoader.CacheDb mWidgetPreviewCacheDb;
Daniel Sandlere4f98912013-06-25 15:13:26 -040034 private boolean mIsScreenLarge;
35 private float mScreenDensity;
36 private int mLongPressTimeout = 300;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040037
Daniel Sandlere4f98912013-06-25 15:13:26 -040038 private static WeakReference<LauncherProvider> sLauncherProvider;
39 private static Context sContext;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040040
Daniel Sandlere4f98912013-06-25 15:13:26 -040041 private static Object mLock = new Object();
42 private static LauncherAppState INSTANCE;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040043
44 public static LauncherAppState getInstance() {
Daniel Sandlere4f98912013-06-25 15:13:26 -040045 if (INSTANCE == null) {
46 INSTANCE = new LauncherAppState();
47 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040048 return INSTANCE;
49 }
50
Daniel Sandlercc8befa2013-06-11 14:45:48 -040051 public Context getContext() {
Daniel Sandlere4f98912013-06-25 15:13:26 -040052 return sContext;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040053 }
54
Daniel Sandlere4f98912013-06-25 15:13:26 -040055 public static void setApplicationContext(Context context) {
56 if (sContext != null) {
57 throw new IllegalStateException("setApplicationContext called twice! old=" + sContext + " new=" + context);
58 }
59 sContext = context.getApplicationContext();
60 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040061
Daniel Sandlere4f98912013-06-25 15:13:26 -040062 private LauncherAppState() {
63 if (sContext == null) {
64 throw new IllegalStateException("LauncherAppState inited before app context set");
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040065 }
66
Daniel Sandlere4f98912013-06-25 15:13:26 -040067 Log.v(Launcher.TAG, "LauncherAppState inited");
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040068
Daniel Sandlere4f98912013-06-25 15:13:26 -040069 if (sContext.getResources().getBoolean(R.bool.debug_memory_enabled)) {
70 MemoryTracker.startTrackingMe(sContext, "L");
71 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040072
Daniel Sandlere4f98912013-06-25 15:13:26 -040073 // set sIsScreenXLarge and mScreenDensity *before* creating icon cache
74 mIsScreenLarge = sContext.getResources().getBoolean(R.bool.is_large_screen);
75 mScreenDensity = sContext.getResources().getDisplayMetrics().density;
76
77 mWidgetPreviewCacheDb = new WidgetPreviewLoader.CacheDb(sContext);
78 mIconCache = new IconCache(sContext);
79 mModel = new LauncherModel(this, mIconCache);
Daniel Sandlercc8befa2013-06-11 14:45:48 -040080
81 // Register intent receivers
82 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
83 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
84 filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
85 filter.addDataScheme("package");
Daniel Sandlere4f98912013-06-25 15:13:26 -040086 sContext.registerReceiver(mModel, filter);
Daniel Sandlercc8befa2013-06-11 14:45:48 -040087 filter = new IntentFilter();
88 filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);
89 filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
90 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
91 filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
Daniel Sandlere4f98912013-06-25 15:13:26 -040092 sContext.registerReceiver(mModel, filter);
Daniel Sandlercc8befa2013-06-11 14:45:48 -040093 filter = new IntentFilter();
94 filter.addAction(SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED);
Daniel Sandlere4f98912013-06-25 15:13:26 -040095 sContext.registerReceiver(mModel, filter);
Daniel Sandlercc8befa2013-06-11 14:45:48 -040096 filter = new IntentFilter();
97 filter.addAction(SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED);
Daniel Sandlere4f98912013-06-25 15:13:26 -040098 sContext.registerReceiver(mModel, filter);
Daniel Sandlercc8befa2013-06-11 14:45:48 -040099
100 // Register for changes to the favorites
Daniel Sandlere4f98912013-06-25 15:13:26 -0400101 ContentResolver resolver = sContext.getContentResolver();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400102 resolver.registerContentObserver(LauncherSettings.Favorites.CONTENT_URI, true,
103 mFavoritesObserver);
104 }
105
106 /**
107 * Call from Application.onTerminate(), which is not guaranteed to ever be called.
108 */
109 public void onTerminate() {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400110 sContext.unregisterReceiver(mModel);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400111
Daniel Sandlere4f98912013-06-25 15:13:26 -0400112 ContentResolver resolver = sContext.getContentResolver();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400113 resolver.unregisterContentObserver(mFavoritesObserver);
114 }
115
116 /**
117 * Receives notifications whenever the user favorites have changed.
118 */
119 private final ContentObserver mFavoritesObserver = new ContentObserver(new Handler()) {
120 @Override
121 public void onChange(boolean selfChange) {
122 // If the database has ever changed, then we really need to force a reload of the
123 // workspace on the next load
124 mModel.resetLoadedState(false, true);
125 mModel.startLoaderFromBackground();
126 }
127 };
128
129 LauncherModel setLauncher(Launcher launcher) {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400130 if (mModel == null) {
131 throw new IllegalStateException("setLauncher() called before init()");
132 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400133 mModel.initialize(launcher);
134 return mModel;
135 }
136
137 IconCache getIconCache() {
138 return mIconCache;
139 }
140
141 LauncherModel getModel() {
142 return mModel;
143 }
144
145 WidgetPreviewLoader.CacheDb getWidgetPreviewCacheDb() {
146 return mWidgetPreviewCacheDb;
147 }
148
Daniel Sandlere4f98912013-06-25 15:13:26 -0400149 static void setLauncherProvider(LauncherProvider provider) {
150 sLauncherProvider = new WeakReference<LauncherProvider>(provider);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400151 }
152
Daniel Sandlere4f98912013-06-25 15:13:26 -0400153 static LauncherProvider getLauncherProvider() {
154 return sLauncherProvider.get();
Daniel Sandler924b9932013-06-12 00:38:25 -0400155 }
156
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400157 public static String getSharedPreferencesKey() {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400158 return SHARED_PREFERENCES_KEY;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400159 }
160
Daniel Sandlere4f98912013-06-25 15:13:26 -0400161 public boolean isScreenLarge() {
162 return mIsScreenLarge;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400163 }
164
165 public static boolean isScreenLandscape(Context context) {
166 return context.getResources().getConfiguration().orientation ==
167 Configuration.ORIENTATION_LANDSCAPE;
168 }
169
Daniel Sandlere4f98912013-06-25 15:13:26 -0400170 public float getScreenDensity() {
171 return mScreenDensity;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400172 }
173
Daniel Sandlere4f98912013-06-25 15:13:26 -0400174 public int getLongPressTimeout() {
175 return mLongPressTimeout;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400176 }
177}