import * as application from "tns-core-modules/application" ; import { LoginResponse } from './login-response' ; import { FacebookAccessToken } from "./facebook-access-token" ; const LOGIN_PERMISSIONS = [ "public_profile" , "email" ] ; export let onLoginCallback ; export let onLogoutCallback ; let androidApplication ; let _act ; let loginManager ; export function _registerLogoutCallback ( callback ) { onLogoutCallback = callback ; } export function init ( fbId ) { setAppId ( fbId ) ; androidApplication = application . android ; try { com . facebook . FacebookSdk . sdkInitialize ( androidApplication . context . getApplicationContext ( ) ) ; com . facebook . FacebookSdk . setAutoLogAppEventsEnabled ( true ) ; } catch ( e ) { console . log ( e ) ; } loginManager = com . facebook . login . LoginManager . getInstance ( ) ; let LogoutAccessTokenTracker = ( com . facebook . AccessTokenTracker as any ) . extend ( { onCurrentAccessTokenChanged : function ( oldToken , newToken ) { if ( oldToken != null && newToken == null && onLogoutCallback ) { onLogoutCallback ( ) ; } } } ) ; let accessTokenTracker = new LogoutAccessTokenTracker ( ) ; accessTokenTracker . startTracking ( ) ; } export function _registerLoginCallback ( callback ) { let onLoginCallback = com . facebook . CallbackManager . Factory . create ( ) ; _act = androidApplication . startActivity || androidApplication . foregroundActivity ; loginManager . registerCallback ( onLoginCallback , new com . facebook . FacebookCallback < any > ( { onSuccess : function ( result ) { let token = result . getAccessToken ( ) . getToken ( ) ; let loginResponse = new LoginResponse ( token ) ; callback ( null , loginResponse ) ; } , onCancel : function ( ) { callback ( new Error ( 'canceled' ) ) ; } , onError : function ( e ) { let errorMessage = "Error with Facebook" ; if ( e [ 'getErrorMessage' ] ) { errorMessage += ": " + e [ 'getErrorMessage' ] ( ) ; } else if ( e [ 'getErrorCode' ] ) { errorMessage += ": Code " + e [ 'getErrorCode' ] ( ) ; } else { errorMessage += ": " + e ; } callback ( new Error ( errorMessage ) ) ; } } ) ) ; let onActivityResult = ( args ) => { if ( onLoginCallback . onActivityResult ( args . requestCode , args . resultCode , args . intent ) ) { unsubscribe ( ) ; } } ; let unsubscribe = ( ) => { androidApplication . off ( application . AndroidApplication . activityResultEvent , onActivityResult ) ; } ; androidApplication . on ( application . AndroidApplication . activityResultEvent , onActivityResult ) ; } function setAppId ( fbAppId ) { com . facebook . FacebookSdk . setApplicationId ( fbAppId ) ; } export function requestPublishPermissions ( permissions , callback ) { _registerLoginCallback ( callback ) ; let javaPermissionsList = java . util . Arrays . asList ( permissions ) ; loginManager . logInWithPublishPermissions ( _act , javaPermissionsList ) ; } export function requestReadPermissions ( permissions , callback ) { _registerLoginCallback ( callback ) ; let javaPermissionsList = java . util . Arrays . asList ( permissions ) ; loginManager . logInWithReadPermissions ( _act , javaPermissionsList ) ; } export function login ( callback ) { requestReadPermissions ( LOGIN_PERMISSIONS , callback ) ; } export function getCurrentAccessToken ( ) { let sdkAccessToken = com . facebook . AccessToken . getCurrentAccessToken ( ) ; let accessToken = null ; if ( sdkAccessToken ) { accessToken = new FacebookAccessToken ( ) ; accessToken . accessToken = sdkAccessToken . getToken ( ) ; accessToken . userId = sdkAccessToken . getUserId ( ) ; accessToken . expirationDate = convertToISOStringDate ( sdkAccessToken . getExpires ( ) . getTime ( ) ) ; accessToken . refreshDate = convertToISOStringDate ( sdkAccessToken . getLastRefresh ( ) . getTime ( ) ) ; } return accessToken ; } export function logout ( callback ) { loginManager . logOut ( ) ; if ( callback ) { callback ( ) ; } } function convertToISOStringDate ( time ) { if ( time != null && ! isNaN ( time ) && isFinite ( time ) ) { return new Date ( time ) . toISOString ( ) ; } else { return "" ; } }