Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package se.malmo.lighthouse.siberianhusky;
- import android.app.NotificationManager;
- import android.app.PendingIntent;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.os.Bundle;
- import android.support.v4.app.NotificationCompat;
- import se.malmo.lighthouse.siberianhusky.Services.GpsService;
- import se.malmo.lighthouse.siberianhusky.Services.SavedRouteService;
- import se.malmo.lighthouse.siberianhusky.utils.Constants;
- import se.malmo.lighthouse.siberianhusky.utils.NotificationWrapper;
- public class NotificationActivityHandler extends BroadcastReceiver {
- private static final String PAUSE_ACTION = "PAUSE_ACTION";
- private static final Object RESUME_ACTION = "RESUME_ACTION";
- private static final Object FINISH_ACTION = "FINISH_ACTION";
- private static final Object PAUSE_GPS_SERVICE = "PAUSE_GPS_SERVICE";
- private static final Object RESUME_GPS_SERVICE = "RESUME_GPS_SERVICE";
- private static final Object FINISH_GPS_SERVICE = "FINISH_GPS_SERVICE";
- private int routeID;
- private int counterHours;
- private int counterMinutes;
- private int counterSeconds;
- private String flag;
- @Override
- public void onReceive(Context context, Intent intent) {
- Bundle bundle = intent.getExtras();
- if(bundle != null){
- flag = bundle.getString("Flag");
- if(flag != null && flag.equals(Constants.TRACKING_ACTIVITY)){
- counterHours = bundle.getInt("timerHour");
- counterMinutes = bundle.getInt("timerMinutes");
- counterSeconds = bundle.getInt("timerSeconds");
- }
- else if(flag != null && flag.equals(Constants.RUN_SELECTED_ROUTE)){
- routeID = bundle.getInt("id");
- }
- }
- if(PAUSE_ACTION.equals(intent.getAction())) {
- Intent pauseIntent = new Intent(context, SavedRouteService.class);
- context.stopService(pauseIntent);
- Constants.serviceStatus = "notRunning";
- NotificationWrapper.NotificationCancel(context, Constants.SMALL_NOTI_ID);
- Intent resumeIntent = new Intent();
- resumeIntent.setAction("RESUME_ACTION");
- Bundle resumeBundle = new Bundle();
- resumeBundle.putInt("id", intent.getExtras().getInt("id"));
- resumeBundle.putString("Flag", "RunSelectedRoute");
- resumeIntent.putExtras(resumeBundle);
- PendingIntent resumePending = PendingIntent.getBroadcast(context, 0, resumeIntent, PendingIntent.FLAG_UPDATE_CURRENT);
- Intent terminateIntent = new Intent();
- pauseIntent.setAction("FINISH_ACTION");
- Bundle terminateBundle = new Bundle();
- terminateBundle.putInt("id", intent.getExtras().getInt("id"));
- terminateIntent.putExtras(terminateBundle);
- PendingIntent terminatePending = PendingIntent.getBroadcast(context, 0, terminateIntent, PendingIntent.FLAG_UPDATE_CURRENT);
- NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
- .setSmallIcon(R.mipmap.iconsafepassage)
- .setContentTitle("Tracking")
- .addAction(R.drawable.resume_icon, "", resumePending)
- .addAction(R.drawable.stop_icon, "", terminatePending);
- NotificationManager nManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
- nManager.notify(Constants.SMALL_NOTI_ID, builder.build());
- }
- else if(RESUME_ACTION.equals(intent.getAction())){
- Intent resumeIntent = new Intent(context, SavedRouteService.class);
- Bundle b2 = new Bundle();
- b2.putInt("routeID", routeID);
- resumeIntent.putExtras(b2);
- context.startService(resumeIntent);
- NotificationWrapper.NotificationCancelAll(context);
- }
- else if (FINISH_ACTION.equals(intent.getAction())) {
- Intent killIntent = new Intent(context, SavedRouteService.class);
- context.stopService(killIntent);
- Constants.serviceStatus = "notRunning";
- }
- else if(PAUSE_GPS_SERVICE.equals(intent.getAction())){
- //Pause the service
- Intent pauseIntent = new Intent(context, GpsService.class);
- context.stopService(pauseIntent);
- Constants.serviceStatus = "notRunning";
- Intent resumeIntent = new Intent();
- Bundle resumeBundle = new Bundle();
- resumeBundle.putInt("timerHour", counterHours);
- resumeBundle.putInt("timerMinutes", counterMinutes);
- resumeBundle.putInt("timerSeconds", counterSeconds);
- resumeBundle.putString("Flag", "TrackingActivity");
- resumeIntent.setAction("RESUME_GPS_SERVICE");
- PendingIntent resumePending = PendingIntent.getBroadcast(context, 0, resumeIntent, PendingIntent.FLAG_UPDATE_CURRENT);
- Intent terminateIntent = new Intent();
- pauseIntent.setAction("FINISH_GPS_SERVICE");
- PendingIntent terminatePending = PendingIntent.getBroadcast(context, 0, terminateIntent, PendingIntent.FLAG_UPDATE_CURRENT);
- NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
- .setSmallIcon(R.mipmap.iconsafepassage)
- .setContentTitle("Tracking")
- .addAction(R.drawable.resume_icon, "", resumePending)
- .addAction(R.drawable.stop_icon, "", terminatePending);
- NotificationManager nManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
- nManager.notify(Constants.SMALL_NOTI_ID, builder.build());
- }
- else if(RESUME_GPS_SERVICE.equals(intent.getAction())){
- //Start the service again with the provided bundle containing the values we are fetching in our onStartCommand
- Intent resumeIntent = new Intent(context, GpsService.class);
- Bundle b = new Bundle();
- b.putInt("timerHour", counterHours);
- b.putInt("timerMinutes", counterMinutes);
- b.putInt("timerSeconds", counterSeconds);
- resumeIntent.putExtras(b);
- context.startService(resumeIntent);
- NotificationWrapper.NotificationCancelAll(context);
- }
- else if(FINISH_GPS_SERVICE.equals(intent.getAction())){
- //Kill the service completely, finish the route, bring the user to where you actually save the route and so on, for this we either
- //have to create a new AlertDialog in here or we some how have to simulate a click on the finish button, perhaps send a flag in
- //the bundle and search for it in onStartCommand?
- Intent killServiceIntent = new Intent(context, GpsService.class);
- context.stopService(killServiceIntent);
- Intent launchActivity = new Intent(context, TrackingActivity.class);
- launchActivity.setAction(Constants.GPS_SERVICE_FINISH_TRIGGER);
- launchActivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- context.startActivity(launchActivity);
- NotificationWrapper.NotificationCancelAll(context);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement