Last active
September 10, 2016 17:13
-
-
Save roblav96/2e32a742ec0b4b7b492d22287c1b4839 to your computer and use it in GitHub Desktop.
Example of platform specific ng2 injectable services in NativeScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| import * as application from "application" | |
| import {Injectable} from "@angular/core" | |
| function permissionResult(permission: string, type: string): boolean { | |
| if (!android.support || !android.support.v4 || !android.support.v4.content || !android.support.v4.content.ContextCompat || !android.support.v4.content.ContextCompat.checkSelfPermission) { | |
| let result: boolean = (type == 'PERMISSION_GRANTED') ? true : false | |
| return result | |
| } | |
| let manifest: string = android.Manifest.permission[permission] | |
| let result: boolean = ( | |
| android.content.pm.PackageManager[type] | |
| == | |
| android.support.v4.content.ContextCompat.checkSelfPermission(application.android.foregroundActivity, manifest) | |
| ) | |
| if (type == 'PERMISSION_DENIED') { | |
| let rational: boolean = application.android.foregroundActivity.shouldShowRequestPermissionRationale(manifest) | |
| return result && !rational | |
| } else { | |
| return result | |
| } | |
| } | |
| function permissionResults(permissions: any, type: string): boolean { | |
| let result: boolean = true | |
| let i: number, len: number = permissions.length | |
| for (i = 0; i < len; i++) { | |
| if (!permissionResult(permissions[i], type)) { | |
| result = false | |
| break | |
| } | |
| } | |
| return result | |
| } | |
| function buildRequests(permissions: any): Promise<boolean> { | |
| return new Promise(function(resolve): void { | |
| let perms: Array<any> = [] | |
| let i: number, len: number = permissions.length | |
| for (i = 0; i < len; i++) { | |
| perms.push(android.Manifest.permission[permissions[i]]) | |
| } | |
| let reqid: number = Math.floor(Math.random() * 999) | |
| function onPermissionsEvent(args: any): void { | |
| if (args.requestCode == reqid) { | |
| let good: boolean = true | |
| let i: number, len: number = args.permissions.length | |
| for (i = 0; i < len; i++) { | |
| if (args.grantResults[i] != android.content.pm.PackageManager.PERMISSION_GRANTED) { | |
| good = false | |
| break | |
| } | |
| } | |
| // removeEventListener to reduce memory usage since it doesnt need to listen anymore | |
| application.android.removeEventListener(application.AndroidApplication.activityRequestPermissionsEvent, onPermissionsEvent) | |
| resolve(good) | |
| } | |
| } | |
| application.android.addEventListener(application.AndroidApplication.activityRequestPermissionsEvent, onPermissionsEvent) | |
| android.support.v4.app.ActivityCompat.requestPermissions(application.android.foregroundActivity, perms, reqid) | |
| }) | |
| } | |
| @Injectable() | |
| export class Permissions { | |
| isContactsAuthorized(): boolean { | |
| return permissionResult('READ_CONTACTS', 'PERMISSION_GRANTED') | |
| } | |
| isContactsDenied(): boolean { | |
| return permissionResult('READ_CONTACTS', 'PERMISSION_DENIED') | |
| } | |
| requestContactsAuthorization(): Promise<boolean> { | |
| if (this.isContactsAuthorized() == true) { | |
| return Promise.resolve(true) | |
| } else { | |
| return buildRequests(['READ_CONTACTS']) | |
| } | |
| } | |
| isSMSAuthorized(): boolean { | |
| return permissionResults([ | |
| 'SEND_SMS', | |
| 'RECEIVE_SMS', | |
| 'READ_SMS', | |
| 'RECEIVE_MMS', | |
| 'RECEIVE_WAP_PUSH', | |
| ], 'PERMISSION_GRANTED') | |
| } | |
| isSMSDenied(): boolean { | |
| return permissionResults([ | |
| 'SEND_SMS', | |
| 'RECEIVE_SMS', | |
| 'READ_SMS', | |
| 'RECEIVE_MMS', | |
| 'RECEIVE_WAP_PUSH', | |
| ], 'PERMISSION_DENIED') | |
| } | |
| requestSMSAuthorization(): Promise<boolean> { | |
| if (this.isSMSAuthorized() == true) { | |
| return Promise.resolve(true) | |
| } else { | |
| return buildRequests([ | |
| 'SEND_SMS', | |
| 'RECEIVE_SMS', | |
| 'READ_SMS', | |
| 'RECEIVE_MMS', | |
| 'RECEIVE_WAP_PUSH', | |
| ]) | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| import {confirm} from "ui/dialogs" | |
| export class Permissions { | |
| public IOS_OVER_HERE: boolean = true | |
| private addressBook: any = null | |
| isContactsAuthorized(): boolean { | |
| return ABAddressBookGetAuthorizationStatus() == ABAuthorizationStatus.kABAuthorizationStatusAuthorized | |
| } | |
| isContactsDenied(): boolean { | |
| let status: any = ABAddressBookGetAuthorizationStatus() | |
| let denied: boolean = ( | |
| status == ABAuthorizationStatus.kABAuthorizationStatusDenied | |
| || | |
| status == ABAuthorizationStatus.kABAuthorizationStatusRestricted | |
| ) | |
| return denied | |
| } | |
| requestContactsAuthorization(): Promise<boolean> { | |
| let status: any = ABAddressBookGetAuthorizationStatus() | |
| if (status == ABAuthorizationStatus.kABAuthorizationStatusAuthorized) { | |
| return Promise.resolve(true) | |
| } else { | |
| if (!this.addressBook) { | |
| this.addressBook = ABAddressBookCreateWithOptions(null, null) | |
| } | |
| return new Promise((resolve) => { | |
| ABAddressBookRequestAccessWithCompletion(this.addressBook, function(status: boolean, error: NSError): void { | |
| if (error) { | |
| resolve(false) | |
| } else { | |
| resolve(status) | |
| } | |
| }) | |
| }) | |
| } | |
| } | |
| isPhotosAuthorized(): boolean { | |
| return PHPhotoLibrary.authorizationStatus() == PHAuthorizationStatus.PHAuthorizationStatusAuthorized | |
| } | |
| isPhotosDenied(): boolean { | |
| let status: any = PHPhotoLibrary.authorizationStatus() | |
| let denied: boolean = ( | |
| status == PHAuthorizationStatus.PHAuthorizationStatusDenied | |
| || | |
| status == PHAuthorizationStatus.PHAuthorizationStatusRestricted | |
| ) | |
| return denied | |
| } | |
| requestPhotosAuthorization(): Promise<boolean> { | |
| return new Promise(function(resolve) { | |
| PHPhotoLibrary.requestAuthorization(function(status: number): void { | |
| let authed: boolean = (status == PHAuthorizationStatus.PHAuthorizationStatusAuthorized) | |
| resolve(authed) | |
| }) | |
| }) | |
| } | |
| isSMSAuthorized(): boolean { | |
| return true | |
| } | |
| isSMSDenied(): boolean { | |
| return false | |
| } | |
| requestSMSAuthorization(): Promise<boolean> { | |
| return Promise.resolve(true) | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import {Injectable} from "@angular/core" | |
| @Injectable() | |
| export declare class Permissions { | |
| isContactsAuthorized(): boolean | |
| isContactsDenied(): boolean | |
| requestContactsAuthorization(): Promise<boolean> | |
| isPhotosAuthorized(): boolean | |
| isPhotosDenied(): boolean | |
| requestPhotosAuthorization(): Promise<boolean> | |
| isSMSAuthorized(): boolean | |
| isSMSDenied(): boolean | |
| requestSMSAuthorization(): Promise<boolean> | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment