package com.sheep.jiuyan.samllsheep.utils;

import android.app.Activity;
import android.app.AppOpsManager;
import android.content.Context;
import android.content.Intent;
import android.os.Process;

/**
 * PACKAGE_USAGE_STATS 权限申请
 * Created by kemllor on 2017/12/20.
 */

public class PremissUtils {
    public static final int MY_PERMISSIONS_REQUEST_PACKAGE_USAGE_STATS = 1101;

    public static boolean hasPermission(Context context) {
        if (context == null) {
            return false;
        }
        int checkOpNoThrow = 0;
        AppOpsManager appOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
        checkOpNoThrow = appOpsManager.checkOpNoThrow("android:get_usage_stats", Process.myUid(), context.getPackageName());
        return checkOpNoThrow == 0;
    }

    public static boolean openUsageAccessPage(Activity context) {
        if (context == null) {
            return false;
        }
        try {
            context.startActivityForResult(new Intent("android.settings.USAGE_ACCESS_SETTINGS"),MY_PERMISSIONS_REQUEST_PACKAGE_USAGE_STATS);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}