Skip to content

Commit 3c7900f

Browse files
committed
Added convenience method to redirect a user to the app's Settings to PermissionUtils
1 parent 247df56 commit 3c7900f

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

library/src/main/android/permissions/dispatcher/PermissionUtils.java

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,16 @@
22

33
import android.app.Activity;
44
import android.content.Context;
5+
import android.content.Intent;
56
import android.content.pm.PackageManager;
7+
import android.net.Uri;
8+
import android.os.Build;
9+
import android.provider.Settings;
10+
import android.support.annotation.Nullable;
611
import android.support.v4.app.ActivityCompat;
712

13+
import static android.os.Build.VERSION_CODES.FROYO;
14+
import static android.os.Build.VERSION_CODES.GINGERBREAD;
815
import static android.support.v4.content.PermissionChecker.checkSelfPermission;
916

1017
public final class PermissionUtils {
@@ -30,7 +37,7 @@ public static boolean verifyPermissions(int... grantResults) {
3037
/**
3138
* Returns true if the Activity or Fragment has access to all given permissions.
3239
*
33-
* @param context context
40+
* @param context context
3441
* @param permissions permission list
3542
* @return returns true if the Activity or Fragment has access to all given permissions.
3643
*/
@@ -58,4 +65,33 @@ public static boolean shouldShowRequestPermissionRationale(Activity activity, St
5865
}
5966
return false;
6067
}
68+
69+
/**
70+
* Creates an {@link Intent} that redirects the user to the app's settings page.
71+
*
72+
* @param context Context used to access the app's package name
73+
* @return An intent that, when used to {@link Activity#startActivity(Intent)}, will open the running app's Settings page.
74+
* If no matching Activity could be found for the device's settings, this method returns null.
75+
*/
76+
public static @Nullable Intent createAppSettingsIntent(Context context) {
77+
Intent intent;
78+
String packageName = context.getPackageName();
79+
if (Build.VERSION.SDK_INT >= GINGERBREAD) {
80+
// Utilize the dedicated Settings Action on API 9+
81+
intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
82+
Uri uri = Uri.fromParts("package", packageName, null);
83+
intent.setData(uri);
84+
85+
} else {
86+
// Compatibility implementation using a View intent and the native Settings app
87+
intent = new Intent(Intent.ACTION_VIEW);
88+
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
89+
intent.setClassName("com.android.settings", "com.android.settings.InstalledAppDetails");
90+
String extraKey = Build.VERSION.SDK_INT == FROYO ? "pkg" : "com.android.settings.ApplicationPkgName";
91+
intent.putExtra(extraKey, packageName);
92+
}
93+
94+
// If the intent can be used to resolve an Activity, return that intent. Otherwise, return null
95+
return context.getPackageManager().resolveActivity(intent, 0) != null ? intent : null;
96+
}
6197
}

0 commit comments

Comments
 (0)