fix permission

androidx
Zhanty 5 years ago
parent 70080586e8
commit fd3961280e
  1. 37
      lib_base/src/main/java/com/android/base/permission/AutoPermissionFragment.java
  2. 37
      lib_base/src/main/java/com/android/base/permission/AutoPermissionRequester.java
  3. 8
      lib_base/src/main/java/com/android/base/permission/PermissionRequesterImpl.java

@ -7,6 +7,8 @@ import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import timber.log.Timber;
/**
* @author Ztiany
* Email: ztiany3@gmail.com
@ -21,7 +23,7 @@ public class AutoPermissionFragment extends Fragment {
private final Handler mHandler = new Handler();
private final Runnable mRunnable = this::startChecked;
private AutoPermissionFragmentCallback mRequester;
private AutoPermissionFragmentCallback mRequesterReference;
private boolean mIsActivityReady = false;
@Override
@ -45,32 +47,40 @@ public class AutoPermissionFragment extends Fragment {
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (mRequester != null) {
mRequester.onRequestPermissionsResult(requestCode, permissions, grantResults);
Timber.d("onRequestPermissionsResult() called " + mRequesterReference);
AutoPermissionFragmentCallback callback = getCallback();
if (callback != null) {
callback.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
mRequester = null;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Timber.d("onActivityResult() called " + mRequesterReference);
super.onActivityResult(requestCode, resultCode, data);
if (mRequester != null) {
mRequester.onActivityResult(requestCode, resultCode, data);
AutoPermissionFragmentCallback callback = getCallback();
if (callback != null) {
callback.onActivityResult(requestCode, resultCode, data);
}
}
void startRequest() {
mHandler.removeCallbacks(mRunnable);
startChecked();
}
private void startChecked() {
Timber.d("startChecked() called " + mRequesterReference);
if (mIsActivityReady) {
if (mRequester != null) {
mRequester.onReady();
AutoPermissionFragmentCallback callback = getCallback();
if (callback != null) {
callback.onReady();
}
mHandler.removeCallbacks(mRunnable);
} else {
mHandler.post(mRunnable);
mHandler.postDelayed(mRunnable, 300);
}
}
@ -81,10 +91,15 @@ public class AutoPermissionFragment extends Fragment {
}
void setRequester(AutoPermissionFragmentCallback requester) {
mRequester = requester;
Timber.d("setRequester() called with: requester = [" + requester + "]");
mRequesterReference = requester;
}
private AutoPermissionFragmentCallback getCallback() {
return mRequesterReference;
}
interface AutoPermissionFragmentCallback{
interface AutoPermissionFragmentCallback {
void onReady();
void onActivityResult(int requestCode, int resultCode, Intent data);

@ -1,5 +1,7 @@
package com.android.base.permission;
import android.arch.lifecycle.DefaultLifecycleObserver;
import android.arch.lifecycle.LifecycleOwner;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
@ -8,6 +10,8 @@ import android.support.v4.app.FragmentManager;
import com.android.base.utils.android.ActFragWrapper;
import timber.log.Timber;
import static com.android.base.permission.PermissionCode.PERMISSION_REQUESTER_CODE;
/**
@ -33,28 +37,38 @@ public class AutoPermissionRequester {
private String[] mPerms;
private boolean mAskAgain = true;
private IPermissionUIProvider mPermissionUIProvider;
private PermissionCallback mPermissionCallback;
private PermissionRequester mPermissionRequester;
private OnAllPermissionGrantedListener mOnAllPermissionGrantedListener;
private OnPermissionDeniedListener mOnPermissionDeniedListener;
private PermissionCallback mPermissionCallback;
private IPermissionUIProvider mPermissionUIProvider;
private PermissionRequester mPermissionRequester;
private AutoPermissionFragment.AutoPermissionFragmentCallback mAutoPermissionFragmentCallback;
private AutoPermissionRequester(FragmentActivity activity) {
private AutoPermissionRequester(FragmentActivity activity, LifecycleOwner lifecycleOwner) {
mActivity = activity;
if (mActivity == null) {
throw new NullPointerException();
}
DefaultLifecycleObserver observer = new DefaultLifecycleObserver() {
@Override
public void onDestroy(@NonNull LifecycleOwner owner) {
mOnAllPermissionGrantedListener = null;
mOnPermissionDeniedListener = null;
}
};
lifecycleOwner.getLifecycle().addObserver(observer);
}
public static AutoPermissionRequester with(Fragment fragment) {
return new AutoPermissionRequester(fragment.getActivity());
return new AutoPermissionRequester(fragment.getActivity(), fragment);
}
public static AutoPermissionRequester with(FragmentActivity activity) {
return new AutoPermissionRequester(activity);
return new AutoPermissionRequester(activity, activity);
}
public AutoPermissionRequester permission(String... permissions) {
@ -114,9 +128,10 @@ public class AutoPermissionRequester {
private AutoPermissionFragment.AutoPermissionFragmentCallback getCallback() {
if (mAutoPermissionFragmentCallback == null) {
return mAutoPermissionFragmentCallback = new AutoPermissionFragment.AutoPermissionFragmentCallback() {
mAutoPermissionFragmentCallback = new AutoPermissionFragment.AutoPermissionFragmentCallback() {
@Override
public void onReady() {
Timber.d("onReady() called");
if (mPermissionRequester != null) {
mPermissionRequester.requestPermission(PERMISSION_REQUESTER_CODE, mPerms);
}
@ -124,16 +139,16 @@ public class AutoPermissionRequester {
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (PERMISSION_REQUESTER_CODE != requestCode) {
Timber.d("onActivityResult() called with: requestCode = [" + requestCode + "], resultCode = [" + resultCode + "], data = [" + data + "]");
if (mPermissionRequester != null) {
mPermissionRequester.onActivityResult(requestCode, resultCode, data);
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (mPermissionRequester != null) {
Timber.d("onRequestPermissionsResult() called with: requestCode = [" + requestCode + "], permissions = [" + permissions + "], grantResults = [" + grantResults + "]");
if (requestCode == PERMISSION_REQUESTER_CODE && mPermissionRequester != null) {
mPermissionRequester.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}

@ -6,6 +6,8 @@ import com.android.base.utils.android.ActFragWrapper;
import java.util.List;
import timber.log.Timber;
import static com.android.base.permission.PermissionCode.REQUEST_PERMISSION_FOR_SETTING;
@ -25,6 +27,7 @@ class PermissionRequesterImpl implements EasyPermissions.PermissionCaller {
@Override
public Object getRequester() {
Timber.d("getRequester() called");
if (mContextWrapper.getFragment() != null) {
return mContextWrapper.getFragment();
}
@ -33,6 +36,7 @@ class PermissionRequesterImpl implements EasyPermissions.PermissionCaller {
@Override
public IPermissionUIProvider getPermissionUIProvider() {
Timber.d("getPermissionUIProvider() called");
if (mIPermissionUIProvider == null) {
return PermissionUIProviderFactory.getPermissionUIProvider();
}
@ -44,6 +48,7 @@ class PermissionRequesterImpl implements EasyPermissions.PermissionCaller {
*/
@Override
public void onPortionPermissionsGranted(boolean allGranted, int requestCode, List<String> perms) {
Timber.d("onPortionPermissionsGranted() called with: allGranted = [" + allGranted + "], requestCode = [" + requestCode + "], perms = [" + perms + "]");
// do nothing
if (allGranted) {
mPermissionCallback.onAllPermissionGranted();
@ -52,6 +57,7 @@ class PermissionRequesterImpl implements EasyPermissions.PermissionCaller {
@Override
public void onPermissionsDenied(final int requestCode, final List<String> perms) {
Timber.d("onPermissionsDenied() called with: requestCode = [" + requestCode + "], perms = [" + perms + "]");
if (!mShouldAskAgain) {
notifyPermissionDenied(perms);
return;
@ -72,9 +78,9 @@ class PermissionRequesterImpl implements EasyPermissions.PermissionCaller {
}
private void notifyPermissionDenied(List<String> perms) {
Timber.d("notifyPermissionDenied() called with: perms = [" + perms + "]");
mPermissionCallback.onPermissionDenied(perms);
getPermissionUIProvider().showPermissionDeniedTip(mContextWrapper.getContext(), perms.toArray(new String[0]));
}
}

Loading…
Cancel
Save