Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.cloudit.dashmatic.util.manager;
- import android.Manifest;
- import android.app.Activity;
- import android.content.pm.PackageManager;
- import android.support.v4.app.ActivityCompat;
- import android.support.v4.content.ContextCompat;
- import com.cloudit.dashmatic.exception.BluetoothException;
- /**
- * Created by tuule on 26.07.16.
- */
- public class PermissionManager {
- private static PermissionManager ourInstance = new PermissionManager();
- private Activity activity;
- private static final int BLUETOOTH_PERMISSION = 141234;
- private OnPermissionCallback bluetoothCallback;
- public static PermissionManager getInstance() {
- return ourInstance;
- }
- private PermissionManager() {
- }
- public void init(Activity activity) {
- this.activity = activity;
- }
- public void processWithBluetoothPermissionRequest(OnPermissionCallback onPermissionCallback) {
- this.bluetoothCallback = onPermissionCallback;
- if (ContextCompat.checkSelfPermission(activity,
- Manifest.permission.ACCESS_FINE_LOCATION)
- != PackageManager.PERMISSION_GRANTED) {
- ActivityCompat.requestPermissions(activity,
- new String[]{Manifest.permission.READ_CONTACTS},
- BLUETOOTH_PERMISSION);
- } else {
- if (onPermissionCallback != null) {
- onPermissionCallback.onPermissionGranted();
- }
- }
- }
- public void onRequestPermissionsResult(int requestCode,
- String permissions[], int[] grantResults) {
- switch (requestCode) {
- case BLUETOOTH_PERMISSION: {
- // If request is cancelled, the result arrays are empty.
- if (grantResults.length > 0
- && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
- // permission was granted, yay! Do the
- // contacts-related task you need to do.
- if (bluetoothCallback != null) {
- bluetoothCallback.onPermissionGranted();
- }
- } else {
- if (bluetoothCallback != null) {
- bluetoothCallback.onPermissionDenied();
- }
- // permission denied, boo! Disable the
- // functionality that depends on this permission.
- }
- return;
- }
- // other 'case' lines to check for other
- // permissions this app might request
- }
- }
- public interface OnPermissionCallback {
- void onPermissionGranted() throws BluetoothException;
- void onPermissionDenied() throws BluetoothException;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement