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