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 19 import android.content.ContentProviderClient; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.content.IntentFilter; 23 import android.os.Looper; 24 import android.util.Log; 25 26 import com.android.launcher3.compat.LauncherAppsCompat; 27 import com.android.launcher3.compat.PackageInstallerCompat; 28 import com.android.launcher3.compat.UserManagerCompat; 29 import com.android.launcher3.config.ProviderConfig; 30 import com.android.launcher3.dynamicui.ExtractionUtils; 31 import com.android.launcher3.util.ConfigMonitor; 32 import com.android.launcher3.util.Preconditions; 33 import com.android.launcher3.util.TestingUtils; 34 35 import java.util.concurrent.Callable; 36 import java.util.concurrent.ExecutionException; 37 38 public class LauncherAppState { 39 40 public static final boolean PROFILE_STARTUP = ProviderConfig.IS_DOGFOOD_BUILD; 41 42 // We do not need any synchronization for this variable as its only written on UI thread. 43 private static LauncherAppState INSTANCE; 44 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; 50 51 52 public static LauncherAppState getInstance(final Context context) { 53 if (INSTANCE == null) { 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 } 68 } 69 return INSTANCE; 70 } 71 72 public static LauncherAppState getInstanceNoCreate() { 73 return INSTANCE; 74 } 75 76 public Context getContext() { 77 return mContext; 78 } 79 80 private LauncherAppState(Context context) { 81 if (getLocalProvider(context) == null) { 82 throw new RuntimeException( 83 "Initializing LauncherAppState in the absence of LauncherProvider"); 84 } 85 Log.v(Launcher.TAG, "LauncherAppState initiated"); 86 Preconditions.assertUIThread(); 87 mContext = context; 88 89 if (TestingUtils.MEMORY_DUMP_ENABLED) { 90 TestingUtils.startTrackingMemory(mContext); 91 } 92 93 mInvariantDeviceProfile = new InvariantDeviceProfile(mContext); 94 mIconCache = new IconCache(mContext, mInvariantDeviceProfile); 95 mWidgetCache = new WidgetPreviewLoader(mContext, mIconCache); 96 97 mModel = new LauncherModel(this, mIconCache, 98 Utilities.getOverrideObject(AppFilter.class, mContext, R.string.app_filter_class)); 99 100 LauncherAppsCompat.getInstance(mContext).addOnAppsChangedCallback(mModel); 101 102 // Register intent receivers 103 IntentFilter filter = new IntentFilter(); 104 filter.addAction(Intent.ACTION_LOCALE_CHANGED); 105 // For handling managed profiles 106 filter.addAction(Intent.ACTION_MANAGED_PROFILE_ADDED); 107 filter.addAction(Intent.ACTION_MANAGED_PROFILE_REMOVED); 108 filter.addAction(Intent.ACTION_MANAGED_PROFILE_AVAILABLE); 109 filter.addAction(Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE); 110 filter.addAction(Intent.ACTION_MANAGED_PROFILE_UNLOCKED); 111 // For extracting colors from the wallpaper 112 if (Utilities.ATLEAST_NOUGAT) { 113 // TODO: add a broadcast entry to the manifest for pre-N. 114 filter.addAction(Intent.ACTION_WALLPAPER_CHANGED); 115 } 116 117 mContext.registerReceiver(mModel, filter); 118 UserManagerCompat.getInstance(mContext).enableAndResetCache(); 119 new ConfigMonitor(mContext).register(); 120 121 ExtractionUtils.startColorExtractionServiceIfNecessary(mContext); 122 } 123 124 /** 125 * Call from Application.onTerminate(), which is not guaranteed to ever be called. 126 */ 127 public void onTerminate() { 128 mContext.unregisterReceiver(mModel); 129 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(mContext); 130 launcherApps.removeOnAppsChangedCallback(mModel); 131 PackageInstallerCompat.getInstance(mContext).onStop(); 132 } 133 134 LauncherModel setLauncher(Launcher launcher) { 135 getLocalProvider(mContext).setLauncherProviderChangeListener(launcher); 136 mModel.initialize(launcher); 137 return mModel; 138 } 139 140 public IconCache getIconCache() { 141 return mIconCache; 142 } 143 144 public LauncherModel getModel() { 145 return mModel; 146 } 147 148 public WidgetPreviewLoader getWidgetCache() { 149 return mWidgetCache; 150 } 151 152 public InvariantDeviceProfile getInvariantDeviceProfile() { 153 return mInvariantDeviceProfile; 154 } 155 156 /** 157 * Shorthand for {@link #getInvariantDeviceProfile()} 158 */ 159 public static InvariantDeviceProfile getIDP(Context context) { 160 return LauncherAppState.getInstance(context).getInvariantDeviceProfile(); 161 } 162 163 private static LauncherProvider getLocalProvider(Context context) { 164 try (ContentProviderClient cl = context.getContentResolver() 165 .acquireContentProviderClient(LauncherProvider.AUTHORITY)) { 166 return (LauncherProvider) cl.getLocalContentProvider(); 167 } 168 } 169 } 170