mobile/android/base/GeckoApplication.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/GeckoApplication.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,139 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +package org.mozilla.gecko;
     1.9 +
    1.10 +import org.mozilla.gecko.db.BrowserContract;
    1.11 +import org.mozilla.gecko.db.BrowserDB;
    1.12 +import org.mozilla.gecko.home.HomePanelsManager;
    1.13 +import org.mozilla.gecko.mozglue.GeckoLoader;
    1.14 +import org.mozilla.gecko.util.Clipboard;
    1.15 +import org.mozilla.gecko.util.HardwareUtils;
    1.16 +import org.mozilla.gecko.util.ThreadUtils;
    1.17 +
    1.18 +import android.app.Application;
    1.19 +import android.content.Context;
    1.20 +import android.content.SharedPreferences;
    1.21 +import android.content.res.Configuration;
    1.22 +import android.util.Log;
    1.23 +
    1.24 +public class GeckoApplication extends Application 
    1.25 +    implements ContextGetter {
    1.26 +    private static final String LOG_TAG = "GeckoApplication";
    1.27 +
    1.28 +    private static volatile GeckoApplication instance;
    1.29 +
    1.30 +    private boolean mInBackground;
    1.31 +    private boolean mPausedGecko;
    1.32 +
    1.33 +    private LightweightTheme mLightweightTheme;
    1.34 +
    1.35 +    public GeckoApplication() {
    1.36 +        super();
    1.37 +        instance = this;
    1.38 +    }
    1.39 +
    1.40 +    public static GeckoApplication get() {
    1.41 +        return instance;
    1.42 +    }
    1.43 +
    1.44 +    @Override
    1.45 +    public Context getContext() {
    1.46 +        return this;
    1.47 +    }
    1.48 +
    1.49 +    @Override
    1.50 +    public SharedPreferences getSharedPreferences() {
    1.51 +        return GeckoSharedPrefs.forApp(this);
    1.52 +    }
    1.53 +
    1.54 +    /**
    1.55 +     * We need to do locale work here, because we need to intercept
    1.56 +     * each hit to onConfigurationChanged.
    1.57 +     */
    1.58 +    @Override
    1.59 +    public void onConfigurationChanged(Configuration config) {
    1.60 +        Log.d(LOG_TAG, "onConfigurationChanged: " + config.locale +
    1.61 +                       ", background: " + mInBackground);
    1.62 +
    1.63 +        // Do nothing if we're in the background. It'll simply cause a loop
    1.64 +        // (Bug 936756 Comment 11), and it's not necessary.
    1.65 +        if (mInBackground) {
    1.66 +            super.onConfigurationChanged(config);
    1.67 +            return;
    1.68 +        }
    1.69 +
    1.70 +        // Otherwise, correct the locale. This catches some cases that GeckoApp
    1.71 +        // doesn't get a chance to.
    1.72 +        try {
    1.73 +            BrowserLocaleManager.getInstance().correctLocale(this, getResources(), config);
    1.74 +        } catch (IllegalStateException ex) {
    1.75 +            // GeckoApp hasn't started, so we have no ContextGetter in BrowserLocaleManager.
    1.76 +            Log.w(LOG_TAG, "Couldn't correct locale.", ex);
    1.77 +        }
    1.78 +
    1.79 +        super.onConfigurationChanged(config);
    1.80 +    }
    1.81 +
    1.82 +    public void onActivityPause(GeckoActivityStatus activity) {
    1.83 +        mInBackground = true;
    1.84 +
    1.85 +        if ((activity.isFinishing() == false) &&
    1.86 +            (activity.isGeckoActivityOpened() == false)) {
    1.87 +            // Notify Gecko that we are pausing; the cache service will be
    1.88 +            // shutdown, closing the disk cache cleanly. If the android
    1.89 +            // low memory killer subsequently kills us, the disk cache will
    1.90 +            // be left in a consistent state, avoiding costly cleanup and
    1.91 +            // re-creation. 
    1.92 +            GeckoAppShell.sendEventToGecko(GeckoEvent.createAppBackgroundingEvent());
    1.93 +            mPausedGecko = true;
    1.94 +
    1.95 +            ThreadUtils.postToBackgroundThread(new Runnable() {
    1.96 +                @Override
    1.97 +                public void run() {
    1.98 +                    BrowserDB.expireHistory(getContentResolver(),
    1.99 +                                            BrowserContract.ExpirePriority.NORMAL);
   1.100 +                }
   1.101 +            });
   1.102 +        }
   1.103 +        GeckoConnectivityReceiver.getInstance().stop();
   1.104 +        GeckoNetworkManager.getInstance().stop();
   1.105 +    }
   1.106 +
   1.107 +    public void onActivityResume(GeckoActivityStatus activity) {
   1.108 +        if (mPausedGecko) {
   1.109 +            GeckoAppShell.sendEventToGecko(GeckoEvent.createAppForegroundingEvent());
   1.110 +            mPausedGecko = false;
   1.111 +        }
   1.112 +
   1.113 +        final Context applicationContext = getApplicationContext();
   1.114 +        GeckoBatteryManager.getInstance().start(applicationContext);
   1.115 +        GeckoConnectivityReceiver.getInstance().start(applicationContext);
   1.116 +        GeckoNetworkManager.getInstance().start(applicationContext);
   1.117 +
   1.118 +        mInBackground = false;
   1.119 +    }
   1.120 +
   1.121 +    @Override
   1.122 +    public void onCreate() {
   1.123 +        HardwareUtils.init(getApplicationContext());
   1.124 +        Clipboard.init(getApplicationContext());
   1.125 +        FilePicker.init(getApplicationContext());
   1.126 +        GeckoLoader.loadMozGlue();
   1.127 +        HomePanelsManager.getInstance().init(getApplicationContext());
   1.128 +        super.onCreate();
   1.129 +    }
   1.130 +
   1.131 +    public boolean isApplicationInBackground() {
   1.132 +        return mInBackground;
   1.133 +    }
   1.134 +
   1.135 +    public LightweightTheme getLightweightTheme() {
   1.136 +        return mLightweightTheme;
   1.137 +    }
   1.138 +
   1.139 +    public void prepareLightweightTheme() {
   1.140 +        mLightweightTheme = new LightweightTheme(this);
   1.141 +    }
   1.142 +}

mercurial