-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support dialogfragment #16
Open
paulpv
wants to merge
13
commits into
greysonp:master
Choose a base branch
from
paulpv:support_dialogfragment
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
1208b29
checkActivity returns the Activity; avoids multiple mActivity.get()s
paulpv 7908841
Prevent multiple PermisoDialogFragment in showRationaleInDialog
paulpv fe800d4
Updating to latest build tools and dependencies
paulpv 1c9f619
Fixing toast message when multiple permissions are denied
paulpv 9fc2cae
Missed a line
paulpv 6702176
android.app.DialogFragment -> android.support.v4.app.DialogFragment
paulpv b7f9a41
Fixing a compiler/lint warning
paulpv fd28afa
Merge branch 'dialogfragment' into support_dialogfragment
paulpv 236b3f4
Moving Handler hack directly in to Permiso.onRequestPermissionResult
paulpv eada4ff
Updating travis-ci config to use build-tools-23.0.3
paulpv 55c3fe8
Merge branch 'dialogfragment' into support_dialogfragment
paulpv 663ba0b
Merge branch 'master' into dialogfragment
paulpv 26898dd
Merge branch 'dialogfragment' into support_dialogfragment
paulpv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,10 +3,13 @@ | |
import android.app.Activity; | ||
import android.content.pm.PackageManager; | ||
import android.os.Bundle; | ||
import android.os.Handler; | ||
import android.support.annotation.MainThread; | ||
import android.support.annotation.NonNull; | ||
import android.support.annotation.Nullable; | ||
import android.support.v4.app.ActivityCompat; | ||
import android.support.v4.app.FragmentActivity; | ||
import android.support.v4.app.FragmentManager; | ||
import android.support.v4.content.ContextCompat; | ||
import android.util.Log; | ||
|
||
|
@@ -34,9 +37,9 @@ public class Permiso { | |
|
||
/** | ||
* The active activity. Used to make permissions requests. This must be set by the library-user through | ||
* {@link Permiso#setActivity(Activity)} or else bad things will happen. | ||
* {@link Permiso#setActivity(FragmentActivity)} or else bad things will happen. | ||
*/ | ||
private WeakReference<Activity> mActivity; | ||
private WeakReference<FragmentActivity> mActivity; | ||
|
||
/** | ||
* This is just a value we increment to generate new request codes for use with | ||
|
@@ -81,13 +84,13 @@ private Permiso() { | |
* <strong>Important: </strong> If your activity subclasses {@link PermisoActivity}, this is already handled for you. | ||
* @param activity The activity that is currently active. | ||
*/ | ||
public void setActivity(@NonNull Activity activity) { | ||
public void setActivity(@NonNull FragmentActivity activity) { | ||
mActivity = new WeakReference<>(activity); | ||
} | ||
|
||
/** | ||
* Request one or more permissions from the system. Make sure that you are either subclassing {@link PermisoActivity} | ||
* or that you have set your current activity using {@link Permiso#setActivity(Activity)}! | ||
* or that you have set your current activity using {@link Permiso#setActivity(FragmentActivity)}! | ||
* @param callback | ||
* A callback that will be triggered when the results of your permission request are available. | ||
* @param permissions | ||
|
@@ -96,13 +99,13 @@ public void setActivity(@NonNull Activity activity) { | |
*/ | ||
@MainThread | ||
public void requestPermissions(@NonNull IOnPermissionResult callback, String... permissions) { | ||
checkActivity(); | ||
FragmentActivity activity = checkActivity(); | ||
|
||
final RequestData requestData = new RequestData(callback, permissions); | ||
|
||
// Mark any permissions that are already granted | ||
for (String permission : permissions) { | ||
if (ContextCompat.checkSelfPermission(mActivity.get(), permission) == PackageManager.PERMISSION_GRANTED) { | ||
if (ContextCompat.checkSelfPermission(activity, permission) == PackageManager.PERMISSION_GRANTED) { | ||
requestData.resultSet.grantPermissions(permission); | ||
} | ||
} | ||
|
@@ -122,7 +125,7 @@ public void requestPermissions(@NonNull IOnPermissionResult callback, String... | |
final int requestCode = markRequestAsActive(requestData); | ||
|
||
// First check if there's any permissions for which we need to provide a rationale for using | ||
String[] permissionsThatNeedRationale = requestData.resultSet.getPermissionsThatNeedRationale(mActivity.get()); | ||
String[] permissionsThatNeedRationale = requestData.resultSet.getPermissionsThatNeedRationale(activity); | ||
|
||
// If there are some that need a rationale, show that rationale, then continue with the request | ||
if (permissionsThatNeedRationale.length > 0) { | ||
|
@@ -152,15 +155,23 @@ public void onRationaleProvided() { | |
* The grant results given to you by {@link Activity#onRequestPermissionsResult(int, String[], int[])}. | ||
*/ | ||
@MainThread | ||
public void onRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults) { | ||
if (mCodesToRequests.containsKey(requestCode)) { | ||
RequestData requestData = mCodesToRequests.get(requestCode); | ||
requestData.resultSet.parsePermissionResults(permissions, grantResults, mActivity.get()); | ||
requestData.onResultListener.onPermissionResult(requestData.resultSet); | ||
mCodesToRequests.remove(requestCode); | ||
} else { | ||
Log.w(TAG, "onRequestPermissionResult() was given an unrecognized request code."); | ||
} | ||
public void onRequestPermissionResult(final int requestCode, final String[] permissions, final int[] grantResults) { | ||
// If we don't do this, android.support.v4.app.DialogFragment will throw IllegalStateException. See bug: | ||
// https://code.google.com/p/android/issues/detail?id=190966 | ||
new Handler().post(new Runnable() { | ||
@Override | ||
public void run() { | ||
FragmentActivity activity = checkActivity(); | ||
if (mCodesToRequests.containsKey(requestCode)) { | ||
RequestData requestData = mCodesToRequests.get(requestCode); | ||
requestData.resultSet.parsePermissionResults(permissions, grantResults, activity); | ||
requestData.onResultListener.onPermissionResult(requestData.resultSet); | ||
mCodesToRequests.remove(requestCode); | ||
} else { | ||
Log.w(TAG, "onRequestPermissionResult() was given an unrecognized request code."); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
|
@@ -178,9 +189,17 @@ public void onRequestPermissionResult(int requestCode, String[] permissions, int | |
*/ | ||
@MainThread | ||
public void showRationaleInDialog(@Nullable String title, @NonNull String message, @Nullable String buttonText, @NonNull final IOnRationaleProvided rationaleCallback) { | ||
checkActivity(); | ||
FragmentActivity activity = checkActivity(); | ||
|
||
FragmentManager fm = activity.getSupportFragmentManager(); | ||
|
||
PermisoDialogFragment dialogFragment = (PermisoDialogFragment) fm.findFragmentByTag(PermisoDialogFragment.TAG); | ||
if (dialogFragment != null) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same-line curly braces. |
||
{ | ||
dialogFragment.dismiss(); | ||
} | ||
|
||
PermisoDialogFragment dialogFragment = PermisoDialogFragment.newInstance(title, message, buttonText); | ||
dialogFragment = PermisoDialogFragment.newInstance(title, message, buttonText); | ||
|
||
// We show the rationale after the dialog is closed. We use setRetainInstance(true) in the dialog to ensure that | ||
// it retains the listener after an app rotation. | ||
|
@@ -190,7 +209,7 @@ public void onClose() { | |
rationaleCallback.onRationaleProvided(); | ||
} | ||
}); | ||
dialogFragment.show(mActivity.get().getFragmentManager(), PermisoDialogFragment.TAG); | ||
dialogFragment.show(fm, PermisoDialogFragment.TAG); | ||
} | ||
|
||
|
||
|
@@ -257,18 +276,21 @@ private int markRequestAsActive(RequestData requestData) { | |
* @param requestCode The request code of the request you want to run. | ||
*/ | ||
private void makePermissionRequest(int requestCode) { | ||
FragmentActivity activity = checkActivity(); | ||
RequestData requestData = mCodesToRequests.get(requestCode); | ||
ActivityCompat.requestPermissions(mActivity.get(), requestData.resultSet.getUngrantedPermissions(), requestCode); | ||
ActivityCompat.requestPermissions(activity, requestData.resultSet.getUngrantedPermissions(), requestCode); | ||
} | ||
|
||
/** | ||
* Ensures that our WeakReference to the Activity is still valid. If it isn't, throw an exception saying that the | ||
* Activity needs to be set. | ||
*/ | ||
private void checkActivity() { | ||
if (mActivity.get() == null) { | ||
private FragmentActivity checkActivity() { | ||
FragmentActivity activity = mActivity.get(); | ||
if (activity == null) { | ||
throw new IllegalStateException("No activity set. Either subclass PermisoActivity or call Permiso.setActivity() in onCreate() and onResume() of your Activity."); | ||
} | ||
return activity; | ||
} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use lowercase 'x'.