UNPKG

cordova.plugins.diagnostic

Version:

Cordova/Phonegap plugin to check the state of Location/WiFi/Camera/Bluetooth device settings.

997 lines (734 loc) 188 kB
Cordova diagnostic plugin [![Latest Stable Version](https://img.shields.io/npm/v/cordova.plugins.diagnostic.svg)](https://www.npmjs.com/package/cordova.plugins.diagnostic) [![Total Downloads](https://img.shields.io/npm/dt/cordova.plugins.diagnostic.svg)](https://npm-stat.com/charts.html?package=cordova.plugins.diagnostic) ========================= <!-- doctoc README.md --maxlevel=3 --> <!-- START doctoc generated TOC please keep comment here to allow auto update --> <!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE --> - [Overview](#overview) - [Important notes](#important-notes) - [Minimum supported versions](#minimum-supported-versions) - [Native environment required](#native-environment-required) - [Building for Android](#building-for-android) - [Installation](#installation) - [Using the Cordova/Phonegap/Ionic CLI](#using-the-cordovaphonegapionic-cli) - [AndroidX Library](#androidx-library) - [Specifying modules](#specifying-modules) - [Available modules](#available-modules) - [Reporting issues](#reporting-issues) - [Reporting a bug or problem](#reporting-a-bug-or-problem) - [Requesting a new feature](#requesting-a-new-feature) - [Usage](#usage) - [Core module](#core-module) - [switchToSettings()](#switchtosettings) - [switchToWirelessSettings()](#switchtowirelesssettings) - [switchToMobileDataSettings()](#switchtomobiledatasettings) - [permissionStatus constants](#permissionstatus-constants) - [getPermissionAuthorizationStatus()](#getpermissionauthorizationstatus) - [getPermissionsAuthorizationStatus()](#getpermissionsauthorizationstatus) - [requestRuntimePermission()](#requestruntimepermission) - [requestRuntimePermissions()](#requestruntimepermissions) - [isRequestingPermission()](#isrequestingpermission) - [registerPermissionRequestCompleteHandler()](#registerpermissionrequestcompletehandler) - [isDataRoamingEnabled()](#isdataroamingenabled) - [isADBModeEnabled()](#isadbmodeenabled) - [isDeviceRooted()](#isdevicerooted) - [isBackgroundRefreshAuthorized()](#isbackgroundrefreshauthorized) - [getBackgroundRefreshStatus()](#getbackgroundrefreshstatus) - [cpuArchitecture constants](#cpuarchitecture-constants) - [getArchitecture()](#getarchitecture) - [restart()](#restart) - [enableDebug()](#enabledebug) - [getCurrentBatteryLevel()](#getcurrentbatterylevel) - [isAirplaneModeEnabled()](#isairplanemodeenabled) - [isMobileDataEnabled()](#ismobiledataenabled) - [isMobileDataAuthorized()](#ismobiledataauthorized) - [isAccessibilityModeEnabled()](#isaccessibilitymodeenabled) - [isTouchExplorationEnabled()](#istouchexplorationenabled) - [getDeviceOSVersion()](#getdeviceosversion) - [getBuildOSVersion()](#getbuildosversion) - [isDebugBuild()](#isdebugbuild) - [Location module](#location-module) - [locationMode constants](#locationmode-constants) - [locationAuthorizationMode constants](#locationauthorizationmode-constants) - [locationAccuracyAuthorization constants](#locationaccuracyauthorization-constants) - [isLocationAvailable()](#islocationavailable) - [isLocationEnabled()](#islocationenabled) - [isGpsLocationAvailable()](#isgpslocationavailable) - [isGpsLocationEnabled()](#isgpslocationenabled) - [isNetworkLocationAvailable()](#isnetworklocationavailable) - [isNetworkLocationEnabled()](#isnetworklocationenabled) - [getLocationMode()](#getlocationmode) - [isLocationAuthorized()](#islocationauthorized) - [getLocationAuthorizationStatus()](#getlocationauthorizationstatus) - [getLocationAuthorizationStatuses()](#getlocationauthorizationstatuses) - [requestLocationAuthorization()](#requestlocationauthorization) - [registerLocationStateChangeHandler()](#registerlocationstatechangehandler) - [getLocationAccuracyAuthorization()](#getlocationaccuracyauthorization) - [requestTemporaryFullAccuracyAuthorization()](#requesttemporaryfullaccuracyauthorization) - [registerLocationAccuracyAuthorizationChangeHandler()](#registerlocationaccuracyauthorizationchangehandler) - [switchToLocationSettings()](#switchtolocationsettings) - [Bluetooth module](#bluetooth-module) - [bluetoothState constants](#bluetoothstate-constants) - [isBluetoothAvailable()](#isbluetoothavailable) - [isBluetoothEnabled()](#isbluetoothenabled) - [hasBluetoothSupport()](#hasbluetoothsupport) - [hasBluetoothLESupport()](#hasbluetoothlesupport) - [hasBluetoothLEPeripheralSupport()](#hasbluetoothleperipheralsupport) - [getBluetoothState()](#getbluetoothstate) - [setBluetoothState()](#setbluetoothstate) - [getBluetoothAuthorizationStatus()](#getbluetoothauthorizationstatus) - [getBluetoothAuthorizationStatuses()](#getbluetoothauthorizationstatuses) - [requestBluetoothAuthorization()](#requestbluetoothauthorization) - [registerBluetoothStateChangeHandler()](#registerbluetoothstatechangehandler) - [switchToBluetoothSettings()](#switchtobluetoothsettings) - [WiFi module](#wifi-module) - [isWifiAvailable()](#iswifiavailable) - [isWifiEnabled()](#iswifienabled) - [setWifiState()](#setwifistate) - [switchToWifiSettings()](#switchtowifisettings) - [Camera module](#camera-module) - [isCameraPresent()](#iscamerapresent) - [isCameraAvailable()](#iscameraavailable) - [isCameraAuthorized()](#iscameraauthorized) - [getCameraAuthorizationStatus()](#getcameraauthorizationstatus) - [getCameraAuthorizationStatuses()](#getcameraauthorizationstatuses) - [requestCameraAuthorization()](#requestcameraauthorization) - [isCameraRollAuthorized()](#iscamerarollauthorized) - [getCameraRollAuthorizationStatus()](#getcamerarollauthorizationstatus) - [requestCameraRollAuthorization()](#requestcamerarollauthorization) - [presentLimitedLibraryPicker()](#presentlimitedlibrarypicker) - [Notifications module](#notifications-module) - [remoteNotificationType constants](#remotenotificationtype-constants) - [isRemoteNotificationsEnabled()](#isremotenotificationsenabled) - [isRegisteredForRemoteNotifications()](#isregisteredforremotenotifications) - [getRemoteNotificationTypes()](#getremotenotificationtypes) - [getRemoteNotificationsAuthorizationStatus()](#getremotenotificationsauthorizationstatus) - [requestRemoteNotificationsAuthorization()](#requestremotenotificationsauthorization) - [switchToNotificationSettings()](#switchtonotificationsettings) - [Microphone module](#microphone-module) - [isMicrophoneAuthorized()](#ismicrophoneauthorized) - [getMicrophoneAuthorizationStatus()](#getmicrophoneauthorizationstatus) - [requestMicrophoneAuthorization()](#requestmicrophoneauthorization) - [Contacts module](#contacts-module) - [isContactsAuthorized()](#iscontactsauthorized) - [getContactsAuthorizationStatus()](#getcontactsauthorizationstatus) - [requestContactsAuthorization()](#requestcontactsauthorization) - [Calendar module](#calendar-module) - [isCalendarAuthorized()](#iscalendarauthorized) - [getCalendarAuthorizationStatus()](#getcalendarauthorizationstatus) - [requestCalendarAuthorization()](#requestcalendarauthorization) - [Reminders module](#reminders-module) - [isRemindersAuthorized()](#isremindersauthorized) - [getRemindersAuthorizationStatus()](#getremindersauthorizationstatus) - [requestRemindersAuthorization()](#requestremindersauthorization) - [Motion module](#motion-module) - [motionStatus constants](#motionstatus-constants) - [isMotionAvailable()](#ismotionavailable) - [isMotionRequestOutcomeAvailable()](#ismotionrequestoutcomeavailable) - [requestMotionAuthorization()](#requestmotionauthorization) - [getMotionAuthorizationStatus()](#getmotionauthorizationstatus) - [NFC module](#nfc-module) - [NFCState constants](#nfcstate-constants) - [isNFCPresent()](#isnfcpresent) - [isNFCEnabled()](#isnfcenabled) - [isNFCAvailable()](#isnfcavailable) - [registerNFCStateChangeHandler()](#registernfcstatechangehandler) - [switchToNFCSettings()](#switchtonfcsettings) - [External storage module](#external-storage-module) - [isExternalStorageAuthorized()](#isexternalstorageauthorized) - [getExternalStorageAuthorizationStatus()](#getexternalstorageauthorizationstatus) - [requestExternalStorageAuthorization()](#requestexternalstorageauthorization) - [getExternalSdCardDetails()](#getexternalsdcarddetails) - [Platform Notes](#platform-notes) - [Android](#android) - [Android permissions](#android-permissions) - [Android Auto Backup](#android-auto-backup) - [iOS](#ios) - [iOS usage description messages](#ios-usage-description-messages) - [Example project](#example-project) - [Screenshots](#screenshots) - [Android](#android-1) - [iOS](#ios-1) - [Release notes](#release-notes) - [Credits](#credits) - [License](#license) <!-- END doctoc generated TOC please keep comment here to allow auto update --> # Overview This Cordova/Phonegap plugin for iOS & Android is used to manage device settings such as Location, Bluetooth and WiFi. It enables management of run-time permissions, device hardware and core OS features. The plugin is registered in on [npm](https://www.npmjs.com/package/cordova.plugins.diagnostic) as `cordova.plugins.diagnostic` <!-- DONATE --> [![donate](https://www.paypalobjects.com/en_US/i/btn/btn_donateCC_LG_global.gif)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=ZRD3W47HQ3EMJ) I dedicate a considerable amount of my free time to developing and maintaining this Cordova plugin, along with my other Open Source software. To help ensure this plugin is kept updated, new features are added and bugfixes are implemented quickly, please donate a couple of dollars (or a little more if you can stretch) as this will help me to afford to dedicate time to its maintenance. Please consider donating if you're using this plugin in an app that makes you money, if you're being paid to make the app, if you're asking for new features or priority bug fixes. <!-- END DONATE --> ## Important notes ### Minimum supported versions - Cordova CLI: `cordova@9.0.0` - Android platform: `cordova-android@8.0.0` (recommended version `cordova-android@9.0.0`) - Android version: Android 5.0 (API 21) - iOS platform: `cordova-ios@5.0.0` - iOS 10.0 Note: If you need to support older OS versions, please use an older version of this plugin. ### Native environment required Note that this plugin is intended for use in a **native** mobile environment. It will **NOT** work in a browser-emulated Cordova environment, for example by running `cordova serve` or using the [Ripple emulator](https://github.com/ripple-emulator/ripple). ### Building for Android In order to avoid build problems with Android, please make sure you have the latest versions of the following Android SDK components installed: - Android SDK Tools - Android SDK Platform-tools - Android SDK Build-tools - Target SDK Platform - e.g. Android 10.0 (API 29) - Google Repository * Make sure you have a [supported version](#minimum-supported-versions) of the `cordova-android` platform installed. * You can check if the Android platform in your Cordova project is up-to-date using `cordova platform check android` and if it's not, update it using `cordova platform rm android && cordova platform add android@latest`. * Since `cordova.plugins.diagnostic@6` the recommended Cordova Android platform version is `cordova-android@9.0.0` (which includes AndroidX support). * To use this plugin with `cordova-android@8`, install [cordova-plugin-androidx](https://github.com/dpa99c/cordova-plugin-androidx) and [cordova-plugin-androidx-adapter](https://github.com/dpa99c/cordova-plugin-androidx-adapter). * Phonegap Build uses should use the latest available CLI version ([listed here](https://build.phonegap.com/current-support)) by specifying using the `phonegap-version` tag in your `config.xml`. # Installation ## Using the Cordova/Phonegap/Ionic CLI $ cordova plugin add cordova.plugins.diagnostic $ cordova plugin add cordova.plugins.diagnostic --variable ANDROIDX_VERSION=1.0.0 $ phonegap plugin add cordova.plugins.diagnostic $ ionic cordova plugin add cordova.plugins.diagnostic ## AndroidX Library This plugin uses/depends on the [AndroidX (Jetpack) libraries](https://developer.android.com/jetpack/androidx) (these supersede the [Android Support Library](https://developer.android.com/topic/libraries/support-library/index.html) which is no longer used by this plugin since `cordova.plugins.diagnostic@6`). This plugin pins default versions of the legacy and appcompat versions of the library in [its `plugin.xml`](https://github.com/dpa99c/cordova-diagnostic-plugin/blob/master/plugin.xml) however you can override these to specify different versions using the `ANDROIDX_VERSION` (legacy) and `ANDROIDX_APPCOMPAT_VERSION` variables at plugin installation time, for example: $ cordova plugin add cordova.plugins.diagnostic --variable ANDROIDX_VERSION=1.0.0 --variable ANDROIDX_APPCOMPAT_VERSION=1.3.1 ## Specifying modules Since `cordova.plugins.diagnostic@4` the plugin is split into optional functional modules. The reason for this is so you can choose to install only those parts of the plugin you'll use and therefore not install redundant code/components/frameworks. By default, all the modules will be added to your project when you install the plugin. You can specify which modules are installed by adding a `<preference>` to your `config.xml` which specifies the modules you wish to add as a space-separated list. Module names should be capitalised. The preference takes the form: <preference name="cordova.plugins.diagnostic.modules" value="[list of modules]" /> For example, to explicitly include all optional modules: <preference name="cordova.plugins.diagnostic.modules" value="LOCATION BLUETOOTH WIFI CAMERA NOTIFICATIONS MICROPHONE CONTACTS CALENDAR REMINDERS MOTION NFC EXTERNAL_STORAGE" /> To install only the core module and no optional modules, leave the preference value blank: <preference name="cordova.plugins.diagnostic.modules" value="" /> **IMPORTANT:** After adding the preference to your `config.xml`, you'll need to uninstall then re-install the plugin to your project to apply the changes: ``` cordova plugin rm cordova.plugins.diagnostic --nosave && cordova plugin add cordova.plugins.diagnostic --nosave ``` ### Available modules The following optional modules are currently supported by the plugin: - [LOCATION](#location-module) - Android & iOS - [BLUETOOTH](#bluetooth-module) - Android & iOS - [WIFI](#wifi-module) - Android & iOS - [CAMERA](#camera-module) - Android & iOS - [NOTIFICATIONS](#notifications-module) - Android & iOS - [MICROPHONE](#microphone-module) - Android & iOS - [CONTACTS](#contacts-module) - Android & iOS - [CALENDAR](#calendar-module) - Android & iOS - [REMINDERS](#reminders-module) - iOS - [MOTION](#motion-module) - iOS - [NFC](#nfc-module) - Android - [EXTERNAL_STORAGE](#external-storage-module) - Android **IMPORTANT:** It's vital that the preference be added to your `config.xml` **before** you install the plugin, otherwise the preference will not be applied and all modules will be added. This is because, due to limitations of the Cordova CLI hooks, this plugin must use the `npm install` process to apply the module preferences and this runs before the Cordova CLI when installing a plugin. If you change the modules specified in the preference, you'll need to uninstall then re-install the plugin to your project to apply the changes. # Reporting issues **IMPORTANT:** Please read the following carefully. Failure to follow the issue template guidelines below will result in the issue being immediately closed. ## Reporting a bug or problem Before [opening a bug issue](https://github.com/dpa99c/cordova-diagnostic-plugin/issues/new?assignees=&labels=&template=bug_report.md&title=), please do the following: - *DO NOT* open issues asking for support in using/integrating the plugin into your project - Only open issues for suspected bugs/issues with the plugin that are generic and will affect other users - I don't have time to offer free technical support: this is free open-source software - Ask for help on StackOverflow, Ionic Forums, etc. - Use the [example project](https://github.com/dpa99c/cordova-diagnostic-plugin-example) as a known working reference - Any issues requesting support will be closed immediately. - *DO NOT* open issues related to the [Ionic Typescript wrapper for this plugin](https://github.com/ionic-team/ionic-native/blob/master/src/%40ionic-native/plugins/diagnostic/index.ts) - This is owned/maintained by [Ionic](https://github.com/ionic-team) and is not part of this plugin - Please raise such issues/PRs against [Ionic Native](https://github.com/ionic-team/ionic-native/) instead. - To verify an if an issue is caused by this plugin or its Typescript wrapper, please re-test using the vanilla Javascript plugin interface (without the Ionic Native wrapper). - Any issue opened here which is obviously an Ionic Typescript wrapper issue will be closed immediately. - Read the above documentation thoroughly - Check the [CHANGELOG](https://github.com/dpa99c/cordova-diagnostic-plugin/blob/master/CHANGELOG.md) for any breaking changes that may be causing your issue. - Check a similar issue (open or closed) does not already exist against this plugin. - Duplicates or near-duplicates will be closed immediately. - When [creating a new issue](https://github.com/dpa99c/cordova-diagnostic-plugin/issues/new/choose) - Choose the "Bug report" template - Fill out the relevant sections of the template and delete irrelevant sections - *WARNING:* Failure to complete the issue template will result in the issue being closed immediately. - Reproduce the issue using the [example project](https://github.com/dpa99c/cordova-diagnostic-plugin-example) - This will eliminate bugs in your code or conflicts with other code as possible causes of the issue - This will also validate your development environment using a known working codebase - If reproducing the issue using the example project is not possible, create an isolated test project that you are able to share - Include full verbose console output when reporting build issues - If the full console output is too large to insert directly into the Github issue, then post it on an external site such as [Pastebin](https://pastebin.com/) and link to it from the issue - Often the details of an error causing a build failure is hidden away when building with the CLI - To get the full detailed console output, append the `--verbose` flag to CLI build commands - e.g. `cordova build ios --verbose` - Failure to include the full console output will result in the issue being closed immediately - If the issue relates to the plugin documentation (and not the code), please of a [documentation issue](https://github.com/dpa99c/cordova-diagnostic-plugin/issues/new?assignees=&labels=&template=documentation-issue.md&title=) ## Requesting a new feature Before [opening a feature request issue](https://github.com/dpa99c/cordova-diagnostic-plugin/issues/new?assignees=&labels=&template=feature_request.md&title=), please do the following: - Check the above documentation to ensure the feature you are requesting doesn't already exist - Check the list if open/closed issues to check if there's a reason that feature hasn't been included already - Ensure the feature you are requesting is actually possible to implement and generically useful to other users than yourself - Where possible, post a link to the documentation related to the feature you are requesting - Include other relevant links, e.g. - Stack Overflow post illustrating a solution - Code within another Github repo that illustrates a solution # Usage The core plugin module is exposed via the global `cordova.plugins.diagnostic` object and it aliases all functions and properties of the other optional modules. If a function is called on the core module for an optional module which is not installed, a JS error will be raised by the core module. ## Core module Purpose: Generic and miscellaneous functionality. Platforms: Android & iOS Configuration name: N/A - always installed, regardless of whether the module preference key is present in `config.xml`. ### switchToSettings() Platforms: Android and iOS Opens settings page for this app. On Android, this opens the "App Info" page in the Settings app. On iOS, this opens the app settings page in the Settings app. cordova.plugins.diagnostic.switchToSettings(successCallback, errorCallback); #### Parameters - {Function} successCallback - The callback which will be called when switch to settings is successful. - {Function} errorCallback - The callback which will be called when switch to settings encounters an error. The function is passed a single string parameter containing the error message. #### Example usage cordova.plugins.diagnostic.switchToSettings(function(){ console.log("Successfully switched to Settings app"); }, function(error){ console.error("The following error occurred: "+error); }); ### switchToWirelessSettings() Platforms: Android Switches to the wireless settings page in the Settings app. Allows configuration of wireless controls such as Wi-Fi, Bluetooth and Mobile networks. cordova.plugins.diagnostic.switchToWirelessSettings(); ### switchToMobileDataSettings() Platforms: Android Displays mobile settings to allow user to enable mobile data. cordova.plugins.diagnostic.switchToMobileDataSettings(); ### permissionStatus constants Platforms: Android and iOS Both Android and iOS define constants for requesting and reporting the various permission states. cordova.plugins.diagnostic.permissionStatus #### Android The following permission states are defined for Android: - `NOT_REQUESTED` - App has not yet requested access to this permission. App can request permission and user will be prompted to allow/deny. - `DENIED_ONCE` - User denied access to this permission (without checking "Never Ask Again" box). App can request permission again and user will be prompted again to allow/deny again. - `DENIED_ALWAYS` - User denied access to this permission and checked "Never Ask Again" box. App can never ask for permission again. The only way around this is to instruct the user to manually change the permission on the app permissions page in Settings. - `GRANTED` - User granted access to this permission or the device is running Android 5.x or below. ⚠ Since it's impossible to distinguish between NOT_REQUESTED and DENIED_ALWAYS using the native Android runtime permissions API (they both return the same constant value), this plugin attempts to distinguish the difference by using HTML5 local storage to keep track of which permissions have been requested since the app was first installed. On requesting a permission for the first time, an entry is put into local storage against the permission name. If the user then selects DENY_ALWAYS, the plugin uses the flag in local storage to distinguish this from NOT_REQUESTED. Some things to watch out for: - Clearing local storage will result in this data being lost and will result in NOT_REQUESTED being returned even if the user previously chose to always deny permission. - If the relevant `<uses-permission>` tag is missing from the Android manifest, then the native API will return the NOT_REQUESTED/DENIED_ALWAYS constant value. Since the plugin is unable to make the native permissions request in order to show the native dialog, the plugin will always return NOT_REQUESTED. If [Android Autobackup](https://developer.android.com/guide/topics/data/backup.html#Choosing) is enabled (which it is by default ), Android does not backup app permissions after uninstall but does backup HTML5 local storage. This may lead to a permission being reported by the plugin as DENIED_ALWAYS when the actual status is NOT_REQUESTED. To avoid this you may want to disable Android Autobackup. You can do this using the [cordova-custom-config plugin](https://github.com/dpa99c/cordova-custom-config), for example: ``` <platform name="android"> <plugin name="cordova-custom-config" version="*"/> <custom-preference name="android-manifest/application/@android:allowBackup" value="false" /> <custom-preference name="android-manifest/application/@android:fullBackupContent" value="false" /> </platform> ``` #### iOS The following permission states are defined for iOS: - `NOT_REQUESTED` - App has not yet requested access to this permission. App can request permission and user will be prompted to allow/deny. - `DENIED_ALWAYS` - User denied access to this permission. App can never ask for permission again. The only way around this is to instruct the user to manually change the permission in Settings. - `RESTRICTED` - Permission is unavailable and user cannot enable it. For example, when parental controls are in effect for the current user. - `GRANTED` - User granted access to this permission. For location permission, this indicates the user has granted access to the permission "always" (when app is both in foreground and background). - `GRANTED_WHEN_IN_USE` - Used only for location permission. Indicates the user has granted access to the permission "when in use" (only when the app is in the foreground). Addtionally, for notifications permissions: - `PROVISIONAL` - The app is provisionally authorized to post non-interruptive user notifications. - `EPHEMERAL` - The app is authorized to schedule or receive notifications for a limited amount of time. #### Example if(somePermissionStatus === cordova.plugins.diagnostic.permissionStatus.GRANTED){ // Do something } ### getPermissionAuthorizationStatus() Platforms: Android Returns the current authorisation status for a given permission. Note: this is intended for Android 6 / API 23 and above. Calling on Android 5.1 / API 22 and below will always return GRANTED status as permissions are already granted at installation time. #### Parameters - {Function} successCallback - function to call on successful retrieval of status. The function is passed a single string parameter which defines the current [permission status](#permissionstatus-constants) - {Function} errorCallback - function to call on failure to retrieve authorisation status. The function is passed a single string parameter containing the error message. - {String} permission - permission to request authorisation status for, defined as a [runtime permission constant](#dangerous-runtime-permissions). #### Example usage cordova.plugins.diagnostic.getPermissionAuthorizationStatus(function(status){ switch(status){ case cordova.plugins.diagnostic.permissionStatus.GRANTED: console.log("Permission granted to use the camera"); break; case cordova.plugins.diagnostic.permissionStatus.NOT_REQUESTED: console.log("Permission to use the camera has not been requested yet"); break; case cordova.plugins.diagnostic.permissionStatus.DENIED_ONCE: console.log("Permission denied to use the camera - ask again?"); break; case cordova.plugins.diagnostic.permissionStatus.DENIED_ALWAYS: console.log("Permission permanently denied to use the camera - guess we won't be using it then!"); break; } }, function(error){ console.error("The following error occurred: "+error); }, cordova.plugins.diagnostic.permission.CAMERA); ### getPermissionsAuthorizationStatus() Platforms: Android Returns the current authorisation status for multiple permissions. Note: this is intended for Android 6 / API 23 and above. Calling on Android 5.1 / API 22 and below will always return GRANTED status as permissions are already granted at installation time. #### Parameters - {Function} successCallback - function to call on successful retrieval of status. The function is passed a single object parameter which defines a key/value map, where the key is the requested [runtime permission](#dangerous-runtime-permissions), and the value is the current [permission status](#permissionstatus-constants). - {Function} errorCallback - function to call on failure to retrieve authorisation status. The function is passed a single string parameter containing the error message. - {Array} permissions - list of permissions to request authorisation statuses for, defined as [runtime permission constants](#dangerous-runtime-permissions). #### Example usage cordova.plugins.diagnostic.getPermissionsAuthorizationStatus(function(statuses){ for (var permission in statuses){ switch(statuses[permission]){ case cordova.plugins.diagnostic.permissionStatus.GRANTED: console.log("Permission granted to use "+permission); break; case cordova.plugins.diagnostic.permissionStatus.NOT_REQUESTED: console.log("Permission to use "+permission+" has not been requested yet"); break; case cordova.plugins.diagnostic.permissionStatus.DENIED_ONCE: console.log("Permission denied to use "+permission+" - ask again?"); break; case cordova.plugins.diagnostic.permissionStatus.DENIED_ALWAYS: console.log("Permission permanently denied to use "+permission+" - guess we won't be using it then!"); break; } } }, function(error){ console.error("The following error occurred: "+error); },[ cordova.plugins.diagnostic.permission.ACCESS_FINE_LOCATION, cordova.plugins.diagnostic.permission.ACCESS_COARSE_LOCATION ]); ### requestRuntimePermission() Platforms: Android Requests app to be granted authorisation for a runtime permission. Note: this is intended for Android 6 / API 23 and above. Calling on Android 5.1 / API 22 and below will have no effect as the permissions are already granted at installation time. #### Parameters - {Function} successCallback - function to call on successful request for runtime permission. The function is passed a single string parameter which defines the resulting [permission status](#permissionstatus-constants) - {Function} errorCallback - function to call on failure to request authorisation. The function is passed a single string parameter containing the error message. - {String} permission - permission to request authorisation for, defined as a [runtime permission constant](#dangerous-runtime-permissions). #### Example usage cordova.plugins.diagnostic.requestRuntimePermission(function(status){ switch(status){ case cordova.plugins.diagnostic.permissionStatus.GRANTED: console.log("Permission granted to use the camera"); break; case cordova.plugins.diagnostic.permissionStatus.NOT_REQUESTED: console.log("Permission to use the camera has not been requested yet"); break; case cordova.plugins.diagnostic.permissionStatus.DENIED_ONCe: console.log("Permission denied to use the camera - ask again?"); break; case cordova.plugins.diagnostic.permissionStatus.DENIED_ALWAYS: console.log("Permission permanently denied to use the camera - guess we won't be using it then!"); break; } }, function(error){ console.error("The following error occurred: "+error); }, cordova.plugins.diagnostic.permission.CAMERA); ### requestRuntimePermissions() Platforms: Android Requests app to be granted authorisation for multiple runtime permissions. Note: this is intended for Android 6 / API 23 and above. Calling on Android 5.1 / API 22 and below will always return GRANTED status as permissions are already granted at installation time. #### Parameters - {Function} successCallback - function to call on successful request for runtime permissions. The function is passed a single object parameter which defines a key/value map, where the key is the [runtime permission](#dangerous-runtime-permissions) to request, and the value is the current [permission status](#permissionstatus-constants). - {Function} errorCallback - function to call on failure to request authorisation. The function is passed a single string parameter containing the error message. - {Array} permissions - list of permissions to request authorisation for, defined as [runtime permission constants](#dangerous-runtime-permissions). #### Example usage cordova.plugins.diagnostic.requestRuntimePermissions(function(statuses){ for (var permission in statuses){ switch(statuses[permission]){ case cordova.plugins.diagnostic.permissionStatus.GRANTED: console.log("Permission granted to use "+permission); break; case cordova.plugins.diagnostic.permissionStatus.NOT_REQUESTED: console.log("Permission to use "+permission+" has not been requested yet"); break; case cordova.plugins.diagnostic.permissionStatus.DENIED_ONCE: console.log("Permission denied to use "+permission+" - ask again?"); break; case cordova.plugins.diagnostic.permissionStatus.DENIED_ALWAYS: console.log("Permission permanently denied to use "+permission+" - guess we won't be using it then!"); break; } } }, function(error){ console.error("The following error occurred: "+error); },[ cordova.plugins.diagnostic.permission.ACCESS_FINE_LOCATION, cordova.plugins.diagnostic.permission.ACCESS_COARSE_LOCATION ]); ### isRequestingPermission() Platforms: Android Indicates if the plugin is currently requesting a runtime permission via the native API. Note that only one request can be made concurrently because the native API cannot handle concurrent requests, so the plugin will invoke the error callback if attempting to make more than one simultaneous request. Multiple permission requests should be grouped into a single call since the native API is setup to handle batch requests of multiple permission groups. var isRequesting = cordova.plugins.diagnostic.isRequestingPermission(); #### Example usage var isRequesting = cordova.plugins.diagnostic.isRequestingPermission(); if(!isRequesting){ requestSomePermissions(); }else{ cordova.plugins.diagnostic.registerPermissionRequestCompleteHandler(function(statuses){ cordova.plugins.diagnostic.registerPermissionRequestCompleteHandler(null); // de-register handler after single call requestSomePermissions(); }); } ### registerPermissionRequestCompleteHandler() Platforms: Android Registers a function to be called when a runtime permission request has completed. Pass in a falsey value to de-register the currently registered function. cordova.plugins.diagnostic.registerPermissionRequestCompleteHandler(successCallback); #### Parameters - {Function} successCallback - The callback which will be called when a runtime permission request has completed. The function is passed a single object parameter which defines a key/value map, where the key is the permission requested (defined as a value in cordova.plugins.diagnostic.permission) and the value is the resulting authorisation status of that permission as a value in cordova.plugins.diagnostic.permissionStatus. #### Example usage function onPermissionRequestComplete(statuses){ console.info("Permission request complete"); for (var permission in statuses){ switch(statuses[permission]){ case cordova.plugins.diagnostic.permissionStatus.GRANTED: console.log("Permission granted to use "+permission); break; case cordova.plugins.diagnostic.permissionStatus.NOT_REQUESTED: console.log("Permission to use "+permission+" has not been requested yet"); break; case cordova.plugins.diagnostic.permissionStatus.DENIED_ONCE: console.log("Permission denied to use "+permission); break; case cordova.plugins.diagnostic.permissionStatus.DENIED_ALWAYS: console.log("Permission permanently denied to use "+permission); break; } } cordova.plugins.diagnostic.registerPermissionRequestCompleteHandler(null); // de-register handler } cordova.plugins.diagnostic.registerPermissionRequestCompleteHandler(onPermissionRequestComplete); ### isDataRoamingEnabled() Platforms: Android Checks if the device data roaming setting is enabled. Returns true if data roaming is enabled. Not available on Android 12L / API 32+ cordova.plugins.diagnostic.isDataRoamingEnabled(successCallback, errorCallback); #### Parameters - {Function} successCallback - The callback which will be called when the operation is successful. The function is passed a single boolean parameter which is TRUE if data roaming is enabled. - {Function} errorCallback - The callback which will be called when operation encounters an error. The function is passed a single string parameter containing the error message. #### Example usage cordova.plugins.diagnostic.isDataRoamingEnabled(function(enabled){ console.log("Data roaming is " + (enabled ? "enabled" : "disabled")); }, function(error){ console.error("The following error occurred: "+error); }); ### isADBModeEnabled() Platforms: Android Checks if the device setting for ADB(debug) is switched on. Returns true if ADB(debug) setting is switched on. cordova.plugins.diagnostic.isADBModeEnabled(successCallback, errorCallback); #### Parameters - {Function} successCallback - The callback which will be called when operation is successful. The function is passed a single boolean parameter which is TRUE if ADB mode(debug mode) is switched on. - {Function} errorCallback - The callback which will be called when operation encounters an error. The function is passed a single string parameter containing the error message. #### Example usage cordova.plugins.diagnostic.isADBModeEnabled(function(enabled){ console.log("ADB mode(debug mode) is " + (enabled ? "enabled" : "disabled")); }, function(error){ console.error("The following error occurred: "+error); }); ### isDeviceRooted() Platforms: Android Checks if the device is rooted. Returns true if the device is rooted. cordova.plugins.diagnostic.isDeviceRooted(successCallback, errorCallback); #### Parameters - {Function} successCallback - The callback which will be called when operation is successful. The function is passed a single boolean parameter which is TRUE if the device is rooted. - {Function} errorCallback - The callback which will be called when operation encounters an error. The function is passed a single string parameter containing the error message. #### Example usage cordova.plugins.diagnostic.isDeviceRooted(function(rooted){ console.log("device is " + (rooted ? "rooted" : "not rooted")); }, function(error){ console.error("The following error occurred: "+error); }); ### isBackgroundRefreshAuthorized() Platforms: iOS Checks if the application is authorized for background refresh. cordova.plugins.diagnostic.isBackgroundRefreshAuthorized(successCallback, errorCallback); #### Parameters - {Function} successCallback - The callback which will be called when operation is successful. The function is passed a single boolean parameter which is TRUE if background refresh access is authorized for use. - {Function} errorCallback - The callback which will be called when operation encounters an error. The function is passed a single string parameter containing the error message. #### Example usage cordova.plugins.diagnostic.isBackgroundRefreshAuthorized(function(authorized){ console.log("App is " + (authorized ? "authorized" : "not authorized") + " to perform background refresh"); }, function(error){ console.error("The following error occurred: "+error); }); ### getBackgroundRefreshStatus() Platforms: iOS Returns the background refresh authorization status for the application. cordova.plugins.diagnostic.getBackgroundRefreshStatus(successCallback, errorCallback); #### Parameters - {Function} successCallback - The callback which will be called when operation is successful. The function is passed a single string parameter which indicates the authorization status as a [permissionStatus constant](#permissionstatus-constants). - {Function} errorCallback - The callback which will be called when operation encounters an error. The function is passed a single string parameter containing the error message. #### Example usage cordova.plugins.diagnostic.getBackgroundRefreshStatus(function(status){ if(status === cordova.plugins.diagnostic.permissionStatus.GRANTED){ console.log("Background refresh is allowed"); } }, function(error){ console.error("The following error occurred: "+error); }); ### cpuArchitecture constants Platforms: Android and iOS Defines constants for the various CPU architectures of the current hardware returned by [getArchitecture()](#getarchitecture). cordova.plugins.diagnostic.cpuArchitecture #### Android - `UNKNOWN` - Unknown CPU architecture - `ARMv6` - ARM v6 or below (32 bit) - `ARMv7` - ARM v7 (32 bit) - `ARMv8` - ARM v8 (64 bit) - `X86` - Intel x86 (32 bit) - `X86_64` - Intel x86 (64 bit) - `MIPS` - MIPS (32 bit) - `MIPS_64` - MIPS (64 bit) #### iOS - `UNKNOWN` - Unknown CPU architecture - `ARMv6` - ARM v6 or below (32 bit) - `ARMv7` - ARM v7 (32 bit) - `ARMv8` - ARM v8 (64 bit) - `X86` - Intel x86 (32 bit) - `X86_64` - Intel x86 (64 bit) #### Example usage See [getArchitecture()](#getarchitecture). ### getArchitecture() Platforms: Android and iOS Returns the CPU architecture of the current device. cordova.plugins.diagnostic.getArchitecture(successCallback, errorCallback); #### Parameters - {Function} successCallback - The callback which will be called when operation is successful. The function is passed a single boolean parameter which indicates the location authorization status as a [cpuArchitecture constant](#cpuarchitecture-constants). - {Function} errorCallback - The callback which will be called when operation encounters an error. The function is passed a single string parameter containing the error message. #### Example usage cordova.plugins.diagnostic.getArchitecture(function(arch){ if(arch === cordova.plugins.diagnostic.cpuArchitecture.X86 || arch === cordova.plugins.diagnostic.cpuArchitecture.X86_64){ console.log("Intel inside"); } }, function(error){ console.error(error); }); ### restart() Platforms: Android Restarts the application. By default, a "warm" restart will be performed in which the main Cordova activity is immediately restarted, causing the Webview instance to be recreated. However, if the `cold` parameter is set to true, then the application will be "cold" restarted, meaning a system exit will be performed, causing the entire application to be restarted. This is useful if you want to fully reset the native application state but will cause the application to briefly disappear and re-appear. Note: There is no `successCallback()` since if the operation is successful, the application will restart immediately before any success callback can be applied. cordova.plugins.diagnostic.restart(errorCallback, cold); #### Parameters - {Function} errorCallback - The callback which will be called when operation encounters an error. The function is passed a single string parameter containing the error message. - {Boolean} cold - if true the application will be cold restarted. Defaults to false. #### Example usage var onError = function(error){ console.error("The following error occurred: "+error); } // Warm restart cordova.plugins.diagnostic.restart(onError, false); // Cold restart cordova.plugins.diagnostic.restart(onError, true); ### enableDebug() Platforms: Android and iOS Enables the plugin's debug mode, which logs native debug messages related to anything done with the plugin to the native and JS consoles. - For Android, log messages will appear in the native logcat output and in the JS console if Chrome Developer Tools is connected to the app Webview. - For iOS, log messages will appear in the native Xcode console output and in the JS console if Safari Web Inspector is connected to the app Webview. - Debug mode is initially disabled on plugin initialisation. ``` cordova.plugins.diagnostic.enableDebug(successCallback); ``` #### Parameters - {Function} successCallback - The callback which will be called when debug has been enabled. #### Example usage cordova.plugins.diagnostic.enableDebug(function(){ console.log("Debug is enabled")); }); ### getCurrentBatteryLevel() Platforms: Android and iOS Returns the current battery level of the device as a percentage. cordova.plugins.diagnostic.getCurrentBatteryLevel(successCallback, successCallback); #### Parameters - {Function} successCallback - The callback which will be called when operation is successful. The function is passed a single `int` parameter which indicates the current battery level percentage. - {Function} errorCallback - The callback which will be called when operation encounters an error. The function is passed a single `string` parameter containing the error message. #### Example usage cordova.plugins.diagnostic.getCurrentBatteryLevel(function(level){ console.log(`Current battery level is ${level}%`); }); ### isAirplaneModeEnabled() Platforms: Android Checks if Airplane mode is currently enabled. cordova.plugins.diagnostic.isAirplaneModeEnabled(successCallback, errorCallback); #### Parameters - {Function} successCallback - The callback which will be called when operation is successful. The function is passed a single boolean parameter which is TRUE if Airplane mode is enabled. - {Function} errorCallback - The callback which will be called when operation encounters an error. The function is passed a single string parameter containing the error message. #### Example usage cordova.plugins.diagnostic.isAirplaneModeEnabled(function(enabled){ console.log(`Airplane mode is currently ${enabled ? 'enabled' : 'disabled'}`); }); ### isMobileDataEnabled() Platforms: Android Checks if mobile (cellular) data is currently enabled in the device settings. Requires permission `<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />` cordova.plugins.diagnostic.isMobileDataEnabled(successCallback, errorCallback); #### Parameters - {Function} successCallback - The callback which will be called when operation is successful. The function is passed a single boolean parameter which is TRUE if mobile data is enabled. - {Function} errorCallback - The callback which will be called when operation encounters an error. The function is passed a single string parameter containing the error message. #### Example usage cordova.plugins.diagnostic.isMobileDataEnabled(function(enabled){ console.log(`Mobile data is currently ${enabled ? 'enabled' : 'disabled'}`); });`` ### isMobileDataAuthorized() Platforms: iOS Checks if mobile data is authorized for this app. Returns true if the per-app Mobile Data setting is set to enabled (regardless of whether the device is currently connected to a cellular network) cordova.plugins.diagnostic.isMobileDataAuthorized(successCallback, errorCallback); #### Parameters - {Function} successCallback - The callback which will be called when operation is successful. The function is passed a single boolean parameter which is TRUE if mobile data is authorized. - {Function} errorCallback - The callback which will be called when operation encounters an error. The function is passed a single string parameter containing the error message. #### Example usage cordova.plugins.diagnostic.isMobileDataAuthorized(function(authorized){ console.log(`Mobile data is currently ${authorized ? 'authorized' : 'unauthorized'}`); }); ### isAcce