blob: bf2ad92e61294279bf4248cd7a3d419666435c25 [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 Goyale755d462014-07-22 13:48:29 -070019import android.content.Context;
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040020import android.util.Log;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080021
Tracy Zhoube13d102020-01-12 01:07:59 -080022import androidx.annotation.Nullable;
23
Sunny Goyalb8239472025-02-14 13:08:00 -080024import com.android.launcher3.dagger.LauncherComponentProvider;
Sunny Goyalc369d1e2024-12-31 00:00:15 -080025import com.android.launcher3.graphics.ThemeManager;
Sunny Goyalf840f102018-09-21 14:41:05 -070026import com.android.launcher3.icons.IconCache;
Sunny Goyal14168432019-10-24 15:59:49 -070027import com.android.launcher3.icons.IconProvider;
Sunny Goyal68af5492021-12-24 01:08:31 +053028import com.android.launcher3.icons.LauncherIconProvider;
Sunny Goyalb8239472025-02-14 13:08:00 -080029import com.android.launcher3.model.ModelInitializer;
Shamali Pea078cb2024-11-04 21:35:38 +000030import com.android.launcher3.model.WidgetsFilterDataProvider;
Sunny Goyal73b5a272019-12-09 14:55:56 -080031import com.android.launcher3.pm.InstallSessionHelper;
Sunny Goyal337c81f2019-12-10 12:19:13 -080032import com.android.launcher3.pm.UserCache;
Sunny Goyald0e360a2018-06-29 14:40:18 -070033import com.android.launcher3.util.MainThreadInitializedObject;
Winson Chung94e8ad02024-05-08 21:14:57 +000034import com.android.launcher3.util.PackageManagerHelper;
Sunny Goyalfdbef272017-02-01 12:52:54 -080035import com.android.launcher3.util.Preconditions;
Sunny Goyalb47172b2021-05-03 19:59:51 -070036import com.android.launcher3.util.RunnableList;
Sunny Goyal14168432019-10-24 15:59:49 -070037import com.android.launcher3.util.SafeCloseable;
Jon Mirandaec1277e2021-03-25 10:41:54 -040038import com.android.launcher3.util.SettingsCache;
Sunny Goyal31e27ed2024-01-09 15:45:14 -080039import com.android.launcher3.util.TraceHelper;
Pinyao Tingc7a6c292019-08-26 14:36:02 -070040import com.android.launcher3.widget.custom.CustomWidgetManager;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080041
Sunny Goyal777d4902021-08-27 21:22:17 +000042public class LauncherAppState implements SafeCloseable {
Chris Wrenaeff7ea2014-02-14 16:59:24 -050043
Charlie Andersonbad2be42024-12-06 16:32:03 -050044 public static final String TAG = "LauncherAppState";
Sunny Goyal29947f02017-12-18 13:49:44 -080045
Sunny Goyalfdbef272017-02-01 12:52:54 -080046 // We do not need any synchronization for this variable as its only written on UI thread.
Sunny Goyalcf845f02019-09-25 09:08:16 -070047 public static final MainThreadInitializedObject<LauncherAppState> INSTANCE =
Sunny Goyale529a862019-08-06 09:48:36 -070048 new MainThreadInitializedObject<>(LauncherAppState::new);
Daniel Sandlercc8befa2013-06-11 14:45:48 -040049
Sunny Goyalfdbef272017-02-01 12:52:54 -080050 private final Context mContext;
51 private final LauncherModel mModel;
Sunny Goyal68af5492021-12-24 01:08:31 +053052 private final LauncherIconProvider mIconProvider;
Sunny Goyalfdbef272017-02-01 12:52:54 -080053 private final IconCache mIconCache;
Sunny Goyalfdbef272017-02-01 12:52:54 -080054 private final InvariantDeviceProfile mInvariantDeviceProfile;
Sunny Goyal31e27ed2024-01-09 15:45:14 -080055 private boolean mIsSafeModeEnabled;
56
Sunny Goyalb47172b2021-05-03 19:59:51 -070057 private final RunnableList mOnTerminateCallback = new RunnableList();
Sunny Goyal045b4fa2019-09-20 12:51:37 -070058
Sunny Goyal10fa0162024-04-21 00:13:35 -070059 public static LauncherAppState getInstance(Context context) {
Sunny Goyald0e360a2018-06-29 14:40:18 -070060 return INSTANCE.get(context);
Daniel Sandlercc8befa2013-06-11 14:45:48 -040061 }
62
Daniel Sandlercc8befa2013-06-11 14:45:48 -040063 public Context getContext() {
Sunny Goyalfdbef272017-02-01 12:52:54 -080064 return mContext;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040065 }
66
Jakob Schneider2f609d02024-01-10 18:03:35 +000067 @SuppressWarnings("NewApi")
Tracy Zhoube13d102020-01-12 01:07:59 -080068 public LauncherAppState(Context context) {
69 this(context, LauncherFiles.APP_ICONS_DB);
Sunny Goyaldf4241c2021-05-19 19:42:14 -070070 Log.v(Launcher.TAG, "LauncherAppState initiated");
71 Preconditions.assertUIThread();
Sunny Goyal27595792015-03-19 13:18:44 -070072
Sunny Goyal31e27ed2024-01-09 15:45:14 -080073 mIsSafeModeEnabled = TraceHelper.allowIpcs("isSafeMode",
74 () -> context.getPackageManager().isSafeMode());
Daniel Sandlercc8befa2013-06-11 14:45:48 -040075
Sunny Goyalb8239472025-02-14 13:08:00 -080076 ModelInitializer initializer = new ModelInitializer(
77 context,
78 LauncherComponentProvider.get(context).getIconPool(),
79 mIconCache,
80 mInvariantDeviceProfile,
81 ThemeManager.INSTANCE.get(context),
82 UserCache.INSTANCE.get(context),
83 SettingsCache.INSTANCE.get(context),
84 mIconProvider,
85 CustomWidgetManager.INSTANCE.get(context),
86 InstallSessionHelper.INSTANCE.get(context),
87 closeable -> mOnTerminateCallback.add(closeable::close)
88 );
89 initializer.initialize(mModel);
Sunny Goyal420d5452018-10-17 10:25:14 -070090 }
91
Tracy Zhoube13d102020-01-12 01:07:59 -080092 public LauncherAppState(Context context, @Nullable String iconCacheFileName) {
Tracy Zhoube13d102020-01-12 01:07:59 -080093 mContext = context;
94
95 mInvariantDeviceProfile = InvariantDeviceProfile.INSTANCE.get(context);
Sunny Goyal68af5492021-12-24 01:08:31 +053096 mIconProvider = new LauncherIconProvider(context);
Sunny Goyalb47172b2021-05-03 19:59:51 -070097 mIconCache = new IconCache(mContext, mInvariantDeviceProfile,
98 iconCacheFileName, mIconProvider);
Shamali Pea078cb2024-11-04 21:35:38 +000099 mModel = new LauncherModel(context, this, mIconCache,
100 WidgetsFilterDataProvider.Companion.newInstance(context), new AppFilter(mContext),
Winson Chung94e8ad02024-05-08 21:14:57 +0000101 PackageManagerHelper.INSTANCE.get(context), iconCacheFileName != null);
Sunny Goyaldf4241c2021-05-19 19:42:14 -0700102 mOnTerminateCallback.add(mIconCache::close);
Sunny Goyale274d972023-05-01 16:55:59 -0700103 mOnTerminateCallback.add(mModel::destroy);
Tracy Zhoube13d102020-01-12 01:07:59 -0800104 }
105
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400106 /**
107 * Call from Application.onTerminate(), which is not guaranteed to ever be called.
108 */
Sunny Goyal777d4902021-08-27 21:22:17 +0000109 @Override
110 public void close() {
Sunny Goyalb47172b2021-05-03 19:59:51 -0700111 mOnTerminateCallback.executeAllAndDestroy();
112 }
Sunny Goyal01615a62019-09-20 12:00:07 -0700113
Sunny Goyalb47172b2021-05-03 19:59:51 -0700114 public IconProvider getIconProvider() {
115 return mIconProvider;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400116 }
117
Sunny Goyal34942622014-08-29 17:20:55 -0700118 public IconCache getIconCache() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400119 return mIconCache;
120 }
121
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700122 public LauncherModel getModel() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400123 return mModel;
124 }
125
Adam Cohen2e6da152015-05-06 11:42:25 -0700126 public InvariantDeviceProfile getInvariantDeviceProfile() {
127 return mInvariantDeviceProfile;
128 }
Sunny Goyal87f784c2017-01-11 10:48:34 -0800129
Sunny Goyal31e27ed2024-01-09 15:45:14 -0800130 public boolean isSafeModeEnabled() {
131 return mIsSafeModeEnabled;
132 }
133
Sunny Goyal87f784c2017-01-11 10:48:34 -0800134 /**
135 * Shorthand for {@link #getInvariantDeviceProfile()}
136 */
137 public static InvariantDeviceProfile getIDP(Context context) {
Sunny Goyald0e360a2018-06-29 14:40:18 -0700138 return InvariantDeviceProfile.INSTANCE.get(context);
Sunny Goyal87f784c2017-01-11 10:48:34 -0800139 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400140}