Skip to content
This repository was archived by the owner on Oct 3, 2024. It is now read-only.

Commit e2062b4

Browse files
authored
Merge pull request #41 from samtstern/master
Version 0.2.0, redo settings dialog
2 parents d9aba34 + 61e9331 commit e2062b4

File tree

7 files changed

+270
-165
lines changed

7 files changed

+270
-165
lines changed

README.md

Lines changed: 26 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ EasyPermissions is installed by adding the following dependency to your `build.g
99

1010
```java
1111
dependencies {
12-
compile 'pub.devrel:easypermissions:0.1.9'
12+
compile 'pub.devrel:easypermissions:0.2.0'
1313
}
1414
```
1515

@@ -112,48 +112,35 @@ public class MainActivity extends AppCompatActivity
112112
In some cases your app will not function properly without certain permissions. If the user
113113
denies these permissions with the "Never Ask Again" option, you will be unable to request
114114
these permissions from the user and they must be changed in app settings. You can use the
115-
method `EasyPermissions.checkDeniedPermissionsNeverAskAgain()` to display a dialog to the
115+
method `EasyPermissions.somePermissionPermanentlyDenied(...)` to display a dialog to the
116116
user in this situation and direct them to the system setting screen for your app:
117117

118118
```java
119-
@Override
120-
public void onPermissionsDenied(int requestCode, List<String> perms) {
121-
Log.d(TAG, "onPermissionsDenied:" + requestCode + ":" + perms.size());
122-
123-
// Handle negative button on click listener. Pass null if you don't want to handle it.
124-
DialogInterface.OnClickListener cancelButtonListener = new DialogInterface.OnClickListener() {
125-
@Override
126-
public void onClick(DialogInterface dialog, int which) {
127-
// Let's show a toast
128-
Toast.makeText(getContext(), R.string.settings_dialog_canceled, Toast.LENGTH_SHORT)
129-
.show();
130-
}
131-
};
132-
133-
// (Optional) Check whether the user denied permissions and checked NEVER ASK AGAIN.
134-
// This will display a dialog directing them to enable the permission in app settings.
135-
EasyPermissions.checkDeniedPermissionsNeverAskAgain(
136-
this,
137-
getString(R.string.rationale_ask_again),
138-
R.string.setting,
139-
R.string.cancel,
140-
cancelButtonListener,
141-
perms);
119+
@Override
120+
public void onPermissionsDenied(int requestCode, List<String> perms) {
121+
Log.d(TAG, "onPermissionsDenied:" + requestCode + ":" + perms.size());
122+
123+
// (Optional) Check whether the user denied any permissions and checked "NEVER ASK AGAIN."
124+
// This will display a dialog directing them to enable the permission in app settings.
125+
if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {
126+
new AppSettingsDialog.Builder(this, getString(R.string.rationale_ask_again))
127+
.setTitle(getString(R.string.title_settings_dialog))
128+
.setPositiveButton(getString(R.string.setting))
129+
.setNegativeButton(getString(R.string.cancel), null /* click listener */)
130+
.setRequestCode(RC_SETTINGS_SCREEN)
131+
.build()
132+
.show();
142133
}
134+
}
143135

144-
@Override
145-
public void onActivityResult(int requestCode, int resultCode, Intent data) {
146-
super.onActivityResult(requestCode, resultCode, data);
147-
148-
// Do something after user returned from app settings screen. User may be
149-
// changed/updated the permissions. Let's check whether the user has some permissions or not
150-
// after returned from settings screen
151-
if (requestCode == EasyPermissions.SETTINGS_REQ_CODE) {
152-
boolean hasSomePermissions = EasyPermissions.hasPermissions(
153-
getContext(), somePermissions
154-
);
155-
156-
// Do something with the updated permissions
157-
}
136+
@Override
137+
public void onActivityResult(int requestCode, int resultCode, Intent data) {
138+
super.onActivityResult(requestCode, resultCode, data);
139+
140+
if (requestCode == RC_SETTINGS_SCREEN) {
141+
// Do something after user returned from app settings screen, like showing a Toast.
142+
Toast.makeText(this, R.string.returned_from_app_settings_to_activity, Toast.LENGTH_SHORT)
143+
.show();
158144
}
145+
}
159146
```

app/src/main/java/pub/devrel/easypermissions/sample/MainActivity.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.List;
2828

2929
import pub.devrel.easypermissions.AfterPermissionGranted;
30+
import pub.devrel.easypermissions.AppSettingsDialog;
3031
import pub.devrel.easypermissions.EasyPermissions;
3132

3233
public class MainActivity extends AppCompatActivity implements
@@ -36,6 +37,7 @@ public class MainActivity extends AppCompatActivity implements
3637

3738
private static final int RC_CAMERA_PERM = 123;
3839
private static final int RC_LOCATION_CONTACTS_PERM = 124;
40+
private static final int RC_SETTINGS_SCREEN = 125;
3941

4042
@Override
4143
protected void onCreate(Bundle savedInstanceState) {
@@ -63,9 +65,8 @@ public void onClick(View v) {
6365
public void onActivityResult(int requestCode, int resultCode, Intent data) {
6466
super.onActivityResult(requestCode, resultCode, data);
6567

66-
if (requestCode == EasyPermissions.SETTINGS_REQ_CODE) {
67-
// Do something after user returned from app settings screen
68-
// Let's show Toast for example
68+
if (requestCode == RC_SETTINGS_SCREEN) {
69+
// Do something after user returned from app settings screen, like showing a Toast.
6970
Toast.makeText(this, R.string.returned_from_app_settings_to_activity, Toast.LENGTH_SHORT)
7071
.show();
7172
}
@@ -113,10 +114,16 @@ public void onPermissionsGranted(int requestCode, List<String> perms) {
113114
public void onPermissionsDenied(int requestCode, List<String> perms) {
114115
Log.d(TAG, "onPermissionsDenied:" + requestCode + ":" + perms.size());
115116

116-
// (Optional) Check whether the user denied permissions and checked NEVER ASK AGAIN.
117+
// (Optional) Check whether the user denied any permissions and checked "NEVER ASK AGAIN."
117118
// This will display a dialog directing them to enable the permission in app settings.
118-
EasyPermissions.checkDeniedPermissionsNeverAskAgain(this,
119-
getString(R.string.rationale_ask_again),
120-
R.string.setting, R.string.cancel, null, perms);
119+
if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {
120+
new AppSettingsDialog.Builder(this, getString(R.string.rationale_ask_again))
121+
.setTitle(getString(R.string.title_settings_dialog))
122+
.setPositiveButton(getString(R.string.setting))
123+
.setNegativeButton(getString(R.string.cancel), null /* click listener */)
124+
.setRequestCode(RC_SETTINGS_SCREEN)
125+
.build()
126+
.show();
127+
}
121128
}
122129
}

app/src/main/java/pub/devrel/easypermissions/sample/MainFragment.java

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package pub.devrel.easypermissions.sample;
22

33
import android.Manifest;
4-
import android.content.DialogInterface;
5-
import android.content.Intent;
64
import android.os.Bundle;
75
import android.support.annotation.NonNull;
86
import android.support.v4.app.Fragment;
@@ -17,11 +15,11 @@
1715
import pub.devrel.easypermissions.AfterPermissionGranted;
1816
import pub.devrel.easypermissions.EasyPermissions;
1917

20-
public class MainFragment extends Fragment implements
21-
EasyPermissions.PermissionCallbacks {
18+
public class MainFragment extends Fragment implements EasyPermissions.PermissionCallbacks {
2219

2320
private static final String TAG = "MainFragment";
2421
private static final int RC_SMS_PERM = 122;
22+
private static final int RC_SETTINGS = 123;
2523

2624
@Override
2725
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
@@ -41,23 +39,6 @@ public void onClick(View v) {
4139
return v;
4240
}
4341

44-
@Override
45-
public void onActivityResult(int requestCode, int resultCode, Intent data) {
46-
super.onActivityResult(requestCode, resultCode, data);
47-
48-
// Do something after user returned from app settings screen. User may be
49-
// changed/updated the permissions. Let's check whether the user has changed sms
50-
// permission or not after returned from settings screen
51-
if (requestCode == EasyPermissions.SETTINGS_REQ_CODE) {
52-
boolean hasReadSmsPermission = EasyPermissions.hasPermissions(getContext(),
53-
Manifest.permission.READ_SMS);
54-
String hasReadSmsPermissionText = getString(R.string.has_read_sms_permission,
55-
hasReadSmsPermission);
56-
57-
Toast.makeText(getContext(), hasReadSmsPermissionText, Toast.LENGTH_SHORT).show();
58-
}
59-
}
60-
6142
@Override
6243
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
6344
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
@@ -86,20 +67,5 @@ public void onPermissionsGranted(int requestCode, List<String> perms) {
8667
@Override
8768
public void onPermissionsDenied(int requestCode, List<String> perms) {
8869
Log.d(TAG, "onPermissionsDenied:" + requestCode + ":" + perms.size());
89-
90-
// Handle negative button on click listener
91-
DialogInterface.OnClickListener onClickListener = new DialogInterface.OnClickListener() {
92-
@Override
93-
public void onClick(DialogInterface dialog, int which) {
94-
// Let's show a toast
95-
Toast.makeText(getContext(), R.string.settings_dialog_canceled, Toast.LENGTH_SHORT).show();
96-
}
97-
};
98-
99-
// (Optional) Check whether the user denied permissions and checked NEVER ASK AGAIN.
100-
// This will display a dialog directing them to enable the permission in app settings.
101-
EasyPermissions.checkDeniedPermissionsNeverAskAgain(this,
102-
getString(R.string.rationale_ask_again),
103-
R.string.setting, R.string.cancel, onClickListener, perms);
10470
}
10571
}

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<string name="rationale_location_contacts">This app needs access to your location and contacts to know where and who you are.</string>
55
<string name="rationale_sms">This app needs access to your sms to read all your great messages.</string>
66
<string name="rationale_ask_again">This app may not work correctly without the requested permissions. Open the app settings screen to modify app permissions.</string>
7+
<string name="title_settings_dialog">Permissions Required</string>
78
<string name="setting">Settings</string>
89
<string name="cancel">Cancel</string>
910
<string name="settings_dialog_canceled">Settings dialog canceled</string>

easypermissions/constants.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ ext {
66

77
mavenGroup = 'pub.devrel'
88
mavenArtifactId = 'easypermissions'
9-
mavenVersion = '0.1.9'
9+
mavenVersion = '0.2.0'
1010

1111
bintrayOrg = 'easygoogle'
1212
}

0 commit comments

Comments
 (0)