131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project/*
231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * Copyright (C) 2008 The Android Open Source Project
331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project *
431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * you may not use this file except in compliance with the License.
631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * You may obtain a copy of the License at
731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project *
831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project *
1031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
1131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
1231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * See the License for the specific language governing permissions and
1431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * limitations under the License.
1531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project */
1631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
17a5902524d4403885eb4c50360bf3465c6be796efJoe Onoratopackage com.android.launcher2;
1831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
1931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.app.Application;
20cb1a4778686a46c46d8dc88b6c83674f6fac6592Narayan Kamathimport android.app.SearchManager;
219c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onoratoimport android.content.ContentResolver;
224afe9b31f51022538b30646fe83780e83bf22490Winson Chungimport android.content.Context;
239c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onoratoimport android.content.Intent;
249c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onoratoimport android.content.IntentFilter;
25aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chungimport android.content.res.Configuration;
269c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onoratoimport android.database.ContentObserver;
279c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onoratoimport android.os.Handler;
2831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
290dca1ec41479a74f8da080224fa0c7eacab674d6Andrew Flynnimport com.android.launcher.R;
300dca1ec41479a74f8da080224fa0c7eacab674d6Andrew Flynn
31a8c760d395e1d2a78522427738302fbca3a72453Michael Jurkaimport java.lang.ref.WeakReference;
32a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka
3331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectpublic class LauncherApplication extends Application {
340589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    public LauncherModel mModel;
350589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    public IconCache mIconCache;
36a2eb17095dcffa75c1e9681fdc318fe6e0564321Michael Jurka    private static boolean sIsScreenLarge;
378e58e916061cbe2623697efac0924f2aa3753a92Patrick Dubroy    private static float sScreenDensity;
3888f334571fe41df620ba903ced9b2c69b0170d5cWinson Chung    private static int sLongPressTimeout = 300;
39f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung    private static final String sSharedPreferencesKey = "com.android.launcher2.prefs";
40a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka    WeakReference<LauncherProvider> mLauncherProvider;
419c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato
4231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    @Override
4331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    public void onCreate() {
4431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        super.onCreate();
459c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato
46c9a961952d1a057029874f8426b90181f6876034Michael Jurka        // set sIsScreenXLarge and sScreenDensity *before* creating icon cache
470dca1ec41479a74f8da080224fa0c7eacab674d6Andrew Flynn        sIsScreenLarge = getResources().getBoolean(R.bool.is_large_screen);
488e58e916061cbe2623697efac0924f2aa3753a92Patrick Dubroy        sScreenDensity = getResources().getDisplayMetrics().density;
490589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
50c9a961952d1a057029874f8426b90181f6876034Michael Jurka        mIconCache = new IconCache(this);
51c9a961952d1a057029874f8426b90181f6876034Michael Jurka        mModel = new LauncherModel(this, mIconCache);
52c9a961952d1a057029874f8426b90181f6876034Michael Jurka
539c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        // Register intent receivers
549c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
559c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
569c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
579c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        filter.addDataScheme("package");
58f99f8c1ad265cf7bea7e30476911d730b7d0eaaeJoe Onorato        registerReceiver(mModel, filter);
5964e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato        filter = new IntentFilter();
6064e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato        filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);
6164e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato        filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
62e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato        filter.addAction(Intent.ACTION_LOCALE_CHANGED);
6393f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee        filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
6464e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato        registerReceiver(mModel, filter);
65cb1a4778686a46c46d8dc88b6c83674f6fac6592Narayan Kamath        filter = new IntentFilter();
66cb1a4778686a46c46d8dc88b6c83674f6fac6592Narayan Kamath        filter.addAction(SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED);
67cb1a4778686a46c46d8dc88b6c83674f6fac6592Narayan Kamath        registerReceiver(mModel, filter);
68cbf7c4d50ce2609518640c3f590f6df435edabaeWinson Chung        filter = new IntentFilter();
69cbf7c4d50ce2609518640c3f590f6df435edabaeWinson Chung        filter.addAction(SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED);
70cbf7c4d50ce2609518640c3f590f6df435edabaeWinson Chung        registerReceiver(mModel, filter);
719c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato
729c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        // Register for changes to the favorites
739c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        ContentResolver resolver = getContentResolver();
749c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        resolver.registerContentObserver(LauncherSettings.Favorites.CONTENT_URI, true,
759c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                mFavoritesObserver);
769c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
779c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato
789c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
799c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * There's no guarantee that this function is ever called.
809c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
819c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    @Override
829c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    public void onTerminate() {
839c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        super.onTerminate();
849c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato
85f99f8c1ad265cf7bea7e30476911d730b7d0eaaeJoe Onorato        unregisterReceiver(mModel);
869c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato
879c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        ContentResolver resolver = getContentResolver();
889c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        resolver.unregisterContentObserver(mFavoritesObserver);
899c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
909c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato
919c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
929c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Receives notifications whenever the user favorites have changed.
939c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
949c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    private final ContentObserver mFavoritesObserver = new ContentObserver(new Handler()) {
959c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        @Override
969c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        public void onChange(boolean selfChange) {
97f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung            // If the database has ever changed, then we really need to force a reload of the
98f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung            // workspace on the next load
99f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung            mModel.resetLoadedState(false, true);
100f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung            mModel.startLoaderFromBackground();
1019c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        }
1029c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    };
1039c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato
1049c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    LauncherModel setLauncher(Launcher launcher) {
105f99f8c1ad265cf7bea7e30476911d730b7d0eaaeJoe Onorato        mModel.initialize(launcher);
106f99f8c1ad265cf7bea7e30476911d730b7d0eaaeJoe Onorato        return mModel;
10731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
1080589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
1090589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    IconCache getIconCache() {
1100589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        return mIconCache;
1110589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    }
1120589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
1130589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    LauncherModel getModel() {
1140589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        return mModel;
1150589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    }
116aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
117a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka    void setLauncherProvider(LauncherProvider provider) {
118a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka        mLauncherProvider = new WeakReference<LauncherProvider>(provider);
119a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka    }
120a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka
121a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka    LauncherProvider getLauncherProvider() {
122a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka        return mLauncherProvider.get();
123a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka    }
124a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka
125f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung    public static String getSharedPreferencesKey() {
126f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung        return sSharedPreferencesKey;
127f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung    }
128f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung
129a2eb17095dcffa75c1e9681fdc318fe6e0564321Michael Jurka    public static boolean isScreenLarge() {
130a2eb17095dcffa75c1e9681fdc318fe6e0564321Michael Jurka        return sIsScreenLarge;
131aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    }
1328e58e916061cbe2623697efac0924f2aa3753a92Patrick Dubroy
1334afe9b31f51022538b30646fe83780e83bf22490Winson Chung    public static boolean isScreenLandscape(Context context) {
1344afe9b31f51022538b30646fe83780e83bf22490Winson Chung        return context.getResources().getConfiguration().orientation ==
1354afe9b31f51022538b30646fe83780e83bf22490Winson Chung            Configuration.ORIENTATION_LANDSCAPE;
1364afe9b31f51022538b30646fe83780e83bf22490Winson Chung    }
1374afe9b31f51022538b30646fe83780e83bf22490Winson Chung
1388e58e916061cbe2623697efac0924f2aa3753a92Patrick Dubroy    public static float getScreenDensity() {
1398e58e916061cbe2623697efac0924f2aa3753a92Patrick Dubroy        return sScreenDensity;
1408e58e916061cbe2623697efac0924f2aa3753a92Patrick Dubroy    }
14188f334571fe41df620ba903ced9b2c69b0170d5cWinson Chung
14288f334571fe41df620ba903ced9b2c69b0170d5cWinson Chung    public static int getLongPressTimeout() {
14388f334571fe41df620ba903ced9b2c69b0170d5cWinson Chung        return sLongPressTimeout;
14488f334571fe41df620ba903ced9b2c69b0170d5cWinson Chung    }
14531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project}
146