Startup optimization
Bug: 5019676
Save one thread by letting CrashRecoveryHandler re-use the BrowserSettings
startup thread
Lazy initialize the wake lock only if we need it (avoids IPC overhead at startup)
Slight ordering shuffle in BrowserActivity to maximize the usefulness of the
BrowserSettings startup thread
Change-Id: I1fc7412d492f93e0630008fa6030da9e0d726ebb
diff --git a/src/com/android/browser/BrowserSettings.java b/src/com/android/browser/BrowserSettings.java
index 97769fc..a4f0f04 100644
--- a/src/com/android/browser/BrowserSettings.java
+++ b/src/com/android/browser/BrowserSettings.java
@@ -22,6 +22,7 @@
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Build;
+import android.os.Looper;
import android.os.Message;
import android.preference.PreferenceManager;
import android.provider.Browser;
@@ -103,6 +104,10 @@
private AutofillHandler mAutofillHandler;
private WeakHashMap<WebSettings, String> mCustomUserAgents;
private boolean mInitialized = false;
+ // Looper shared between some lightweight background operations
+ // Specifically, this is created on the thread that initializes browser settings
+ // and is then reused by CrashRecoveryHandler
+ private Looper mBackgroundLooper;
// Cached values
private int mPageCacheCapacity = 1;
@@ -127,7 +132,7 @@
mCustomUserAgents = new WeakHashMap<WebSettings, String>();
mPrefs.registerOnSharedPreferenceChangeListener(this);
mAutofillHandler.asyncLoadFromDb();
- new Thread(mInitialization, "BrowserSettingsInitialization").start();
+ new Thread(mSetupAndLoop, "BackgroundLooper").start();
}
public void setController(Controller controller) {
@@ -139,6 +144,11 @@
}
}
+ public Looper getBackgroundLooper() {
+ requireInitialization();
+ return mBackgroundLooper;
+ }
+
public void startManagingSettings(WebSettings settings) {
synchronized (mManagedSettings) {
syncStaticSettings(settings);
@@ -147,7 +157,7 @@
}
}
- private Runnable mInitialization = new Runnable() {
+ private Runnable mSetupAndLoop = new Runnable() {
@Override
public void run() {
@@ -189,10 +199,13 @@
}
mPrefs.edit().remove(PREF_TEXT_SIZE).apply();
}
+ Looper.prepare();
+ mBackgroundLooper = Looper.myLooper();
synchronized (BrowserSettings.this) {
mInitialized = true;
BrowserSettings.this.notifyAll();
}
+ Looper.loop();
}
};