Skip to content
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
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ android {
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.android.support:design:23.3.0'
compile project(':permiso')
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ public void onPermissionResult(Permiso.ResultSet resultSet) {
if (resultSet.isPermissionGranted(Manifest.permission.READ_CALENDAR)) {
numGranted++;
}
Toast.makeText(MainActivity.this, numGranted + R.string.two_permission_granted, Toast.LENGTH_SHORT).show();
String message = getString(R.string.X_permissions_granted, numGranted);
Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show();
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
<string name="permission_granted">Permission Granted!</string>
<string name="permission_permanently_denied">Permission Permanently Denied.</string>
<string name="permission_denied">Permission Denied.</string>
<string name="two_permission_granted">2 Permissions Granted.</string>
<string name="X_permissions_granted">%1$d Permissions Granted.</string>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use lowercase 'x'.

</resources>
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.3.0'
classpath 'com.android.tools.build:gradle:2.1.0'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.4'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'

Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.4-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip
2 changes: 1 addition & 1 deletion permiso/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ android {
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:appcompat-v7:23.3.0'
}

// Only add in the bintray stuff if we're building locally
Expand Down
66 changes: 44 additions & 22 deletions permiso/src/main/java/com/greysonparrelli/permiso/Permiso.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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);
}
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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.");
}
}
});
}

/**
Expand All @@ -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)
Copy link
Owner

Choose a reason for hiding this comment

The 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.
Expand All @@ -190,7 +209,7 @@ public void onClose() {
rationaleCallback.onRationaleProvided();
}
});
dialogFragment.show(mActivity.get().getFragmentManager(), PermisoDialogFragment.TAG);
dialogFragment.show(fm, PermisoDialogFragment.TAG);
}


Expand Down Expand Up @@ -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;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;

/**
* An Activity that handles the small amount of boilerplate that {@link Permiso} requires to run. If you'd rather not
* use this as your base activity class, simply remember to do the following in each of your activities:
* <ul>
* <li>Call {@link Permiso#setActivity(Activity)} in {@link Activity#onCreate(Bundle)} and {@link Activity#onResume()}</li>
* <li>Call {@link Permiso#setActivity(FragmentActivity)} in {@link Activity#onCreate(Bundle)} and {@link Activity#onResume()}</li>
* <li>Call {@link Permiso#onRequestPermissionResult(int, String[], int[])} in
* {@link Activity#onRequestPermissionsResult(int, String[], int[])}</li>
* </ul>
Expand All @@ -29,7 +30,7 @@ protected void onResume() {
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
public void onRequestPermissionsResult(final int requestCode, @NonNull final String[] permissions, @NonNull final int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
Permiso.getInstance().onRequestPermissionResult(requestCode, permissions, grantResults);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.greysonparrelli.permiso;

import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;

/**
Expand Down Expand Up @@ -70,6 +70,7 @@ public void onDestroyView() {
super.onDestroyView();
}

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
Expand Down