2012-04-03 11:58:01 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
package org.mozilla.gecko;
|
|
|
|
|
2013-04-05 08:37:28 -07:00
|
|
|
import org.mozilla.gecko.db.BrowserContract;
|
|
|
|
import org.mozilla.gecko.db.BrowserDB;
|
2014-04-23 07:22:34 -07:00
|
|
|
import org.mozilla.gecko.home.HomePanelsManager;
|
2013-03-08 00:27:46 -08:00
|
|
|
import org.mozilla.gecko.mozglue.GeckoLoader;
|
2013-06-07 13:39:37 -07:00
|
|
|
import org.mozilla.gecko.util.Clipboard;
|
2013-04-02 07:03:39 -07:00
|
|
|
import org.mozilla.gecko.util.HardwareUtils;
|
2013-04-05 08:37:28 -07:00
|
|
|
import org.mozilla.gecko.util.ThreadUtils;
|
2012-04-03 11:58:01 -07:00
|
|
|
|
2013-04-08 00:51:56 -07:00
|
|
|
import android.app.Application;
|
2014-02-27 12:32:34 -08:00
|
|
|
import android.content.Context;
|
2014-04-02 18:33:24 -07:00
|
|
|
import android.content.SharedPreferences;
|
2013-12-03 15:05:43 -08:00
|
|
|
import android.content.res.Configuration;
|
|
|
|
import android.util.Log;
|
2013-04-08 00:51:56 -07:00
|
|
|
|
2014-04-02 18:33:24 -07:00
|
|
|
public class GeckoApplication extends Application
|
|
|
|
implements ContextGetter {
|
2013-12-03 15:05:43 -08:00
|
|
|
private static final String LOG_TAG = "GeckoApplication";
|
2012-07-17 17:54:54 -07:00
|
|
|
|
2014-04-02 18:33:24 -07:00
|
|
|
private static volatile GeckoApplication instance;
|
|
|
|
|
2012-09-14 08:19:40 -07:00
|
|
|
private boolean mInBackground;
|
2013-01-29 13:59:42 -08:00
|
|
|
private boolean mPausedGecko;
|
2012-04-03 11:58:01 -07:00
|
|
|
|
2012-10-31 22:10:59 -07:00
|
|
|
private LightweightTheme mLightweightTheme;
|
|
|
|
|
2014-04-02 18:33:24 -07:00
|
|
|
public GeckoApplication() {
|
|
|
|
super();
|
|
|
|
instance = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static GeckoApplication get() {
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Context getContext() {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public SharedPreferences getSharedPreferences() {
|
|
|
|
return GeckoSharedPrefs.forApp(this);
|
|
|
|
}
|
|
|
|
|
2013-12-03 15:05:43 -08:00
|
|
|
/**
|
|
|
|
* We need to do locale work here, because we need to intercept
|
|
|
|
* each hit to onConfigurationChanged.
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public void onConfigurationChanged(Configuration config) {
|
2013-12-03 15:05:43 -08:00
|
|
|
Log.d(LOG_TAG, "onConfigurationChanged: " + config.locale +
|
|
|
|
", background: " + mInBackground);
|
2013-12-03 15:05:43 -08:00
|
|
|
|
|
|
|
// Do nothing if we're in the background. It'll simply cause a loop
|
|
|
|
// (Bug 936756 Comment 11), and it's not necessary.
|
|
|
|
if (mInBackground) {
|
|
|
|
super.onConfigurationChanged(config);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, correct the locale. This catches some cases that GeckoApp
|
|
|
|
// doesn't get a chance to.
|
2013-12-03 15:05:43 -08:00
|
|
|
try {
|
2014-04-15 15:07:14 -07:00
|
|
|
BrowserLocaleManager.getInstance().correctLocale(this, getResources(), config);
|
2013-12-03 15:05:43 -08:00
|
|
|
} catch (IllegalStateException ex) {
|
2014-04-15 15:07:14 -07:00
|
|
|
// GeckoApp hasn't started, so we have no ContextGetter in BrowserLocaleManager.
|
2013-12-03 15:05:43 -08:00
|
|
|
Log.w(LOG_TAG, "Couldn't correct locale.", ex);
|
|
|
|
}
|
|
|
|
|
2013-12-03 15:05:43 -08:00
|
|
|
super.onConfigurationChanged(config);
|
|
|
|
}
|
|
|
|
|
2013-11-11 10:41:16 -08:00
|
|
|
public void onActivityPause(GeckoActivityStatus activity) {
|
2012-05-08 16:40:12 -07:00
|
|
|
mInBackground = true;
|
|
|
|
|
2013-01-29 13:59:42 -08:00
|
|
|
if ((activity.isFinishing() == false) &&
|
|
|
|
(activity.isGeckoActivityOpened() == false)) {
|
|
|
|
// Notify Gecko that we are pausing; the cache service will be
|
|
|
|
// shutdown, closing the disk cache cleanly. If the android
|
|
|
|
// low memory killer subsequently kills us, the disk cache will
|
|
|
|
// be left in a consistent state, avoiding costly cleanup and
|
|
|
|
// re-creation.
|
2013-03-26 11:05:10 -07:00
|
|
|
GeckoAppShell.sendEventToGecko(GeckoEvent.createAppBackgroundingEvent());
|
2013-01-29 13:59:42 -08:00
|
|
|
mPausedGecko = true;
|
2013-04-05 08:37:28 -07:00
|
|
|
|
|
|
|
ThreadUtils.postToBackgroundThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
BrowserDB.expireHistory(getContentResolver(),
|
|
|
|
BrowserContract.ExpirePriority.NORMAL);
|
|
|
|
}
|
|
|
|
});
|
2013-01-29 13:59:42 -08:00
|
|
|
}
|
2012-09-14 08:19:40 -07:00
|
|
|
GeckoConnectivityReceiver.getInstance().stop();
|
|
|
|
GeckoNetworkManager.getInstance().stop();
|
2012-04-03 11:58:01 -07:00
|
|
|
}
|
|
|
|
|
2013-11-11 10:41:16 -08:00
|
|
|
public void onActivityResume(GeckoActivityStatus activity) {
|
2013-01-29 13:59:42 -08:00
|
|
|
if (mPausedGecko) {
|
2013-03-26 11:05:10 -07:00
|
|
|
GeckoAppShell.sendEventToGecko(GeckoEvent.createAppForegroundingEvent());
|
2013-01-29 13:59:42 -08:00
|
|
|
mPausedGecko = false;
|
|
|
|
}
|
2014-02-27 12:32:34 -08:00
|
|
|
|
|
|
|
final Context applicationContext = getApplicationContext();
|
|
|
|
GeckoBatteryManager.getInstance().start(applicationContext);
|
|
|
|
GeckoConnectivityReceiver.getInstance().start(applicationContext);
|
|
|
|
GeckoNetworkManager.getInstance().start(applicationContext);
|
2012-05-08 16:40:12 -07:00
|
|
|
|
|
|
|
mInBackground = false;
|
|
|
|
}
|
|
|
|
|
2013-03-08 00:27:46 -08:00
|
|
|
@Override
|
|
|
|
public void onCreate() {
|
2014-07-24 09:39:48 -07:00
|
|
|
final Context context = getApplicationContext();
|
|
|
|
HardwareUtils.init(context);
|
|
|
|
Clipboard.init(context);
|
|
|
|
FilePicker.init(context);
|
|
|
|
GeckoLoader.loadMozGlue(context);
|
|
|
|
HomePanelsManager.getInstance().init(context);
|
|
|
|
|
|
|
|
// This getInstance call will force initialization of the NotificationHelper, but does nothing with the result
|
|
|
|
NotificationHelper.getInstance(context).init();
|
2013-03-08 00:27:46 -08:00
|
|
|
super.onCreate();
|
|
|
|
}
|
|
|
|
|
2012-05-08 16:40:12 -07:00
|
|
|
public boolean isApplicationInBackground() {
|
|
|
|
return mInBackground;
|
2012-04-03 11:58:01 -07:00
|
|
|
}
|
2012-10-31 22:10:59 -07:00
|
|
|
|
|
|
|
public LightweightTheme getLightweightTheme() {
|
|
|
|
return mLightweightTheme;
|
|
|
|
}
|
2014-02-27 12:32:34 -08:00
|
|
|
|
|
|
|
public void prepareLightweightTheme() {
|
|
|
|
mLightweightTheme = new LightweightTheme(this);
|
|
|
|
}
|
2012-04-03 11:58:01 -07:00
|
|
|
}
|