Bug 1216532 - GeckoPreferences: Request location permission if stumbler setting is changed. r?nalexander draft
authorSebastian Kaspari <s.kaspari@gmail.com>
Mon, 11 Jan 2016 12:27:20 +0100
changeset 321387 bae5d69390db2f38809049c91d14b021f4f08aa5
parent 321386 6c8d9a6e5c4ea5be4096152b18786db8de793c62
child 512889 0b8af38a04d4ab15f63a231ea2c61ebb12d9675f
push id9365
push users.kaspari@gmail.com
push dateWed, 13 Jan 2016 13:21:08 +0000
reviewersnalexander
bugs1216532
milestone46.0a1
Bug 1216532 - GeckoPreferences: Request location permission if stumbler setting is changed. r?nalexander
mobile/android/base/java/org/mozilla/gecko/preferences/GeckoPreferences.java
--- a/mobile/android/base/java/org/mozilla/gecko/preferences/GeckoPreferences.java
+++ b/mobile/android/base/java/org/mozilla/gecko/preferences/GeckoPreferences.java
@@ -1,15 +1,16 @@
 /* -*- 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.preferences;
 
+import android.Manifest;
 import android.annotation.TargetApi;
 import org.mozilla.gecko.AboutPages;
 import org.mozilla.gecko.AdjustConstants;
 import org.mozilla.gecko.AppConstants;
 import org.mozilla.gecko.AppConstants.Versions;
 import org.mozilla.gecko.BrowserApp;
 import org.mozilla.gecko.BrowserLocaleManager;
 import org.mozilla.gecko.DataReportingNotification;
@@ -25,16 +26,17 @@ import org.mozilla.gecko.Locales;
 import org.mozilla.gecko.PrefsHelper;
 import org.mozilla.gecko.R;
 import org.mozilla.gecko.Restrictions;
 import org.mozilla.gecko.Telemetry;
 import org.mozilla.gecko.TelemetryContract;
 import org.mozilla.gecko.TelemetryContract.Method;
 import org.mozilla.gecko.background.common.GlobalConstants;
 import org.mozilla.gecko.db.BrowserContract.SuggestedSites;
+import org.mozilla.gecko.permissions.Permissions;
 import org.mozilla.gecko.restrictions.Restrictable;
 import org.mozilla.gecko.tabqueue.TabQueueHelper;
 import org.mozilla.gecko.updater.UpdateService;
 import org.mozilla.gecko.updater.UpdateServiceHelper;
 import org.mozilla.gecko.util.GeckoEventListener;
 import org.mozilla.gecko.util.HardwareUtils;
 import org.mozilla.gecko.util.InputOptionsUtils;
 import org.mozilla.gecko.util.ThreadUtils;
@@ -607,16 +609,21 @@ OnSharedPreferenceChangeListener
                   finishChoosingTransition();
                   break;
               }
               break;
         }
     }
 
     @Override
+    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
+        Permissions.onRequestPermissionsResult(this, permissions, grantResults);
+    }
+
+    @Override
     public void handleMessage(String event, JSONObject message) {
         try {
             if (event.equals("Sanitize:Finished")) {
                 boolean success = message.getBoolean("success");
                 final int stringRes = success ? R.string.private_data_success : R.string.private_data_fail;
                 final Context context = this;
                 ThreadUtils.postToUiThread(new Runnable () {
                     @Override
@@ -1191,19 +1198,23 @@ OnSharedPreferenceChangeListener
             // The healthreport pref only lives in Android, so we do not persist
             // to Gecko, but we do broadcast intent to the health report
             // background uploader service, which will start or stop the
             // repeated background upload attempts.
             final Boolean newBooleanValue = (Boolean) newValue;
             broadcastHealthReportUploadPref(this, newBooleanValue);
             AdjustConstants.getAdjustHelper().setEnabled(newBooleanValue);
         } else if (PREFS_GEO_REPORTING.equals(prefName)) {
-            broadcastStumblerPref(this, (Boolean) newValue);
-            // Translate boolean value to int for geo reporting pref.
-            newValue = (Boolean) newValue ? 1 : 0;
+            if ((Boolean) newValue) {
+                enableStumbler((CheckBoxPreference) preference);
+                return false;
+            } else {
+                broadcastStumblerPref(GeckoPreferences.this, false);
+                return true;
+            }
         } else if (handlers.containsKey(prefName)) {
             PrefHandler handler = handlers.get(prefName);
             handler.onChange(this, preference, newValue);
         }
 
         // Send Gecko-side pref changes to Gecko
         if (isGeckoPref(prefName)) {
             PrefsHelper.setPref(prefName, newValue, true /* flush */);
@@ -1220,16 +1231,36 @@ OnSharedPreferenceChangeListener
         } else if (preference instanceof FontSizePreference) {
             final FontSizePreference fontSizePref = (FontSizePreference) preference;
             fontSizePref.setSummary(fontSizePref.getSavedFontSizeName());
         }
 
         return true;
     }
 
+    private void enableStumbler(final CheckBoxPreference preference) {
+        Permissions
+                .from(this)
+                .withPermissions(Manifest.permission.ACCESS_FINE_LOCATION)
+                .onUIThread()
+                .andFallback(new Runnable() {
+                    @Override
+                    public void run() {
+                        preference.setChecked(false);
+                    }
+                })
+                .run(new Runnable() {
+                    @Override
+                    public void run() {
+                        preference.setChecked(true);
+                        broadcastStumblerPref(GeckoPreferences.this, true);
+                    }
+                });
+    }
+
     private EditText getTextBox(int aHintText) {
         EditText input = new FloatingHintEditText(this);
         int inputtype = InputType.TYPE_CLASS_TEXT;
         inputtype |= InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS;
         input.setInputType(inputtype);
 
         input.setHint(aHintText);
         return input;