package rs.elfak.mosis.marko.discounthunt;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class BackgroundService extends Service {
    private NotificationManager mNM;

    // Unique Identification Number for the Notification.
    // We use it on Notification start, and to cancel it.
    private int NOTIFICATION = R.string.background_service_started;

    public class LocalBinder extends Binder {
        BackgroundService getService() {
            return BackgroundService.this;
        }
    }

    @Override
    public void onCreate() {
        mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

        // Display a notification about us starting.  We put an icon in the status bar.
        showNotification();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i("LocalService", "Received start id " + startId + ": " + intent);
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        // Cancel the persistent notification.
        mNM.cancel(NOTIFICATION);

        // Tell the user we stopped.
        Toast.makeText(this, R.string.background_service_stopped, Toast.LENGTH_SHORT).show();
    }

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

    // This is the object that receives interactions from clients.  See
    // RemoteService for a more complete example.
    private final IBinder mBinder = new LocalBinder();

    /**
     * Show a notification while this service is running.
     */
    private void showNotification() {
        // In this sample, we'll use the same text for the ticker and the expanded notification
        CharSequence text = getText(R.string.background_service_started);

        // The PendingIntent to launch our activity if the user selects this notification
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                new Intent(this, SigninActivity.class), 0);

        // Set the info for the views that show in the notification panel.
        Notification notification = new Notification.Builder(this)
                .setTicker(text)  // the status text
                .setWhen(System.currentTimeMillis())  // the time stamp
                .setContentTitle(getText(R.string.background_service_label))  // the label of the entry
                .setContentText(text)  // the contents of the entry
                .setContentIntent(contentIntent)  // The intent to send when the entry is clicked
                .build();

        // Send the notification.
        mNM.notify(NOTIFICATION, notification);
    }
}

@@ -195,22 +195,95 @@
 content.
-Intent
+Context;%0Aimport android.content.Intent;%0Aimport android.location.LocationManager
 ;%0Aimport
@@ -388,56 +388,385 @@
 t;%0A%0A
-public class BackgroundService extends Service %7B
+import com.android.volley.Response;%0Aimport com.android.volley.VolleyError;%0A%0Aimport org.json.JSONException;%0Aimport org.json.JSONObject;%0A%0Aimport java.util.Timer;%0Aimport java.util.TimerTask;%0A%0Aimport rs.elfak.mosis.marko.discounthunt.api.endpoints.UserLocationChangeEndpoint;%0A%0Apublic class BackgroundService extends Service %7B%0A    private static final long REFRESH_INTERVAL = 20000;
 %0A   
@@ -1126,24 +1126,157 @@
      %7D%0A    %7D
+%0A    private final IBinder mBinder = new LocalBinder();%0A    private Timer refreshTimer;%0A    private CurrentLocation mCurrentLocation;
 %0A%0A    @Overr
@@ -1383,25 +1383,24 @@
 RVICE);%0A
-%0A
         
 // Displ
@@ -1395,113 +1395,107 @@
     
-// Display a notification about us starting.  We put an icon in the status bar.%0A        showNotification(
+mCurrentLocation = new CurrentLocation((LocationManager) getSystemService(Context.LOCATION_SERVICE)
 );%0A 
@@ -1597,76 +1597,26 @@
     
-Log.i(%22LocalService%22, %22Received start id %22 + startId + %22: %22 + intent
+startRefreshTimer(
 );%0A 
@@ -2010,255 +2010,1332 @@
     
-// This is the object that receives interactions from clients.  See%0A    // RemoteService for a more complete example.%0A    private final IBinder mBinder = new LocalBinder
+private void startRefreshTimer() %7B%0A        if(refreshTimer == null)%7B%0A            TimerTask refreshTimerTask = new TimerTask() %7B%0A                @Override%0A                public void run() %7B%0A                    updateUserLocation();%0A                %7D%0A            %7D;%0A            refreshTimer = new Timer();%0A            refreshTimer.schedule(refreshTimerTask, 1000, REFRESH_INTERVAL);%0A        %7D%0A    %7D%0A%0A    private void updateUserLocation() %7B%0A        JSONObject jsonObject = new JSONObject();%0A        try %7B%0A            JSONObject userJsonObject = DiscountHunt.currentSession.getJSONObject(%22user%22);%0A            jsonObject.put(%22user_id%22, userJsonObject.getInt(%22id%22));%0A            jsonObject.put(%22location_attributes%22, mCurrentLocation.toJSONObject());%0A            UserLocationChangeEndpoint endpoint = new UserLocationChangeEndpoint
 ();%0A
-%0A
     
-/**%0A
      
-* Show a notification while this service is running.
+   endpoint.post(jsonObject,%0A                    new Response.Listener() %7B%0A                        @Override%0A                        public void onResponse(Object response) %7B%0A%0A                        %7D%0A                    %7D,%0A                    new Response.ErrorListener() %7B%0A                        @Override%0A                        public void onErrorResponse(VolleyError error) %7B%0A%0A                        %7D%0A                    %7D%0A            );%0A        %7Dcatch (Exception ex)%7B%7D
 %0A    
- */
+%7D%0A
 %0A   

Send current location from background service