From 47a9b34ea27edccf816a69ddfd7fea597c36e920 Mon Sep 17 00:00:00 2001 From: Ibrahim Hadeed Date: Fri, 26 Aug 2016 08:06:04 -0400 Subject: [PATCH] feat(diagnostic): add full plugin functionality (#424) * feat(diagnostic): add full plugin functionality closes #224 * docs(): calendar methods (#476) * feat(diag): add constants --- src/plugins/diagnostic.ts | 427 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 407 insertions(+), 20 deletions(-) diff --git a/src/plugins/diagnostic.ts b/src/plugins/diagnostic.ts index 1c68370dd..fbb031067 100644 --- a/src/plugins/diagnostic.ts +++ b/src/plugins/diagnostic.ts @@ -1,4 +1,4 @@ -import { Cordova, Plugin } from './plugin'; +import {Cordova, Plugin, CordovaProperty} from './plugin'; /** * @name Diagnostic @@ -8,6 +8,24 @@ import { Cordova, Plugin } from './plugin'; * @usage * ```typescript * import { Diagnostic } from 'ionic-native'; + * + * let successCallback = (isAvailable) => { console.log('Is available? ' + isAvailable); }; + * let errorCallback = (e) => console.error(e); + * + * Diagnostic.isCameraAvailable().then(successCallback).catch(errorCallback); + * + * Diagnostic.isBluetoothAvailable().then(successCallback, errorCallback); + * + * + * Diagnostic.getBluetoothState() + * .then((state) => { + * if(state == Diagnostic.bluetoothStates.POWERED_ON){ + * // do something + * } else { + * // do something else + * } + * }).catch(e => console.error(e)); + * * ``` */ @Plugin({ @@ -16,6 +34,73 @@ import { Cordova, Plugin } from './plugin'; repo: 'https://github.com/dpa99c/cordova-diagnostic-plugin' }) export class Diagnostic { + + static permission = { + 'READ_CALENDAR': 'READ_CALENDAR', + 'WRITE_CALENDAR': 'WRITE_CALENDAR', + 'CAMERA': 'CAMERA', + 'READ_CONTACTS': 'READ_CONTACTS', + 'WRITE_CONTACTS': 'WRITE_CONTACTS', + 'GET_ACCOUNTS': 'GET_ACCOUNTS', + 'ACCESS_FINE_LOCATION': 'ACCESS_FINE_LOCATION', + 'ACCESS_COARSE_LOCATION': 'ACCESS_COARSE_LOCATION', + 'RECORD_AUDIO': 'RECORD_AUDIO', + 'READ_PHONE_STATE': 'READ_PHONE_STATE', + 'CALL_PHONE': 'CALL_PHONE', + 'ADD_VOICEMAIL': 'ADD_VOICEMAIL', + 'USE_SIP': 'USE_SIP', + 'PROCESS_OUTGOING_CALLS': 'PROCESS_OUTGOING_CALLS', + 'READ_CALL_LOG': 'READ_CALL_LOG', + 'WRITE_CALL_LOG': 'WRITE_CALL_LOG', + 'SEND_SMS': 'SEND_SMS', + 'RECEIVE_SMS': 'RECEIVE_SMS', + 'READ_SMS': 'READ_SMS', + 'RECEIVE_WAP_PUSH': 'RECEIVE_WAP_PUSH', + 'RECEIVE_MMS': 'RECEIVE_MMS', + 'WRITE_EXTERNAL_STORAGE': 'WRITE_EXTERNAL_STORAGE', + 'READ_EXTERNAL_STORAGE': 'READ_EXTERNAL_STORAGE', + 'BODY_SENSORS': 'BODY_SENSORS' + }; + + static permissionStatus = { + 'NOT_REQUESTED': 'not_determined', + 'DENIED': 'denied', + 'RESTRICTED': 'restricted', + 'GRANTED': 'authorized', + 'GRANTED_WHEN_IN_USE': 'authorized_when_in_use' + }; + + static locationAuthorizationMode = { + 'ALWAYS': 'always', + 'WHEN_IN_USE': 'when_in_use' + }; + + static permissionGroups = { + 'GRANTED': 'GRANTED', + 'DENIED': 'DENIED', + 'NOT_REQUESTED': 'NOT_REQUESTED', + 'DENIED_ALWAYS': 'DENIED_ALWAYS' + }; + + static locationMode = { + 'HIGH_ACCURACY': 'high_accuracy', + 'DEVICE_ONLY': 'device_only', + 'BATTERY_SAVING': 'battery_saving', + 'LOCATION_OFF': 'location_off' + }; + + static bluetoothState = { + 'UNKNOWN': 'unknown', + 'RESETTING': 'resetting', // iOS + 'UNSUPPORTED': 'unsupported', // iOS + 'UNAUTHORIZED': 'unauthorized', // iOS + 'POWERED_OFF': 'powered_off', + 'POWERED_ON': 'powered_on', + 'POWERING_OFF': 'powering_off', + 'POWERING_ON': 'powering_on' + }; + + /** * Checks if app is able to access device location. */ @@ -44,13 +129,56 @@ export class Diagnostic { static isBluetoothAvailable(): Promise { return; } /** - * Returns the location authorization status for the application. - * Note for Android: 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. - * - * mode - (iOS-only / optional) location authorization mode: "always" or "when_in_use". If not specified, defaults to "when_in_use". + * Displays the device location settings to allow user to enable location services/change location mode. */ - @Cordova() - static requestLocationAuthorization(mode?: string): Promise { return; } + @Cordova({sync: true, platforms: ['Android', 'Windows 10']}) + static switchToLocationSettings(): void { } + + /** + * Displays mobile settings to allow user to enable mobile data. + */ + @Cordova({sync: true, platforms: ['Android', 'Windows 10']}) + static switchToMobileDataSettings(): void { } + + /** + * Displays Bluetooth settings to allow user to enable Bluetooth. + */ + @Cordova({sync: true, platforms: ['Android', 'Windows 10']}) + static switchToBluetoothSettings(): void { } + + /** + * Displays WiFi settings to allow user to enable WiFi. + */ + @Cordova({sync: true, platforms: ['Android', 'Windows 10']}) + static switchToWifiSettings(): void { } + + /** + * Returns true if the WiFi setting is set to enabled, and is the same as `isWifiAvailable()` + */ + @Cordova({platforms: ['Android', 'Windows 10']}) + static isWifiEnabled(): Promise { return; } + + /** + * Enables/disables WiFi on the device. + * Requires `ACCESS_WIFI_STATE` and `CHANGE_WIFI_STATE` permissions on Android + * @param state {boolean} + */ + @Cordova({callbackOrder: 'reverse', platforms: ['Android', 'Windows 10']}) + static setWifiState(state: boolean): Promise { return; } + + /** + * Enables/disables Bluetooth on the device. + * Requires `BLUETOOTH` and `BLUETOOTH_ADMIN` permissions on Android + * @param state {boolean} + */ + @Cordova({callbackOrder: 'reverse', platforms: ['Android', 'Windows 10']}) + static setBluetoothState(state: boolean): Promise { return; } + + /** + * Returns true if the device setting for location is on. On Android this returns true if Location Mode is switched on. On iOS this returns true if Location Services is switched on. + */ + @Cordova({platforms: ['Android', 'iOS']}) + static isLocationEnabled(): Promise {return; } /** * Checks if the application is authorized to use location. @@ -59,50 +187,309 @@ export class Diagnostic { @Cordova() static isLocationAuthorized(): Promise { return; } + /** + * Returns the location authorization status for the application. + */ + @Cordova({platforms: ['Android', 'iOS']}) + static getLocationAuthorizationStatus(): Promise {return; } + + /** + * Returns the location authorization status for the application. + * Note for Android: 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. + * + * mode - (iOS-only / optional) location authorization mode: "always" or "when_in_use". If not specified, defaults to "when_in_use". + */ + @Cordova({platforms: ['Android', 'iOS']}) + static requestLocationAuthorization(mode?: string): Promise { return; } + /** * Checks if camera hardware is present on device. */ - @Cordova() + @Cordova({platforms: ['Android', 'iOS']}) static isCameraPresent(): Promise { return; } /** * Checks if the application is authorized to use the camera. * Note for Android: 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. */ - @Cordova() + @Cordova({platforms: ['Android', 'iOS']}) static isCameraAuthorized(): Promise { return; } + + /** + * Returns the camera authorization status for the application. + */ + @Cordova({platforms: ['Android', 'iOS']}) + static getCameraAuthorizationStatus(): Promise { return; } + + /** + * Requests camera authorization for the application. + */ + @Cordova({platforms: ['Android', 'iOS']}) + static requestCameraAuthorization(): Promise { return; } + + /** + * Checks if the application is authorized to use the microphone. + */ + @Cordova({platforms: ['Android', 'iOS']}) + static isMicrophoneAuthorized(): Promise { return; } + + /** + * Returns the microphone authorization status for the application. + */ + @Cordova({platforms: ['Android', 'iOS']}) + static getMicrophoneAuthorizationStatus(): Promise { return; } + + /** + * Requests microphone authorization for the application. + */ + @Cordova({platforms: ['Android', 'iOS']}) + static requestMicrophoneAuthorization(): Promise { return; } + + /** + * Checks if the application is authorized to use contacts (address book). + */ + @Cordova({platforms: ['Android', 'iOS']}) + static isContactsAuthorized(): Promise { return; } + + /** + * Returns the contacts authorization status for the application. + */ + @Cordova({platforms: ['Android', 'iOS']}) + static getContactsAuthroizationStatus(): Promise { return; } + + /** + * Requests contacts authorization for the application. + */ + @Cordova({platforms: ['Android', 'iOS']}) + static requestContactsAuthorization(): Promise { return; } + + /** + * Checks if the application is authorized to use the calendar. + * + * Notes for Android: + * - 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. + * + * Notes for iOS: + * - This relates to Calendar Events (not Calendar Reminders) + */ + @Cordova({platforms: ['Android', 'iOS']}) + static isCalendarAuthorized(): Promise { return; } + + /** + * Returns the calendar authorization status for the application. + * + * Notes for Android: + * - 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. + * + * Notes for iOS: + * - This relates to Calendar Events (not Calendar Reminders) + * + */ + @Cordova({platforms: ['Android', 'iOS']}) + static getCalendarAuthorizationStatus(): Promise { return; } + + /** + * Requests calendar authorization for the application. + * + * Notes for iOS: + * - Should only be called if authorization status is NOT_DETERMINED. Calling it when in any other state will have no effect and just return the current authorization status. + * - This relates to Calendar Events (not Calendar Reminders) + * + * Notes for Android: + * - 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. + * - This requests permission for `READ_CALENDAR` run-time permission + * - Required permissions must be added to `AndroidManifest.xml` as appropriate - see Android permissions: `READ_CALENDAR`, `WRITE_CALENDAR` + * + */ + @Cordova({platforms: ['Android', 'iOS']}) + static requestCalendarAuthorization(): Promise { return; } + + /** + * 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. This works only on iOS 8+ - iOS 7 and below will invoke the errorCallback. + */ + @Cordova({platforms: ['Android', 'iOS']}) + static switchToSettings(): Promise {return; } + + /** + * Returns the state of Bluetooth on the device. + */ + @Cordova({platforms: ['Android', 'iOS']}) + static getBluetoothState(): Promise {return; } + + /** + * Registers a function to be called when a change in Bluetooth state occurs. + * @param handler + */ + @Cordova({platforms: ['Android', 'iOS'], sync: true}) + static registerBluetoothStateChangeHandler(handler: Function): void { } + + /** + * Registers a function to be called when a change in Location state occurs. + * @param handler + */ + @Cordova({platforms: ['Android', 'iOS'], sync: true}) + static registerLocationStateChangeHandler(handler: Function): void { } + + /** + * 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. + */ + @Cordova({platforms: ['Android']}) + static isGpsLocationAvailable(): Promise {return; } + /** * Checks if location mode 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) */ - @Cordova() + @Cordova({platforms: ['Android']}) static isGpsLocationEnabled(): Promise { return; } + /** + * 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. + */ + @Cordova({platforms: ['Android']}) + static isNetworkLocationAvailable(): Promise {return; } + /** * Checks if location mode 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) */ - @Cordova() + @Cordova({platforms: ['Android']}) static isNetworkLocationEnabled(): Promise { return; } /** - * Checks if remote (push) notifications are enabled. - * On iOS 8+, returns true if app is registered for remote notifications AND "Allow Notifications" switch is ON AND alert style is not set to "None" (i.e. "Banners" or "Alerts"). - * On iOS <=7, returns true if app is registered for remote notifications AND alert style is not set to "None" (i.e. "Banners" or "Alerts") - same as isRegisteredForRemoteNotifications(). + * Returns the current location mode setting for the device. */ - @Cordova() - static isRemoteNotificationsEnabled(): Promise { return; } + @Cordova({platforms: ['Android']}) + static getLocationMode(): Promise {return; } + + /** + * 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 permission + */ + @Cordova({platforms: ['Android'], callbackOrder: 'reverse'}) + static getPermissionAuthorizationStatus(permission: any): Promise {return; } + + /** + * 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 permissions + */ + @Cordova({platforms: ['Android'], callbackOrder: 'reverse'}) + static getPermissionsAuthorizationStatus(permissions: any[]): Promise {return; } + + /** + * 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 permission + */ + @Cordova({platforms: ['Android'], callbackOrder: 'reverse'}) + static requestRuntimePermission(permission: any): Promise {return; } + + /** + * 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 always return GRANTED status as permissions are already granted at installation time. + * @param permissions + */ + @Cordova({platforms: ['Android'], callbackOrder: 'reverse'}) + static requestRuntimePermissions(permissions: any[]): Promise {return; } + + /** + * Checks if the device setting for Bluetooth is switched on. + * This requires `BLUETOOTH` permission on Android + */ + @Cordova({platforms: ['Android']}) + static isBluetoothEnabled(): Promise {return; } + + /** + * Checks if the device has Bluetooth capabilities. + */ + @Cordova({platforms: ['Android']}) + static hasBluetoothSupport(): Promise {return; } + + /** + * Checks if the device has Bluetooth Low Energy (LE) capabilities. + */ + @Cordova({platforms: ['Android']}) + static hasBluetoothLESupport(): Promise {return; } + + /** + * Checks if the device supports Bluetooth Low Energy (LE) Peripheral mode. + */ + @Cordova({platforms: ['Android']}) + static hasBluetoothLEPeripheralSupport(): Promise {return; } + + /** + * Checks if the application is authorized to use the Camera Roll in Photos app. + */ + @Cordova({platforms: ['iOS']}) + static isCameraRollAuthorized(): Promise {return; } + + /** + * Returns the authorization status for the application to use the Camera Roll in Photos app. + */ + @Cordova({platforms: ['iOS']}) + static getCameraRollAuthorizationStatus(): Promise {return; } + + /** + * Requests camera roll authorization for the application. Should only be called if authorization status is NOT_REQUESTED. Calling it when in any other state will have no effect. + */ + @Cordova({platforms: ['iOS']}) + static requestCameraRollAuthorization(): Promise {return; } + + /** + * Checks if remote (push) notifications are enabled. + */ + @Cordova({platforms: ['iOS']}) + static isRemoteNotificationsEnabled(): Promise {return; } /** * Indicates if the app is registered for remote (push) notifications on the device. - * On iOS 8+, returns true if the app is registered for remote notifications and received its device token, or false if registration has not occurred, has failed, or has been denied by the user. Note that user preferences for notifications in the Settings app will not affect this. - * On iOS <=7, returns true if app is registered for remote notifications AND alert style is not set to "None" (i.e. "Banners" or "Alerts") - same as isRemoteNotificationsEnabled(). */ - @Cordova() - static isRegisteredForRemoteNotifications(): Promise { return; } + @Cordova({platforms: ['iOS']}) + static isRegisteredForRemoteNotifications(): Promise {return; } + + /** + * Indicates the current setting of notification types for the app in the Settings app. + * Note: on iOS 8+, if "Allow Notifications" switch is OFF, all types will be returned as disabled. + */ + @Cordova({platforms: ['iOS']}) + static getRemoteNotificationTypes(): Promise {return; } + + /** + * Checks if the application is authorized to use reminders. + */ + @Cordova({platforms: ['iOS']}) + static isRemindersAuthorized(): Promise {return; } + + /** + * Returns the reminders authorization status for the application. + */ + @Cordova({platforms: ['iOS']}) + static getRemindersAuthorizationStatus(): Promise {return; } + + /** + * Requests reminders authorization for the application. + */ + @Cordova({platforms: ['iOS']}) + static requestRemindersAuthorization(): Promise {return; } + + /** + * Checks if the application is authorized for background refresh. + */ + @Cordova({platforms: ['iOS']}) + static isBackgroundRefreshAuthorized(): Promise {return; } + + /** + * Returns the background refresh authorization status for the application. + */ + @Cordova({platforms: ['iOS']}) + static getBackgroundRefreshStatus(): Promise {return; } }