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 | |
Sunny Goyal | e755d46 | 2014-07-22 13:48:29 -0700 | [diff] [blame] | 19 | import android.content.Context; |
Daniel Sandler | b9eb286 | 2013-06-14 20:17:30 -0400 | [diff] [blame] | 20 | import android.util.Log; |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 21 | |
Tracy Zhou | be13d10 | 2020-01-12 01:07:59 -0800 | [diff] [blame] | 22 | import androidx.annotation.Nullable; |
| 23 | |
Sunny Goyal | b823947 | 2025-02-14 13:08:00 -0800 | [diff] [blame] | 24 | import com.android.launcher3.dagger.LauncherComponentProvider; |
Sunny Goyal | c369d1e | 2024-12-31 00:00:15 -0800 | [diff] [blame] | 25 | import com.android.launcher3.graphics.ThemeManager; |
Sunny Goyal | f840f10 | 2018-09-21 14:41:05 -0700 | [diff] [blame] | 26 | import com.android.launcher3.icons.IconCache; |
Sunny Goyal | 1416843 | 2019-10-24 15:59:49 -0700 | [diff] [blame] | 27 | import com.android.launcher3.icons.IconProvider; |
Sunny Goyal | 68af549 | 2021-12-24 01:08:31 +0530 | [diff] [blame] | 28 | import com.android.launcher3.icons.LauncherIconProvider; |
Sunny Goyal | b823947 | 2025-02-14 13:08:00 -0800 | [diff] [blame] | 29 | import com.android.launcher3.model.ModelInitializer; |
Shamali P | ea078cb | 2024-11-04 21:35:38 +0000 | [diff] [blame] | 30 | import com.android.launcher3.model.WidgetsFilterDataProvider; |
Sunny Goyal | 73b5a27 | 2019-12-09 14:55:56 -0800 | [diff] [blame] | 31 | import com.android.launcher3.pm.InstallSessionHelper; |
Sunny Goyal | 337c81f | 2019-12-10 12:19:13 -0800 | [diff] [blame] | 32 | import com.android.launcher3.pm.UserCache; |
Sunny Goyal | d0e360a | 2018-06-29 14:40:18 -0700 | [diff] [blame] | 33 | import com.android.launcher3.util.MainThreadInitializedObject; |
Winson Chung | 94e8ad0 | 2024-05-08 21:14:57 +0000 | [diff] [blame] | 34 | import com.android.launcher3.util.PackageManagerHelper; |
Sunny Goyal | fdbef27 | 2017-02-01 12:52:54 -0800 | [diff] [blame] | 35 | import com.android.launcher3.util.Preconditions; |
Sunny Goyal | b47172b | 2021-05-03 19:59:51 -0700 | [diff] [blame] | 36 | import com.android.launcher3.util.RunnableList; |
Sunny Goyal | 1416843 | 2019-10-24 15:59:49 -0700 | [diff] [blame] | 37 | import com.android.launcher3.util.SafeCloseable; |
Jon Miranda | ec1277e | 2021-03-25 10:41:54 -0400 | [diff] [blame] | 38 | import com.android.launcher3.util.SettingsCache; |
Sunny Goyal | 31e27ed | 2024-01-09 15:45:14 -0800 | [diff] [blame] | 39 | import com.android.launcher3.util.TraceHelper; |
Pinyao Ting | c7a6c29 | 2019-08-26 14:36:02 -0700 | [diff] [blame] | 40 | import com.android.launcher3.widget.custom.CustomWidgetManager; |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 41 | |
Sunny Goyal | 777d490 | 2021-08-27 21:22:17 +0000 | [diff] [blame] | 42 | public class LauncherAppState implements SafeCloseable { |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 43 | |
Charlie Anderson | bad2be4 | 2024-12-06 16:32:03 -0500 | [diff] [blame] | 44 | public static final String TAG = "LauncherAppState"; |
Sunny Goyal | 29947f0 | 2017-12-18 13:49:44 -0800 | [diff] [blame] | 45 | |
Sunny Goyal | fdbef27 | 2017-02-01 12:52:54 -0800 | [diff] [blame] | 46 | // We do not need any synchronization for this variable as its only written on UI thread. |
Sunny Goyal | cf845f0 | 2019-09-25 09:08:16 -0700 | [diff] [blame] | 47 | public static final MainThreadInitializedObject<LauncherAppState> INSTANCE = |
Sunny Goyal | e529a86 | 2019-08-06 09:48:36 -0700 | [diff] [blame] | 48 | new MainThreadInitializedObject<>(LauncherAppState::new); |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 49 | |
Sunny Goyal | fdbef27 | 2017-02-01 12:52:54 -0800 | [diff] [blame] | 50 | private final Context mContext; |
| 51 | private final LauncherModel mModel; |
Sunny Goyal | 68af549 | 2021-12-24 01:08:31 +0530 | [diff] [blame] | 52 | private final LauncherIconProvider mIconProvider; |
Sunny Goyal | fdbef27 | 2017-02-01 12:52:54 -0800 | [diff] [blame] | 53 | private final IconCache mIconCache; |
Sunny Goyal | fdbef27 | 2017-02-01 12:52:54 -0800 | [diff] [blame] | 54 | private final InvariantDeviceProfile mInvariantDeviceProfile; |
Sunny Goyal | 31e27ed | 2024-01-09 15:45:14 -0800 | [diff] [blame] | 55 | private boolean mIsSafeModeEnabled; |
| 56 | |
Sunny Goyal | b47172b | 2021-05-03 19:59:51 -0700 | [diff] [blame] | 57 | private final RunnableList mOnTerminateCallback = new RunnableList(); |
Sunny Goyal | 045b4fa | 2019-09-20 12:51:37 -0700 | [diff] [blame] | 58 | |
Sunny Goyal | 10fa016 | 2024-04-21 00:13:35 -0700 | [diff] [blame] | 59 | public static LauncherAppState getInstance(Context context) { |
Sunny Goyal | d0e360a | 2018-06-29 14:40:18 -0700 | [diff] [blame] | 60 | return INSTANCE.get(context); |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 61 | } |
| 62 | |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 63 | public Context getContext() { |
Sunny Goyal | fdbef27 | 2017-02-01 12:52:54 -0800 | [diff] [blame] | 64 | return mContext; |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 65 | } |
| 66 | |
Jakob Schneider | 2f609d0 | 2024-01-10 18:03:35 +0000 | [diff] [blame] | 67 | @SuppressWarnings("NewApi") |
Tracy Zhou | be13d10 | 2020-01-12 01:07:59 -0800 | [diff] [blame] | 68 | public LauncherAppState(Context context) { |
| 69 | this(context, LauncherFiles.APP_ICONS_DB); |
Sunny Goyal | df4241c | 2021-05-19 19:42:14 -0700 | [diff] [blame] | 70 | Log.v(Launcher.TAG, "LauncherAppState initiated"); |
| 71 | Preconditions.assertUIThread(); |
Sunny Goyal | 2759579 | 2015-03-19 13:18:44 -0700 | [diff] [blame] | 72 | |
Sunny Goyal | 31e27ed | 2024-01-09 15:45:14 -0800 | [diff] [blame] | 73 | mIsSafeModeEnabled = TraceHelper.allowIpcs("isSafeMode", |
| 74 | () -> context.getPackageManager().isSafeMode()); |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 75 | |
Sunny Goyal | b823947 | 2025-02-14 13:08:00 -0800 | [diff] [blame] | 76 | 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 Goyal | 420d545 | 2018-10-17 10:25:14 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Tracy Zhou | be13d10 | 2020-01-12 01:07:59 -0800 | [diff] [blame] | 92 | public LauncherAppState(Context context, @Nullable String iconCacheFileName) { |
Tracy Zhou | be13d10 | 2020-01-12 01:07:59 -0800 | [diff] [blame] | 93 | mContext = context; |
| 94 | |
| 95 | mInvariantDeviceProfile = InvariantDeviceProfile.INSTANCE.get(context); |
Sunny Goyal | 68af549 | 2021-12-24 01:08:31 +0530 | [diff] [blame] | 96 | mIconProvider = new LauncherIconProvider(context); |
Sunny Goyal | b47172b | 2021-05-03 19:59:51 -0700 | [diff] [blame] | 97 | mIconCache = new IconCache(mContext, mInvariantDeviceProfile, |
| 98 | iconCacheFileName, mIconProvider); |
Shamali P | ea078cb | 2024-11-04 21:35:38 +0000 | [diff] [blame] | 99 | mModel = new LauncherModel(context, this, mIconCache, |
| 100 | WidgetsFilterDataProvider.Companion.newInstance(context), new AppFilter(mContext), |
Winson Chung | 94e8ad0 | 2024-05-08 21:14:57 +0000 | [diff] [blame] | 101 | PackageManagerHelper.INSTANCE.get(context), iconCacheFileName != null); |
Sunny Goyal | df4241c | 2021-05-19 19:42:14 -0700 | [diff] [blame] | 102 | mOnTerminateCallback.add(mIconCache::close); |
Sunny Goyal | e274d97 | 2023-05-01 16:55:59 -0700 | [diff] [blame] | 103 | mOnTerminateCallback.add(mModel::destroy); |
Tracy Zhou | be13d10 | 2020-01-12 01:07:59 -0800 | [diff] [blame] | 104 | } |
| 105 | |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 106 | /** |
| 107 | * Call from Application.onTerminate(), which is not guaranteed to ever be called. |
| 108 | */ |
Sunny Goyal | 777d490 | 2021-08-27 21:22:17 +0000 | [diff] [blame] | 109 | @Override |
| 110 | public void close() { |
Sunny Goyal | b47172b | 2021-05-03 19:59:51 -0700 | [diff] [blame] | 111 | mOnTerminateCallback.executeAllAndDestroy(); |
| 112 | } |
Sunny Goyal | 01615a6 | 2019-09-20 12:00:07 -0700 | [diff] [blame] | 113 | |
Sunny Goyal | b47172b | 2021-05-03 19:59:51 -0700 | [diff] [blame] | 114 | public IconProvider getIconProvider() { |
| 115 | return mIconProvider; |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 116 | } |
| 117 | |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 118 | public IconCache getIconCache() { |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 119 | return mIconCache; |
| 120 | } |
| 121 | |
Sunny Goyal | 18bf8e2 | 2015-04-08 18:13:46 -0700 | [diff] [blame] | 122 | public LauncherModel getModel() { |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 123 | return mModel; |
| 124 | } |
| 125 | |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 126 | public InvariantDeviceProfile getInvariantDeviceProfile() { |
| 127 | return mInvariantDeviceProfile; |
| 128 | } |
Sunny Goyal | 87f784c | 2017-01-11 10:48:34 -0800 | [diff] [blame] | 129 | |
Sunny Goyal | 31e27ed | 2024-01-09 15:45:14 -0800 | [diff] [blame] | 130 | public boolean isSafeModeEnabled() { |
| 131 | return mIsSafeModeEnabled; |
| 132 | } |
| 133 | |
Sunny Goyal | 87f784c | 2017-01-11 10:48:34 -0800 | [diff] [blame] | 134 | /** |
| 135 | * Shorthand for {@link #getInvariantDeviceProfile()} |
| 136 | */ |
| 137 | public static InvariantDeviceProfile getIDP(Context context) { |
Sunny Goyal | d0e360a | 2018-06-29 14:40:18 -0700 | [diff] [blame] | 138 | return InvariantDeviceProfile.INSTANCE.get(context); |
Sunny Goyal | 87f784c | 2017-01-11 10:48:34 -0800 | [diff] [blame] | 139 | } |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 140 | } |