/*
 *  Copyright 2016 Google Inc. All Rights Reserved.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package com.google.android.apps.forscience.whistlepunk;

import android.app.Activity;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;

/**
 * Static permission functions shared across many parts of the app.
 */
public class PermissionUtils {
    public static final String TAG = "PermissionUtils";

    // The microphone requires explicit permission in Android M. Check that we have permission
    // before adding the decibel sensor option.
    public static boolean tryRequestingPermission(Activity activity, String permission,
                                                  int permissionType, boolean forceRetry) {
        if (!permissionIsGranted(activity, permission)) {
            // Should we show an explanation?
            if (canRequestAgain(activity, permission) && forceRetry) {
                // No explanation needed, so we can request the permission.
                ActivityCompat.requestPermissions(activity, new String[]{permission},
                        permissionType);
            } else {
                // Then the user didn't explicitly ask for us to retry the permission,
                // so we won't do anything.
            }
            return false;
        }
        return true;
    }

    public static boolean permissionIsGranted(Activity activity, String permission) {
        int permissionCheck = ContextCompat.checkSelfPermission(activity, permission);
        return permissionCheck == PackageManager.PERMISSION_GRANTED;
    }

    public static boolean canRequestAgain(Activity activity, String permission) {
        return ActivityCompat.shouldShowRequestPermissionRationale(activity, permission);
    }
}

@@ -733,316 +733,674 @@
 ent.
-pm.PackageManager;%0Aimport android.support.v4.app.ActivityCompat;%0Aimport android.support.v4.content.ContextCompat;%0A%0A/**%0A * Static permission functions shared across many parts of the app.%0A */%0Apublic class PermissionUtils %7B%0A    public static final String TAG = %22PermissionUtils%22;%0A%0A    // The microphone
+SharedPreferences;%0Aimport android.content.pm.PackageManager;%0Aimport android.preference.PreferenceManager;%0Aimport android.support.v4.app.ActivityCompat;%0Aimport android.support.v4.content.ContextCompat;%0Aimport android.text.TextUtils;%0A%0Aimport java.util.Collections;%0Aimport java.util.HashSet;%0Aimport java.util.Set;%0A%0A/**%0A * Static permission functions shared across many parts of the app.%0A */%0Apublic class PermissionUtils %7B%0A    public static final String TAG = %22PermissionUtils%22;%0A%0A    private static final String KEY_PERMISSIONS_REQUESTED_SET = %22permissions_requested%22;%0A    private static final Set%3CString%3E NO_STRINGS = Collections.emptySet();%0A%0A    // Some things
  require
 s ex
@@ -1395,17 +1395,16 @@
  require
-s
  explici
@@ -1428,16 +1428,17 @@
 ndroid M
++
 . Check 
@@ -1464,55 +1464,8 @@
 sion
-%0A    // before adding the decibel sensor option
 .%0A  
@@ -1909,31 +1909,16 @@
         
-ActivityCompat.
 requestP
@@ -1926,17 +1926,16 @@
 rmission
-s
 (activit
@@ -1941,57 +1941,19 @@
 ty, 
-new String%5B%5D%7Bpermission%7D,%0A                       
+permission,
  per
@@ -2539,87 +2539,1441 @@
     
-return ActivityCompat.shouldShowRequestPermissionRationale(activity, permission
+// If the user has denied the permissions request, but not either never asked before%0A        // or clicked %22never ask again%22, this will return true. In that case, we can request again.%0A        if (ActivityCompat.shouldShowRequestPermissionRationale(activity, permission)) %7B%0A            return true;%0A        %7D%0A        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);%0A        Set%3CString%3E requestedPerms = prefs.getStringSet(KEY_PERMISSIONS_REQUESTED_SET, NO_STRINGS);%0A%0A        // If the permission is in the set, they have asked for it already, and%0A        // at this point shouldShowRequestPermissionRationale is false, so they have also%0A        // clicked %22never ask again%22. Return false -- we cannot request again.%0A        // If it was not found in the set, they haven't asked for it yet, so we can still ask.%0A        return !requestedPerms.contains(permission);%0A    %7D%0A%0A    private static void requestPermission(Activity activity, String permission,%0A            int permissionType) %7B%0A        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);%0A        Set%3CString%3E requestedPerms = prefs.getStringSet(KEY_PERMISSIONS_REQUESTED_SET, NO_STRINGS);%0A        requestedPerms.add(permission);%0A        prefs.edit().putStringSet(KEY_PERMISSIONS_REQUESTED_SET, requestedPerms).apply();%0A%0A        ActivityCompat.requestPermissions(activity, new String%5B%5D%7Bpermission%7D, permissionType
 );%0A 

Store whether we've ever requested permission before.