blob: e7aa3d8eaa7476640d40f7dbcfa9e55ac2e12194 [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;
Daniel Sandlera127b7a2013-06-17 14:25:46 -040026import android.net.Uri;
27import android.os.Debug;
28import android.os.Environment;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040029import android.os.Handler;
30
Daniel Sandlera127b7a2013-06-17 14:25:46 -040031import java.io.File;
32import java.io.IOException;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040033import java.lang.ref.WeakReference;
Daniel Sandlera127b7a2013-06-17 14:25:46 -040034import java.util.ArrayList;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040035
36public class LauncherAppState {
37 private Context mContext;
38 private LauncherModel mModel;
39 private IconCache mIconCache;
40 private WidgetPreviewLoader.CacheDb mWidgetPreviewCacheDb;
41 private static boolean sIsScreenLarge;
42 private static float sScreenDensity;
43 private static int sLongPressTimeout = 300;
44 private static final String sSharedPreferencesKey = "com.android.launcher3.prefs";
Bjorn Bringertc6e2f942013-06-12 11:42:30 +010045 private long mStarttime;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040046
Daniel Sandler924b9932013-06-12 00:38:25 -040047 WeakReference<LauncherProvider> mLauncherProvider;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040048
49 private static final LauncherAppState INSTANCE = new LauncherAppState();
50
51 public static LauncherAppState getInstance() {
52 return INSTANCE;
53 }
54
55 public static void init(Context context) {
56 INSTANCE.initialize(context);
57 }
58
59 public Context getContext() {
60 return mContext;
61 }
62
63 private LauncherAppState() { }
64
65 private void initialize(Context context) {
66 mContext = context;
67
Bjorn Bringertc6e2f942013-06-12 11:42:30 +010068 mStarttime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -040069
Daniel Sandlera127b7a2013-06-17 14:25:46 -040070 if (context.getResources().getBoolean(R.bool.debug_memory_enabled)) {
71 WeightWatcher.sUpdateThread.start();
72 }
73
Daniel Sandlercc8befa2013-06-11 14:45:48 -040074 // set sIsScreenXLarge and sScreenDensity *before* creating icon cache
75 sIsScreenLarge = context.getResources().getBoolean(R.bool.is_large_screen);
76 sScreenDensity = context.getResources().getDisplayMetrics().density;
77
78 mWidgetPreviewCacheDb = new WidgetPreviewLoader.CacheDb(context);
79 mIconCache = new IconCache(context);
80 mModel = new LauncherModel(context, mIconCache);
81
82 // Register intent receivers
83 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
84 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
85 filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
86 filter.addDataScheme("package");
87 context.registerReceiver(mModel, filter);
88 filter = new IntentFilter();
89 filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);
90 filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
91 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
92 filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
93 context.registerReceiver(mModel, filter);
94 filter = new IntentFilter();
95 filter.addAction(SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED);
96 context.registerReceiver(mModel, filter);
97 filter = new IntentFilter();
98 filter.addAction(SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED);
99 context.registerReceiver(mModel, filter);
100
101 // Register for changes to the favorites
102 ContentResolver resolver = context.getContentResolver();
103 resolver.registerContentObserver(LauncherSettings.Favorites.CONTENT_URI, true,
104 mFavoritesObserver);
105 }
106
107 /**
108 * Call from Application.onTerminate(), which is not guaranteed to ever be called.
109 */
110 public void onTerminate() {
111 mContext.unregisterReceiver(mModel);
112
113 ContentResolver resolver = mContext.getContentResolver();
114 resolver.unregisterContentObserver(mFavoritesObserver);
115 }
116
117 /**
118 * Receives notifications whenever the user favorites have changed.
119 */
120 private final ContentObserver mFavoritesObserver = new ContentObserver(new Handler()) {
121 @Override
122 public void onChange(boolean selfChange) {
123 // If the database has ever changed, then we really need to force a reload of the
124 // workspace on the next load
125 mModel.resetLoadedState(false, true);
126 mModel.startLoaderFromBackground();
127 }
128 };
129
130 LauncherModel setLauncher(Launcher launcher) {
131 mModel.initialize(launcher);
132 return mModel;
133 }
134
135 IconCache getIconCache() {
136 return mIconCache;
137 }
138
139 LauncherModel getModel() {
140 return mModel;
141 }
142
143 WidgetPreviewLoader.CacheDb getWidgetPreviewCacheDb() {
144 return mWidgetPreviewCacheDb;
145 }
146
Daniel Sandler924b9932013-06-12 00:38:25 -0400147 void setLauncherProvider(LauncherProvider provider) {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400148 mLauncherProvider = new WeakReference<LauncherProvider>(provider);
149 }
150
151 LauncherProvider getLauncherProvider() {
152 return mLauncherProvider.get();
153 }
154
Daniel Sandler924b9932013-06-12 00:38:25 -0400155 /**
156 * @return Milliseconds since the application state was created.
157 */
158 public long getUptime() {
Bjorn Bringertc6e2f942013-06-12 11:42:30 +0100159 return System.currentTimeMillis() - mStarttime;
Daniel Sandler924b9932013-06-12 00:38:25 -0400160 }
161
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400162 public static String getSharedPreferencesKey() {
163 return sSharedPreferencesKey;
164 }
165
166 public static boolean isScreenLarge() {
167 return sIsScreenLarge;
168 }
169
170 public static boolean isScreenLandscape(Context context) {
171 return context.getResources().getConfiguration().orientation ==
172 Configuration.ORIENTATION_LANDSCAPE;
173 }
174
175 public static float getScreenDensity() {
176 return sScreenDensity;
177 }
178
179 public static int getLongPressTimeout() {
180 return sLongPressTimeout;
181 }
182}