blob: af9acb1527c3f3567e2a5e6f1037d0c1b0bd1282 [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 Sandlercc8befa2013-06-11 14:45:48 -040070 // set sIsScreenXLarge and sScreenDensity *before* creating icon cache
71 sIsScreenLarge = context.getResources().getBoolean(R.bool.is_large_screen);
72 sScreenDensity = context.getResources().getDisplayMetrics().density;
73
74 mWidgetPreviewCacheDb = new WidgetPreviewLoader.CacheDb(context);
75 mIconCache = new IconCache(context);
76 mModel = new LauncherModel(context, mIconCache);
77
78 // Register intent receivers
79 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
80 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
81 filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
82 filter.addDataScheme("package");
83 context.registerReceiver(mModel, filter);
84 filter = new IntentFilter();
85 filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);
86 filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
87 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
88 filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
89 context.registerReceiver(mModel, filter);
90 filter = new IntentFilter();
91 filter.addAction(SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED);
92 context.registerReceiver(mModel, filter);
93 filter = new IntentFilter();
94 filter.addAction(SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED);
95 context.registerReceiver(mModel, filter);
96
97 // Register for changes to the favorites
98 ContentResolver resolver = context.getContentResolver();
99 resolver.registerContentObserver(LauncherSettings.Favorites.CONTENT_URI, true,
100 mFavoritesObserver);
101 }
102
103 /**
104 * Call from Application.onTerminate(), which is not guaranteed to ever be called.
105 */
106 public void onTerminate() {
107 mContext.unregisterReceiver(mModel);
108
109 ContentResolver resolver = mContext.getContentResolver();
110 resolver.unregisterContentObserver(mFavoritesObserver);
111 }
112
113 /**
114 * Receives notifications whenever the user favorites have changed.
115 */
116 private final ContentObserver mFavoritesObserver = new ContentObserver(new Handler()) {
117 @Override
118 public void onChange(boolean selfChange) {
119 // If the database has ever changed, then we really need to force a reload of the
120 // workspace on the next load
121 mModel.resetLoadedState(false, true);
122 mModel.startLoaderFromBackground();
123 }
124 };
125
126 LauncherModel setLauncher(Launcher launcher) {
127 mModel.initialize(launcher);
128 return mModel;
129 }
130
131 IconCache getIconCache() {
132 return mIconCache;
133 }
134
135 LauncherModel getModel() {
136 return mModel;
137 }
138
139 WidgetPreviewLoader.CacheDb getWidgetPreviewCacheDb() {
140 return mWidgetPreviewCacheDb;
141 }
142
Daniel Sandler924b9932013-06-12 00:38:25 -0400143 void setLauncherProvider(LauncherProvider provider) {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400144 mLauncherProvider = new WeakReference<LauncherProvider>(provider);
145 }
146
147 LauncherProvider getLauncherProvider() {
148 return mLauncherProvider.get();
149 }
150
Daniel Sandler924b9932013-06-12 00:38:25 -0400151 /**
152 * @return Milliseconds since the application state was created.
153 */
154 public long getUptime() {
Bjorn Bringertc6e2f942013-06-12 11:42:30 +0100155 return System.currentTimeMillis() - mStarttime;
Daniel Sandler924b9932013-06-12 00:38:25 -0400156 }
157
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400158 public static String getSharedPreferencesKey() {
159 return sSharedPreferencesKey;
160 }
161
162 public static boolean isScreenLarge() {
163 return sIsScreenLarge;
164 }
165
166 public static boolean isScreenLandscape(Context context) {
167 return context.getResources().getConfiguration().orientation ==
168 Configuration.ORIENTATION_LANDSCAPE;
169 }
170
171 public static float getScreenDensity() {
172 return sScreenDensity;
173 }
174
175 public static int getLongPressTimeout() {
176 return sLongPressTimeout;
177 }
178}