mobile/android/base/GeckoApplication.java

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 package org.mozilla.gecko;
michael@0 6
michael@0 7 import org.mozilla.gecko.db.BrowserContract;
michael@0 8 import org.mozilla.gecko.db.BrowserDB;
michael@0 9 import org.mozilla.gecko.home.HomePanelsManager;
michael@0 10 import org.mozilla.gecko.mozglue.GeckoLoader;
michael@0 11 import org.mozilla.gecko.util.Clipboard;
michael@0 12 import org.mozilla.gecko.util.HardwareUtils;
michael@0 13 import org.mozilla.gecko.util.ThreadUtils;
michael@0 14
michael@0 15 import android.app.Application;
michael@0 16 import android.content.Context;
michael@0 17 import android.content.SharedPreferences;
michael@0 18 import android.content.res.Configuration;
michael@0 19 import android.util.Log;
michael@0 20
michael@0 21 public class GeckoApplication extends Application
michael@0 22 implements ContextGetter {
michael@0 23 private static final String LOG_TAG = "GeckoApplication";
michael@0 24
michael@0 25 private static volatile GeckoApplication instance;
michael@0 26
michael@0 27 private boolean mInBackground;
michael@0 28 private boolean mPausedGecko;
michael@0 29
michael@0 30 private LightweightTheme mLightweightTheme;
michael@0 31
michael@0 32 public GeckoApplication() {
michael@0 33 super();
michael@0 34 instance = this;
michael@0 35 }
michael@0 36
michael@0 37 public static GeckoApplication get() {
michael@0 38 return instance;
michael@0 39 }
michael@0 40
michael@0 41 @Override
michael@0 42 public Context getContext() {
michael@0 43 return this;
michael@0 44 }
michael@0 45
michael@0 46 @Override
michael@0 47 public SharedPreferences getSharedPreferences() {
michael@0 48 return GeckoSharedPrefs.forApp(this);
michael@0 49 }
michael@0 50
michael@0 51 /**
michael@0 52 * We need to do locale work here, because we need to intercept
michael@0 53 * each hit to onConfigurationChanged.
michael@0 54 */
michael@0 55 @Override
michael@0 56 public void onConfigurationChanged(Configuration config) {
michael@0 57 Log.d(LOG_TAG, "onConfigurationChanged: " + config.locale +
michael@0 58 ", background: " + mInBackground);
michael@0 59
michael@0 60 // Do nothing if we're in the background. It'll simply cause a loop
michael@0 61 // (Bug 936756 Comment 11), and it's not necessary.
michael@0 62 if (mInBackground) {
michael@0 63 super.onConfigurationChanged(config);
michael@0 64 return;
michael@0 65 }
michael@0 66
michael@0 67 // Otherwise, correct the locale. This catches some cases that GeckoApp
michael@0 68 // doesn't get a chance to.
michael@0 69 try {
michael@0 70 BrowserLocaleManager.getInstance().correctLocale(this, getResources(), config);
michael@0 71 } catch (IllegalStateException ex) {
michael@0 72 // GeckoApp hasn't started, so we have no ContextGetter in BrowserLocaleManager.
michael@0 73 Log.w(LOG_TAG, "Couldn't correct locale.", ex);
michael@0 74 }
michael@0 75
michael@0 76 super.onConfigurationChanged(config);
michael@0 77 }
michael@0 78
michael@0 79 public void onActivityPause(GeckoActivityStatus activity) {
michael@0 80 mInBackground = true;
michael@0 81
michael@0 82 if ((activity.isFinishing() == false) &&
michael@0 83 (activity.isGeckoActivityOpened() == false)) {
michael@0 84 // Notify Gecko that we are pausing; the cache service will be
michael@0 85 // shutdown, closing the disk cache cleanly. If the android
michael@0 86 // low memory killer subsequently kills us, the disk cache will
michael@0 87 // be left in a consistent state, avoiding costly cleanup and
michael@0 88 // re-creation.
michael@0 89 GeckoAppShell.sendEventToGecko(GeckoEvent.createAppBackgroundingEvent());
michael@0 90 mPausedGecko = true;
michael@0 91
michael@0 92 ThreadUtils.postToBackgroundThread(new Runnable() {
michael@0 93 @Override
michael@0 94 public void run() {
michael@0 95 BrowserDB.expireHistory(getContentResolver(),
michael@0 96 BrowserContract.ExpirePriority.NORMAL);
michael@0 97 }
michael@0 98 });
michael@0 99 }
michael@0 100 GeckoConnectivityReceiver.getInstance().stop();
michael@0 101 GeckoNetworkManager.getInstance().stop();
michael@0 102 }
michael@0 103
michael@0 104 public void onActivityResume(GeckoActivityStatus activity) {
michael@0 105 if (mPausedGecko) {
michael@0 106 GeckoAppShell.sendEventToGecko(GeckoEvent.createAppForegroundingEvent());
michael@0 107 mPausedGecko = false;
michael@0 108 }
michael@0 109
michael@0 110 final Context applicationContext = getApplicationContext();
michael@0 111 GeckoBatteryManager.getInstance().start(applicationContext);
michael@0 112 GeckoConnectivityReceiver.getInstance().start(applicationContext);
michael@0 113 GeckoNetworkManager.getInstance().start(applicationContext);
michael@0 114
michael@0 115 mInBackground = false;
michael@0 116 }
michael@0 117
michael@0 118 @Override
michael@0 119 public void onCreate() {
michael@0 120 HardwareUtils.init(getApplicationContext());
michael@0 121 Clipboard.init(getApplicationContext());
michael@0 122 FilePicker.init(getApplicationContext());
michael@0 123 GeckoLoader.loadMozGlue();
michael@0 124 HomePanelsManager.getInstance().init(getApplicationContext());
michael@0 125 super.onCreate();
michael@0 126 }
michael@0 127
michael@0 128 public boolean isApplicationInBackground() {
michael@0 129 return mInBackground;
michael@0 130 }
michael@0 131
michael@0 132 public LightweightTheme getLightweightTheme() {
michael@0 133 return mLightweightTheme;
michael@0 134 }
michael@0 135
michael@0 136 public void prepareLightweightTheme() {
michael@0 137 mLightweightTheme = new LightweightTheme(this);
michael@0 138 }
michael@0 139 }

mercurial