Bug 1249384 - Support configuring switchboard server endpoint through intent extra. r=liuche draft
authorMargaret Leibovic <margaret.leibovic@gmail.com>
Fri, 26 Feb 2016 16:23:28 -0800
changeset 335095 8f1816c2770a63410248bbd223f91aac8a03322d
parent 335003 0f3938e0df6405678c951e2541019354c109732b
child 515076 cdda552b4ddd8021edff8ad6e105ef75def95e5c
push id11723
push usermleibovic@mozilla.com
push dateSat, 27 Feb 2016 00:23:50 +0000
reviewersliuche
bugs1249384
milestone47.0a1
Bug 1249384 - Support configuring switchboard server endpoint through intent extra. r=liuche MozReview-Commit-ID: 4fY94tZebip
mobile/android/base/java/org/mozilla/gecko/BrowserApp.java
--- a/mobile/android/base/java/org/mozilla/gecko/BrowserApp.java
+++ b/mobile/android/base/java/org/mozilla/gecko/BrowserApp.java
@@ -149,16 +149,18 @@ import com.nineoldandroids.animation.Obj
 import org.json.JSONException;
 import org.json.JSONObject;
 
 import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.lang.reflect.Method;
+import java.net.MalformedURLException;
+import java.net.URL;
 import java.net.URLEncoder;
 import java.util.EnumSet;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Locale;
 import java.util.UUID;
 import java.util.Vector;
 import java.util.regex.Pattern;
@@ -175,19 +177,23 @@ public class BrowserApp extends GeckoApp
                                    ReadingListHelper.OnReadingListEventListener,
                                    AnchoredPopup.OnVisibilityChangeListener,
                                    ActionModeCompat.Presenter,
                                    LayoutInflater.Factory {
     private static final String LOGTAG = "GeckoBrowserApp";
 
     private static final int TABS_ANIMATION_DURATION = 450;
 
-    private static final String ADD_SHORTCUT_TOAST = "add_shortcut_toast";
     public static final String GUEST_BROWSING_ARG = "--guest";
-    public static final String INTENT_KEY_SWITCHBOARD_UUID = "switchboard-uuid";
+
+    // Intent String extras used to specify custom Switchboard configurations.
+    private static final String INTENT_KEY_SWITCHBOARD_UUID = "switchboard-uuid";
+    private static final String INTENT_KEY_SWITCHBOARD_HOST = "switchboard-host";
+
+    private static final String DEFAULT_SWITCHBOARD_HOST = "switchboard.services.mozilla.com";
 
     private static final String STATE_ABOUT_HOME_TOP_PADDING = "abouthome_top_padding";
 
     private static final String BROWSER_SEARCH_TAG = "browser_search";
 
     // Request ID for startActivityForResult.
     private static final int ACTIVITY_REQUEST_PREFERENCES = 1001;
     private static final int ACTIVITY_REQUEST_TAB_QUEUE = 2001;
@@ -583,30 +589,17 @@ public class BrowserApp extends GeckoApp
         // This has to be prepared prior to calling GeckoApp.onCreate, because
         // widget code and BrowserToolbar need it, and they're created by the
         // layout, which GeckoApp takes care of.
         ((GeckoApplication) getApplication()).prepareLightweightTheme();
         super.onCreate(savedInstanceState);
 
         final Context appContext = getApplicationContext();
 
-        if (!Experiments.isDisabled(new SafeIntent(intent)) && AppConstants.MOZ_SWITCHBOARD) {
-            // Initializes the default URLs the first time.
-            SwitchBoard.initDefaultServerUrls("https://switchboard.services.mozilla.com/urls", "https://switchboard.services.mozilla.com/v1", true);
-
-            final String switchboardUUID = ContextUtils.getStringExtra(intent, INTENT_KEY_SWITCHBOARD_UUID);
-            SwitchBoard.setUUIDFromExtra(switchboardUUID);
-
-            // Looks at the server if there are changes in the server URL that should be used in the future
-            new AsyncConfigLoader(this, AsyncConfigLoader.UPDATE_SERVER, switchboardUUID).execute();
-
-            // Loads the actual config. This can be done on app start or on app onResume() depending
-            // how often you want to update the config.
-            new AsyncConfigLoader(this, AsyncConfigLoader.CONFIG_SERVER, switchboardUUID).execute();
-        }
+        initSwitchboard(intent);
 
         mBrowserChrome = (ViewGroup) findViewById(R.id.browser_chrome);
         mActionBarFlipper = (ViewFlipper) findViewById(R.id.browser_actionbar);
         mActionBar = (ActionModeCompatView) findViewById(R.id.actionbar);
 
         mBrowserToolbar = (BrowserToolbar) findViewById(R.id.browser_toolbar);
         mProgressView = (ToolbarProgressView) findViewById(R.id.progress);
         mBrowserToolbar.setProgressBar(mProgressView);
@@ -776,16 +769,51 @@ public class BrowserApp extends GeckoApp
                            public void run() {
                                showUpdaterPermissionSnackbar();
                            }
                        })
                       .run();
         }
     }
 
+    /**
+     * Initializes the default Switchboard URLs the first time.
+     * @param intent
+     */
+    private void initSwitchboard(Intent intent) {
+        if (Experiments.isDisabled(new SafeIntent(intent)) || !AppConstants.MOZ_SWITCHBOARD) {
+            return;
+        }
+
+        final String hostExtra = ContextUtils.getStringExtra(intent, INTENT_KEY_SWITCHBOARD_HOST);
+        final String host = TextUtils.isEmpty(hostExtra) ? DEFAULT_SWITCHBOARD_HOST : hostExtra;
+
+        final String configServerUpdateUrl;
+        final String configServerUrl;
+        try {
+            configServerUpdateUrl = new URL("https", host, "urls").toString();
+            configServerUrl = new URL("https", host, "v1").toString();
+        } catch (MalformedURLException e) {
+            Log.e(LOGTAG, "Error creating Switchboard server URL", e);
+            return;
+        }
+
+        SwitchBoard.initDefaultServerUrls(configServerUpdateUrl, configServerUrl, true);
+
+        final String switchboardUUID = ContextUtils.getStringExtra(intent, INTENT_KEY_SWITCHBOARD_UUID);
+        SwitchBoard.setUUIDFromExtra(switchboardUUID);
+
+        // Looks at the server if there are changes in the server URL that should be used in the future
+        new AsyncConfigLoader(this, AsyncConfigLoader.UPDATE_SERVER, switchboardUUID).execute();
+
+        // Loads the actual config. This can be done on app start or on app onResume() depending
+        // how often you want to update the config.
+        new AsyncConfigLoader(this, AsyncConfigLoader.CONFIG_SERVER, switchboardUUID).execute();
+    }
+
     private void showUpdaterPermissionSnackbar() {
         SnackbarHelper.SnackbarCallback allowCallback = new SnackbarHelper.SnackbarCallback() {
             @Override
             public void onClick(View v) {
                 Permissions.from(BrowserApp.this)
                         .withPermissions(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                         .run();
             }