Guest User

Untitled

a guest
May 27th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.78 KB | None | 0 0
  1. public boolean checkLocationPermission() {
  2. if (ContextCompat.checkSelfPermission(this,
  3. Manifest.permission.ACCESS_FINE_LOCATION)
  4. != PackageManager.PERMISSION_GRANTED) {
  5.  
  6. // Should we show an explanation?
  7. if (ActivityCompat.shouldShowRequestPermissionRationale(this,
  8. Manifest.permission.ACCESS_FINE_LOCATION)) {
  9.  
  10. // Show an explanation to the user *asynchronously* -- don't block
  11. // this thread waiting for the user's response! After the user
  12. // sees the explanation, try again to request the permission.
  13. new AlertDialog.Builder(this)
  14. .setTitle(R.string.title_location_permission)
  15. .setMessage(R.string.text_location_permission)
  16. .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
  17. @Override
  18. public void onClick(DialogInterface dialogInterface, int i) {
  19. //Prompt the user once explanation has been shown
  20. ActivityCompat.requestPermissions(SalesPersonActivity.this,
  21. new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
  22. MY_PERMISSIONS_REQUEST_LOCATION);
  23. }
  24. })
  25. .create()
  26. .show();
  27.  
  28.  
  29. } else {
  30. // No explanation needed, we can request the permission.
  31. ActivityCompat.requestPermissions(this,
  32. new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
  33. MY_PERMISSIONS_REQUEST_LOCATION);
  34. }
  35. return false;
  36. } else {
  37. return true;
  38. }
  39. }
  40.  
  41. @Override
  42. public void onRequestPermissionsResult(int requestCode,
  43. String permissions[], int[] grantResults) {
  44. switch (requestCode) {
  45. case MY_PERMISSIONS_REQUEST_LOCATION: {
  46. // If request is cancelled, the result arrays are empty.
  47. if (grantResults.length > 0
  48. && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  49.  
  50. // permission was granted, yay! Do the
  51. // location-related task you need to do.
  52. if (ContextCompat.checkSelfPermission(this,
  53. Manifest.permission.ACCESS_FINE_LOCATION)
  54. == PackageManager.PERMISSION_GRANTED) {
  55.  
  56. //Request location updates:
  57. getLocation();
  58. }
  59.  
  60. } else {
  61.  
  62. // permission denied, boo! Disable the
  63. // functionality that depends on this permission.
  64. checkLocationPermission();
  65. }
  66. return;
  67. }
  68.  
  69. }
  70.  
  71. String[] permissions = {Manifest.permission.ACCESS_FINE_LOCATION};
  72.  
  73. getBulkPermissions(permissions, new RequestPermissionAction() {
  74. @Override
  75. public void permissionDenied() {
  76. // TODO: 5/27/2018 handle permission deny
  77. }
  78.  
  79. @Override
  80. public void permissionGranted() {
  81. // TODO: 5/27/2018 you code do further operations
  82. }
  83. });
  84.  
  85. RequestPermissionAction onPermissionCallBack;
  86. private final static int REQUEST_BULK_PERMISSION = 3006;
  87.  
  88. private boolean checkBulkPermissions(String[] permissions) {
  89. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  90. for (String permission : permissions) {
  91. if (checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
  92. return false;
  93. }
  94. }
  95. return true;
  96. } else {
  97. return true;
  98. }
  99. }
  100.  
  101. public void getBulkPermissions(String[] permissions, RequestPermissionAction onPermissionCallBack) {
  102. this.onPermissionCallBack = onPermissionCallBack;
  103. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  104. if (!checkBulkPermissions(permissions)) {
  105. requestPermissions(permissions, REQUEST_BULK_PERMISSION);
  106. return;
  107. }
  108. }
  109. if (onPermissionCallBack != null)
  110. onPermissionCallBack.permissionGranted();
  111. }
  112.  
  113. @Override
  114. public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
  115. super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  116. if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  117. if (onPermissionCallBack != null)
  118. onPermissionCallBack.permissionGranted();
  119. } else if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_DENIED) {
  120. if (onPermissionCallBack != null)
  121. onPermissionCallBack.permissionDenied();
  122. }
  123. }
  124.  
  125. public interface RequestPermissionAction {
  126. void permissionDenied();
  127. void permissionGranted();
  128. }
Add Comment
Please, Sign In to add comment