All files / src/providers/music-controls-manager music-controls-manager.ts

100% Statements 42/42
100% Branches 24/24
100% Functions 10/10
100% Lines 40/40
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120              1x         33x 33x 33x 33x     1x 2x 2x 2x     1x 7x     5x       1x 2x     1x 1x       1x 1x                           1x     1x     1x 5x     5x 1x       5x 1x     5x   5x 1x       5x         1x   5x             25x 5x 2x 2x 2x               1x 1x                 1x  
import { Injectable } from '@angular/core';
import { MusicControls } from '@ionic-native/music-controls';
import { Events, Platform } from 'ionic-angular';
import { TrackerService } from '../tracker-service/tracker-service';
import { ISong } from '../../interfaces';
 
@Injectable()
export class MusicControlsManagerProvider {
 
    private currentSong: ISong;
    private isPlaying: boolean;
 
    constructor( private plt: Platform,
                 private musicControls: MusicControls,
                 private tracker: TrackerService,
                 private events: Events,
    ) {}
 
    public init ( currentSong: ISong, isPlaying: boolean ) {
        this.currentSong = currentSong;
        this.isPlaying = isPlaying;
        this.plt.ready().then( this.onPlatformReady.bind( this ) );
    }
 
    public updatePlayState( isPlaying: boolean ) {
        if ( this.plt.is( 'cordova' ) &&
            this.musicControls &&
            typeof this.musicControls !== 'undefined' ) {
            this.musicControls.updateIsPlaying( isPlaying );
        }
    }
 
    private onPlatformReady() {
        if ( this.plt.is( 'cordova' ) &&
            this.musicControls &&
            typeof this.musicControls !== 'undefined' ) {
            this.destroyMusicControls();
            this.createMusicControls( this.currentSong, this.isPlaying );
        }
    }
 
    private createMusicControls ( currentSong: ISong, isPlaying: boolean ) {
        this.musicControls.create( {
            album: 'Faubourg Simone Radio', // iOS only
            artist: currentSong.artist,
            cover: currentSong.cover.jpg,
            dismissable: true,
            hasClose: false, // show close button, optional, default: false
            hasNext: false, // show next button, optional, default: true
            hasPrev: false, // show previous button, optional, default: true
            hasScrubbing: false, // iOS only
            isPlaying,
            ticker: `# Faubourg Simone # ${ currentSong.title }`, // Android only
            track: currentSong.track
        } );
 
        this.musicControls.subscribe().subscribe( this.onMusicControlsEvent.bind( this ) );
 
        // activates the observable above
        this.musicControls.listen();
    }
 
    private onMusicControlsEvent ( action ) {
        const message = JSON.parse( action ).message;
 
        // Headset event headset-unplugged (Android only)
        if ( message === 'music-controls-pause' || message === 'music-controls-headset-unplugged' ) {
            this.events.publish( '[MusicControlsManager]pause' );
        }
 
        // Headset event headset-plugged (Android only)
        if ( message === 'music-controls-play' || message === 'music-controls-headset-plugged' ) {
            this.events.publish( '[MusicControlsManager]play' );
        }
 
        this.trackEventIfNeeded( message );
 
        if ( message === 'music-controls-destroy' ) {
            this.destroyMusicControls();
        }
 
        // External controls (iOS only)
        if ( message === 'music-controls-toggle-play-pause' ) {
            // TODO : how to know if we must call play or pause
        }
    }
 
    private trackEventIfNeeded( msg ) {
        // If it's one of those events, we track on the same way with just a different action parameter
        const eventsToTrack = [
            'pause',
            'play',
            'headset-unplugged',
            'headset-plugged',
            'media-button'
        ];
        const indexOfEvent = eventsToTrack.map( evtName => `music-controls-${ evtName }` ).indexOf( msg );
        if ( indexOfEvent !== -1 ) {
            const eventToTrackKey = eventsToTrack[ indexOfEvent ].replace( '-', '_' ).toUpperCase();
            const actionKey = `TRACKING.PLAYER.ACTION.${ eventToTrackKey }`;
            this.tracker.translateAndTrack(
                'TRACKING.PLAYER.CATEGORY',
                actionKey,
                'TRACKING.PLAYER.LABEL.MUSIC_CONTROLS'
            );
        }
    }
 
    private destroyMusicControls () {
        this.musicControls.destroy();
 
        // this.tracker.translateAndTrack(
        //     'TRACKING.PLAYER.CATEGORY',
        //     'TRACKING.PLAYER.ACTION.DESTROY',
        //     'TRACKING.PLAYER.LABEL.MUSIC_CONTROLS'
        // );
    }
 
}