/**
* 权限获取页面
* <p/>
* Created by wangchenlong on 16/1/26.
*/
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 =
"me.chunyu.clwang.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.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.help);
builder.setMessage(R.string.string_help_text);
builder.setNegativeButton(R.string.quit,
new
DialogInterface.OnClickListener() {
@Override
public
void
onClick(DialogInterface dialog,
int
which) {
setResult(PERMISSIONS_DENIED);
finish();
}
});
builder.setPositiveButton(R.string.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);
}
}