package com.agorask.map.location;

import android.app.Service;
import android.content.Intent;
import android.location.Location;
import android.os.IBinder;
import android.util.Log;

import com.agorask.map.location.AgoraskLocationSource;

/**
 * Created by nolitsou on 2/3/17.
 */

public class MockLocationService extends Service {
    private final static String TAG = "MockLocationService";

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        AgoraskLocationSource locationService = AgoraskLocationSource.getInstance();
        if (locationService != null) {
            mock(locationService, intent);
        } else {
            Log.w(TAG, "LocationSource not created yet");
            startLocationSource(intent);
        }
        return START_NOT_STICKY;
    }

    private void startLocationSource(final Intent intent) {
        final AgoraskLocationSource locationSource = AgoraskLocationSource.getInstance(this);
        locationSource.addServiceListener(new AgoraskLocationSource.LocationServiceCallbacks() {

            @Override
            public void onConnected() {
                mock(locationSource, intent);
            }

            @Override
            public void onConnectionSuspended() {

            }

            @Override
            public void onConnectionFailed() {

            }
        });

    }
    

    private void unmock(AgoraskLocationSource locationSource) {
        locationSource.setMockMode(false);
    }
    private void mock(AgoraskLocationSource locationSource, Intent intent) {
        double latitude = 0;
        double longitude = 0;
        String latitudeStr = intent.getStringExtra("latitude");
        String longitudeStr = intent.getStringExtra("longitude");
        Log.d(TAG, latitude + ", " + longitude);
        if (latitudeStr == null) {
            Log.e(TAG, "Missing intent extra 'latitude'");
            return;
        }
        if (longitudeStr == null) {
            Log.e(TAG, "Missing intent extra 'longitude'");
            return;
        }
        try {
            latitude = Double.parseDouble(latitudeStr);
        } catch(Exception e) {
            Log.e(TAG, "invalide value for 'latitude'");
            return;
        }
        try {
            longitude = Double.parseDouble(longitudeStr);
        } catch(Exception e) {
            Log.e(TAG, "invalide value for 'longitude'");
            return;
        }
        Location loc = new Location("mockedLocation");
        loc.setLatitude(latitude);
        loc.setLongitude(longitude);
        locationSource.setMockLocation(loc);
    }

    @Override
    public void onDestroy(){
        AgoraskLocationSource locationService = AgoraskLocationSource.getInstance();
        if (locationService != null) {
            unmock(locationService);
        }
        super.onDestroy();
    }
}
@@ -47,23 +47,95 @@
 oid.app.
-Service
+NotificationManager;%0Aimport android.app.Service;%0Aimport android.content.Context
 ;%0Aimport
@@ -239,72 +239,89 @@
 oid.
-util.Log;%0A%0Aimport com.agorask.map.location.AgoraskLocationSource
+support.v4.app.NotificationCompat;%0Aimport android.util.Log;%0A%0Aimport com.agorask.R
 ;%0A%0A/
@@ -475,16 +475,68 @@
 vice%22;%0A%0A
+    private final static int NOTIFICATION_ID = 64;%0A%0A
     @Ove
@@ -1706,24 +1706,52 @@
 ode(false);%0A
+        hideNotification();%0A
     %7D%0A    pr
@@ -2860,16 +2860,930 @@
 n(loc);%0A
+        showNotification();%0A    %7D%0A%0A    private void showNotification() %7B%0A        NotificationCompat.Builder mBuilder =%0A                new NotificationCompat.Builder(this)%0A                .setContentTitle(%22Mock location service enabled!%22)%0A                .setContentText(%22...%22)%0A                .setSmallIcon(R.drawable.common_google_signin_btn_icon_dark);%0A%0A        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);%0A        // Hide the notification after its selected%0A%0A        Log.d(TAG, %22Show notification%22);%0A        notificationManager.notify(NOTIFICATION_ID, mBuilder.build());%0A    %7D%0A%0A    private void hideNotification() %7B%0A        Log.d(TAG, %22Hiding notification%22);%0A        NotificationManager mNotificationManager =%0A                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);%0A        mNotificationManager.cancel(NOTIFICATION_ID);%0A%0A
     %7D%0A%0A 

Add notification when MockLocationService is running