blob: 55c370387a6f0efa187f318b44b7678db9b7e5c9 [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
Sunny Goyalfdbef272017-02-01 12:52:54 -080019import android.content.ContentProviderClient;
Sunny Goyale755d462014-07-22 13:48:29 -070020import android.content.Context;
21import android.content.Intent;
22import android.content.IntentFilter;
Sunny Goyalfdbef272017-02-01 12:52:54 -080023import android.os.Looper;
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040024import android.util.Log;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080025
Kenny Guyed131872014-04-30 03:02:21 +010026import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal4390ace2014-10-13 11:33:11 -070027import com.android.launcher3.compat.PackageInstallerCompat;
Sunny Goyal823fd502015-08-04 11:40:13 -070028import com.android.launcher3.compat.UserManagerCompat;
Sunny Goyale26d1002016-06-20 14:52:14 -070029import com.android.launcher3.config.ProviderConfig;
Tony Wickham827cef22016-03-17 15:39:39 -070030import com.android.launcher3.dynamicui.ExtractionUtils;
Sunny Goyalae502842016-06-17 08:43:56 -070031import com.android.launcher3.util.ConfigMonitor;
Sunny Goyalfdbef272017-02-01 12:52:54 -080032import com.android.launcher3.util.Preconditions;
Sunny Goyal322d5562015-06-25 19:35:49 -070033import com.android.launcher3.util.TestingUtils;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080034
Sunny Goyalfdbef272017-02-01 12:52:54 -080035import java.util.concurrent.Callable;
36import java.util.concurrent.ExecutionException;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040037
Sunny Goyalc6205602015-05-21 20:46:33 -070038public class LauncherAppState {
Chris Wrenaeff7ea2014-02-14 16:59:24 -050039
Sunny Goyale26d1002016-06-20 14:52:14 -070040 public static final boolean PROFILE_STARTUP = ProviderConfig.IS_DOGFOOD_BUILD;
41
Sunny Goyalfdbef272017-02-01 12:52:54 -080042 // We do not need any synchronization for this variable as its only written on UI thread.
Daniel Sandlere4f98912013-06-25 15:13:26 -040043 private static LauncherAppState INSTANCE;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040044
Sunny Goyalfdbef272017-02-01 12:52:54 -080045 private final Context mContext;
46 private final LauncherModel mModel;
47 private final IconCache mIconCache;
48 private final WidgetPreviewLoader mWidgetCache;
49 private final InvariantDeviceProfile mInvariantDeviceProfile;
Sunny Goyal7779d622015-06-11 16:18:39 -070050
Sunny Goyalfdbef272017-02-01 12:52:54 -080051
52 public static LauncherAppState getInstance(final Context context) {
Daniel Sandlere4f98912013-06-25 15:13:26 -040053 if (INSTANCE == null) {
Sunny Goyalfdbef272017-02-01 12:52:54 -080054 if (Looper.myLooper() == Looper.getMainLooper()) {
55 INSTANCE = new LauncherAppState(context.getApplicationContext());
56 } else {
57 try {
58 return new MainThreadExecutor().submit(new Callable<LauncherAppState>() {
59 @Override
60 public LauncherAppState call() throws Exception {
61 return LauncherAppState.getInstance(context);
62 }
63 }).get();
64 } catch (InterruptedException|ExecutionException e) {
65 throw new RuntimeException(e);
66 }
67 }
Daniel Sandlere4f98912013-06-25 15:13:26 -040068 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040069 return INSTANCE;
70 }
71
Chris Wrend8fe6de2013-10-04 10:42:14 -040072 public static LauncherAppState getInstanceNoCreate() {
73 return INSTANCE;
74 }
75
Daniel Sandlercc8befa2013-06-11 14:45:48 -040076 public Context getContext() {
Sunny Goyalfdbef272017-02-01 12:52:54 -080077 return mContext;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040078 }
79
Sunny Goyalfdbef272017-02-01 12:52:54 -080080 private LauncherAppState(Context context) {
81 if (getLocalProvider(context) == null) {
82 throw new RuntimeException(
83 "Initializing LauncherAppState in the absence of LauncherProvider");
Daniel Sandlere4f98912013-06-25 15:13:26 -040084 }
Hyunyoung Song0de01172016-10-05 16:27:48 -070085 Log.v(Launcher.TAG, "LauncherAppState initiated");
Sunny Goyalfdbef272017-02-01 12:52:54 -080086 Preconditions.assertUIThread();
87 mContext = context;
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040088
Sunny Goyal322d5562015-06-25 19:35:49 -070089 if (TestingUtils.MEMORY_DUMP_ENABLED) {
Sunny Goyalfdbef272017-02-01 12:52:54 -080090 TestingUtils.startTrackingMemory(mContext);
Daniel Sandlere4f98912013-06-25 15:13:26 -040091 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040092
Sunny Goyalfdbef272017-02-01 12:52:54 -080093 mInvariantDeviceProfile = new InvariantDeviceProfile(mContext);
94 mIconCache = new IconCache(mContext, mInvariantDeviceProfile);
95 mWidgetCache = new WidgetPreviewLoader(mContext, mIconCache);
Bjorn Bringert1307f632013-10-03 22:31:03 +010096
Sunny Goyalab121c12016-12-02 19:29:43 +053097 mModel = new LauncherModel(this, mIconCache,
Sunny Goyalfdbef272017-02-01 12:52:54 -080098 Utilities.getOverrideObject(AppFilter.class, mContext, R.string.app_filter_class));
Sunny Goyal27595792015-03-19 13:18:44 -070099
Sunny Goyalfdbef272017-02-01 12:52:54 -0800100 LauncherAppsCompat.getInstance(mContext).addOnAppsChangedCallback(mModel);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400101
102 // Register intent receivers
Kenny Guyed131872014-04-30 03:02:21 +0100103 IntentFilter filter = new IntentFilter();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400104 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
Sunny Goyal957c13f2015-05-01 13:02:20 -0700105 // For handling managed profiles
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700106 filter.addAction(Intent.ACTION_MANAGED_PROFILE_ADDED);
107 filter.addAction(Intent.ACTION_MANAGED_PROFILE_REMOVED);
108 filter.addAction(Intent.ACTION_MANAGED_PROFILE_AVAILABLE);
109 filter.addAction(Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE);
110 filter.addAction(Intent.ACTION_MANAGED_PROFILE_UNLOCKED);
Tony Wickham827cef22016-03-17 15:39:39 -0700111 // For extracting colors from the wallpaper
Sunny Goyalf5e37442016-11-02 10:31:24 -0700112 if (Utilities.ATLEAST_NOUGAT) {
Tony Wickham827cef22016-03-17 15:39:39 -0700113 // TODO: add a broadcast entry to the manifest for pre-N.
114 filter.addAction(Intent.ACTION_WALLPAPER_CHANGED);
115 }
Sunny Goyal957c13f2015-05-01 13:02:20 -0700116
Sunny Goyalfdbef272017-02-01 12:52:54 -0800117 mContext.registerReceiver(mModel, filter);
118 UserManagerCompat.getInstance(mContext).enableAndResetCache();
119 new ConfigMonitor(mContext).register();
Tony Wickham827cef22016-03-17 15:39:39 -0700120
Sunny Goyalfdbef272017-02-01 12:52:54 -0800121 ExtractionUtils.startColorExtractionServiceIfNecessary(mContext);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400122 }
Kenny Guy1317e2d2014-05-08 18:52:50 +0100123
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400124 /**
125 * Call from Application.onTerminate(), which is not guaranteed to ever be called.
126 */
127 public void onTerminate() {
Sunny Goyalfdbef272017-02-01 12:52:54 -0800128 mContext.unregisterReceiver(mModel);
129 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(mContext);
Kenny Guyc2bd8102014-06-30 12:30:31 +0100130 launcherApps.removeOnAppsChangedCallback(mModel);
Sunny Goyalfdbef272017-02-01 12:52:54 -0800131 PackageInstallerCompat.getInstance(mContext).onStop();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400132 }
133
134 /**
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700135 * Reloads the workspace items from the DB and re-binds the workspace. This should generally
136 * not be called as DB updates are automatically followed by UI update
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400137 */
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700138 public void reloadWorkspace() {
139 mModel.resetLoadedState(false, true);
140 mModel.startLoaderFromBackground();
141 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400142
143 LauncherModel setLauncher(Launcher launcher) {
Sunny Goyalfdbef272017-02-01 12:52:54 -0800144 getLocalProvider(mContext).setLauncherProviderChangeListener(launcher);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400145 mModel.initialize(launcher);
146 return mModel;
147 }
148
Sunny Goyal34942622014-08-29 17:20:55 -0700149 public IconCache getIconCache() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400150 return mIconCache;
151 }
152
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700153 public LauncherModel getModel() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400154 return mModel;
155 }
156
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700157 public WidgetPreviewLoader getWidgetCache() {
158 return mWidgetCache;
159 }
Michael Jurkaa6a05472013-11-13 17:59:46 +0100160
Adam Cohen2e6da152015-05-06 11:42:25 -0700161 public InvariantDeviceProfile getInvariantDeviceProfile() {
162 return mInvariantDeviceProfile;
163 }
Sunny Goyal87f784c2017-01-11 10:48:34 -0800164
165 /**
166 * Shorthand for {@link #getInvariantDeviceProfile()}
167 */
168 public static InvariantDeviceProfile getIDP(Context context) {
169 return LauncherAppState.getInstance(context).getInvariantDeviceProfile();
170 }
Sunny Goyalfdbef272017-02-01 12:52:54 -0800171
172 private static LauncherProvider getLocalProvider(Context context) {
173 try (ContentProviderClient cl = context.getContentResolver()
174 .acquireContentProviderClient(LauncherProvider.AUTHORITY)) {
175 return (LauncherProvider) cl.getLocalContentProvider();
176 }
177 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400178}