blob: cf20febd5bc061e03ddb255569ed1c2693fe56f1 [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 Goyal3d706ad2017-03-06 16:56:39 -080029import com.android.launcher3.config.FeatureFlags;
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 Goyal3d706ad2017-03-06 16:56:39 -080040 public static final boolean PROFILE_STARTUP = FeatureFlags.IS_DOGFOOD_BUILD;
Sunny Goyale26d1002016-06-20 14:52:14 -070041
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);
Sunny Goyalc6e97692017-06-02 13:46:55 -070096 mModel = new LauncherModel(this, mIconCache, AppFilter.newInstance(mContext));
Sunny Goyal27595792015-03-19 13:18:44 -070097
Sunny Goyalfdbef272017-02-01 12:52:54 -080098 LauncherAppsCompat.getInstance(mContext).addOnAppsChangedCallback(mModel);
Daniel Sandlercc8befa2013-06-11 14:45:48 -040099
100 // Register intent receivers
Kenny Guyed131872014-04-30 03:02:21 +0100101 IntentFilter filter = new IntentFilter();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400102 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
Sunny Goyal957c13f2015-05-01 13:02:20 -0700103 // For handling managed profiles
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700104 filter.addAction(Intent.ACTION_MANAGED_PROFILE_ADDED);
105 filter.addAction(Intent.ACTION_MANAGED_PROFILE_REMOVED);
106 filter.addAction(Intent.ACTION_MANAGED_PROFILE_AVAILABLE);
107 filter.addAction(Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE);
108 filter.addAction(Intent.ACTION_MANAGED_PROFILE_UNLOCKED);
Tony Wickham827cef22016-03-17 15:39:39 -0700109 // For extracting colors from the wallpaper
Sunny Goyalf5e37442016-11-02 10:31:24 -0700110 if (Utilities.ATLEAST_NOUGAT) {
Tony Wickham827cef22016-03-17 15:39:39 -0700111 // TODO: add a broadcast entry to the manifest for pre-N.
112 filter.addAction(Intent.ACTION_WALLPAPER_CHANGED);
113 }
Sunny Goyal957c13f2015-05-01 13:02:20 -0700114
Sunny Goyalfdbef272017-02-01 12:52:54 -0800115 mContext.registerReceiver(mModel, filter);
116 UserManagerCompat.getInstance(mContext).enableAndResetCache();
117 new ConfigMonitor(mContext).register();
Tony Wickham827cef22016-03-17 15:39:39 -0700118
Sunny Goyalfdbef272017-02-01 12:52:54 -0800119 ExtractionUtils.startColorExtractionServiceIfNecessary(mContext);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400120 }
Kenny Guy1317e2d2014-05-08 18:52:50 +0100121
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400122 /**
123 * Call from Application.onTerminate(), which is not guaranteed to ever be called.
124 */
125 public void onTerminate() {
Sunny Goyalfdbef272017-02-01 12:52:54 -0800126 mContext.unregisterReceiver(mModel);
127 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(mContext);
Kenny Guyc2bd8102014-06-30 12:30:31 +0100128 launcherApps.removeOnAppsChangedCallback(mModel);
Sunny Goyalfdbef272017-02-01 12:52:54 -0800129 PackageInstallerCompat.getInstance(mContext).onStop();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400130 }
131
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400132 LauncherModel setLauncher(Launcher launcher) {
Sunny Goyalfdbef272017-02-01 12:52:54 -0800133 getLocalProvider(mContext).setLauncherProviderChangeListener(launcher);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400134 mModel.initialize(launcher);
135 return mModel;
136 }
137
Sunny Goyal34942622014-08-29 17:20:55 -0700138 public IconCache getIconCache() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400139 return mIconCache;
140 }
141
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700142 public LauncherModel getModel() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400143 return mModel;
144 }
145
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700146 public WidgetPreviewLoader getWidgetCache() {
147 return mWidgetCache;
148 }
Michael Jurkaa6a05472013-11-13 17:59:46 +0100149
Adam Cohen2e6da152015-05-06 11:42:25 -0700150 public InvariantDeviceProfile getInvariantDeviceProfile() {
151 return mInvariantDeviceProfile;
152 }
Sunny Goyal87f784c2017-01-11 10:48:34 -0800153
154 /**
155 * Shorthand for {@link #getInvariantDeviceProfile()}
156 */
157 public static InvariantDeviceProfile getIDP(Context context) {
158 return LauncherAppState.getInstance(context).getInvariantDeviceProfile();
159 }
Sunny Goyalfdbef272017-02-01 12:52:54 -0800160
161 private static LauncherProvider getLocalProvider(Context context) {
162 try (ContentProviderClient cl = context.getContentResolver()
163 .acquireContentProviderClient(LauncherProvider.AUTHORITY)) {
164 return (LauncherProvider) cl.getLocalContentProvider();
165 }
166 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400167}