LauncherAppState.java revision 713edfce264db7edc409216d5c083f8dd6a7083f
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
17package com.android.launcher3;
18
19import android.app.SearchManager;
20import android.content.BroadcastReceiver;
21import android.content.Context;
22import android.content.Intent;
23import android.content.IntentFilter;
24import android.util.Log;
25
26import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
27import com.android.launcher3.compat.LauncherAppsCompat;
28import com.android.launcher3.compat.PackageInstallerCompat;
29import com.android.launcher3.compat.UserManagerCompat;
30import com.android.launcher3.config.FeatureFlags;
31import com.android.launcher3.dynamicui.ExtractionUtils;
32import com.android.launcher3.util.ConfigMonitor;
33import com.android.launcher3.logging.FileLog;
34import com.android.launcher3.util.TestingUtils;
35import com.android.launcher3.util.Thunk;
36
37import java.lang.ref.WeakReference;
38
39public class LauncherAppState {
40
41    private final AppFilter mAppFilter;
42    @Thunk final LauncherModel mModel;
43    private final IconCache mIconCache;
44    private final WidgetPreviewLoader mWidgetCache;
45
46    @Thunk boolean mWallpaperChangedSinceLastCheck;
47
48    private static WeakReference<LauncherProvider> sLauncherProvider;
49    private static Context sContext;
50
51    private static LauncherAppState INSTANCE;
52
53    private InvariantDeviceProfile mInvariantDeviceProfile;
54
55    private LauncherAccessibilityDelegate mAccessibilityDelegate;
56
57    public static LauncherAppState getInstance() {
58        if (INSTANCE == null) {
59            INSTANCE = new LauncherAppState();
60        }
61        return INSTANCE;
62    }
63
64    public static LauncherAppState getInstanceNoCreate() {
65        return INSTANCE;
66    }
67
68    public Context getContext() {
69        return sContext;
70    }
71
72    static void setLauncherProvider(LauncherProvider provider) {
73        if (sLauncherProvider != null) {
74            Log.w(Launcher.TAG, "setLauncherProvider called twice! old=" +
75                    sLauncherProvider.get() + " new=" + provider);
76        }
77        sLauncherProvider = new WeakReference<>(provider);
78
79        // The content provider exists for the entire duration of the launcher main process and
80        // is the first component to get created. Initializing application context here ensures
81        // that LauncherAppState always exists in the main process.
82        sContext = provider.getContext().getApplicationContext();
83        FileLog.setDir(sContext.getFilesDir());
84    }
85
86    private LauncherAppState() {
87        if (sContext == null) {
88            throw new IllegalStateException("LauncherAppState inited before app context set");
89        }
90
91        Log.v(Launcher.TAG, "LauncherAppState inited");
92
93        if (TestingUtils.MEMORY_DUMP_ENABLED) {
94            TestingUtils.startTrackingMemory(sContext);
95        }
96
97        mInvariantDeviceProfile = new InvariantDeviceProfile(sContext);
98        mIconCache = new IconCache(sContext, mInvariantDeviceProfile);
99        mWidgetCache = new WidgetPreviewLoader(sContext, mIconCache);
100
101        mAppFilter = AppFilter.loadByName(sContext.getString(R.string.app_filter_class));
102        mModel = new LauncherModel(this, mIconCache, mAppFilter);
103
104        LauncherAppsCompat.getInstance(sContext).addOnAppsChangedCallback(mModel);
105
106        // Register intent receivers
107        IntentFilter filter = new IntentFilter();
108        filter.addAction(Intent.ACTION_LOCALE_CHANGED);
109        filter.addAction(SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED);
110        // For handling managed profiles
111        filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED);
112        filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED);
113        filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_AVAILABLE);
114        filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_UNAVAILABLE);
115        // For extracting colors from the wallpaper
116        if (Utilities.isNycOrAbove()) {
117            // TODO: add a broadcast entry to the manifest for pre-N.
118            filter.addAction(Intent.ACTION_WALLPAPER_CHANGED);
119        }
120
121        sContext.registerReceiver(mModel, filter);
122        UserManagerCompat.getInstance(sContext).enableAndResetCache();
123        if (!Utilities.ATLEAST_KITKAT) {
124            sContext.registerReceiver(new BroadcastReceiver() {
125
126                @Override
127                public void onReceive(Context context, Intent intent) {
128                    mWallpaperChangedSinceLastCheck = true;
129                }
130            }, new IntentFilter(Intent.ACTION_WALLPAPER_CHANGED));
131        }
132        new ConfigMonitor(sContext).register();
133
134        ExtractionUtils.startColorExtractionServiceIfNecessary(sContext);
135    }
136
137    /**
138     * Call from Application.onTerminate(), which is not guaranteed to ever be called.
139     */
140    public void onTerminate() {
141        sContext.unregisterReceiver(mModel);
142        final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(sContext);
143        launcherApps.removeOnAppsChangedCallback(mModel);
144        PackageInstallerCompat.getInstance(sContext).onStop();
145    }
146
147    /**
148     * Reloads the workspace items from the DB and re-binds the workspace. This should generally
149     * not be called as DB updates are automatically followed by UI update
150     */
151    public void reloadWorkspace() {
152        mModel.resetLoadedState(false, true);
153        mModel.startLoaderFromBackground();
154    }
155
156    LauncherModel setLauncher(Launcher launcher) {
157        sLauncherProvider.get().setLauncherProviderChangeListener(launcher);
158        mModel.initialize(launcher);
159        mAccessibilityDelegate = ((launcher != null) && Utilities.ATLEAST_LOLLIPOP) ?
160            new LauncherAccessibilityDelegate(launcher) : null;
161        return mModel;
162    }
163
164    public LauncherAccessibilityDelegate getAccessibilityDelegate() {
165        return mAccessibilityDelegate;
166    }
167
168    public IconCache getIconCache() {
169        return mIconCache;
170    }
171
172    public LauncherModel getModel() {
173        return mModel;
174    }
175
176    public WidgetPreviewLoader getWidgetCache() {
177        return mWidgetCache;
178    }
179
180    public boolean hasWallpaperChangedSinceLastCheck() {
181        boolean result = mWallpaperChangedSinceLastCheck;
182        mWallpaperChangedSinceLastCheck = false;
183        return result;
184    }
185
186    public InvariantDeviceProfile getInvariantDeviceProfile() {
187        return mInvariantDeviceProfile;
188    }
189
190    public static boolean isDogfoodBuild() {
191        return FeatureFlags.IS_ALPHA_BUILD || FeatureFlags.IS_DEV_BUILD;
192    }
193}
194