LauncherAppState.java revision 9637364c447cd8df12dd70d3c769d101b7565df7
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.ComponentName;
21import android.content.Context;
22import android.content.Intent;
23import android.content.IntentFilter;
24import android.content.res.Resources;
25import android.util.Log;
26
27import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
28import com.android.launcher3.compat.LauncherAppsCompat;
29import com.android.launcher3.compat.PackageInstallerCompat;
30import com.android.launcher3.util.Thunk;
31
32import java.lang.ref.WeakReference;
33
34public class LauncherAppState {
35
36    private final AppFilter mAppFilter;
37    private final BuildInfo mBuildInfo;
38    @Thunk final LauncherModel mModel;
39    private final IconCache mIconCache;
40    private final WidgetPreviewLoader mWidgetCache;
41
42    private final boolean mIsScreenLarge;
43    private final int mLongPressTimeout = 300;
44
45    private 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    private LauncherAccessibilityDelegate mAccessibilityDelegate;
54
55    public static LauncherAppState getInstance() {
56        if (INSTANCE == null) {
57            INSTANCE = new LauncherAppState();
58        }
59        return INSTANCE;
60    }
61
62    public static LauncherAppState getInstanceNoCreate() {
63        return INSTANCE;
64    }
65
66    public Context getContext() {
67        return sContext;
68    }
69
70    public static void setApplicationContext(Context context) {
71        if (sContext != null) {
72            Log.w(Launcher.TAG, "setApplicationContext called twice! old=" + sContext + " new=" + context);
73        }
74        sContext = context.getApplicationContext();
75    }
76
77    private LauncherAppState() {
78        if (sContext == null) {
79            throw new IllegalStateException("LauncherAppState inited before app context set");
80        }
81
82        Log.v(Launcher.TAG, "LauncherAppState inited");
83
84        if (sContext.getResources().getBoolean(R.bool.debug_memory_enabled)) {
85            MemoryTracker.startTrackingMe(sContext, "L");
86        }
87
88        // set sIsScreenXLarge and mScreenDensity *before* creating icon cache
89        mIsScreenLarge = isScreenLarge(sContext.getResources());
90        mInvariantDeviceProfile = new InvariantDeviceProfile(sContext);
91        mIconCache = new IconCache(sContext, mInvariantDeviceProfile);
92        mWidgetCache = new WidgetPreviewLoader(sContext, mInvariantDeviceProfile, mIconCache);
93
94        mAppFilter = AppFilter.loadByName(sContext.getString(R.string.app_filter_class));
95        mBuildInfo = BuildInfo.loadByName(sContext.getString(R.string.build_info_class));
96        mModel = new LauncherModel(this, mIconCache, mAppFilter);
97
98        LauncherAppsCompat.getInstance(sContext).addOnAppsChangedCallback(mModel);
99
100        // Register intent receivers
101        IntentFilter filter = new IntentFilter();
102        filter.addAction(Intent.ACTION_LOCALE_CHANGED);
103        filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
104        filter.addAction(SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED);
105        filter.addAction(SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED);
106        // For handling managed profiles
107        filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED);
108        filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED);
109
110        sContext.registerReceiver(mModel, filter);
111    }
112
113    /**
114     * Call from Application.onTerminate(), which is not guaranteed to ever be called.
115     */
116    public void onTerminate() {
117        sContext.unregisterReceiver(mModel);
118        final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(sContext);
119        launcherApps.removeOnAppsChangedCallback(mModel);
120        PackageInstallerCompat.getInstance(sContext).onStop();
121    }
122
123    /**
124     * Reloads the workspace items from the DB and re-binds the workspace. This should generally
125     * not be called as DB updates are automatically followed by UI update
126     */
127    public void reloadWorkspace() {
128        mModel.resetLoadedState(false, true);
129        mModel.startLoaderFromBackground();
130    }
131
132    LauncherModel setLauncher(Launcher launcher) {
133        mModel.initialize(launcher);
134        mAccessibilityDelegate = ((launcher != null) && Utilities.isLmpOrAbove()) ?
135            new LauncherAccessibilityDelegate(launcher) : null;
136        return mModel;
137    }
138
139    public LauncherAccessibilityDelegate getAccessibilityDelegate() {
140        return mAccessibilityDelegate;
141    }
142
143    public IconCache getIconCache() {
144        return mIconCache;
145    }
146
147    public LauncherModel getModel() {
148        return mModel;
149    }
150
151    boolean shouldShowAppOrWidgetProvider(ComponentName componentName) {
152        return mAppFilter == null || mAppFilter.shouldShowApp(componentName);
153    }
154
155    static void setLauncherProvider(LauncherProvider provider) {
156        sLauncherProvider = new WeakReference<LauncherProvider>(provider);
157    }
158
159    static LauncherProvider getLauncherProvider() {
160        return sLauncherProvider.get();
161    }
162
163    public static String getSharedPreferencesKey() {
164        return LauncherFiles.SHARED_PREFERENCES_KEY;
165    }
166
167    public WidgetPreviewLoader getWidgetCache() {
168        return mWidgetCache;
169    }
170
171    public boolean isScreenLarge() {
172        return mIsScreenLarge;
173    }
174
175    // Need a version that doesn't require an instance of LauncherAppState for the wallpaper picker
176    public static boolean isScreenLarge(Resources res) {
177        return res.getBoolean(R.bool.is_large_tablet);
178    }
179
180    public int getLongPressTimeout() {
181        return mLongPressTimeout;
182    }
183
184    public void onWallpaperChanged() {
185        mWallpaperChangedSinceLastCheck = true;
186    }
187
188    public boolean hasWallpaperChangedSinceLastCheck() {
189        boolean result = mWallpaperChangedSinceLastCheck;
190        mWallpaperChangedSinceLastCheck = false;
191        return result;
192    }
193
194    public InvariantDeviceProfile getInvariantDeviceProfile() {
195        return mInvariantDeviceProfile;
196    }
197
198    public static boolean isDogfoodBuild() {
199        return getInstance().mBuildInfo.isDogfoodBuild();
200    }
201}
202