Android Open Source - android-obd-reader Abstract Gateway Service






From Project

Back to project page android-obd-reader.

License

The source code is released under:

Apache License

If you think the Android project android-obd-reader listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package pt.lighthouselabs.obd.reader.io;
// w w  w  . ja va2s  . c o  m
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import com.google.inject.Inject;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import pt.lighthouselabs.obd.reader.activity.MainActivity;
import roboguice.service.RoboService;


public abstract class AbstractGatewayService extends RoboService {
  private static final String TAG = AbstractGatewayService.class.getName();
  public static final int NOTIFICATION_ID = 1;
  @Inject
  protected NotificationManager notificationManager;

  protected Context ctx;
  protected boolean isRunning = false;
  private final IBinder binder = new AbstractGatewayServiceBinder();
  protected boolean isQueueRunning = false;
  protected Long queueCounter = 0L;
  protected BlockingQueue<ObdCommandJob> jobsQueue = new LinkedBlockingQueue<ObdCommandJob>();

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

  @Override
  public void onCreate() {
    super.onCreate();
    Log.d(TAG, "Creating service..");
    Log.d(TAG, "Service created.");
  }

  @Override
  public void onDestroy() {
    super.onDestroy();
    Log.d(TAG, "Destroying service...");
    notificationManager.cancel(NOTIFICATION_ID);
    Log.d(TAG, "Service destroyed.");
  }

  public boolean isRunning() {
    return isRunning;
  }

  public boolean queueEmpty() {
  return jobsQueue.isEmpty();
  }


  public class AbstractGatewayServiceBinder extends Binder {
    public AbstractGatewayService getService() {
      return AbstractGatewayService.this;
    }
  }

   /**
   * This method will add a job to the queue while setting its ID to the
   * internal queue counter.
   *
   * @param job the job to queue.
   */
  public void queueJob(ObdCommandJob job) {
      queueCounter++;
      Log.d(TAG, "Adding job[" + queueCounter + "] to queue..");

      job.setId(queueCounter);
      try {
        jobsQueue.put(job);
        Log.d(TAG, "Job queued successfully.");
      } catch (InterruptedException e) {
        job.setState(ObdCommandJob.ObdCommandJobState.QUEUE_ERROR);
        Log.e(TAG, "Failed to queue job.");
      }

    if (!isQueueRunning) {
      // Run the executeQueue in a different thread to lighten the UI thread
      Thread t = new Thread(new Runnable() {
        @Override
        public void run() {
          executeQueue();
        }
      });

      t.start();
    }
  }

    /**
     * Show a notification while this service is running.
     */
  protected void showNotification(String contentTitle, String contentText, int icon, boolean ongoing, boolean notify, boolean vibrate) {
    final PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0, new Intent(ctx, MainActivity.class), 0);
    final NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder( ctx);
    notificationBuilder.setContentTitle(contentTitle)
              .setContentText(contentText).setSmallIcon(icon)
              .setContentIntent(contentIntent)
              .setWhen(System.currentTimeMillis());
    // can cancel?
    if (ongoing) {
      notificationBuilder.setOngoing(true);
    } else {
      notificationBuilder.setAutoCancel(true);
    }
    if (vibrate) {
      notificationBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
    }
    if (notify) {
      notificationManager.notify(NOTIFICATION_ID, notificationBuilder.getNotification());
    }
  }

  public void setContext(Context c) {
    ctx = c;
  }

  abstract protected void executeQueue();
  abstract public void startService();
  abstract public void stopService();
}




Java Source Code List

pt.lighthouselabs.obd.reader.ObdProgressListener.java
pt.lighthouselabs.obd.reader.activity.ConfigActivity.java
pt.lighthouselabs.obd.reader.activity.MainActivity.java
pt.lighthouselabs.obd.reader.activity.TroubleCodesActivity.java
pt.lighthouselabs.obd.reader.config.ObdConfig.java
pt.lighthouselabs.obd.reader.io.AbstractGatewayService.java
pt.lighthouselabs.obd.reader.io.MockObdGatewayService.java
pt.lighthouselabs.obd.reader.io.ObdCommandJob.java
pt.lighthouselabs.obd.reader.io.ObdGatewayService.java
pt.lighthouselabs.obd.reader.net.ObdReading.java
pt.lighthouselabs.obd.reader.net.ObdService.java