2013-06-11 14:45:48 -04:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2013 The Android Open Source Project
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package com.android.launcher3;
|
|
|
|
|
|
2018-06-29 14:40:18 -07:00
|
|
|
import static com.android.launcher3.SettingsActivity.NOTIFICATION_BADGING;
|
|
|
|
|
|
2017-07-27 23:23:58 -07:00
|
|
|
import android.content.ComponentName;
|
2017-02-01 12:52:54 -08:00
|
|
|
import android.content.ContentProviderClient;
|
2014-07-22 13:48:29 -07:00
|
|
|
import android.content.Context;
|
|
|
|
|
import android.content.Intent;
|
|
|
|
|
import android.content.IntentFilter;
|
2013-06-14 20:17:30 -04:00
|
|
|
import android.util.Log;
|
2015-01-08 16:59:04 -08:00
|
|
|
|
2014-04-30 03:02:21 +01:00
|
|
|
import com.android.launcher3.compat.LauncherAppsCompat;
|
2014-10-13 11:33:11 -07:00
|
|
|
import com.android.launcher3.compat.PackageInstallerCompat;
|
2015-08-04 11:40:13 -07:00
|
|
|
import com.android.launcher3.compat.UserManagerCompat;
|
2017-12-18 13:49:44 -08:00
|
|
|
import com.android.launcher3.config.FeatureFlags;
|
2018-09-21 14:41:05 -07:00
|
|
|
import com.android.launcher3.icons.IconCache;
|
2017-07-27 23:23:58 -07:00
|
|
|
import com.android.launcher3.notification.NotificationListener;
|
2018-06-29 14:40:18 -07:00
|
|
|
import com.android.launcher3.util.MainThreadInitializedObject;
|
2017-02-01 12:52:54 -08:00
|
|
|
import com.android.launcher3.util.Preconditions;
|
2017-07-27 23:23:58 -07:00
|
|
|
import com.android.launcher3.util.SettingsObserver;
|
2015-01-08 16:59:04 -08:00
|
|
|
|
2015-05-21 20:46:33 -07:00
|
|
|
public class LauncherAppState {
|
2014-02-14 16:59:24 -05:00
|
|
|
|
2017-12-18 13:49:44 -08:00
|
|
|
public static final String ACTION_FORCE_ROLOAD = "force-reload-launcher";
|
|
|
|
|
|
2017-02-01 12:52:54 -08:00
|
|
|
// We do not need any synchronization for this variable as its only written on UI thread.
|
2018-06-29 14:40:18 -07:00
|
|
|
private static final MainThreadInitializedObject<LauncherAppState> INSTANCE =
|
|
|
|
|
new MainThreadInitializedObject<>((c) -> new LauncherAppState(c));
|
2017-02-01 12:52:54 -08:00
|
|
|
|
|
|
|
|
private final Context mContext;
|
|
|
|
|
private final LauncherModel mModel;
|
2014-11-10 18:05:31 -08:00
|
|
|
private final IconCache mIconCache;
|
2015-03-19 14:31:19 -07:00
|
|
|
private final WidgetPreviewLoader mWidgetCache;
|
2017-02-01 12:52:54 -08:00
|
|
|
private final InvariantDeviceProfile mInvariantDeviceProfile;
|
2017-07-27 23:23:58 -07:00
|
|
|
private final SettingsObserver mNotificationBadgingObserver;
|
2013-06-11 14:45:48 -04:00
|
|
|
|
2017-02-01 12:52:54 -08:00
|
|
|
public static LauncherAppState getInstance(final Context context) {
|
2018-06-29 14:40:18 -07:00
|
|
|
return INSTANCE.get(context);
|
2013-06-11 14:45:48 -04:00
|
|
|
}
|
|
|
|
|
|
2013-10-04 10:42:14 -04:00
|
|
|
public static LauncherAppState getInstanceNoCreate() {
|
2018-06-29 14:40:18 -07:00
|
|
|
return INSTANCE.getNoCreate();
|
2013-10-04 10:42:14 -04:00
|
|
|
}
|
|
|
|
|
|
2013-06-11 14:45:48 -04:00
|
|
|
public Context getContext() {
|
2017-02-01 12:52:54 -08:00
|
|
|
return mContext;
|
2013-06-11 14:45:48 -04:00
|
|
|
}
|
|
|
|
|
|
2017-02-01 12:52:54 -08:00
|
|
|
private LauncherAppState(Context context) {
|
|
|
|
|
if (getLocalProvider(context) == null) {
|
|
|
|
|
throw new RuntimeException(
|
|
|
|
|
"Initializing LauncherAppState in the absence of LauncherProvider");
|
2013-06-25 15:13:26 -04:00
|
|
|
}
|
2016-10-05 16:27:48 -07:00
|
|
|
Log.v(Launcher.TAG, "LauncherAppState initiated");
|
2017-02-01 12:52:54 -08:00
|
|
|
Preconditions.assertUIThread();
|
|
|
|
|
mContext = context;
|
2013-06-12 00:38:25 -04:00
|
|
|
|
2018-06-29 14:40:18 -07:00
|
|
|
mInvariantDeviceProfile = InvariantDeviceProfile.INSTANCE.get(mContext);
|
2017-02-01 12:52:54 -08:00
|
|
|
mIconCache = new IconCache(mContext, mInvariantDeviceProfile);
|
|
|
|
|
mWidgetCache = new WidgetPreviewLoader(mContext, mIconCache);
|
2017-06-02 13:46:55 -07:00
|
|
|
mModel = new LauncherModel(this, mIconCache, AppFilter.newInstance(mContext));
|
2015-03-19 13:18:44 -07:00
|
|
|
|
2017-02-01 12:52:54 -08:00
|
|
|
LauncherAppsCompat.getInstance(mContext).addOnAppsChangedCallback(mModel);
|
2013-06-11 14:45:48 -04:00
|
|
|
|
|
|
|
|
// Register intent receivers
|
2014-04-30 03:02:21 +01:00
|
|
|
IntentFilter filter = new IntentFilter();
|
2013-06-11 14:45:48 -04:00
|
|
|
filter.addAction(Intent.ACTION_LOCALE_CHANGED);
|
2015-05-01 13:02:20 -07:00
|
|
|
// For handling managed profiles
|
2016-07-28 12:11:54 -07:00
|
|
|
filter.addAction(Intent.ACTION_MANAGED_PROFILE_ADDED);
|
|
|
|
|
filter.addAction(Intent.ACTION_MANAGED_PROFILE_REMOVED);
|
|
|
|
|
filter.addAction(Intent.ACTION_MANAGED_PROFILE_AVAILABLE);
|
|
|
|
|
filter.addAction(Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE);
|
|
|
|
|
filter.addAction(Intent.ACTION_MANAGED_PROFILE_UNLOCKED);
|
2015-05-01 13:02:20 -07:00
|
|
|
|
2017-12-18 13:49:44 -08:00
|
|
|
if (FeatureFlags.IS_DOGFOOD_BUILD) {
|
|
|
|
|
filter.addAction(ACTION_FORCE_ROLOAD);
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-01 12:52:54 -08:00
|
|
|
mContext.registerReceiver(mModel, filter);
|
|
|
|
|
UserManagerCompat.getInstance(mContext).enableAndResetCache();
|
2016-03-17 15:39:39 -07:00
|
|
|
|
2017-07-27 23:23:58 -07:00
|
|
|
if (!mContext.getResources().getBoolean(R.bool.notification_badging_enabled)) {
|
|
|
|
|
mNotificationBadgingObserver = null;
|
|
|
|
|
} else {
|
|
|
|
|
// Register an observer to rebind the notification listener when badging is re-enabled.
|
|
|
|
|
mNotificationBadgingObserver = new SettingsObserver.Secure(
|
|
|
|
|
mContext.getContentResolver()) {
|
|
|
|
|
@Override
|
|
|
|
|
public void onSettingChanged(boolean isNotificationBadgingEnabled) {
|
|
|
|
|
if (isNotificationBadgingEnabled) {
|
|
|
|
|
NotificationListener.requestRebind(new ComponentName(
|
|
|
|
|
mContext, NotificationListener.class));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
mNotificationBadgingObserver.register(NOTIFICATION_BADGING);
|
|
|
|
|
}
|
2013-06-11 14:45:48 -04:00
|
|
|
}
|
2014-05-08 18:52:50 +01:00
|
|
|
|
2013-06-11 14:45:48 -04:00
|
|
|
/**
|
|
|
|
|
* Call from Application.onTerminate(), which is not guaranteed to ever be called.
|
|
|
|
|
*/
|
|
|
|
|
public void onTerminate() {
|
2017-02-01 12:52:54 -08:00
|
|
|
mContext.unregisterReceiver(mModel);
|
|
|
|
|
final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(mContext);
|
2014-06-30 12:30:31 +01:00
|
|
|
launcherApps.removeOnAppsChangedCallback(mModel);
|
2017-02-01 12:52:54 -08:00
|
|
|
PackageInstallerCompat.getInstance(mContext).onStop();
|
2017-07-27 23:23:58 -07:00
|
|
|
if (mNotificationBadgingObserver != null) {
|
|
|
|
|
mNotificationBadgingObserver.unregister();
|
|
|
|
|
}
|
2013-06-11 14:45:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LauncherModel setLauncher(Launcher launcher) {
|
2017-02-01 12:52:54 -08:00
|
|
|
getLocalProvider(mContext).setLauncherProviderChangeListener(launcher);
|
2013-06-11 14:45:48 -04:00
|
|
|
mModel.initialize(launcher);
|
|
|
|
|
return mModel;
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-29 17:20:55 -07:00
|
|
|
public IconCache getIconCache() {
|
2013-06-11 14:45:48 -04:00
|
|
|
return mIconCache;
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-08 18:13:46 -07:00
|
|
|
public LauncherModel getModel() {
|
2013-06-11 14:45:48 -04:00
|
|
|
return mModel;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-19 14:31:19 -07:00
|
|
|
public WidgetPreviewLoader getWidgetCache() {
|
|
|
|
|
return mWidgetCache;
|
|
|
|
|
}
|
2013-11-13 17:59:46 +01:00
|
|
|
|
2015-05-06 11:42:25 -07:00
|
|
|
public InvariantDeviceProfile getInvariantDeviceProfile() {
|
|
|
|
|
return mInvariantDeviceProfile;
|
|
|
|
|
}
|
2017-01-11 10:48:34 -08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Shorthand for {@link #getInvariantDeviceProfile()}
|
|
|
|
|
*/
|
|
|
|
|
public static InvariantDeviceProfile getIDP(Context context) {
|
2018-06-29 14:40:18 -07:00
|
|
|
return InvariantDeviceProfile.INSTANCE.get(context);
|
2017-01-11 10:48:34 -08:00
|
|
|
}
|
2017-02-01 12:52:54 -08:00
|
|
|
|
|
|
|
|
private static LauncherProvider getLocalProvider(Context context) {
|
|
|
|
|
try (ContentProviderClient cl = context.getContentResolver()
|
|
|
|
|
.acquireContentProviderClient(LauncherProvider.AUTHORITY)) {
|
|
|
|
|
return (LauncherProvider) cl.getLocalContentProvider();
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-06-11 14:45:48 -04:00
|
|
|
}
|