forked from Blankj/AndroidUtilCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
2,220 additions
and
1,714 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
<resources> | ||
<dimen name="spacing_16">16dp</dimen> | ||
<dimen name="font_32">32sp</dimen> | ||
<dimen name="font_26">26sp</dimen> | ||
</resources> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
#project | ||
project.name=ALog | ||
project.name=UtilCode | ||
project.groupId=com.blankj | ||
project.artifactId=alog | ||
project.artifactId=utilcode | ||
project.packaging=aar | ||
project.siteUrl=https://github.com/Blankj/ALog | ||
project.gitUrl=https://github.com/Blankj/ALog.git | ||
project.siteUrl=https://github.com/Blankj/AndroidUtilCode | ||
project.gitUrl=https://github.com/Blankj/AndroidUtilCode.git | ||
|
||
#javadoc | ||
javadoc.name=ALog | ||
javadoc.name=UtilCode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
138 changes: 138 additions & 0 deletions
138
utilcode/src/main/java/com/blankj/utilcode/util/PermissionUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
package com.blankj.utilcode.util; | ||
|
||
import android.annotation.TargetApi; | ||
import android.app.Activity; | ||
import android.content.Context; | ||
import android.content.pm.PackageManager; | ||
import android.os.Build; | ||
import android.support.v4.app.ActivityCompat; | ||
import android.support.v4.content.ContextCompat; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* <pre> | ||
* author: Blankj | ||
* blog : http://blankj.com | ||
* time : 2017/04/16 | ||
* desc : 权限相关工具类 | ||
* </pre> | ||
*/ | ||
public final class PermissionUtils { | ||
|
||
private static int mRequestCode = -1; | ||
|
||
private static OnPermissionListener mOnPermissionListener; | ||
|
||
public interface OnPermissionListener { | ||
|
||
void onPermissionGranted(); | ||
|
||
void onPermissionDenied(String[] deniedPermissions); | ||
} | ||
|
||
public abstract static class RationaleHandler { | ||
private Context context; | ||
private int requestCode; | ||
private String[] permissions; | ||
|
||
protected abstract void showRationale(); | ||
|
||
void showRationale(Context context, int requestCode, String[] permissions) { | ||
this.context = context; | ||
this.requestCode = requestCode; | ||
this.permissions = permissions; | ||
showRationale(); | ||
} | ||
|
||
@TargetApi(Build.VERSION_CODES.M) | ||
public void requestPermissionsAgain() { | ||
((Activity) context).requestPermissions(permissions, requestCode); | ||
} | ||
} | ||
|
||
@TargetApi(Build.VERSION_CODES.M) | ||
public static void requestPermissions(Context context, int requestCode | ||
, String[] permissions, OnPermissionListener listener) { | ||
requestPermissions(context, requestCode, permissions, listener, null); | ||
} | ||
|
||
@TargetApi(Build.VERSION_CODES.M) | ||
public static void requestPermissions(Context context, int requestCode | ||
, String[] permissions, OnPermissionListener listener, RationaleHandler handler) { | ||
if (context instanceof Activity) { | ||
mRequestCode = requestCode; | ||
mOnPermissionListener = listener; | ||
String[] deniedPermissions = getDeniedPermissions(context, permissions); | ||
if (deniedPermissions.length > 0) { | ||
boolean rationale = shouldShowRequestPermissionRationale(context, deniedPermissions); | ||
if (rationale && handler != null) { | ||
handler.showRationale(context, requestCode, deniedPermissions); | ||
} else { | ||
((Activity) context).requestPermissions(deniedPermissions, requestCode); | ||
} | ||
} else { | ||
if (mOnPermissionListener != null) | ||
mOnPermissionListener.onPermissionGranted(); | ||
} | ||
} else { | ||
throw new RuntimeException("Context must be an Activity"); | ||
} | ||
} | ||
|
||
/** | ||
* 请求权限结果,对应Activity中onRequestPermissionsResult()方法。 | ||
*/ | ||
public static void onRequestPermissionsResult(Activity context, int requestCode, String[] permissions, int[] | ||
grantResults) { | ||
if (mRequestCode != -1 && requestCode == mRequestCode) { | ||
if (mOnPermissionListener != null) { | ||
String[] deniedPermissions = getDeniedPermissions(context, permissions); | ||
if (deniedPermissions.length > 0) { | ||
mOnPermissionListener.onPermissionDenied(deniedPermissions); | ||
} else { | ||
mOnPermissionListener.onPermissionGranted(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* 获取请求权限中需要授权的权限 | ||
*/ | ||
private static String[] getDeniedPermissions(Context context, String[] permissions) { | ||
List<String> deniedPermissions = new ArrayList<>(); | ||
for (String permission : permissions) { | ||
if (ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_DENIED) { | ||
deniedPermissions.add(permission); | ||
} | ||
} | ||
return deniedPermissions.toArray(new String[deniedPermissions.size()]); | ||
} | ||
|
||
/** | ||
* 是否彻底拒绝了某项权限 | ||
*/ | ||
public static boolean hasAlwaysDeniedPermission(Context context, String... deniedPermissions) { | ||
for (String deniedPermission : deniedPermissions) { | ||
if (!shouldShowRequestPermissionRationale(context, deniedPermission)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* 是否有权限需要说明提示 | ||
*/ | ||
private static boolean shouldShowRequestPermissionRationale(Context context, String... deniedPermissions) { | ||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) return false; | ||
boolean rationale; | ||
for (String permission : deniedPermissions) { | ||
rationale = ActivityCompat.shouldShowRequestPermissionRationale((Activity) context, permission); | ||
if (rationale) return true; | ||
} | ||
return false; | ||
} | ||
} |
Oops, something went wrong.