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