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