Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.

public final class Permissions

  1. Object
  2. Permissions

Checks and requests runtime permissions in one place.

This class declares nothing. A permission reaches the Android manifest or the iOS Info.plist only because the build detected a feature class that needs it (the camera, location, contacts and so on). Asking for a permission no feature declared answers DENIED, and the log names the reason.

#request(String, String) blocks until the user answers, so call it from the EDT (it uses invokeAndBlock where needed) or from a background thread.

if (Permissions.check(Permissions.CAMERA) != Permissions.GRANTED) {
    int status = Permissions.request(Permissions.CAMERA, "To scan receipts");
    Log.p("Camera permission: " + status);
}

Fields

public static final String CAMERA = "camera"The camera.
public static final String MICROPHONE = "microphone"The microphone.
public static final String LOCATION = "location"Location while the application is in use.
public static final String LOCATION_ALWAYS = "locationAlways"Location in the background.
public static final String CONTACTS = "contacts"The address book.
public static final String CALENDAR = "calendar"The calendar.
public static final String PHOTOS = "photos"The photo library.
public static final String NOTIFICATIONS = "notifications"Posting notifications.
public static final String BLUETOOTH = "bluetooth"Bluetooth devices.
public static final int UNKNOWN = 0The platform cannot tell, or has no such permission.
public static final int NOT_DETERMINED = 1The user has not been asked yet.
public static final int GRANTED = 2The permission is granted.
public static final int DENIED = 3The permission is denied, or was never declared.
public static final int DENIED_PERMANENTLY = 4The user denied it and the platform no longer shows the prompt; only the system settings can grant it now.
public static final int LIMITED = 5Granted with restrictions, such as limited photo access or approximate location.

Methods

public static void setImplementation(CodenameOneImplementation impl)Invoked internally from Display, this method is for internal use only
public static int check(String permission)The current status of a permission.
public static int request(String permission, String reason)Asks the user for a permission when it is not granted yet, and blocks until they answer.
public static boolean isGranted(String permission)True when the permission is granted, fully or limited.

Inherited methods

Field details

CAMERA

public static final String CAMERA = "camera"
The camera.

MICROPHONE

public static final String MICROPHONE = "microphone"
The microphone.

LOCATION

public static final String LOCATION = "location"
Location while the application is in use.

LOCATION_ALWAYS

public static final String LOCATION_ALWAYS = "locationAlways"
Location in the background.

CONTACTS

public static final String CONTACTS = "contacts"
The address book.

CALENDAR

public static final String CALENDAR = "calendar"
The calendar.

PHOTOS

public static final String PHOTOS = "photos"
The photo library.

NOTIFICATIONS

public static final String NOTIFICATIONS = "notifications"
Posting notifications.

BLUETOOTH

public static final String BLUETOOTH = "bluetooth"
Bluetooth devices.

UNKNOWN

public static final int UNKNOWN = 0
The platform cannot tell, or has no such permission.

NOT_DETERMINED

public static final int NOT_DETERMINED = 1
The user has not been asked yet.

GRANTED

public static final int GRANTED = 2
The permission is granted.

DENIED

public static final int DENIED = 3
The permission is denied, or was never declared.

DENIED_PERMANENTLY

public static final int DENIED_PERMANENTLY = 4
The user denied it and the platform no longer shows the prompt; only the system settings can grant it now.

LIMITED

public static final int LIMITED = 5
Granted with restrictions, such as limited photo access or approximate location.

Method details

setImplementation

public static void setImplementation(CodenameOneImplementation impl)
Invoked internally from Display, this method is for internal use only

Parameters

impl CodenameOneImplementation
implementation instance

check

public static int check(String permission)

The current status of a permission. Never prompts.

Bluetooth is answered by com.codename1.bluetooth.Bluetooth#hasPermission(BluetoothPermission), and the platform is asked only to tell a refusal from a question not asked yet. Contacts are not answered by Display#isContactsPermissionGranted() here: on iOS that call prompts.

Parameters

permission String
one of the permission names of this class

Returns

one of the status constants

request

public static int request(String permission, String reason)

Asks the user for a permission when it is not granted yet, and blocks until they answer.

Contacts go through Display#isContactsPermissionGranted(), notifications through Display#requestNotificationPermission(NotificationPermissionCallback) and Bluetooth through com.codename1.bluetooth.Bluetooth#requestPermissions(BluetoothPermission...) (scan and connect), so the prompt is the one those APIs show.

Parameters

permission String
one of the permission names of this class
reason String
the explanation shown where the platform asks for one

Returns

the status after the request

isGranted

public static boolean isGranted(String permission)
True when the permission is granted, fully or limited.

Parameters

permission String
one of the permission names of this class

Returns

true when granted