LauncherAppState.java revision c268148f06f5c6c30deb1678e74530ef5aa85a67
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.Context;
21import android.content.Intent;
22import android.content.IntentFilter;
23import android.util.Log;
24
25import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
26import com.android.launcher3.compat.LauncherAppsCompat;
27import com.android.launcher3.compat.PackageInstallerCompat;
28import com.android.launcher3.compat.UserManagerCompat;
29import com.android.launcher3.util.Thunk;
30
31import java.lang.ref.WeakReference;
32
33public class LauncherAppState {
34
35    private final AppFilter mAppFilter;
36    @Thunk final LauncherModel mModel;
37    private final IconCache mIconCache;
38    private final WidgetPreviewLoader mWidgetCache;
39
40    private boolean mWallpaperChangedSinceLastCheck;
41
42    private static WeakReference<LauncherProvider> sLauncherProvider;
43    private static Context sContext;
44
45    private static LauncherAppState INSTANCE;
46
47    private InvariantDeviceProfile mInvariantDeviceProfile;
48
49    private LauncherAccessibilityDelegate mAccessibilityDelegate;
50
51    public static LauncherAppState getInstance() {
52        if (INSTANCE == null) {
53            INSTANCE = new LauncherAppState();
54        }
55        return INSTANCE;
56    }
57
58    public static LauncherAppState getInstanceNoCreate() {
59        return INSTANCE;
60    }
61
62    public Context getContext() {
63        return sContext;
64    }
65
66    public static void setApplicationContext(Context context) {
67        if (sContext != null) {
68            Log.w(Launcher.TAG, "setApplicationContext called twice! old=" + sContext + " new=" + context);
69        }
70        sContext = context.getApplicationContext();
71    }
72
73    private LauncherAppState() {
74        if (sContext == null) {
75            throw new IllegalStateException("LauncherAppState inited before app context set");
76        }
77
78        Log.v(Launcher.TAG, "LauncherAppState inited");
79
80        if (sContext.getResources().getBoolean(R.bool.debug_memory_enabled)) {
81            MemoryTracker.startTrackingMe(sContext, "L");
82        }
83
84        mInvariantDeviceProfile = new InvariantDeviceProfile(sContext);
85        mIconCache = new IconCache(sContext, mInvariantDeviceProfile);
86        mWidgetCache = new WidgetPreviewLoader(sContext, mIconCache);
87
88        mAppFilter = AppFilter.loadByName(sContext.getString(R.string.app_filter_class));
89        mModel = new LauncherModel(this, mIconCache, mAppFilter);
90
91        LauncherAppsCompat.getInstance(sContext).addOnAppsChangedCallback(mModel);
92
93        // Register intent receivers
94        IntentFilter filter = new IntentFilter();
95        filter.addAction(Intent.ACTION_LOCALE_CHANGED);
96        filter.addAction(SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED);
97        // For handling managed profiles
98        filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED);
99        filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED);
100
101        sContext.registerReceiver(mModel, filter);
102        UserManagerCompat.getInstance(sContext).enableAndResetCache();
103    }
104
105    /**
106     * Call from Application.onTerminate(), which is not guaranteed to ever be called.
107     */
108    public void onTerminate() {
109        sContext.unregisterReceiver(mModel);
110        final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(sContext);
111        launcherApps.removeOnAppsChangedCallback(mModel);
112        PackageInstallerCompat.getInstance(sContext).onStop();
113    }
114
115    /**
116     * Reloads the workspace items from the DB and re-binds the workspace. This should generally
117     * not be called as DB updates are automatically followed by UI update
118     */
119    public void reloadWorkspace() {
120        mModel.resetLoadedState(false, true);
121        mModel.startLoaderFromBackground();
122    }
123
124    LauncherModel setLauncher(Launcher launcher) {
125        getLauncherProvider().setLauncherProviderChangeListener(launcher);
126        mModel.initialize(launcher);
127        mAccessibilityDelegate = ((launcher != null) && Utilities.ATLEAST_LOLLIPOP) ?
128            new LauncherAccessibilityDelegate(launcher) : null;
129        return mModel;
130    }
131
132    public LauncherAccessibilityDelegate getAccessibilityDelegate() {
133        return mAccessibilityDelegate;
134    }
135
136    public IconCache getIconCache() {
137        return mIconCache;
138    }
139
140    public LauncherModel getModel() {
141        return mModel;
142    }
143
144    static void setLauncherProvider(LauncherProvider provider) {
145        sLauncherProvider = new WeakReference<LauncherProvider>(provider);
146    }
147
148    public static LauncherProvider getLauncherProvider() {
149        return sLauncherProvider.get();
150    }
151
152    public static String getSharedPreferencesKey() {
153        return LauncherFiles.SHARED_PREFERENCES_KEY;
154    }
155
156    public WidgetPreviewLoader getWidgetCache() {
157        return mWidgetCache;
158    }
159
160    public void onWallpaperChanged() {
161        mWallpaperChangedSinceLastCheck = true;
162    }
163
164    public boolean hasWallpaperChangedSinceLastCheck() {
165        boolean result = mWallpaperChangedSinceLastCheck;
166        mWallpaperChangedSinceLastCheck = false;
167        return result;
168    }
169
170    public InvariantDeviceProfile getInvariantDeviceProfile() {
171        return mInvariantDeviceProfile;
172    }
173}
174