com.god.gallery.util.PermissionUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.god.gallery.util.PermissionUtil.java

Source

/*
* Copyright 2015 The Android Open Source Project
*
* 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.god.gallery.util;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;

/**
 * Utility class that wraps access to the runtime permissions API in M and provides basic helper
 * methods.
 */
public abstract class PermissionUtil {

    /**
     * activity 
     */
    public static boolean requestPermission(Activity activity, String[] permissions, int request_contacts) {
        return _requestPermissions(activity, permissions, request_contacts);
    }

    /**
     * fragment 
     */
    public static boolean requestPermission(Fragment fragment, String[] permissions, int request_contacts) {
        return _requestPermissions(fragment, permissions, request_contacts);
    }

    /**
     * Check that all given permissions have been granted by verifying that each entry in the
     * given array is of the value {@link PackageManager#PERMISSION_GRANTED}.
     *
     * @see Activity#onRequestPermissionsResult(int, String[], int[])
     */
    public static boolean verifyPermissions(final Context context, String[] permissions, int[] grantResults) {
        // At least one result must be checked.
        String msg = "";

        if (grantResults.length < 1) {
            return false;
        }

        // Verify that each required permission has been granted, otherwise return false.
        for (int i = 0; i < grantResults.length; i++) {
            if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
                msg += getPermissionCN(permissions[i]) + "\n";
            }
        }
        if (!TextUtils.isEmpty(msg)) {
            return false;
        }
        return true;
    }

    private static boolean _requestPermissions(Object object, String[] permissions, int request_contacts) {
        if (permissions.length < 1) {
            return false;
        }
        if (object instanceof Activity) {
            if (shouldShowRequestPermissionRationale((Activity) object, permissions))
                return false;
        }
        // Verify that each required permission has been granted, otherwise return false.
        for (String p : permissions) {
            if (object instanceof Activity) {
                //PERMISSION_GRANTED if you have the permission, or PERMISSION_DENIED if not.
                if (ActivityCompat.checkSelfPermission((Activity) object, p) != PackageManager.PERMISSION_GRANTED) {
                    // Should we show an explanation?
                    ActivityCompat.requestPermissions((Activity) object, permissions, request_contacts);
                    return false;
                }
            } else if (object instanceof Fragment) {
                Fragment fragment = (Fragment) object;
                if (ContextCompat.checkSelfPermission(fragment.getActivity(),
                        p) != PackageManager.PERMISSION_GRANTED) {
                    fragment.requestPermissions(permissions, request_contacts);
                    return false;
                }
            }
        }
        return true;
    }

    /**
     * ???
     */
    private static boolean shouldShowRequestPermissionRationale(final Activity activity, String[] permissions) {
        StringBuilder msgbff = new StringBuilder();
        if (permissions.length < 1) {
            return false;
        }
        // Verify that each required permission has been granted, otherwise return false.
        for (String p : permissions) {
            //app???,?, true.
            //??????Dont ask again?,false.
            //????, false.
            if (ActivityCompat.shouldShowRequestPermissionRationale(activity, p)) {
                msgbff.append(getPermissionCN(p)).append("\n");
            }
        }
        String msg = msgbff.toString();
        if (msg.length() > 0) {
            return true;
        }
        return false;
    }

    private static final String[] pGroup = { "CALENDAR", "CAMERA", "CONTACTS", "LOCATION", "MICROPHONE", "PHONE",
            "SENSORS", "SMS", "STORAGE" };
    private static final String[] pGroupCN = { "", "", "?", "?", "", "",
            "", "SMS", "" };

    private static String getPermissionCN(String p) {
        for (int i = 0; i < pGroup.length; i++) {
            if (p.endsWith(pGroup[i]))
                return pGroupCN[i];
        }
        return p.substring(p.lastIndexOf('.') + 1);
    }

}