[toc]java
RxPermissions 是基於 RxJava 開發的用於幫助 在Android 6.0 中處理運行時權限檢測的框架。在 Android 6.0 中增長了對危險權限的動態申請,而不是像 Android 6.0 以前的默認所有獲取的方式。android
若是按照以往的獲取權限方式的話,那麼咱們獲取權限通常須要有 3 個步驟,第一步是先判斷當前是否已經獲取到該權限了;第 2 步申請對應的權限;第 3 步在 Activity 或者 Fragment 中處理獲取權限的結果。具體的實現步驟以下:數組
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.CAMERA)) {
//用於開發者提示用戶權限的用途
} else {
//申請權限
}
複製代碼
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.CAMERA},
REQUEST_CAMERA);
複製代碼
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
// 判斷請求碼,肯定當前申請的權限
if (requestCode == REQUEST_CAMERA) {
//判斷權限是否申請經過
if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//受權成功
} else {
//受權失敗
}
} else {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
複製代碼
其實 RxPermissions 的使用方式有兩種緩存
RxPermissions rxPermissions = new RxPermissions(MainActivity.this);
rxPermissions
.request(Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE)//這裏填寫所須要的權限
.subscribe(new Consumer<Boolean>() {
@Override
public void accept(Boolean aBoolean) throws Exception {
if (aBoolean) {
// 經過
}else{
// 拒絕
}
}
});
複製代碼
RxPermissions rxPermissions = new RxPermissions(MainActivity.this);
// Must be done during an initialization phase like onCreate
RxView.clicks(findViewById(R.id.enableCamera))
.compose(rxPermissions.ensure(Manifest.permission.CAMERA))
.subscribe(granted -> {
// R.id.enableCamera has been clicked
});
複製代碼
接着咱們來對這個 RxPermissions 進行一個源碼的解析,可是打開源碼的時候,咱們能夠發現,這個庫裏面,其實就只有 3 個類:RxPermissions、RxPermissionsFragment、Permissionbash
對於源碼的分析,咱們應該先從簡單的使用入手。下面咱們能夠先看看實例化 RxPermissionsFragment 的時候是作了什麼?app
RxPermissionsFragment mRxPermissionsFragment;
public RxPermissions(@NonNull Activity activity) {
mRxPermissionsFragment = getRxPermissionsFragment(activity);
}
複製代碼
咱們能夠看到,上面的代碼中,實例化 RxPermissionsFragment 的時候,裏面先建立了一個 RxPermissionsFragment 的實例。咱們再接着看 getRxPermissionsFragment 這個方法的實現。框架
private RxPermissionsFragment getRxPermissionsFragment(Activity activity) {
// 查找 RxPermissionsFragment 是否已經被添加了
RxPermissionsFragment rxPermissionsFragment = findRxPermissionsFragment(activity);
boolean isNewInstance = rxPermissionsFragment == null;
if (isNewInstance) {
rxPermissionsFragment = new RxPermissionsFragment();
FragmentManager fragmentManager = activity.getFragmentManager();
fragmentManager
.beginTransaction()
.add(rxPermissionsFragment, TAG)
.commitAllowingStateLoss();
fragmentManager.executePendingTransactions();
}
return rxPermissionsFragment;
}
複製代碼
在 getRxPermissionsFragment() 這個方法中,首先是先查找當前是否已經添加了這個 rxPermissionsFragment 的實例,若是已經添加,那麼直接返回已經添加的實例,若是沒有添加過的話,那麼就從新再建立一個 RxPermissionsFragment 實例並提交;dom
private RxPermissionsFragment findRxPermissionsFragment(Activity activity) {
return (RxPermissionsFragment) activity.getFragmentManager().findFragmentByTag(TAG);
}
複製代碼
到此,rxPermissionsFragment 的實例化已經完成,接着咱們須要看看 request 這個方法中實現了什麼。ide
public Observable<Boolean> request(final String... permissions) {
return Observable.just(TRIGGER).compose(ensure(permissions));
}
複製代碼
static final Object TRIGGER = new Object();
複製代碼
從上面的代碼中,咱們能夠看到,request 方法中須要傳入的參數是一個 權限的數組,返回值是 Observable 對象。Observable.just(TRIGGER) 是快捷建立一個 Observable 的方式,因爲 TRIGGER 是一個空的 Object 對象,因此 TRIGGER 就是一個佔位符而已,Observable.just(TRIGGER) 建立的是一個 Observable,以後經過 compose 將 Observable 轉化爲 Observable 並返回。在 compose 中須要的參數是一個 ObservableTransformer,那麼咱們接着看 ensure() 這個方法。函數
public <T> ObservableTransformer<T, Boolean> ensure(final String... permissions) {
// 建立一個Transformer對象返回
return new ObservableTransformer<T, Boolean>() {
@Override
public ObservableSource<Boolean> apply(Observable<T> o) {
//request(o, permissions) 方法返回 Observable<Permission> 對象
return request(o, permissions)
// 將 Observable<Permission> 轉換爲 Observable<Boolean>,在這裏會等待全部的權限都返回了一次性發射數據。
.buffer(permissions.length)
.flatMap(new Function<List<Permission>, ObservableSource<Boolean>>() {
@Override
public ObservableSource<Boolean> apply(List<Permission> permissions) throws Exception {
// 若是permissions爲空那麼直接返回Observable.empty();
if (permissions.isEmpty()) {
// Occurs during orientation change, when the subject receives onComplete.
// In that case we don't want to propagate that empty list to the // subscriber, only the onComplete. return Observable.empty(); } // Return true if all permissions are granted. for (Permission p : permissions) { if (!p.granted) { return Observable.just(false); } } return Observable.just(true); } }); } }; } 複製代碼
在 ensure 的這個方法中,最終會返回的是 ObservableTransformer<T, Boolean> 對象。接着咱們看看 ObservableTransformer 的匿名實現類裏面的 apply 方法,這裏實現的就是將 Observable 轉換爲 Observable 的操做。咱們對 apply 這個方法裏面的代碼進行簡化一下。
return request(o,permissions)
.buffer(permissions.length)
.flatMap(new Function<List<Permission>, ObservableSource<Boolean>>{});
複製代碼
private Observable<Permission> request(final Observable<?> trigger, final String... permissions) {
if (permissions == null || permissions.length == 0) {
throw new IllegalArgumentException("RxPermissions.request/requestEach requires at least one input permission");
}
return oneOf(trigger, pending(permissions))
.flatMap(new Function<Object, Observable<Permission>>() {
@Override
public Observable<Permission> apply(Object o) throws Exception {
return requestImplementation(permissions);
}
});
}
複製代碼
在 request 這個方法裏面,其實 oneOf() 和 pending() 方法咱們能夠忽略的,主要的話,咱們應該關注 requestImplementation(final String... permissions) 這個方法,在這個方法裏面,主要實現了權限的請求。
@TargetApi(Build.VERSION_CODES.M)
private Observable<Permission> requestImplementation(final String... permissions) {
List<Observable<Permission>> list = new ArrayList<>(permissions.length);
List<String> unrequestedPermissions = new ArrayList<>();
// In case of multiple permissions, we create an Observable for each of them.
// At the end, the observables are combined to have a unique response.
for (String permission : permissions) {
mRxPermissionsFragment.log("Requesting permission " + permission);
if (isGranted(permission)) {
// Already granted, or not Android M
// Return a granted Permission object.
// 權限已經被贊成或者不是 Android 6.0 以上版本,建立一個 贊成的 Permission 對象。
list.add(Observable.just(new Permission(permission, true, false)));
continue;
}
if (isRevoked(permission)) {
// 權限被拒絕,返回一個 拒絕的 Permission 對象。
list.add(Observable.just(new Permission(permission, false, false)));
continue;
}
PublishSubject<Permission> subject = mRxPermissionsFragment.getSubjectByPermission(permission);
// 若是 subject 不存在,那麼建立一個 subject。
if (subject == null) {
unrequestedPermissions.add(permission);
subject = PublishSubject.create();
mRxPermissionsFragment.setSubjectForPermission(permission, subject);
}
list.add(subject);
}
// 還未提起申請的權限進行申請
if (!unrequestedPermissions.isEmpty()) {
String[] unrequestedPermissionsArray = unrequestedPermissions.toArray(new String[unrequestedPermissions.size()]);
requestPermissionsFromFragment(unrequestedPermissionsArray);
}
// 嚴格按照順序發射數據
return Observable.concat(Observable.fromIterable(list));
}
複製代碼
@TargetApi(Build.VERSION_CODES.M)
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode != PERMISSIONS_REQUEST_CODE) return;
boolean[] shouldShowRequestPermissionRationale = new boolean[permissions.length];
for (int i = 0; i < permissions.length; i++) {
shouldShowRequestPermissionRationale[i] = shouldShowRequestPermissionRationale(permissions[i]);
}
onRequestPermissionsResult(permissions, grantResults, shouldShowRequestPermissionRationale);
}
void onRequestPermissionsResult(String permissions[], int[] grantResults, boolean[] shouldShowRequestPermissionRationale) {
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]);
if (subject == null) {
// No subject found
Log.e(RxPermissions.TAG, "RxPermissions.onRequestPermissionsResult invoked but didn't find the corresponding permission request.");
return;
}
// 發射權限申請結果
mSubjects.remove(permissions[i]);
boolean granted = grantResults[i] == PackageManager.PERMISSION_GRANTED;
subject.onNext(new Permission(permissions[i], granted, shouldShowRequestPermissionRationale[i]));
subject.onComplete();
}
}
複製代碼
just 操做符是將一個對象轉化爲 Observable 的操做符。這個對象能夠是一個數字、字符串或者是數組對象等,是 RxJava 中快速建立一個 Observable 對象的操做符。若是有 subscriber 訂閱的話,那麼會依次調用 onNext() 和 OnComplete() 方法。因此這裏只是建立了一個 Observable 對象,方便後續的調用。
compose 操做符是對 Observable 對象的總體轉化。例如:經過 Transformer,咱們能夠將 Observable 對象轉換成 Observable 對象了。
public static ObservableTransformer<String,Boolean> getTransformer(){
return new ObservableTransformer<String, Boolean>() {
@Override
public ObservableSource<Boolean> apply(Observable<String> upstream) {
return upstream.flatMap(new Function<String, ObservableSource<Boolean>>() {
@Override
public ObservableSource<Boolean> apply(String s) throws Exception {
return Observable.just(true);
}
});
}
};
}
複製代碼
/**
* 線程切換
* @return
*/
public static <T> ObservableTransformer<T,T> getScheduler(){
return new ObservableTransformer<T, T>() {
@Override
public ObservableSource<T> apply(Observable<T> upstream) {
return upstream.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread());
}
};
}
複製代碼
buffer 操做符將一個 Observable 變換爲另外一個,原來的 Observable 正常發射數據,變換產生的 Observable 發射這些數據的緩存集合。buffer將數據緩存到一個集合當中,而後在適當的時機一塊兒發送。 buffer(count) 以列表(List)的形式發射非重疊的緩存,每個緩存至多包含來自原始Observable的count項數據(最後發射的列表數據可能少於count項)
Observable.just(1,2,3,4,5,6)
.buffer(2)
.subscribe(integers -> {
Log.i(TAG, "accept size: "+integers.size());
for (Integer integer : integers) {
Log.i(TAG, "accept: "+integer);
}
});
複製代碼
2018-12-14 11:16:28.452 28126-28126/com.luwei.lwbaselib I/LwBaseActivity: accept size: 2
2018-12-14 11:16:28.452 28126-28126/com.luwei.lwbaselib I/LwBaseActivity: accept: 1
2018-12-14 11:16:28.453 28126-28126/com.luwei.lwbaselib I/LwBaseActivity: accept: 2
2018-12-14 11:16:28.453 28126-28126/com.luwei.lwbaselib I/LwBaseActivity: accept size: 2
2018-12-14 11:16:28.453 28126-28126/com.luwei.lwbaselib I/LwBaseActivity: accept: 3
2018-12-14 11:16:28.453 28126-28126/com.luwei.lwbaselib I/LwBaseActivity: accept: 4
2018-12-14 11:16:28.453 28126-28126/com.luwei.lwbaselib I/LwBaseActivity: accept size: 2
2018-12-14 11:16:28.453 28126-28126/com.luwei.lwbaselib I/LwBaseActivity: accept: 5
2018-12-14 11:16:28.453 28126-28126/com.luwei.lwbaselib I/LwBaseActivity: accept: 6
複製代碼
Observable.just(1,2,3,4)
.buffer(3,1)
.subscribe(integers -> {
Log.i(TAG, "accept size: "+integers.size());
for (Integer integer : integers) {
Log.i(TAG, "accept: "+integer);
}
});
複製代碼
2018-12-14 11:24:31.455 29164-29164/com.luwei.lwbaselib I/LwBaseActivity: accept size: 3
2018-12-14 11:24:31.455 29164-29164/com.luwei.lwbaselib I/LwBaseActivity: accept: 1
2018-12-14 11:24:31.455 29164-29164/com.luwei.lwbaselib I/LwBaseActivity: accept: 2
2018-12-14 11:24:31.455 29164-29164/com.luwei.lwbaselib I/LwBaseActivity: accept: 3
2018-12-14 11:24:31.455 29164-29164/com.luwei.lwbaselib I/LwBaseActivity: accept size: 3
2018-12-14 11:24:31.455 29164-29164/com.luwei.lwbaselib I/LwBaseActivity: accept: 2
2018-12-14 11:24:31.455 29164-29164/com.luwei.lwbaselib I/LwBaseActivity: accept: 3
2018-12-14 11:24:31.455 29164-29164/com.luwei.lwbaselib I/LwBaseActivity: accept: 4
2018-12-14 11:24:31.456 29164-29164/com.luwei.lwbaselib I/LwBaseActivity: accept size: 2
2018-12-14 11:24:31.456 29164-29164/com.luwei.lwbaselib I/LwBaseActivity: accept: 3
2018-12-14 11:24:31.456 29164-29164/com.luwei.lwbaselib I/LwBaseActivity: accept: 4
2018-12-14 11:24:31.456 29164-29164/com.luwei.lwbaselib I/LwBaseActivity: accept size: 1
2018-12-14 11:24:31.456 29164-29164/com.luwei.lwbaselib I/LwBaseActivity: accept: 4
複製代碼
是接收若干個Observables,發射數據是有序的,不會交叉。
繼承至 Subject,它的 Observer 只會接收到 PublishSubject 被訂閱以後發送的數據。示例代碼以下;咱們只會接收到 publishSubject3 和 publishSubject4;
PublishSubject<String> publishSubject = PublishSubject.create();
publishSubject.onNext("publishSubject1");
publishSubject.onNext("publishSubject2");
publishSubject.subscribe(new Consumer<String>() {
@Override
public void accept(String s) throws Exception {
Log.i(TAG, "accept: "+s);
}
});
publishSubject.onNext("publishSubject3");
publishSubject.onNext("publishSubject4");
複製代碼
2018-12-14 11:33:18.168 29916-29916/com.luwei.lwbaselib I/LwBaseActivity: accept: publishSubject3
2018-12-14 11:33:18.168 29916-29916/com.luwei.lwbaselib I/LwBaseActivity: accept: publishSubject4
複製代碼
能夠看到,在 RxPermissions 這個獲取權限的開源框架中,往 Activity 中添加了一個空的 Fragment,這個 fragment 纔是用來發起申請權限和處理權限的請求,最後再將結果返回,這樣子就避免了咱們發送請求以後,還須要在 onRequestPermissionsResult 中進行處理,並判斷 requestCode 等繁瑣操做。想到這裏,咱們平時使用 startActivityForResult 時,咱們也能夠一樣採用這樣的思路來簡化咱們的請求。
一樣的,咱們採用添加空白的 fragment,來作 startActivityForResult 請求,主要的實現類有 SimpleForResult 和 SimpleOnResultFragment,ActivityResultInfo 是請求 model,接下咱們先看代碼。
/**
* @Author: chenjianrun
* @Time: 2018/12/7
* @Description: 避免調用 startActivity 時,須要 onActivityResult 處理的類
*/
public class SimpleForResult {
private static final String TAG = "SimpleForResult";
private SimpleOnResultFragment mSimpleOnResultFragment;
public SimpleForResult(AppCompatActivity activity) {
mSimpleOnResultFragment = getOnResultFragment(activity.getSupportFragmentManager());
}
public SimpleForResult(Fragment fragment){
mSimpleOnResultFragment = getOnResultFragment(fragment.getChildFragmentManager());
}
private SimpleOnResultFragment getOnResultFragment(FragmentManager fragmentManager) {
SimpleOnResultFragment simpleOnResultFragment = findSimpleOnResultFragment(fragmentManager);
if (simpleOnResultFragment == null) {
simpleOnResultFragment = new SimpleOnResultFragment();
fragmentManager
.beginTransaction()
.add(simpleOnResultFragment, TAG)
.commitAllowingStateLoss();
fragmentManager.executePendingTransactions();
}
return simpleOnResultFragment;
}
private SimpleOnResultFragment findSimpleOnResultFragment(FragmentManager fragmentManager) {
return (SimpleOnResultFragment) fragmentManager.findFragmentByTag(TAG);
}
public Observable<ActivityResultInfo> startForResult(Intent intent) {
return mSimpleOnResultFragment.startForResult(intent);
}
public Observable<ActivityResultInfo> startForResult(Class<?> clazz) {
Intent intent = new Intent(mSimpleOnResultFragment.getActivity(), clazz);
return startForResult(intent);
}
public void startForResult(Intent intent, Callback callback) {
mSimpleOnResultFragment.startForResult(intent, callback);
}
public void startForResult(Class<?> clazz, Callback callback) {
Intent intent = new Intent(mSimpleOnResultFragment.getActivity(), clazz);
startForResult(intent, callback);
}
public interface Callback {
void onActivityResult(int requestCode, int resultCode, Intent data);
}
}
複製代碼
/**
* @Author: chenjianrun
* @Time: 2018/12/7
* @Description: 真正調用 startActivity 和處理 onActivityResult 的類。
*/
public class SimpleOnResultFragment extends Fragment {
private static Map<Integer, PublishSubject<ActivityResultInfo>> mSubjects = new HashMap<>();
private static Map<Integer, SimpleForResult.Callback> mCallbacks = new HashMap<>();
public SimpleOnResultFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
public Observable<ActivityResultInfo> startForResult(final Intent intent) {
int requestCode = generateRequestCode();
PublishSubject<ActivityResultInfo> subject = PublishSubject.create();
mSubjects.put(requestCode, subject);
startActivityForResult(intent, requestCode);
return subject;
}
public void startForResult(Intent intent, SimpleForResult.Callback callback) {
int requestCode = generateRequestCode();
mCallbacks.put(requestCode, callback);
startActivityForResult(intent, requestCode);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//rxjava方式的處理
PublishSubject<ActivityResultInfo> subject = mSubjects.remove(requestCode);
if (subject != null) {
subject.onNext(new ActivityResultInfo(requestCode, resultCode, data));
subject.onComplete();
}
//callback方式的處理
SimpleForResult.Callback callback = mCallbacks.remove(requestCode);
if (callback != null) {
callback.onActivityResult(requestCode, resultCode, data);
}
}
private int generateRequestCode(){
Random random = new Random();
for (;;){
int code = random.nextInt(65536);
if (!mSubjects.containsKey(code) && !mCallbacks.containsKey(code)){
return code;
}
}
}
}
複製代碼
package com.luwei.util.forresult;
import android.content.Intent;
/**
* @Author: chenjianrun
* @Time: 2018/12/7
* @Description:
*/
public class ActivityResultInfo {
private int requestCode;
private int resultCode;
private Intent data;
public ActivityResultInfo(int requestCode, int resultCode, Intent data) {
this.requestCode = requestCode;
this.resultCode = resultCode;
this.data = data;
}
public int getRequestCode() {
return requestCode;
}
public void setRequestCode(int requestCode) {
this.requestCode = requestCode;
}
public ActivityResultInfo(int resultCode, Intent data) {
this.resultCode = resultCode;
this.data = data;
}
public int getResultCode() {
return resultCode;
}
public void setResultCode(int resultCode) {
this.resultCode = resultCode;
}
public Intent getData() {
return data;
}
public void setData(Intent data) {
this.data = data;
}
}
複製代碼
// 簡化調用 startActivityForResult 及避免在 onActivityResult 中處理繁瑣的結果
SimpleForResult simpleForResult = new SimpleForResult(this);
simpleForResult.startForResult(ToastActivity.class)
.subscribe((resultInfo) -> {
if (resultInfo.getData() != null) {
ToastUtils.showLong(resultInfo.getData().getStringExtra("result"));
}
});
複製代碼
/**
* 打開攝像頭
*/
private void openCamera() {
try {
mTmpFile = FileUtils.createTmpFile(this);
} catch (IOException e) {
e.printStackTrace();
}
simpleForResult.startForResult(getOpenCameraIntent(this, mTmpFile))
.subscribe((resultInfo -> {
if (resultInfo.getResultCode() == RESULT_OK) {
mHeadUrl = mTmpFile.getAbsolutePath();
ImageLoaderUtils.loadCircleImage(this, ivHeader, mHeadUrl);
// 裁剪(若是沒有要求可裁剪,也能夠不要)
startPictureZoom(mTmpFile);
}
}));
}
/**
* 獲取打開照相機的 intent,適配 Android 7.0
* @param activity
* @param mTmpFile
* @return
*/
public static Intent getOpenCameraIntent(Activity activity,File mTmpFile){
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (intent.resolveActivity(activity.getPackageManager()) != null) {
if (mTmpFile != null && mTmpFile.exists()) {
if (Build.VERSION.SDK_INT >= 24) {
// 適配 Android 7.0
intent.putExtra(MediaStore.EXTRA_OUTPUT,
FileProvider.getUriForFile(activity, activity.getPackageName()+".provider",mTmpFile));
} else {
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mTmpFile));
}
} else {
Toast.makeText(activity, me.nereo.image_selector.R.string.error_image_not_exist, Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(activity, me.nereo.image_selector.R.string.msg_no_camera, Toast.LENGTH_SHORT).show();
}
return intent;
}
複製代碼