Java tutorial
/* * Copyright by Netease (c) 2016. * This source code is licensed under the MIT-style license found in the LICENSE file * in the root directory of this source tree. */ package com.netease.hearttouch.htimagepicker.core.util.permission; import android.app.Activity; import android.content.DialogInterface; import android.content.Intent; import android.content.pm.PackageManager; import android.net.Uri; import android.os.Bundle; import android.provider.Settings; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.app.ActivityCompat; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import com.netease.hearttouch.htimagepicker.R; /** * ???? * Created by zyl06 on 9/13/16. * copyed from http://www.jianshu.com/p/dbe4d37731e6 */ public class PermissionsActivity extends AppCompatActivity { public static final int PERMISSIONS_GRANTED = 0; // ??? public static final int PERMISSIONS_DENIED = 1; // ??? private static final int PERMISSION_REQUEST_CODE = 0; // ????? private static final String EXTRA_PERMISSIONS = "com.netease.hearttouch.permission.extra_permission"; // ??? private static final String PACKAGE_URL_SCHEME = "package:"; // private PermissionsChecker mChecker; // ?? private boolean isRequireCheck; // ???? // ?????? public static void startActivityForResult(Activity activity, int requestCode, String... permissions) { Intent intent = new Intent(activity, PermissionsActivity.class); intent.putExtra(EXTRA_PERMISSIONS, permissions); ActivityCompat.startActivityForResult(activity, intent, requestCode, null); } @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getIntent() == null || !getIntent().hasExtra(EXTRA_PERMISSIONS)) { throw new RuntimeException("PermissionsActivity???startActivityForResult?!"); } setContentView(R.layout.htimagepicker_activity_permissions); mChecker = new PermissionsChecker(this); isRequireCheck = true; } @Override protected void onResume() { super.onResume(); if (isRequireCheck) { String[] permissions = getPermissions(); if (mChecker.lacksPermissions(permissions)) { requestPermissions(permissions); // ?? } else { allPermissionsGranted(); // ??? } } else { isRequireCheck = true; } } // ??? private String[] getPermissions() { return getIntent().getStringArrayExtra(EXTRA_PERMISSIONS); } // ?? private void requestPermissions(String... permissions) { ActivityCompat.requestPermissions(this, permissions, PERMISSION_REQUEST_CODE); } // ???? private void allPermissionsGranted() { setResult(PERMISSIONS_GRANTED); finish(); } /** * ???, * ?, . * ??, ??Dialog. * * @param requestCode ? * @param permissions ?? * @param grantResults */ @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { if (requestCode == PERMISSION_REQUEST_CODE && hasAllPermissionsGranted(grantResults)) { isRequireCheck = true; allPermissionsGranted(); } else { isRequireCheck = false; showMissingPermissionDialog(); } } // ??? private boolean hasAllPermissionsGranted(@NonNull int[] grantResults) { for (int grantResult : grantResults) { if (grantResult == PackageManager.PERMISSION_DENIED) { return false; } } return true; } // ???? private void showMissingPermissionDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(PermissionsActivity.this); builder.setTitle(R.string.ne_pick_image_help); builder.setMessage(R.string.ne_pick_image_help_content); // ?, builder.setNegativeButton(R.string.ne_pick_image_quit, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { setResult(PERMISSIONS_DENIED); finish(); } }); builder.setPositiveButton(R.string.ne_pick_image_settings, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { startAppSettings(); } }); builder.show(); } // ? private void startAppSettings() { Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); intent.setData(Uri.parse(PACKAGE_URL_SCHEME + getPackageName())); startActivity(intent); } }