Bug 936756 - Part 0: refactor prefs access in GeckoApp. r=mfinkle
authorRichard Newman <rnewman@mozilla.com>
Sun, 01 Dec 2013 21:53:16 -0800
changeset 172849 9ebed437d78fdc839e50dfca9122ee3c6fd36a4a
parent 172848 73a3818eeef118f90ddb6bb495a39503e82f7c27
child 172850 606c4b4ab77f858f023e1f838219ceba45c7448d
push id3224
push userlsblakk@mozilla.com
push dateTue, 04 Feb 2014 01:06:49 +0000
treeherdermozilla-beta@60c04d0987f1 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmfinkle
bugs936756
milestone28.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 936756 - Part 0: refactor prefs access in GeckoApp. r=mfinkle
mobile/android/base/ContextGetter.java
mobile/android/base/GeckoApp.java
mobile/android/base/GeckoAppShell.java
mobile/android/base/GeckoView.java
--- a/mobile/android/base/ContextGetter.java
+++ b/mobile/android/base/ContextGetter.java
@@ -1,6 +1,15 @@
+/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
+ * 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;
-import android.content.Context;
 
-interface ContextGetter {
-  Context getContext();
+import android.content.Context;
+import android.content.SharedPreferences;
+
+public interface ContextGetter {
+    Context getContext();
+    SharedPreferences getSharedPreferences();
 }
+
--- a/mobile/android/base/GeckoApp.java
+++ b/mobile/android/base/GeckoApp.java
@@ -121,22 +121,28 @@ import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Locale;
 import java.util.Map;
 import java.util.Set;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
-abstract public class GeckoApp
-                extends GeckoActivity 
-    implements GeckoEventListener, SensorEventListener, LocationListener,
-                           Tabs.OnTabsChangedListener, GeckoEventResponder,
-                           GeckoMenu.Callback, GeckoMenu.MenuPresenter,
-                           ContextGetter, GeckoAppShell.GeckoInterface
+public abstract class GeckoApp
+    extends GeckoActivity 
+    implements
+    ContextGetter,
+    GeckoAppShell.GeckoInterface,
+    GeckoEventListener,
+    GeckoEventResponder,
+    GeckoMenu.Callback,
+    GeckoMenu.MenuPresenter,
+    LocationListener,
+    SensorEventListener,
+    Tabs.OnTabsChangedListener
 {
     private static final String LOGTAG = "GeckoApp";
 
     private static enum StartupAction {
         NORMAL,     /* normal application start */
         URL,        /* launched with a passed URL */
         PREFETCH    /* launched with a passed URL that we prefetch */
     }
@@ -228,20 +234,30 @@ abstract public class GeckoApp
             super(message);
         }
     }
 
     void toggleChrome(final boolean aShow) { }
 
     void focusChrome() { }
 
+    @Override
     public Context getContext() {
         return sAppContext;
     }
 
+    @Override
+    public SharedPreferences getSharedPreferences() {
+        return GeckoApp.getAppSharedPreferences();
+    }
+
+    public static SharedPreferences getAppSharedPreferences() {
+        return GeckoApp.sAppContext.getSharedPreferences(GeckoApp.PREFS_NAME, 0);
+    }
+
     public Activity getActivity() {
         return this;
     }
 
     public LocationListener getLocationListener() {
         if (mShouldReportGeoData && mPhoneStateListener == null) {
             mPhoneStateListener = new PhoneStateListener() {
                 public void onSignalStrengthsChanged(SignalStrength signalStrength) {
@@ -253,20 +269,16 @@ abstract public class GeckoApp
         }
         return this;
     }
 
     public SensorEventListener getSensorEventListener() {
         return this;
     }
 
-    public static SharedPreferences getAppSharedPreferences() {
-        return GeckoApp.sAppContext.getSharedPreferences(PREFS_NAME, 0);
-    }
-
     public View getCameraView() {
         return mCameraView;
     }
 
     public void addAppStateListener(GeckoAppShell.AppStateListener listener) {
         mAppStateListeners.add(listener);
     }
 
--- a/mobile/android/base/GeckoAppShell.java
+++ b/mobile/android/base/GeckoAppShell.java
@@ -224,18 +224,17 @@ public class GeckoAppShell
                     if (mainThread != null && thread != mainThread) {
                         Log.e(LOGTAG, "Main thread stack:");
                         for (StackTraceElement ste : mainThread.getStackTrace()) {
                             Log.e(LOGTAG, ste.toString());
                         }
                     }
 
                     if (e instanceof OutOfMemoryError) {
-                        SharedPreferences prefs =
-                            getContext().getSharedPreferences(GeckoApp.PREFS_NAME, 0);
+                        SharedPreferences prefs = getSharedPreferences();
                         SharedPreferences.Editor editor = prefs.edit();
                         editor.putBoolean(GeckoApp.PREFS_OOM_EXCEPTION, true);
                         editor.commit();
                     }
                 } finally {
                     reportJavaCrash(getStackTraceString(e));
                 }
             }
@@ -2141,16 +2140,23 @@ public class GeckoAppShell
     public static Context getContext() {
         return sContextGetter.getContext();
     }
 
     public static void setContextGetter(ContextGetter cg) {
         sContextGetter = cg;
     }
 
+    public static SharedPreferences getSharedPreferences() {
+        if (sContextGetter == null) {
+            throw new IllegalStateException("No ContextGetter; cannot fetch prefs.");
+        }
+        return sContextGetter.getSharedPreferences();
+    }
+
     public interface AppStateListener {
         public void onPause();
         public void onResume();
         public void onOrientationChanged();
     }
 
     public interface GeckoInterface {
         public GeckoProfile getProfile();
--- a/mobile/android/base/GeckoView.java
+++ b/mobile/android/base/GeckoView.java
@@ -14,27 +14,29 @@ import org.mozilla.gecko.util.GeckoEvent
 import org.mozilla.gecko.util.ThreadUtils;
 
 import org.json.JSONException;
 import org.json.JSONObject;
 
 import android.app.Activity;
 import android.content.Context;
 import android.content.Intent;
+import android.content.SharedPreferences;
 import android.content.res.TypedArray;
 import android.os.Handler;
 import android.util.AttributeSet;
 import android.util.Log;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
 
 public class GeckoView extends LayerView
     implements GeckoEventListener, ContextGetter {
 
+    private static final String DEFAULT_SHARED_PREFERENCES_FILE = "GeckoView";
     private static final String LOGTAG = "GeckoView";
 
     private ChromeDelegate mChromeDelegate;
     private ContentDelegate mContentDelegate;
 
     public GeckoView(Context context, AttributeSet attrs) {
         super(context, attrs);
         TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.GeckoView);
@@ -300,16 +302,24 @@ public class GeckoView extends LayerView
     public static void setGeckoInterface(final BaseGeckoInterface geckoInterface) {
         GeckoAppShell.setGeckoInterface(geckoInterface);
     }
 
     public static GeckoAppShell.GeckoInterface getGeckoInterface() {
         return GeckoAppShell.getGeckoInterface();
     }
 
+    protected String getSharedPreferencesFile() {
+        return DEFAULT_SHARED_PREFERENCES_FILE;
+    }
+
+    public SharedPreferences getSharedPreferences() {
+        return getContext().getSharedPreferences(getSharedPreferencesFile(), 0);
+    }
+
     /**
     * Wrapper for a browser in the GeckoView container. Associated with a browser
     * element in the Gecko system.
     */
     public class Browser {
         private final int mId;
         private Browser(int Id) {
             mId = Id;