Skip to content

Commit

Permalink
1.1.3
Browse files Browse the repository at this point in the history
  • Loading branch information
喻凯 committed Jan 9, 2019
1 parent 00b3a8d commit cb0e325
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 43 deletions.
2 changes: 1 addition & 1 deletion permission/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ group = "com.paozhuanyinyu"
//发布到JCenter上的项目名字,必须填写
def libName = "XPermission"
// 版本号,下次更新是只需要更改版本号即可
version = "1.1.2"
version = "1.1.3"
/** 上面配置后上传至jcenter后的编译路径是这样的: compile 'cn.bluemobi.dylan:sqlitelibrary:1.0' **/

//生成源文件
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import com.paozhuanyinyu.runtime.permission.manufacturer.PermissionsChecker;
import com.paozhuanyinyu.runtime.permission.manufacturer.PermissionsPageManager;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
Expand All @@ -35,6 +38,8 @@ public class XPermission {
static final String TAG = "XPermission";
static final Object TRIGGER = new Object();
private static XPermission sInstance;
private Map<String, PublishSubject<Permission>> mSubjects = new HashMap<>();
private Map<String, Params> mParams = new HashMap<String, Params>();
private XPermission(){
}
public static XPermission getInstance(){
Expand Down Expand Up @@ -166,7 +171,7 @@ public Observable<Permission> apply(Object o) throws Exception {

private Observable<?> pending(final Params... permissions) {
for (Params p : permissions) {
if (!XPermissionActivity.containsByPermission(p)) {
if (!containsByPermission(p)) {
return Observable.empty();
}
}
Expand Down Expand Up @@ -204,8 +209,8 @@ private Observable<Permission> requestImplementation(Context context,final Param

unrequestedPermissions.add(permission.permissionName);
PublishSubject<Permission> subject = PublishSubject.create();
XPermissionActivity.setSubjectForPermission(permission.permissionName, subject);
XPermissionActivity.setParams(permission);
setSubjectForPermission(permission.permissionName, subject);
setParams(permission);

list.add(subject);
}
Expand Down Expand Up @@ -257,5 +262,30 @@ boolean isMarshmallow() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M;
}

public boolean containsByPermission(@NonNull Params permission) {
return mSubjects.containsKey(permission);
}

public void setSubjectForPermission(@NonNull String permission, @NonNull PublishSubject<Permission> subject) {
if(mSubjects==null){
mSubjects = new HashMap<String, PublishSubject<Permission>>();
}
mSubjects.clear();
mSubjects.put(permission, subject);
}

public void setParams(Params params){
if(mParams == null){
mParams = new HashMap<String,Params>();
}
mParams.clear();
mParams.put(params.permissionName,params);
}

public Map<String, PublishSubject<Permission>> getSubjects(){
return mSubjects;
}
public Map<String,Params> getParams(){
return mParams;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ public class XPermissionActivity extends Activity{
private static final int GO_TO_SETTINGS_REQUEST_CODE = 43;
// Contains all the current permission requests.
// Once granted or denied, they are removed from it.
private static Map<String, PublishSubject<Permission>> mSubjects = new HashMap<>();
private static Map<String, Params> mParams = new HashMap<String, Params>();
private static boolean mLogging;
private static String permissionName;
@Override
Expand Down Expand Up @@ -69,7 +67,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try{
if(requestCode == GO_TO_SETTINGS_REQUEST_CODE) {
PublishSubject<Permission> subject = mSubjects.get(permissionName);
PublishSubject<Permission> subject = XPermission.getInstance().getSubjects().get(permissionName);
if (Manifest.permission.WRITE_SETTINGS.equals(permissionName) || Manifest.permission.SYSTEM_ALERT_WINDOW.equals(permissionName)) {
if(PermissionsChecker.isPermissionGranted(this, permissionName, false)){
subject.onNext(new Permission(permissionName, true, true));
Expand All @@ -91,8 +89,6 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
}catch (Exception e){
e.printStackTrace();
}finally {
mSubjects.clear();
mParams.clear();
finish();
}
}
Expand All @@ -101,7 +97,8 @@ void onRequestPermissionsResult(String permissions[], int[] grantResults, boolea
for (int i = 0, size = permissions.length; i < size; i++) {
log("onRequestPermissionsResult " + permissions[i]);
// Find the corresponding subject
PublishSubject<Permission> subject = mSubjects.get(permissions[i]);
Map<String, PublishSubject<Permission>> map = XPermission.getInstance().getSubjects();
PublishSubject<Permission> subject = map.get(permissions[i]);
boolean granted = grantResults[i] == PackageManager.PERMISSION_GRANTED;
boolean showRequestPermissionRationale = shouldShowRequestPermissionRationale[i];
log("granted: " + granted + "; showRequestPermissionRationale: " + showRequestPermissionRationale);
Expand All @@ -123,7 +120,7 @@ void onRequestPermissionsResult(String permissions[], int[] grantResults, boolea
}
}
}
if(!granted && !showRequestPermissionRationale && (mParams.get(permissions[i])!=null && mParams.get(permissions[i]).isShowGuide)){
if(!granted && !showRequestPermissionRationale && (XPermission.getInstance().getParams().get(permissions[i])!=null && XPermission.getInstance().getParams().get(permissions[i]).isShowGuide)){
showReadPhoneStateHintDialog(subject,permissions[i]);
}else{
try{
Expand All @@ -132,8 +129,6 @@ void onRequestPermissionsResult(String permissions[], int[] grantResults, boolea
}catch (Exception e){
e.printStackTrace();
}finally {
mSubjects.clear();
mParams.clear();
finish();
}
}
Expand All @@ -143,7 +138,7 @@ private void showReadPhoneStateHintDialog(final PublishSubject<Permission> subje
permissionName = name;
MyDialog.Builder builder = new MyDialog.Builder(this);
builder.setTitle(getString(R.string.hint));
builder.setMessage(String.format(getString(R.string.message),mParams.get(name).permissionDesc));
builder.setMessage(String.format(getString(R.string.message),XPermission.getInstance().getParams().get(name).permissionDesc));
builder.setPositiveButton(getString(R.string.go_to_set), new DialogInterface.OnClickListener() {

@Override
Expand All @@ -164,8 +159,6 @@ public void onClick(DialogInterface dialog, int id) {
}catch (Exception e){
e.printStackTrace();
}finally {
mSubjects.clear();
mParams.clear();
finish();
}

Expand All @@ -182,8 +175,6 @@ public void onCancel(DialogInterface dialog) {
}catch (Exception e){
e.printStackTrace();
}finally {
mSubjects.clear();
mParams.clear();
finish();
}

Expand All @@ -192,20 +183,6 @@ public void onCancel(DialogInterface dialog) {
dialog.show();
}

public static boolean containsByPermission(@NonNull Params permission) {
return mSubjects.containsKey(permission);
}

public static void setSubjectForPermission(@NonNull String permission, @NonNull PublishSubject<Permission> subject) {
mSubjects.clear();
mSubjects.put(permission, subject);
}

public static void setParams(Params params){
mParams.clear();
mParams.put(params.permissionName,params);
}

public static void setLogging(boolean logging) {
mLogging = logging;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,45 @@ public void onClick(View v) {
@Override
public void onClick(final int i) {
Log.e("MainActivity","onItemClick");
PermissionManager.getInstance().requestPermission(MainActivity.this, permissions[i], permissionDiscription[i],true, new IPermissionResponse() {
@Override
public void onResponse(int code) {
if(code==IPermissionResponse.SUCCESS){
Toast.makeText(MainActivity.this,"授权成功",Toast.LENGTH_SHORT).show();
}else if(code == IPermissionResponse.REFUSE){
Toast.makeText(MainActivity.this,"拒绝授权",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this,"拒绝授权, 请去设置中开启授权",Toast.LENGTH_SHORT).show();
if(i == 3){
PermissionManager.getInstance().requestPermission(MainActivity.this, permissions[i], permissionDiscription[i],true, new IPermissionResponse() {
@Override
public void onResponse(int code) {
if(code==IPermissionResponse.SUCCESS){
PermissionManager.getInstance().requestPermission(MainActivity.this, permissions[10], permissionDiscription[10],true, new IPermissionResponse() {
@Override
public void onResponse(int code) {
if(code==IPermissionResponse.SUCCESS){
Toast.makeText(MainActivity.this,"授权成功",Toast.LENGTH_SHORT).show();
}else if(code == IPermissionResponse.REFUSE){
Toast.makeText(MainActivity.this,"拒绝授权",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this,"拒绝授权, 请去设置中开启授权",Toast.LENGTH_SHORT).show();
}
}
});
}else if(code == IPermissionResponse.REFUSE){
Toast.makeText(MainActivity.this,"拒绝授权",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this,"拒绝授权, 请去设置中开启授权",Toast.LENGTH_SHORT).show();
}
}
}
});
});
}else{
PermissionManager.getInstance().requestPermission(MainActivity.this, permissions[i], permissionDiscription[i],true, new IPermissionResponse() {
@Override
public void onResponse(int code) {
if(code==IPermissionResponse.SUCCESS){
Toast.makeText(MainActivity.this,"授权成功",Toast.LENGTH_SHORT).show();
}else if(code == IPermissionResponse.REFUSE){
Toast.makeText(MainActivity.this,"拒绝授权",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this,"拒绝授权, 请去设置中开启授权",Toast.LENGTH_SHORT).show();
}
}
});
}

}

@Override
Expand Down

0 comments on commit cb0e325

Please sign in to comment.