Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.launcher3; |
| 18 | |
| 19 | import android.app.SearchManager; |
Daniel Sandler | b9eb286 | 2013-06-14 20:17:30 -0400 | [diff] [blame^] | 20 | import android.content.*; |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 21 | import android.content.res.Configuration; |
| 22 | import android.database.ContentObserver; |
Daniel Sandler | a127b7a | 2013-06-17 14:25:46 -0400 | [diff] [blame] | 23 | import android.net.Uri; |
| 24 | import android.os.Debug; |
| 25 | import android.os.Environment; |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 26 | import android.os.Handler; |
Daniel Sandler | b9eb286 | 2013-06-14 20:17:30 -0400 | [diff] [blame^] | 27 | import android.os.IBinder; |
| 28 | import android.util.Log; |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 29 | |
Daniel Sandler | a127b7a | 2013-06-17 14:25:46 -0400 | [diff] [blame] | 30 | import java.io.File; |
| 31 | import java.io.IOException; |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 32 | import java.lang.ref.WeakReference; |
Daniel Sandler | a127b7a | 2013-06-17 14:25:46 -0400 | [diff] [blame] | 33 | import java.util.ArrayList; |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 34 | |
| 35 | public 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 Bringert | c6e2f94 | 2013-06-12 11:42:30 +0100 | [diff] [blame] | 44 | private long mStarttime; |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 45 | |
Daniel Sandler | 924b993 | 2013-06-12 00:38:25 -0400 | [diff] [blame] | 46 | WeakReference<LauncherProvider> mLauncherProvider; |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 47 | |
| 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 Sandler | b9eb286 | 2013-06-14 20:17:30 -0400 | [diff] [blame^] | 65 | Log.v(Launcher.TAG, "LauncherAppState initialize() called in process " + android.os.Process.myPid()); |
| 66 | |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 67 | mContext = context; |
| 68 | |
Bjorn Bringert | c6e2f94 | 2013-06-12 11:42:30 +0100 | [diff] [blame] | 69 | mStarttime = System.currentTimeMillis(); |
Daniel Sandler | 924b993 | 2013-06-12 00:38:25 -0400 | [diff] [blame] | 70 | |
Daniel Sandler | b9eb286 | 2013-06-14 20:17:30 -0400 | [diff] [blame^] | 71 | 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 Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 80 | // 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 Sandler | 924b993 | 2013-06-12 00:38:25 -0400 | [diff] [blame] | 153 | void setLauncherProvider(LauncherProvider provider) { |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 154 | mLauncherProvider = new WeakReference<LauncherProvider>(provider); |
| 155 | } |
| 156 | |
| 157 | LauncherProvider getLauncherProvider() { |
| 158 | return mLauncherProvider.get(); |
| 159 | } |
| 160 | |
Daniel Sandler | 924b993 | 2013-06-12 00:38:25 -0400 | [diff] [blame] | 161 | /** |
| 162 | * @return Milliseconds since the application state was created. |
| 163 | */ |
| 164 | public long getUptime() { |
Bjorn Bringert | c6e2f94 | 2013-06-12 11:42:30 +0100 | [diff] [blame] | 165 | return System.currentTimeMillis() - mStarttime; |
Daniel Sandler | 924b993 | 2013-06-12 00:38:25 -0400 | [diff] [blame] | 166 | } |
| 167 | |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 168 | 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 | } |