Advertisement
Guest User

Untitled

a guest
Jul 26th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. package com.cloudit.dashmatic.util.manager;
  2.  
  3. import android.Manifest;
  4. import android.app.Activity;
  5. import android.content.pm.PackageManager;
  6. import android.support.v4.app.ActivityCompat;
  7. import android.support.v4.content.ContextCompat;
  8.  
  9. import com.cloudit.dashmatic.exception.BluetoothException;
  10.  
  11. /**
  12. * Created by tuule on 26.07.16.
  13. */
  14. public class PermissionManager {
  15. private static PermissionManager ourInstance = new PermissionManager();
  16. private Activity activity;
  17.  
  18. private static final int BLUETOOTH_PERMISSION = 141234;
  19. private OnPermissionCallback bluetoothCallback;
  20.  
  21. public static PermissionManager getInstance() {
  22. return ourInstance;
  23. }
  24.  
  25. private PermissionManager() {
  26. }
  27.  
  28. public void init(Activity activity) {
  29. this.activity = activity;
  30. }
  31.  
  32. public void processWithBluetoothPermissionRequest(OnPermissionCallback onPermissionCallback) {
  33. this.bluetoothCallback = onPermissionCallback;
  34. if (ContextCompat.checkSelfPermission(activity,
  35. Manifest.permission.ACCESS_FINE_LOCATION)
  36. != PackageManager.PERMISSION_GRANTED) {
  37. ActivityCompat.requestPermissions(activity,
  38. new String[]{Manifest.permission.READ_CONTACTS},
  39. BLUETOOTH_PERMISSION);
  40. } else {
  41. if (onPermissionCallback != null) {
  42. onPermissionCallback.onPermissionGranted();
  43. }
  44. }
  45. }
  46.  
  47. public void onRequestPermissionsResult(int requestCode,
  48. String permissions[], int[] grantResults) {
  49. switch (requestCode) {
  50. case BLUETOOTH_PERMISSION: {
  51. // If request is cancelled, the result arrays are empty.
  52. if (grantResults.length > 0
  53. && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  54.  
  55. // permission was granted, yay! Do the
  56. // contacts-related task you need to do.
  57. if (bluetoothCallback != null) {
  58. bluetoothCallback.onPermissionGranted();
  59. }
  60.  
  61. } else {
  62.  
  63. if (bluetoothCallback != null) {
  64. bluetoothCallback.onPermissionDenied();
  65. }
  66. // permission denied, boo! Disable the
  67. // functionality that depends on this permission.
  68. }
  69. return;
  70. }
  71.  
  72. // other 'case' lines to check for other
  73. // permissions this app might request
  74. }
  75. }
  76.  
  77.  
  78.  
  79. public interface OnPermissionCallback {
  80. void onPermissionGranted() throws BluetoothException;
  81. void onPermissionDenied() throws BluetoothException;
  82. }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement