michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko; michael@0: michael@0: import org.mozilla.gecko.db.BrowserContract; michael@0: import org.mozilla.gecko.db.BrowserDB; michael@0: import org.mozilla.gecko.home.HomePanelsManager; michael@0: import org.mozilla.gecko.mozglue.GeckoLoader; michael@0: import org.mozilla.gecko.util.Clipboard; michael@0: import org.mozilla.gecko.util.HardwareUtils; michael@0: import org.mozilla.gecko.util.ThreadUtils; michael@0: michael@0: import android.app.Application; michael@0: import android.content.Context; michael@0: import android.content.SharedPreferences; michael@0: import android.content.res.Configuration; michael@0: import android.util.Log; michael@0: michael@0: public class GeckoApplication extends Application michael@0: implements ContextGetter { michael@0: private static final String LOG_TAG = "GeckoApplication"; michael@0: michael@0: private static volatile GeckoApplication instance; michael@0: michael@0: private boolean mInBackground; michael@0: private boolean mPausedGecko; michael@0: michael@0: private LightweightTheme mLightweightTheme; michael@0: michael@0: public GeckoApplication() { michael@0: super(); michael@0: instance = this; michael@0: } michael@0: michael@0: public static GeckoApplication get() { michael@0: return instance; michael@0: } michael@0: michael@0: @Override michael@0: public Context getContext() { michael@0: return this; michael@0: } michael@0: michael@0: @Override michael@0: public SharedPreferences getSharedPreferences() { michael@0: return GeckoSharedPrefs.forApp(this); michael@0: } michael@0: michael@0: /** michael@0: * We need to do locale work here, because we need to intercept michael@0: * each hit to onConfigurationChanged. michael@0: */ michael@0: @Override michael@0: public void onConfigurationChanged(Configuration config) { michael@0: Log.d(LOG_TAG, "onConfigurationChanged: " + config.locale + michael@0: ", background: " + mInBackground); michael@0: michael@0: // Do nothing if we're in the background. It'll simply cause a loop michael@0: // (Bug 936756 Comment 11), and it's not necessary. michael@0: if (mInBackground) { michael@0: super.onConfigurationChanged(config); michael@0: return; michael@0: } michael@0: michael@0: // Otherwise, correct the locale. This catches some cases that GeckoApp michael@0: // doesn't get a chance to. michael@0: try { michael@0: BrowserLocaleManager.getInstance().correctLocale(this, getResources(), config); michael@0: } catch (IllegalStateException ex) { michael@0: // GeckoApp hasn't started, so we have no ContextGetter in BrowserLocaleManager. michael@0: Log.w(LOG_TAG, "Couldn't correct locale.", ex); michael@0: } michael@0: michael@0: super.onConfigurationChanged(config); michael@0: } michael@0: michael@0: public void onActivityPause(GeckoActivityStatus activity) { michael@0: mInBackground = true; michael@0: michael@0: if ((activity.isFinishing() == false) && michael@0: (activity.isGeckoActivityOpened() == false)) { michael@0: // Notify Gecko that we are pausing; the cache service will be michael@0: // shutdown, closing the disk cache cleanly. If the android michael@0: // low memory killer subsequently kills us, the disk cache will michael@0: // be left in a consistent state, avoiding costly cleanup and michael@0: // re-creation. michael@0: GeckoAppShell.sendEventToGecko(GeckoEvent.createAppBackgroundingEvent()); michael@0: mPausedGecko = true; michael@0: michael@0: ThreadUtils.postToBackgroundThread(new Runnable() { michael@0: @Override michael@0: public void run() { michael@0: BrowserDB.expireHistory(getContentResolver(), michael@0: BrowserContract.ExpirePriority.NORMAL); michael@0: } michael@0: }); michael@0: } michael@0: GeckoConnectivityReceiver.getInstance().stop(); michael@0: GeckoNetworkManager.getInstance().stop(); michael@0: } michael@0: michael@0: public void onActivityResume(GeckoActivityStatus activity) { michael@0: if (mPausedGecko) { michael@0: GeckoAppShell.sendEventToGecko(GeckoEvent.createAppForegroundingEvent()); michael@0: mPausedGecko = false; michael@0: } michael@0: michael@0: final Context applicationContext = getApplicationContext(); michael@0: GeckoBatteryManager.getInstance().start(applicationContext); michael@0: GeckoConnectivityReceiver.getInstance().start(applicationContext); michael@0: GeckoNetworkManager.getInstance().start(applicationContext); michael@0: michael@0: mInBackground = false; michael@0: } michael@0: michael@0: @Override michael@0: public void onCreate() { michael@0: HardwareUtils.init(getApplicationContext()); michael@0: Clipboard.init(getApplicationContext()); michael@0: FilePicker.init(getApplicationContext()); michael@0: GeckoLoader.loadMozGlue(); michael@0: HomePanelsManager.getInstance().init(getApplicationContext()); michael@0: super.onCreate(); michael@0: } michael@0: michael@0: public boolean isApplicationInBackground() { michael@0: return mInBackground; michael@0: } michael@0: michael@0: public LightweightTheme getLightweightTheme() { michael@0: return mLightweightTheme; michael@0: } michael@0: michael@0: public void prepareLightweightTheme() { michael@0: mLightweightTheme = new LightweightTheme(this); michael@0: } michael@0: }