|
|
@ -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); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|