Advertisement
Neshoz

Untitled

Feb 18th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.28 KB | None | 0 0
  1. package se.malmo.lighthouse.siberianhusky;
  2.  
  3. import android.app.NotificationManager;
  4. import android.app.PendingIntent;
  5. import android.content.BroadcastReceiver;
  6. import android.content.Context;
  7. import android.content.Intent;
  8. import android.os.Bundle;
  9. import android.support.v4.app.NotificationCompat;
  10.  
  11. import se.malmo.lighthouse.siberianhusky.Services.GpsService;
  12. import se.malmo.lighthouse.siberianhusky.Services.SavedRouteService;
  13. import se.malmo.lighthouse.siberianhusky.utils.Constants;
  14. import se.malmo.lighthouse.siberianhusky.utils.NotificationWrapper;
  15.  
  16. public class NotificationActivityHandler extends BroadcastReceiver {
  17.  
  18.     private static final String PAUSE_ACTION = "PAUSE_ACTION";
  19.     private static final Object RESUME_ACTION = "RESUME_ACTION";
  20.     private static final Object FINISH_ACTION = "FINISH_ACTION";
  21.     private static final Object PAUSE_GPS_SERVICE = "PAUSE_GPS_SERVICE";
  22.     private static final Object RESUME_GPS_SERVICE = "RESUME_GPS_SERVICE";
  23.     private static final Object FINISH_GPS_SERVICE = "FINISH_GPS_SERVICE";
  24.     private int routeID;
  25.     private int counterHours;
  26.     private int counterMinutes;
  27.     private int counterSeconds;
  28.     private String flag;
  29.  
  30.     @Override
  31.     public void onReceive(Context context, Intent intent) {
  32.         Bundle bundle = intent.getExtras();
  33.         if(bundle != null){
  34.             flag = bundle.getString("Flag");
  35.             if(flag != null && flag.equals(Constants.TRACKING_ACTIVITY)){
  36.                 counterHours = bundle.getInt("timerHour");
  37.                 counterMinutes = bundle.getInt("timerMinutes");
  38.                 counterSeconds = bundle.getInt("timerSeconds");
  39.             }
  40.             else if(flag != null && flag.equals(Constants.RUN_SELECTED_ROUTE)){
  41.                 routeID = bundle.getInt("id");
  42.             }
  43.         }
  44.  
  45.         if(PAUSE_ACTION.equals(intent.getAction())) {
  46.             Intent pauseIntent = new Intent(context, SavedRouteService.class);
  47.             context.stopService(pauseIntent);
  48.             Constants.serviceStatus = "notRunning";
  49.             NotificationWrapper.NotificationCancel(context, Constants.SMALL_NOTI_ID);
  50.  
  51.             Intent resumeIntent = new Intent();
  52.             resumeIntent.setAction("RESUME_ACTION");
  53.             Bundle resumeBundle = new Bundle();
  54.             resumeBundle.putInt("id", intent.getExtras().getInt("id"));
  55.             resumeBundle.putString("Flag", "RunSelectedRoute");
  56.             resumeIntent.putExtras(resumeBundle);
  57.             PendingIntent resumePending = PendingIntent.getBroadcast(context, 0, resumeIntent, PendingIntent.FLAG_UPDATE_CURRENT);
  58.  
  59.             Intent terminateIntent = new Intent();
  60.             pauseIntent.setAction("FINISH_ACTION");
  61.             Bundle terminateBundle = new Bundle();
  62.             terminateBundle.putInt("id", intent.getExtras().getInt("id"));
  63.             terminateIntent.putExtras(terminateBundle);
  64.             PendingIntent terminatePending = PendingIntent.getBroadcast(context, 0, terminateIntent, PendingIntent.FLAG_UPDATE_CURRENT);
  65.  
  66.             NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
  67.                     .setSmallIcon(R.mipmap.iconsafepassage)
  68.                     .setContentTitle("Tracking")
  69.                     .addAction(R.drawable.resume_icon, "", resumePending)
  70.                     .addAction(R.drawable.stop_icon, "", terminatePending);
  71.  
  72.             NotificationManager nManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
  73.             nManager.notify(Constants.SMALL_NOTI_ID, builder.build());
  74.         }
  75.         else if(RESUME_ACTION.equals(intent.getAction())){
  76.             Intent resumeIntent = new Intent(context, SavedRouteService.class);
  77.             Bundle b2 = new Bundle();
  78.             b2.putInt("routeID", routeID);
  79.             resumeIntent.putExtras(b2);
  80.             context.startService(resumeIntent);
  81.             NotificationWrapper.NotificationCancelAll(context);
  82.         }
  83.         else if (FINISH_ACTION.equals(intent.getAction())) {
  84.             Intent killIntent = new Intent(context, SavedRouteService.class);
  85.             context.stopService(killIntent);
  86.             Constants.serviceStatus = "notRunning";
  87.         }
  88.         else if(PAUSE_GPS_SERVICE.equals(intent.getAction())){
  89.             //Pause the service
  90.             Intent pauseIntent = new Intent(context, GpsService.class);
  91.             context.stopService(pauseIntent);
  92.             Constants.serviceStatus = "notRunning";
  93.  
  94.             Intent resumeIntent = new Intent();
  95.             Bundle resumeBundle = new Bundle();
  96.  
  97.             resumeBundle.putInt("timerHour", counterHours);
  98.             resumeBundle.putInt("timerMinutes", counterMinutes);
  99.             resumeBundle.putInt("timerSeconds", counterSeconds);
  100.             resumeBundle.putString("Flag", "TrackingActivity");
  101.             resumeIntent.setAction("RESUME_GPS_SERVICE");
  102.             PendingIntent resumePending = PendingIntent.getBroadcast(context, 0, resumeIntent, PendingIntent.FLAG_UPDATE_CURRENT);
  103.  
  104.             Intent terminateIntent = new Intent();
  105.             pauseIntent.setAction("FINISH_GPS_SERVICE");
  106.             PendingIntent terminatePending = PendingIntent.getBroadcast(context, 0, terminateIntent, PendingIntent.FLAG_UPDATE_CURRENT);
  107.  
  108.             NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
  109.                     .setSmallIcon(R.mipmap.iconsafepassage)
  110.                     .setContentTitle("Tracking")
  111.                     .addAction(R.drawable.resume_icon, "", resumePending)
  112.                     .addAction(R.drawable.stop_icon, "", terminatePending);
  113.  
  114.             NotificationManager nManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
  115.             nManager.notify(Constants.SMALL_NOTI_ID, builder.build());
  116.         }
  117.         else if(RESUME_GPS_SERVICE.equals(intent.getAction())){
  118.             //Start the service again with the provided bundle containing the values we are fetching in our onStartCommand
  119.             Intent resumeIntent = new Intent(context, GpsService.class);
  120.             Bundle b = new Bundle();
  121.             b.putInt("timerHour", counterHours);
  122.             b.putInt("timerMinutes", counterMinutes);
  123.             b.putInt("timerSeconds", counterSeconds);
  124.             resumeIntent.putExtras(b);
  125.             context.startService(resumeIntent);
  126.             NotificationWrapper.NotificationCancelAll(context);
  127.  
  128.         }
  129.         else if(FINISH_GPS_SERVICE.equals(intent.getAction())){
  130.             //Kill the service completely, finish the route, bring the user to where you actually save the route and so on, for this we either
  131.             //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
  132.             //the bundle and search for it in onStartCommand?
  133.             Intent killServiceIntent = new Intent(context, GpsService.class);
  134.             context.stopService(killServiceIntent);
  135.  
  136.             Intent launchActivity = new Intent(context, TrackingActivity.class);
  137.             launchActivity.setAction(Constants.GPS_SERVICE_FINISH_TRIGGER);
  138.             launchActivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  139.             context.startActivity(launchActivity);
  140.             NotificationWrapper.NotificationCancelAll(context);
  141.         }
  142.     }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement