LauncherApplication.java revision e9ad59eba6d8ffd2cbf28520c237ccefd291a33c
1/*
2 * Copyright (C) 2008 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.launcher2;
18
19import android.app.Application;
20import android.content.ContentResolver;
21import android.content.Intent;
22import android.content.IntentFilter;
23import android.content.res.Configuration;
24import android.database.ContentObserver;
25import android.os.Handler;
26import dalvik.system.VMRuntime;
27
28public class LauncherApplication extends Application {
29    public LauncherModel mModel;
30    public IconCache mIconCache;
31    private static boolean sIsScreenXLarge;
32    private static float sScreenDensity;
33    private static final boolean ENABLE_ROTATION = false;
34
35    @Override
36    public void onCreate() {
37        VMRuntime.getRuntime().setMinimumHeapSize(4 * 1024 * 1024);
38
39        super.onCreate();
40
41        mIconCache = new IconCache(this);
42        mModel = new LauncherModel(this, mIconCache);
43        sIsScreenXLarge = (getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE;
44        sScreenDensity = getResources().getDisplayMetrics().density;
45
46        // Register intent receivers
47        IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
48        filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
49        filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
50        filter.addDataScheme("package");
51        registerReceiver(mModel, filter);
52        filter = new IntentFilter();
53        filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);
54        filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
55        filter.addAction(Intent.ACTION_LOCALE_CHANGED);
56        registerReceiver(mModel, filter);
57
58        // Register for changes to the favorites
59        ContentResolver resolver = getContentResolver();
60        resolver.registerContentObserver(LauncherSettings.Favorites.CONTENT_URI, true,
61                mFavoritesObserver);
62    }
63
64    /**
65     * There's no guarantee that this function is ever called.
66     */
67    @Override
68    public void onTerminate() {
69        super.onTerminate();
70
71        unregisterReceiver(mModel);
72
73        ContentResolver resolver = getContentResolver();
74        resolver.unregisterContentObserver(mFavoritesObserver);
75    }
76
77    /**
78     * Receives notifications whenever the user favorites have changed.
79     */
80    private final ContentObserver mFavoritesObserver = new ContentObserver(new Handler()) {
81        @Override
82        public void onChange(boolean selfChange) {
83            mModel.startLoader(LauncherApplication.this, false);
84        }
85    };
86
87    LauncherModel setLauncher(Launcher launcher) {
88        mModel.initialize(launcher);
89        return mModel;
90    }
91
92    IconCache getIconCache() {
93        return mIconCache;
94    }
95
96    LauncherModel getModel() {
97        return mModel;
98    }
99
100    public static boolean isInPlaceRotationEnabled() {
101        return sIsScreenXLarge && ENABLE_ROTATION;
102    }
103
104    public static boolean isScreenXLarge() {
105        return sIsScreenXLarge;
106    }
107
108    public static float getScreenDensity() {
109        return sScreenDensity;
110    }
111}
112