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 | |
Tony | d48710c | 2017-07-27 23:23:58 -0700 | [diff] [blame] | 19 | import android.content.ComponentName; |
Sunny Goyal | fdbef27 | 2017-02-01 12:52:54 -0800 | [diff] [blame] | 20 | import android.content.ContentProviderClient; |
Sunny Goyal | e755d46 | 2014-07-22 13:48:29 -0700 | [diff] [blame] | 21 | import android.content.Context; |
| 22 | import android.content.Intent; |
| 23 | import android.content.IntentFilter; |
Sunny Goyal | fdbef27 | 2017-02-01 12:52:54 -0800 | [diff] [blame] | 24 | import android.os.Looper; |
Daniel Sandler | b9eb286 | 2013-06-14 20:17:30 -0400 | [diff] [blame] | 25 | import android.util.Log; |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 26 | |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 27 | import com.android.launcher3.compat.LauncherAppsCompat; |
Sunny Goyal | 4390ace | 2014-10-13 11:33:11 -0700 | [diff] [blame] | 28 | import com.android.launcher3.compat.PackageInstallerCompat; |
Sunny Goyal | 823fd50 | 2015-08-04 11:40:13 -0700 | [diff] [blame] | 29 | import com.android.launcher3.compat.UserManagerCompat; |
Tony | d48710c | 2017-07-27 23:23:58 -0700 | [diff] [blame] | 30 | import com.android.launcher3.notification.NotificationListener; |
Sunny Goyal | ae50284 | 2016-06-17 08:43:56 -0700 | [diff] [blame] | 31 | import com.android.launcher3.util.ConfigMonitor; |
Sunny Goyal | fdbef27 | 2017-02-01 12:52:54 -0800 | [diff] [blame] | 32 | import com.android.launcher3.util.Preconditions; |
Tony | d48710c | 2017-07-27 23:23:58 -0700 | [diff] [blame] | 33 | import com.android.launcher3.util.SettingsObserver; |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 34 | |
Sunny Goyal | fdbef27 | 2017-02-01 12:52:54 -0800 | [diff] [blame] | 35 | import java.util.concurrent.Callable; |
| 36 | import java.util.concurrent.ExecutionException; |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 37 | |
Tony | d48710c | 2017-07-27 23:23:58 -0700 | [diff] [blame] | 38 | import static com.android.launcher3.SettingsActivity.NOTIFICATION_BADGING; |
| 39 | |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame] | 40 | public class LauncherAppState { |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 41 | |
Sunny Goyal | fdbef27 | 2017-02-01 12:52:54 -0800 | [diff] [blame] | 42 | // We do not need any synchronization for this variable as its only written on UI thread. |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 43 | private static LauncherAppState INSTANCE; |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 44 | |
Sunny Goyal | fdbef27 | 2017-02-01 12:52:54 -0800 | [diff] [blame] | 45 | private final Context mContext; |
| 46 | private final LauncherModel mModel; |
| 47 | private final IconCache mIconCache; |
| 48 | private final WidgetPreviewLoader mWidgetCache; |
| 49 | private final InvariantDeviceProfile mInvariantDeviceProfile; |
Tony | d48710c | 2017-07-27 23:23:58 -0700 | [diff] [blame] | 50 | private final SettingsObserver mNotificationBadgingObserver; |
Sunny Goyal | fdbef27 | 2017-02-01 12:52:54 -0800 | [diff] [blame] | 51 | |
| 52 | public static LauncherAppState getInstance(final Context context) { |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 53 | if (INSTANCE == null) { |
Sunny Goyal | fdbef27 | 2017-02-01 12:52:54 -0800 | [diff] [blame] | 54 | 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 Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 68 | } |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 69 | return INSTANCE; |
| 70 | } |
| 71 | |
Chris Wren | d8fe6de | 2013-10-04 10:42:14 -0400 | [diff] [blame] | 72 | public static LauncherAppState getInstanceNoCreate() { |
| 73 | return INSTANCE; |
| 74 | } |
| 75 | |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 76 | public Context getContext() { |
Sunny Goyal | fdbef27 | 2017-02-01 12:52:54 -0800 | [diff] [blame] | 77 | return mContext; |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 78 | } |
| 79 | |
Sunny Goyal | fdbef27 | 2017-02-01 12:52:54 -0800 | [diff] [blame] | 80 | private LauncherAppState(Context context) { |
| 81 | if (getLocalProvider(context) == null) { |
| 82 | throw new RuntimeException( |
| 83 | "Initializing LauncherAppState in the absence of LauncherProvider"); |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 84 | } |
Hyunyoung Song | 0de0117 | 2016-10-05 16:27:48 -0700 | [diff] [blame] | 85 | Log.v(Launcher.TAG, "LauncherAppState initiated"); |
Sunny Goyal | fdbef27 | 2017-02-01 12:52:54 -0800 | [diff] [blame] | 86 | Preconditions.assertUIThread(); |
| 87 | mContext = context; |
Daniel Sandler | b9eb286 | 2013-06-14 20:17:30 -0400 | [diff] [blame] | 88 | |
Sunny Goyal | fdbef27 | 2017-02-01 12:52:54 -0800 | [diff] [blame] | 89 | mInvariantDeviceProfile = new InvariantDeviceProfile(mContext); |
| 90 | mIconCache = new IconCache(mContext, mInvariantDeviceProfile); |
| 91 | mWidgetCache = new WidgetPreviewLoader(mContext, mIconCache); |
Sunny Goyal | c6e9769 | 2017-06-02 13:46:55 -0700 | [diff] [blame] | 92 | mModel = new LauncherModel(this, mIconCache, AppFilter.newInstance(mContext)); |
Sunny Goyal | 2759579 | 2015-03-19 13:18:44 -0700 | [diff] [blame] | 93 | |
Sunny Goyal | fdbef27 | 2017-02-01 12:52:54 -0800 | [diff] [blame] | 94 | LauncherAppsCompat.getInstance(mContext).addOnAppsChangedCallback(mModel); |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 95 | |
| 96 | // Register intent receivers |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 97 | IntentFilter filter = new IntentFilter(); |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 98 | filter.addAction(Intent.ACTION_LOCALE_CHANGED); |
Sunny Goyal | 957c13f | 2015-05-01 13:02:20 -0700 | [diff] [blame] | 99 | // For handling managed profiles |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 100 | filter.addAction(Intent.ACTION_MANAGED_PROFILE_ADDED); |
| 101 | filter.addAction(Intent.ACTION_MANAGED_PROFILE_REMOVED); |
| 102 | filter.addAction(Intent.ACTION_MANAGED_PROFILE_AVAILABLE); |
| 103 | filter.addAction(Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE); |
| 104 | filter.addAction(Intent.ACTION_MANAGED_PROFILE_UNLOCKED); |
Sunny Goyal | 957c13f | 2015-05-01 13:02:20 -0700 | [diff] [blame] | 105 | |
Sunny Goyal | fdbef27 | 2017-02-01 12:52:54 -0800 | [diff] [blame] | 106 | mContext.registerReceiver(mModel, filter); |
| 107 | UserManagerCompat.getInstance(mContext).enableAndResetCache(); |
| 108 | new ConfigMonitor(mContext).register(); |
Tony Wickham | 827cef2 | 2016-03-17 15:39:39 -0700 | [diff] [blame] | 109 | |
Tony | d48710c | 2017-07-27 23:23:58 -0700 | [diff] [blame] | 110 | if (!mContext.getResources().getBoolean(R.bool.notification_badging_enabled)) { |
| 111 | mNotificationBadgingObserver = null; |
| 112 | } else { |
| 113 | // Register an observer to rebind the notification listener when badging is re-enabled. |
| 114 | mNotificationBadgingObserver = new SettingsObserver.Secure( |
| 115 | mContext.getContentResolver()) { |
| 116 | @Override |
| 117 | public void onSettingChanged(boolean isNotificationBadgingEnabled) { |
| 118 | if (isNotificationBadgingEnabled) { |
| 119 | NotificationListener.requestRebind(new ComponentName( |
| 120 | mContext, NotificationListener.class)); |
| 121 | } |
| 122 | } |
| 123 | }; |
| 124 | mNotificationBadgingObserver.register(NOTIFICATION_BADGING); |
| 125 | } |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 126 | } |
Kenny Guy | 1317e2d | 2014-05-08 18:52:50 +0100 | [diff] [blame] | 127 | |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 128 | /** |
| 129 | * Call from Application.onTerminate(), which is not guaranteed to ever be called. |
| 130 | */ |
| 131 | public void onTerminate() { |
Sunny Goyal | fdbef27 | 2017-02-01 12:52:54 -0800 | [diff] [blame] | 132 | mContext.unregisterReceiver(mModel); |
| 133 | final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(mContext); |
Kenny Guy | c2bd810 | 2014-06-30 12:30:31 +0100 | [diff] [blame] | 134 | launcherApps.removeOnAppsChangedCallback(mModel); |
Sunny Goyal | fdbef27 | 2017-02-01 12:52:54 -0800 | [diff] [blame] | 135 | PackageInstallerCompat.getInstance(mContext).onStop(); |
Tony | d48710c | 2017-07-27 23:23:58 -0700 | [diff] [blame] | 136 | if (mNotificationBadgingObserver != null) { |
| 137 | mNotificationBadgingObserver.unregister(); |
| 138 | } |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 139 | } |
| 140 | |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 141 | LauncherModel setLauncher(Launcher launcher) { |
Sunny Goyal | fdbef27 | 2017-02-01 12:52:54 -0800 | [diff] [blame] | 142 | getLocalProvider(mContext).setLauncherProviderChangeListener(launcher); |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 143 | mModel.initialize(launcher); |
| 144 | return mModel; |
| 145 | } |
| 146 | |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 147 | public IconCache getIconCache() { |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 148 | return mIconCache; |
| 149 | } |
| 150 | |
Sunny Goyal | 18bf8e2 | 2015-04-08 18:13:46 -0700 | [diff] [blame] | 151 | public LauncherModel getModel() { |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 152 | return mModel; |
| 153 | } |
| 154 | |
Sunny Goyal | 5b0e669 | 2015-03-19 14:31:19 -0700 | [diff] [blame] | 155 | public WidgetPreviewLoader getWidgetCache() { |
| 156 | return mWidgetCache; |
| 157 | } |
Michael Jurka | a6a0547 | 2013-11-13 17:59:46 +0100 | [diff] [blame] | 158 | |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 159 | public InvariantDeviceProfile getInvariantDeviceProfile() { |
| 160 | return mInvariantDeviceProfile; |
| 161 | } |
Sunny Goyal | 87f784c | 2017-01-11 10:48:34 -0800 | [diff] [blame] | 162 | |
| 163 | /** |
| 164 | * Shorthand for {@link #getInvariantDeviceProfile()} |
| 165 | */ |
| 166 | public static InvariantDeviceProfile getIDP(Context context) { |
| 167 | return LauncherAppState.getInstance(context).getInvariantDeviceProfile(); |
| 168 | } |
Sunny Goyal | fdbef27 | 2017-02-01 12:52:54 -0800 | [diff] [blame] | 169 | |
| 170 | private static LauncherProvider getLocalProvider(Context context) { |
| 171 | try (ContentProviderClient cl = context.getContentResolver() |
| 172 | .acquireContentProviderClient(LauncherProvider.AUTHORITY)) { |
| 173 | return (LauncherProvider) cl.getLocalContentProvider(); |
| 174 | } |
| 175 | } |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 176 | } |