package de.c3nav.droid;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.preference.CheckBoxPreference;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceFragmentCompat;

public class SettingsFragment extends PreferenceFragmentCompat {

    public CheckBoxPreference useWifiLocating;

    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        setPreferencesFromResource(R.xml.preferences, rootKey);

        useWifiLocating = (CheckBoxPreference)this.findPreference(getString(R.string.use_wifi_locating_key));
        useWifiLocating.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {
                if (!(boolean)newValue) {
                    // always let user disable wifi locating
                    return true;
                }

                boolean permissionAsked = false;
                while (permissionAsked == false) {
                    int permissionCheck = ContextCompat.checkSelfPermission(getContext(),
                            Manifest.permission.ACCESS_FINE_LOCATION);
                    if (permissionCheck == PackageManager.PERMISSION_GRANTED) {
                        return true;
                    }
                    permissionAsked = true;
                    ActivityCompat.requestPermissions(getActivity(),
                            new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                            MainActivity.PERM_REQUEST);
                }
                return false;
            }
        });
    }
}

@@ -43,16 +43,58 @@
 nifest;%0A
+import android.content.SharedPreferences;%0A
 import a
@@ -153,16 +153,61 @@
 Bundle;%0A
+import android.preference.PreferenceManager;%0A
 import a
@@ -529,16 +529,59 @@
 mpat %7B%0A%0A
+    private SharedPreferences sharePrefs;%0A%0A
     publ
@@ -890,16 +890,270 @@
 _key));%0A
+        sharePrefs = PreferenceManager.getDefaultSharedPreferences(getContext());%0A        if (sharePrefs.getBoolean(getString(R.string.use_wifi_locating_key), true) && !checkLocationPermisson()) %7B%0A            useWifiLocating.setChecked(false);%0A        %7D%0A
         
@@ -1634,224 +1634,35 @@
    i
-nt permissionCheck = ContextCompat.checkSelfPermission(getContext(),%0A                            Manifest.p
+f (checkLocationP
 ermiss
-i
 on
-.ACCESS_FINE_LOCATION);%0A                    if (permissionCheck == PackageManager.PERMISSION_GRANTED
+()
 ) %7B%0A
@@ -2046,16 +2046,270 @@
      %7D);
+%0A%0A    private boolean checkLocationPermisson() %7B%0A        int permissionCheck = ContextCompat.checkSelfPermission(getContext(),%0A                Manifest.permission.ACCESS_FINE_LOCATION);%0A        return permissionCheck == PackageManager.PERMISSION_GRANTED;
 %0A    %7D%0A%7D

check locationpermission before dispalying setting