super.onCreate(savedInstanceState);
// 设置为 true,表示 configuration change 的时候,fragment 实例不会背重新创建
setRetainInstance(true);
mActivity = getActivity();
}
public void requestPermissions(@NonNull String[] permissions, IPermissionListenerWrap.IPermissionListener callback) {
int requestCode = makeRequestCode();
mCallbacks.put(requestCode, callback);
requestPermissions(permissions, requestCode);
}
public void requestEachPermissions(@NonNull String[] permissions, IPermissionListenerWrap.IEachPermissionListener callback) {
int requestCode = makeRequestCode();
mEachCallbacks.put(requestCode, callback);
requestPermissions(permissions, requestCode);
}
/**
-
随机生成唯一的requestCode,最多尝试10次
-
@return
*/
private int makeRequestCode() {
int requestCode;
int tryCount = 0;
do {
requestCode = mCodeGenerator.nextInt(0x0000FFFF);
tryCount++;
} while (mCallbacks.indexOfKey(requestCode) >= 0 && tryCount < 10);
return requestCode;
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
handlePermissionCallBack(requestCode, grantResults);
handleEachPermissionCallBack(requestCode, permissions, grantResults);
}
private void handlePermissionCallBack(int requestCode, @NonNull int[] grantResults) {
IPermissionListenerWrap.IPermissionListener callback = mCallbacks.get(requestCode);
mCallbacks.remove(requestCode);
if (callback == null) {
return;
}
boolean allGranted = false;
int length = grantResults.length;
for (int i = 0; i < length; i++) {
int grantResult = grantRe