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