PermissionUtil.java 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package com.owncloud.android.utils;
  2. import android.Manifest;
  3. import android.app.Activity;
  4. import android.content.Context;
  5. import android.support.v4.app.ActivityCompat;
  6. import android.support.v4.content.ContextCompat;
  7. /**
  8. * Created by scherzia on 29.12.2015.
  9. */
  10. public class PermissionUtil {
  11. public static final int PERMISSIONS_WRITE_EXTERNAL_STORAGE = 1;
  12. public static final int PERMISSIONS_READ_CONTACTS_AUTOMATIC = 2;
  13. public static final int PERMISSIONS_READ_CONTACTS_MANUALLY = 3;
  14. public static final int PERMISSIONS_WRITE_CONTACTS = 4;
  15. /**
  16. * Wrapper method for ContextCompat.checkSelfPermission().
  17. * Determine whether <em>the app</em> has been granted a particular permission.
  18. *
  19. * @param permission The name of the permission being checked.
  20. * @return <code>true</code> if app has the permission, or <code>false</code> if not.
  21. */
  22. public static boolean checkSelfPermission(Context context, String permission) {
  23. return ContextCompat.checkSelfPermission(context, permission)
  24. == android.content.pm.PackageManager.PERMISSION_GRANTED;
  25. }
  26. /**
  27. * Wrapper method for ActivityCompat.shouldShowRequestPermissionRationale().
  28. * Gets whether you should show UI with rationale for requesting a permission.
  29. * You should do this only if you do not have the permission and the context in
  30. * which the permission is requested does not clearly communicate to the user
  31. * what would be the benefit from granting this permission.
  32. *
  33. * @param activity The target activity.
  34. * @param permission A permission to be requested.
  35. * @return Whether to show permission rationale UI.
  36. */
  37. public static boolean shouldShowRequestPermissionRationale(Activity activity, String permission) {
  38. return ActivityCompat.shouldShowRequestPermissionRationale(activity, permission);
  39. }
  40. /**
  41. * request the write permission for external storage.
  42. *
  43. * @param activity The target activity.
  44. */
  45. public static void requestWriteExternalStoreagePermission(Activity activity) {
  46. ActivityCompat.requestPermissions(activity,
  47. new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
  48. PERMISSIONS_WRITE_EXTERNAL_STORAGE);
  49. }
  50. }