UNPKG

cordova.plugins.diagnostic

Version:

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

993 lines (910 loc) 81.2 kB
/** * Diagnostic plugin for Android * * Copyright (c) 2015 Working Edge Ltd. * Copyright (c) 2012 AVANTIC ESTUDIO DE INGENIEROS **/ var Diagnostic = (function(){ /*********************** * * Internal properties * *********************/ var Diagnostic = {}; // Indicates if a runtime permissions request is in progress var requestInProgress = false; /******************** * * Public properties * ********************/ /** * "Dangerous" permissions that need to be requested at run-time (Android 6.0/API 23 and above) * See http://developer.android.com/guide/topics/security/permissions.html#perm-groups * @type {Object} */ Diagnostic.permission = { "ACCEPT_HANDOVER": "ACCEPT_HANDOVER", "ACCESS_BACKGROUND_LOCATION": "ACCESS_BACKGROUND_LOCATION", "ACCESS_COARSE_LOCATION": "ACCESS_COARSE_LOCATION", "ACCESS_FINE_LOCATION": "ACCESS_FINE_LOCATION", "ACCESS_MEDIA_LOCATION": "ACCESS_MEDIA_LOCATION", "ACTIVITY_RECOGNITION": "ACTIVITY_RECOGNITION", "ADD_VOICEMAIL": "ADD_VOICEMAIL", "ANSWER_PHONE_CALLS": "ANSWER_PHONE_CALLS", "BLUETOOTH_ADVERTISE": "BLUETOOTH_ADVERTISE", "BLUETOOTH_CONNECT": "BLUETOOTH_CONNECT", "BLUETOOTH_SCAN": "BLUETOOTH_SCAN", "BODY_SENSORS": "BODY_SENSORS", "BODY_SENSORS_BACKGROUND": "BODY_SENSORS_BACKGROUND", "CALL_PHONE": "CALL_PHONE", "CAMERA": "CAMERA", "GET_ACCOUNTS": "GET_ACCOUNTS", "NEARBY_WIFI_DEVICES": "NEARBY_WIFI_DEVICES", "POST_NOTIFICATIONS": "POST_NOTIFICATIONS", "PROCESS_OUTGOING_CALLS": "PROCESS_OUTGOING_CALLS", "READ_CALENDAR": "READ_CALENDAR", "READ_CALL_LOG": "READ_CALL_LOG", "READ_CONTACTS": "READ_CONTACTS", "READ_EXTERNAL_STORAGE": "READ_EXTERNAL_STORAGE", "READ_MEDIA_AUDIO": "READ_MEDIA_AUDIO", "READ_MEDIA_IMAGES": "READ_MEDIA_IMAGES", "READ_MEDIA_VIDEO": "READ_MEDIA_VIDEO", "READ_PHONE_NUMBERS": "READ_PHONE_NUMBERS", "READ_PHONE_STATE": "READ_PHONE_STATE", "READ_SMS": "READ_SMS", "RECEIVE_MMS": "RECEIVE_MMS", "RECEIVE_SMS": "RECEIVE_SMS", "RECEIVE_WAP_PUSH": "RECEIVE_WAP_PUSH", "RECORD_AUDIO": "RECORD_AUDIO", "SEND_SMS": "SEND_SMS", "USE_SIP": "USE_SIP", "UWB_RANGING": "UWB_RANGING", "WRITE_CALENDAR": "WRITE_CALENDAR", "WRITE_CALL_LOG": "WRITE_CALL_LOG", "WRITE_CONTACTS": "WRITE_CONTACTS", "WRITE_EXTERNAL_STORAGE": "WRITE_EXTERNAL_STORAGE" }; Diagnostic.permissionStatus = { // Location permission requested and // app build SDK/user device is Android >10 and user granted background location ("all the time") permission, // or app build SDK/user device is Android 6-9 and user granted location permission, // or non-location permission requested // and app build SDK/user device is Android >=6 and user granted permission // or app build SDK/user device is Android <6 "GRANTED": "GRANTED", // Location permission requested // and app build SDK/user device is Android >10 // and user granted background foreground location ("while-in-use") permission "GRANTED_WHEN_IN_USE": "authorized_when_in_use", // User denied access to this permission "DENIED_ONCE": "DENIED_ONCE", // User denied access to this permission and checked "Never Ask Again" box. "DENIED_ALWAYS": "DENIED_ALWAYS", // App has not yet requested access to this permission. "NOT_REQUESTED": "NOT_REQUESTED", // Limited access to the photo library on Android 14 (API 34) and above "LIMITED": "LIMITED" }; Diagnostic.cpuArchitecture = { UNKNOWN: "unknown", ARMv6: "ARMv6", ARMv7: "ARMv7", ARMv8: "ARMv8", X86: "X86", X86_64: "X86_64", MIPS: "MIPS", MIPS_64: "MIPS_64" }; /***************************** * * Protected member functions * ****************************/ // Placeholder listeners Diagnostic._onNFCStateChange = Diagnostic._onPermissionRequestComplete = function(){}; Diagnostic._combinePermissionStatuses = function(statuses){ var status = Diagnostic.permissionStatus.NOT_REQUESTED; if(anyStatusIs(statuses, Diagnostic.permissionStatus.DENIED_ALWAYS)){ status = Diagnostic.permissionStatus.DENIED_ALWAYS; }else if(anyStatusIs(statuses, Diagnostic.permissionStatus.DENIED_ONCE)){ status = Diagnostic.permissionStatus.DENIED_ONCE; }else if(anyStatusIs(statuses, Diagnostic.permissionStatus.GRANTED)){ status = Diagnostic.permissionStatus.GRANTED; } return status; }; /******************** * * Internal functions * ********************/ function anyStatusIs(statuses, status){ var anyStatus = false; for(var permission in statuses){ if(statuses[permission] === status){ anyStatus = true; break; } } return anyStatus; } function checkForInvalidPermissions(permissions, errorCallback){ if(typeof(permissions) !== "object") permissions = [permissions]; var valid = true, invalidPermissions = []; permissions.forEach(function(permission){ if(!Diagnostic.permission[permission]){ invalidPermissions.push(permission); } }); if(invalidPermissions.length > 0){ errorCallback("Invalid permissions specified: "+invalidPermissions.join(", ")); valid = false; } return valid; } /***************************** * * Protected member functions * ****************************/ Diagnostic._ensureBoolean = function (callback){ return function(result){ callback(!!result); } }; /********************** * * Public API functions * **********************/ /*********** * General ***********/ /** * Enables debug mode, which logs native debug messages to the native and JS consoles. * Debug mode is initially disabled on plugin initialisation. * * @param {Function} successCallback - The callback which will be called when enabling debug is successful. */ Diagnostic.enableDebug = function(successCallback) { return cordova.exec(successCallback, null, 'Diagnostic', 'enableDebug', []); }; /** * Opens settings page for this app. * * @param {Function} successCallback - The callback which will be called when switch to settings is successful. * @param {Function} errorCallback - The callback which will be called when switch to settings encounters an error. * This callback function is passed a single string parameter containing the error message. */ Diagnostic.switchToSettings = function(successCallback, errorCallback) { return cordova.exec(successCallback, errorCallback, 'Diagnostic', 'switchToSettings', []); }; /** * Returns the current authorisation status for a given permission. * Note: this is intended for Android 6 / API 23 and above. Calling on Android 5 / API 22 and below will always return GRANTED status as permissions are already granted at installation time. * * @param {Function} successCallback - function to call on successful retrieval of status. * This callback function is passed a single string parameter which defines the current authorisation status as a value in cordova.plugins.diagnostic.permissionStatus. * @param {Function} errorCallback - function to call on failure to retrieve authorisation status. * This callback function is passed a single string parameter containing the error message. * @param {String} permission - permission to request authorisation status for, defined as a value in cordova.plugins.diagnostic.permission */ Diagnostic.getPermissionAuthorizationStatus = function(successCallback, errorCallback, permission){ if(!checkForInvalidPermissions(permission, errorCallback)) return; return cordova.exec( successCallback, errorCallback, 'Diagnostic', 'getPermissionAuthorizationStatus', [permission]); }; /** * Returns the current authorisation status for multiple permissions. * Note: this is intended for Android 6 / API 23 and above. Calling on Android 5 / API 22 and below will always return GRANTED status as permissions are already granted at installation time. * * @param {Function} successCallback - function to call on successful retrieval of status. * This callback function is passed a single object parameter which defines a key/value map, where the key is the requested permission defined as a value in cordova.plugins.diagnostic.permission, and the value is the current authorisation status of that permission as a value in cordova.plugins.diagnostic.permissionStatus. * @param {Function} errorCallback - function to call on failure to retrieve authorisation statuses. * This callback function is passed a single string parameter containing the error message. * @param {Array} permissions - list of permissions to request authorisation statuses for, defined as values in cordova.plugins.diagnostic.permission */ Diagnostic.getPermissionsAuthorizationStatus = function(successCallback, errorCallback, permissions){ if(!checkForInvalidPermissions(permissions, errorCallback)) return; return cordova.exec( successCallback, errorCallback, 'Diagnostic', 'getPermissionsAuthorizationStatus', [permissions]); }; /** * 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 / API 22 and below will have no effect as the permissions are already granted at installation time. * * @param {Function} successCallback - function to call on successful request for runtime permission. * This callback function is passed a single string parameter which defines the resulting authorisation status as a value in cordova.plugins.diagnostic.permissionStatus. * @param {Function} errorCallback - function to call on failure to request authorisation. * This callback function is passed a single string parameter containing the error message. * @param {String} permission - permission to request authorisation for, defined as a value in cordova.plugins.diagnostic.permission */ Diagnostic.requestRuntimePermission = function(successCallback, errorCallback, permission) { if(!checkForInvalidPermissions(permission, errorCallback)) return; if(requestInProgress){ return onError("A runtime permissions request is already in progress"); } function onSuccess(statuses){ requestInProgress = false; successCallback(statuses[permission]); Diagnostic._onPermissionRequestComplete(statuses); } function onError(error){ requestInProgress = false; errorCallback(error); } requestInProgress = true; return cordova.exec( onSuccess, onError, 'Diagnostic', 'requestRuntimePermission', [permission]); }; /** * 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 / API 22 and below will have no effect as the permissions are already granted at installation time. * * @param {Function} successCallback - function to call on successful request for runtime permissions. * This callback function is passed a single object parameter which defines a key/value map, where the key is the permission to request 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. * @param {Function} errorCallback - function to call on failure to request authorisation. * This callback function is passed a single string parameter containing the error message. * @param {Array} permissions - permissions to request authorisation for, defined as values in cordova.plugins.diagnostic.permission */ Diagnostic.requestRuntimePermissions = function(successCallback, errorCallback, permissions){ if(!checkForInvalidPermissions(permissions, errorCallback)) return; if(requestInProgress){ return onError("A runtime permissions request is already in progress"); } function onSuccess(statuses){ requestInProgress = false; successCallback(statuses); Diagnostic._onPermissionRequestComplete(statuses); } function onError(error){ requestInProgress = false; errorCallback(error); } requestInProgress = true; return cordova.exec( onSuccess, onError, 'Diagnostic', 'requestRuntimePermissions', [permissions]); }; /** * 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. * * @return {boolean} true if a permission request is currently in progress. */ Diagnostic.isRequestingPermission = function(){ return requestInProgress; }; /** * 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. * * @param {Function} successCallback - The callback which will be called when a runtime permission request has completed. * This callback 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. */ Diagnostic.registerPermissionRequestCompleteHandler = function(successCallback) { Diagnostic._onPermissionRequestComplete = successCallback || function(){}; }; /** * Switches to the wireless settings page in the Settings app. * Allows configuration of wireless controls such as Wi-Fi, Bluetooth and Mobile networks. */ Diagnostic.switchToWirelessSettings = function() { return cordova.exec(null, null, 'Diagnostic', 'switchToWirelessSettings', []); }; /** * Switches to the Mobile Data page in the Settings app */ Diagnostic.switchToMobileDataSettings = function() { return cordova.exec(null, null, 'Diagnostic', 'switchToMobileDataSettings', []); }; /** * Checks if ADB mode(debug mode) is switched on. * Returns true if ADB mode is switched on. * * @param {Function} successCallback - The callback which will be called when the operation is successful. * This callback function is passed a single boolean parameter which is TRUE if ADB mode(debug mode) is switched on. * @param {Function} errorCallback - The callback which will be called when the operation encounters an error. * This callback function is passed a single string parameter containing the error message. */ Diagnostic.isADBModeEnabled = function(successCallback, errorCallback) { return cordova.exec(Diagnostic._ensureBoolean(successCallback), errorCallback, 'Diagnostic', 'isADBModeEnabled', []); }; /** * Checks if the device is rooted. * Returns true if the device is rooted. * * @param {Function} successCallback - The callback which will be called when the operation is successful. * This callback function is passed a single boolean parameter which is TRUE if the device is rooted. * @param {Function} errorCallback - The callback which will be called when the operation encounters an error. * This callback function is passed a single string parameter containing the error message. */ Diagnostic.isDeviceRooted = function(successCallback, errorCallback) { return cordova.exec(Diagnostic._ensureBoolean(successCallback), errorCallback, 'Diagnostic', 'isDeviceRooted', []); }; /** * 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. * * @param {Function} errorCallback - function to call on failure to retrieve authorisation status. * This callback function is passed a single string parameter containing the error message. * @param {Boolean} cold - if true the application will be cold restarted. Defaults to false. */ Diagnostic.restart = function(errorCallback, cold){ return cordova.exec( null, errorCallback, 'Diagnostic', 'restart', [cold]); }; /** * Returns CPU architecture of the current device. * * @param {Function} successCallback - The callback which will be called when the operation is successful. * This callback function is passed a single string parameter defined as a constant in `cordova.plugins.diagnostic.cpuArchitecture`. * @param {Function} errorCallback - The callback which will be called when the operation encounters an error. * This callback function is passed a single string parameter containing the error message. */ Diagnostic.getArchitecture = function(successCallback, errorCallback) { return cordova.exec(successCallback, errorCallback, 'Diagnostic', 'getArchitecture', []); }; /** * Checks if the device data roaming setting is enabled. * Returns true if data roaming is enabled. * * @param {Function} successCallback - The callback which will be called when the operation is successful. * This callback function is passed a single boolean parameter which is TRUE if data roaming is enabled. * @param {Function} errorCallback - The callback which will be called when the operation encounters an error. * This callback function is passed a single string parameter containing the error message. */ Diagnostic.isDataRoamingEnabled = function(successCallback, errorCallback) { return cordova.exec(Diagnostic._ensureBoolean(successCallback), errorCallback, 'Diagnostic', 'isDataRoamingEnabled', []); }; /** * Returns the current battery level of the device as a percentage. * * @param {Function} successCallback - The callback which will be called when the operation is successful. * This callback function is passed a single integer parameter which the current battery level percentage. * @param {Function} errorCallback - The callback which will be called when the operation encounters an error. * This callback function is passed a single string parameter containing the error message. */ Diagnostic.getCurrentBatteryLevel = function(successCallback, errorCallback){ return cordova.exec(successCallback, errorCallback, 'Diagnostic', 'getCurrentBatteryLevel', []); }; /** * Checks if airplane mode is enabled on device. * * @param {Function} successCallback - The callback which will be called when the operation is successful. * This callback function is passed a single boolean parameter which is TRUE if airplane mode is enabled. * @param {Function} errorCallback - The callback which will be called when the operation encounters an error. * This callback function is passed a single string parameter containing the error message. */ Diagnostic.isAirplaneModeEnabled = function(successCallback, errorCallback) { return cordova.exec(Diagnostic._ensureBoolean(successCallback), errorCallback, 'Diagnostic', 'isAirplaneModeEnabled', []); }; /** * Checks if mobile data is enabled in device settings. * * @param {Function} successCallback - The callback which will be called when the operation is successful. * This callback function is passed a single boolean parameter which is TRUE if mobile data is enabled. * @param {Function} errorCallback - The callback which will be called when the operation encounters an error. * This callback function is passed a single string parameter containing the error message. */ Diagnostic.isMobileDataEnabled = function(successCallback, errorCallback) { return cordova.exec(Diagnostic._ensureBoolean(successCallback), errorCallback, 'Diagnostic', 'isMobileDataEnabled', []); }; /** * Checks if accessibility mode (talkback) is enabled on device. * * @param {Function} successCallback - The callback which will be called when the operation is successful. * This callback function is passed a single boolean parameter which is TRUE if accessibility mode is enabled. * @param {Function} errorCallback - The callback which will be called when the operation encounters an error. * This callback function is passed a single string parameter containing the error message. */ Diagnostic.isAccessibilityModeEnabled = function(successCallback, errorCallback) { return cordova.exec(Diagnostic._ensureBoolean(successCallback), errorCallback, 'Diagnostic', 'isAccessibilityModeEnabled', []); }; /** * Checks if touch exploration (in accessibility mode) is enabled on device. * * @param {Function} successCallback - The callback which will be called when the operation is successful. * This callback function is passed a single boolean parameter which is TRUE if touch exploration (in accessibility mode) is enabled. * @param {Function} errorCallback - The callback which will be called when the operation encounters an error. * This callback function is passed a single string parameter containing the error message. */ Diagnostic.isTouchExplorationEnabled = function(successCallback, errorCallback) { return cordova.exec(Diagnostic._ensureBoolean(successCallback), errorCallback, 'Diagnostic', 'isTouchExplorationEnabled', []); }; /** * Returns details of the OS of the device on which the app is currently running * * @param {Function} successCallback - The callback which will be called when the operation is successful. * This callback function is passed a single object parameter with the following fields: * - {string} version - version string of the OS e.g. "11.0" * - {integer} apiLevel - API level of the OS e.g. 30 * - {string} apiName - code name for API level e.g. "FROYO" * @param {Function} errorCallback - The callback which will be called when the operation encounters an error. * This callback function is passed a single string parameter containing the error message. */ Diagnostic.getDeviceOSVersion = function(successCallback, errorCallback) { return cordova.exec(successCallback, errorCallback, 'Diagnostic', 'getDeviceOSVersion', []); }; /** * Returns details of the SDK levels used to build the app. * * @param {Function} successCallback - The callback which will be called when the operation is successful. * This callback function is passed a single object parameter with the following fields: * - {integer} targetApiLevel - API level of the target SDK (used to build the app) * - {string} targetApiName - code name for API level of the target SDK e.g. "FROYO" * - {integer} minApiLevel - API level of the minimum SDK (lowest on which the app can be installed) * - {string} minApiName - code name for API level of the minimum SDK e.g. "FROYO" * @param {Function} errorCallback - The callback which will be called when the operation encounters an error. * This callback function is passed a single string parameter containing the error message. */ Diagnostic.getBuildOSVersion = function(successCallback, errorCallback) { return cordova.exec(successCallback, errorCallback, 'Diagnostic', 'getBuildOSVersion', []); }; /************ * Location * ************/ /** * Checks if location is available for use by the app. * On Android, this returns true if Location Mode is enabled and any mode is selected (e.g. Battery saving, Device only, High accuracy) * AND if the app is authorised to use location. * * @param {Function} successCallback - The callback which will be called when the operation is successful. * This callback function is passed a single boolean parameter which is TRUE if location is available for use. * @param {Function} errorCallback - The callback which will be called when the operation encounters an error. * This callback function is passed a single string parameter containing the error message. */ Diagnostic.isLocationAvailable = function(successCallback, errorCallback) { if(cordova.plugins.diagnostic.location){ cordova.plugins.diagnostic.location.isLocationAvailable.apply(this, arguments); }else{ throw "Diagnostic Location module is not installed"; } }; /** * Checks if the device location setting is enabled. * On Android, this returns true if Location Mode is enabled and any mode is selected (e.g. Battery saving, Device only, High accuracy) * * @param {Function} successCallback - The callback which will be called when the operation is successful. * This callback function is passed a single boolean parameter which is TRUE if location setting is enabled. * @param {Function} errorCallback - The callback which will be called when the operation encounters an error. * This callback function is passed a single string parameter containing the error message. */ Diagnostic.isLocationEnabled = function(successCallback, errorCallback) { if(cordova.plugins.diagnostic.location){ cordova.plugins.diagnostic.location.isLocationEnabled.apply(this, arguments); }else{ throw "Diagnostic Location module is not installed"; } }; /** * Checks if high-accuracy locations are available to the app from GPS hardware. * Returns true if Location mode is enabled and is set to "Device only" or "High accuracy" * AND if the app is authorised to use location. * * @param {Function} successCallback - The callback which will be called when the operation is successful. * This callback function is passed a single boolean parameter which is TRUE if high-accuracy GPS-based location is available. * @param {Function} errorCallback - The callback which will be called when the operation encounters an error. * This callback function is passed a single string parameter containing the error message. */ Diagnostic.isGpsLocationAvailable = function(successCallback, errorCallback) { if(cordova.plugins.diagnostic.location){ cordova.plugins.diagnostic.location.isGpsLocationAvailable.apply(this, arguments); }else{ throw "Diagnostic Location module is not installed"; } }; /** * Checks if the device location setting is set to return high-accuracy locations from GPS hardware. * Returns true if Location mode is enabled and is set to either: * Device only = GPS hardware only (high accuracy) * High accuracy = GPS hardware, network triangulation and Wifi network IDs (high and low accuracy) * * @param {Function} successCallback - The callback which will be called when the operation is successful. * This callback function is passed a single boolean parameter which is TRUE if device setting is set to return high-accuracy GPS-based location. * @param {Function} errorCallback - The callback which will be called when the operation encounters an error. * This callback function is passed a single string parameter containing the error message. */ Diagnostic.isGpsLocationEnabled = function(successCallback, errorCallback) { if(cordova.plugins.diagnostic.location){ cordova.plugins.diagnostic.location.isGpsLocationEnabled.apply(this, arguments); }else{ throw "Diagnostic Location module is not installed"; } }; /** * Checks if low-accuracy locations are available to the app from network triangulation/WiFi access points. * Returns true if Location mode is enabled and is set to "Battery saving" or "High accuracy" * AND if the app is authorised to use location. * * @param {Function} successCallback - The callback which will be called when the operation is successful. * This callback function is passed a single boolean parameter which is TRUE if low-accuracy network-based location is available. * @param {Function} errorCallback - The callback which will be called when the operation encounters an error. * This callback function is passed a single string parameter containing the error message. */ Diagnostic.isNetworkLocationAvailable = function(successCallback, errorCallback) { if(cordova.plugins.diagnostic.location){ cordova.plugins.diagnostic.location.isNetworkLocationAvailable.apply(this, arguments); }else{ throw "Diagnostic Location module is not installed"; } }; /** * Checks if the device location setting is set to return low-accuracy locations from network triangulation/WiFi access points. * Returns true if Location mode is enabled and is set to either: * Battery saving = network triangulation and Wifi network IDs (low accuracy) * High accuracy = GPS hardware, network triangulation and Wifi network IDs (high and low accuracy) * * @param {Function} successCallback - The callback which will be called when the operation is successful. * This callback function is passed a single boolean parameter which is TRUE if device setting is set to return low-accuracy network-based location. * @param {Function} errorCallback - The callback which will be called when the operation encounters an error. * This callback function is passed a single string parameter containing the error message. */ Diagnostic.isNetworkLocationEnabled = function(successCallback, errorCallback) { if(cordova.plugins.diagnostic.location){ cordova.plugins.diagnostic.location.isNetworkLocationEnabled.apply(this, arguments); }else{ throw "Diagnostic Location module is not installed"; } }; /** * Returns the current location mode setting for the device. * * @param {Function} successCallback - The callback which will be called when the operation is successful. * This callback function is passed a single string parameter defined as a constant in `cordova.plugins.diagnostic.locationMode`. * @param {Function} errorCallback - The callback which will be called when the operation encounters an error. * This callback function is passed a single string parameter containing the error message. */ Diagnostic.getLocationMode = function(successCallback, errorCallback) { if(cordova.plugins.diagnostic.location){ cordova.plugins.diagnostic.location.getLocationMode.apply(this, arguments); }else{ throw "Diagnostic Location module is not installed"; } }; /** * Switches to the Location page in the Settings app */ Diagnostic.switchToLocationSettings = function() { if(cordova.plugins.diagnostic.location){ cordova.plugins.diagnostic.location.switchToLocationSettings.apply(this, arguments); }else{ throw "Diagnostic Location module is not installed"; } }; /** * Requests location authorization for the application. * Note: this is intended for Android 6 / API 23 and above. Calling on Android 5 / API 22 and below will have no effect as the permissions are already granted at installation time. * @param {Function} successCallback - function to call on successful request for runtime permissions. * This callback function is passed a single string parameter which defines the resulting authorisation status as a value in cordova.plugins.diagnostic.permissionStatus. * @param {Function} errorCallback - function to call on failure to request authorisation. * @param {String} mode - (optional) location authorization mode as a constant in `cordova.plugins.diagnostic.locationAuthorizationMode`. * If not specified, defaults to `cordova.plugins.diagnostic.locationAuthorizationMode.WHEN_IN_USE`. * @param {String} accuracy - (optional) requested location accuracy as a constant in in `cordova.plugins.diagnostic.locationAccuracyAuthorization`. * If not specified, defaults to `cordova.plugins.diagnostic.locationAccuracyAuthorization.FULL`. */ Diagnostic.requestLocationAuthorization = function(successCallback, errorCallback, mode, accuracy){ if(cordova.plugins.diagnostic.location){ cordova.plugins.diagnostic.location.requestLocationAuthorization.apply(this, arguments); }else{ throw "Diagnostic Location module is not installed"; } }; /** * Returns the location authorization status for the application. * Note: this is intended for Android 6 / API 23 and above. Calling on Android 5 / API 22 and below will always return GRANTED status as permissions are already granted at installation time. * @param {Function} successCallback - function to call on successful request for runtime permissions status. * This callback function is passed a single string parameter which defines the current authorisation status as a value in cordova.plugins.diagnostic.permissionStatus. * @param {Function} errorCallback - function to call on failure to request authorisation status. */ Diagnostic.getLocationAuthorizationStatus = function(successCallback, errorCallback){ if(cordova.plugins.diagnostic.location){ cordova.plugins.diagnostic.location.getLocationAuthorizationStatus.apply(this, arguments); }else{ throw "Diagnostic Location module is not installed"; } }; /** * Returns the individual location authorization status for each type of location access (FINE, COARSE and BACKGROUND) * Note: this is intended for Android 6 / API 23 and above. Calling on Android 5 / API 22 and below will always return GRANTED status as permissions are already granted at installation time. * @param {Function} successCallback - function to call on successful request for runtime permissions statuses. * This callback function is passed a single string parameter which defines the current authorisation status as a value in cordova.plugins.diagnostic.permissionStatus. * @param {Function} errorCallback - function to call on failure to request authorisation status. */ Diagnostic.getLocationAuthorizationStatuses = function(successCallback, errorCallback){ if(cordova.plugins.diagnostic.location){ cordova.plugins.diagnostic.location.getLocationAuthorizationStatuses.apply(this, arguments); }else{ throw "Diagnostic Location module is not installed"; } }; /** * Checks if the application is authorized to use location. * Note: this is intended for Android 6 / API 23 and above. Calling on Android 5 / API 22 and below will always return TRUE as permissions are already granted at installation time. * @param {Function} successCallback - function to call on successful request for runtime permissions status. * This callback function is passed a single boolean parameter which is TRUE if the app currently has runtime authorisation to use location. * @param {Function} errorCallback - function to call on failure to request authorisation status. */ Diagnostic.isLocationAuthorized = function(successCallback, errorCallback){ if(cordova.plugins.diagnostic.location){ cordova.plugins.diagnostic.location.isLocationAuthorized.apply(this, arguments); }else{ throw "Diagnostic Location module is not installed"; } }; /** * Registers a function to be called when a change in Location state occurs. * On Android, this occurs when the Location Mode is changed. * Pass in a falsey value to de-register the currently registered function. * * @param {Function} successCallback - The callback which will be called when the Location state changes. * This callback function is passed a single string parameter defined as a constant in `cordova.plugins.diagnostic.locationMode`. */ Diagnostic.registerLocationStateChangeHandler = function(successCallback) { if(cordova.plugins.diagnostic.location){ cordova.plugins.diagnostic.location.registerLocationStateChangeHandler.apply(this, arguments); }else{ throw "Diagnostic Location module is not installed"; } }; /** * Returns the location accuracy authorization for the application. * Will invoke the error callback if location permission is not yet granted. * * @param {Function} successCallback - The callback which will be called when operation is successful. * This callback function is passed a single string parameter which indicates the location accuracy authorization as a constant in `cordova.plugins.diagnostic.locationAccuracyAuthorization`. * Possible values are: * `cordova.plugins.diagnostic.locationAccuracyAuthorization.FULL` * `cordova.plugins.diagnostic.locationAccuracyAuthorization.REDUCED` * @param {Function} errorCallback - The callback which will be called when operation encounters an error. * This callback function is passed a single string parameter containing the error message. */ Diagnostic.getLocationAccuracyAuthorization = function(successCallback, errorCallback){ if(cordova.plugins.diagnostic.location){ cordova.plugins.diagnostic.location.getLocationAccuracyAuthorization.apply(this, arguments); }else{ throw "Diagnostic Location module is not installed"; } }; /************ * WiFi * ************/ /** * Checks if Wifi is enabled. * On Android this returns true if the WiFi setting is set to enabled. * * @param {Function} successCallback - The callback which will be called when the operation is successful. * This callback function is passed a single boolean parameter which is TRUE if WiFi is enabled. * @param {Function} errorCallback - The callback which will be called when the operation encounters an error. * This callback function is passed a single string parameter containing the error message. */ Diagnostic.isWifiAvailable = Diagnostic.isWifiEnabled = function(successCallback, errorCallback) { if(cordova.plugins.diagnostic.wifi){ cordova.plugins.diagnostic.wifi.isWifiAvailable.apply(this, arguments); }else{ throw "Diagnostic Wifi module is not installed"; } }; /** * Switches to the WiFi page in the Settings app */ Diagnostic.switchToWifiSettings = function() { if(cordova.plugins.diagnostic.wifi){ cordova.plugins.diagnostic.wifi.switchToWifiSettings.apply(this, arguments); }else{ throw "Diagnostic Wifi module is not installed"; } }; /** * Enables/disables WiFi on the device. * * @param {Function} successCallback - function to call on successful setting of WiFi state * @param {Function} errorCallback - function to call on failure to set WiFi state. * This callback function is passed a single string parameter containing the error message. * @param {Boolean} state - WiFi state to set: TRUE for enabled, FALSE for disabled. */ Diagnostic.setWifiState = function(successCallback, errorCallback, state) { if(cordova.plugins.diagnostic.wifi){ cordova.plugins.diagnostic.wifi.setWifiState.apply(this, arguments); }else{ throw "Diagnostic Wifi module is not installed"; } }; /************ * Camera * ************/ /** * Checks if camera is usable: both present and authorised for use. * * @param {Object} params - (optional) parameters: * - {Function} successCallback - The callback which will be called when the operation is successful. * This callback function is passed a single boolean parameter which is TRUE if camera is present and authorized for use. * - {Function} errorCallback - The callback which will be called when the operation encounters an error. * This callback function is passed a single string parameter containing the error message. * - {Boolean} externalStorage - (Android only) If true, checks permission for READ_EXTERNAL_STORAGE in addition to CAMERA run-time permission. * cordova-plugin-camera@2.2+ requires both of these permissions. Defaults to true. */ Diagnostic.isCameraAvailable = function(params) { if(cordova.plugins.diagnostic.camera){ cordova.plugins.diagnostic.camera.isCameraAvailable.apply(this, arguments); }else{ throw "Diagnostic Camera module is not installed"; } }; /** * Checks if camera hardware is present on device. * * @param {Function} successCallback - The callback which will be called when the operation is successful. * This callback function is passed a single boolean parameter which is TRUE if camera is present * @param {Function} errorCallback - The callback which will be called when the operation encounters an error. * This callback function is passed a single string parameter containing the error message. */ Diagnostic.isCameraPresent = function(successCallback, errorCallback) { if(cordova.plugins.diagnostic.camera){ cordova.plugins.diagnostic.camera.isCameraPresent.apply(this, arguments); }else{ throw "Diagnostic Camera module is not installed"; } }; /** * Requests authorisation for runtime permissions to use the camera. * Note: this is intended for Android 6 / API 23 and above. Calling on Android 5 / API 22 and below will have no effect as the permissions are already granted at installation time. * @param {Object} params - (optional) parameters: * - {Function} successCallback - function to call on successful request for runtime permissions. * This callback function is passed a single string parameter which defines the resulting authorisation status as a value in cordova.plugins.diagnostic.permissionStatus. * - {Function} errorCallback - function to call on failure to request authorisation. * - {Boolean} externalStorage - (Android only) If true, requests permission for READ_EXTERNAL_STORAGE in addition to CAMERA run-time permission. * cordova-plugin-camera@2.2+ requires both of these permissions. Defaults to true. */ Diagnostic.requestCameraAuthorization = function(params){ if(cordova.plugins.diagnostic.camera){ cordova.plugins.diagnostic.camera.requestCameraAuthorization.apply(this, arguments); }else{ throw "Diagnostic Camera module is not installed"; } }; /** * Returns the authorisation status for runtime permissions to use the camera. * Note: this is intended for Android 6 / API 23 and above. Calling on Android 5 / API 22 and below will always return GRANTED status as permissions are already granted at installation time. * @param {Object} params - (optional) parameters: * - {Function} successCallback - function to call on successful request for runtime permissions status. * This callback function is passed a single string parameter which defines the current authorisation status as a value in cordova.plugins.diagnostic.permissionStatus. * - {Function} errorCallback - function to call on failure to request authorisation status. * - {Boolean} externalStorage - (Android only) If true, checks permission for READ_EXTERNAL_STORAGE in addition to CAMERA run-time permission. * cordova-plugin-camera@2.2+ requires both of these permissions. Defaults to true. */ Diagnostic.getCameraAuthorizationStatus = function(params){ if(cordova.plugins.diagnostic.camera){ cordova.plugins.diagnostic.camera.getCameraAuthorizationStatus.apply(this, arguments); }else{ throw "Diagnostic Camera module is not installed"; } }; /** * Returns the individual authorisation statuses for runtime permissions to use the camera. * Note: this is intended for Android 6 / API 23 and above. Calling on Android 5 / API 22 and below will always return GRANTED status as permissions are already granted at installation time. * @param {Object} params - (optional) parameters: * - {Function} successCallback - function to call on successful request for runtime permission status. * This callback function is passed a single object parameter where each key indicates the permission name and the value defines the current authorisation status as a value in cordova.plugins.diagnostic.permissionStatus. * - {Function} errorCallback - function to call on failure to request authorisation status. * - {Boolean} storage - (Android only) If true, queries storage permissions in addition to CAMERA run-time permission. * On Android 13+, storage permissions are READ_MEDIA_IMAGES and READ_MEDIA_VIDEO. On Android 9-12, storage permission is READ_EXTERNAL_STORAGE. * cordova-plugin-camera requires both storage and camera permissions. * Defaults to true. */ Diagnostic.getCameraAuthorizationStatuses = function(params){ if(cordova.plugins.diagnostic.camera){ cordova.plugins.diagnostic.camera.getCameraAuthorizationStatuses.apply(this, arguments); }else{ throw "Diagnostic Camera module is not installed"; } }; /** * Checks if the application is authorized to use the camera. * Note: this is intended for Android 6 / API 23 and above. Calling on Android 5 / API 22 and below will always return TRUE as permissions are already granted at installation time. * @param {Object} params - (optional) parameters: * - {Function} successCallback - function to call on successful request for runtime permissions status. * This callback function is passed a single boolean parameter which is TRUE if the app currently has runtime authorisation to use location. * - {Function} errorCallback - function to call on failure to request authorisation status. * - {Boolean} externalStorage - (Android only) If true, checks permission for READ_EXTERNAL_STORAGE in addition to CAMERA run-time permission. * cordova-plugin-camera@2.2+ requires both of these permissions. Defaults to true. */ Diagnostic.isCameraAuthorized = function(params){ if(cordova.plugins.diagnostic.camera){ cordova.plugins.diagnostic.camera.isCameraAuthorized.apply(this, arguments); }else{ throw "Diagnostic Camera module is not installed"; } }; /********************** * External storage * **********************/ /** * Reque