PermissionHelper.java
Go to the documentation of this file.
1 /*
2  * Copyright 2018 Google Inc. All Rights Reserved.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 package com.introlab.rtabmap;
16 
17 import android.Manifest;
18 
19 import android.app.Activity;
20 import android.content.Intent;
21 import android.content.pm.PackageManager;
22 import android.net.Uri;
23 import android.provider.Settings;
24 import android.support.v4.app.ActivityCompat;
25 import android.support.v4.content.ContextCompat;
26 
28 public class PermissionHelper {
29  public static final int CAMERA_CODE = 0;
30  public static final int READ_EXTERNAL_STORAGE_CODE = 1;
31  public static final int WRITE_EXTERNAL_STORAGE_CODE = 2;
32  public static final int INTERNET_CODE = 3;
33  public static final int ACCESS_NETWORK_STATE_CODE = 4;
34  public static final int ACCESS_FINE_LOCATION_CODE = 5;
35  public static final int ACCESS_WIFI_STATE_CODE = 6;
36 
38  public static boolean hasPermission(Activity activity, String permission) {
39  return ContextCompat.checkSelfPermission(activity, permission) == PackageManager.PERMISSION_GRANTED;
40  }
41 
43  public static void requestPermission(Activity activity, String permission) {
44  int requestCode = -1;
45  if (permission == Manifest.permission.CAMERA) {
46  requestCode = CAMERA_CODE;
47  } else if(permission == Manifest.permission.READ_EXTERNAL_STORAGE) {
48  requestCode = READ_EXTERNAL_STORAGE_CODE;
49  } else if(permission == Manifest.permission.WRITE_EXTERNAL_STORAGE) {
50  requestCode = WRITE_EXTERNAL_STORAGE_CODE;
51  } else if(permission == Manifest.permission.INTERNET) {
52  requestCode = INTERNET_CODE;
53  } else if(permission == Manifest.permission.ACCESS_NETWORK_STATE) {
54  requestCode = ACCESS_NETWORK_STATE_CODE;
55  } else if(permission == Manifest.permission.ACCESS_FINE_LOCATION) {
56  requestCode = ACCESS_FINE_LOCATION_CODE;
57  } else if(permission == Manifest.permission.ACCESS_WIFI_STATE) {
58  requestCode = ACCESS_WIFI_STATE_CODE;
59  }
60  if(requestCode >=0)
61  {
62  ActivityCompat.requestPermissions(
63  activity, new String[] {permission}, requestCode);
64  }
65  }
66 
68  public static boolean shouldShowRequestPermissionRationale(Activity activity, String permission) {
69  return ActivityCompat.shouldShowRequestPermissionRationale(activity, permission);
70  }
71 
73  public static void launchPermissionSettings(Activity activity) {
74  Intent intent = new Intent();
75  intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
76  intent.setData(Uri.fromParts("package", activity.getPackageName(), null));
77  activity.startActivity(intent);
78  }
79 }
static boolean hasPermission(Activity activity, String permission)
static void requestPermission(Activity activity, String permission)
static boolean shouldShowRequestPermissionRationale(Activity activity, String permission)
static void launchPermissionSettings(Activity activity)


rtabmap
Author(s): Mathieu Labbe
autogenerated on Mon Dec 14 2020 03:34:59