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

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

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

Loading…
Cancel
Save