Android Open Source - sana Queue Manager






From Project

Back to project page sana.

License

The source code is released under:

Copyright (c) 2010, Moca All rights reserved. The source code for Moca is licensed under the BSD license as follows: Redistribution and use in source and binary forms, with or without modification, ...

If you think the Android project sana 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 org.moca.service;
/*  w ww  .j  a v  a 2  s . c o m*/
import java.util.Collection;
import java.util.PriorityQueue;

import org.moca.db.MocaDB.SavedProcedureSQLFormat;

import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;

public class QueueManager {
  private static final String TAG = QueueManager.class.toString();

  public static final int UPLOAD_STATUS_NOT_IN_QUEUE = -1;
  public static final int UPLOAD_STATUS_WAITING = 1;
  public static final int UPLOAD_STATUS_SUCCESS = 2;
  public static final int UPLOAD_STATUS_IN_PROGRESS = 3;
  public static final int UPLOAD_NO_CONNECTIVITY = 4;
  public static final int UPLOAD_STATUS_FAILURE = 5;
  public static final int UPLOAD_STATUS_CREDENTIALS_INVALID = 6;
  
  private static final String[] PROJECTION = { SavedProcedureSQLFormat._ID,
    SavedProcedureSQLFormat.GUID, SavedProcedureSQLFormat.PROCEDURE_ID,
    SavedProcedureSQLFormat.UPLOAD_QUEUE };
  
  /**
   * Initializes the in-memory queue with what is stored in the database.
   */
  public static PriorityQueue<Uri> initQueue(Context c) {
    PriorityQueue<Uri> queue = new PriorityQueue<Uri>();
    Cursor cursor = null;
    try {
      // Initialize the queue from the database
      Log.i(TAG, "In initQueue - getting queue from database");
      cursor = c.getContentResolver().query(
          SavedProcedureSQLFormat.CONTENT_URI, PROJECTION,
          SavedProcedureSQLFormat.UPLOAD_QUEUE + " >= 0", null,
          SavedProcedureSQLFormat.QUEUE_SORT_ORDER);
      cursor.moveToFirst();

      int position = 0;
      while (!cursor.isAfterLast()) {
        int savedProcedureId = cursor.getInt(0);
        Uri savedProcedureUri = ContentUris.withAppendedId(
            SavedProcedureSQLFormat.CONTENT_URI, savedProcedureId);
        Log.i(TAG, "Queue item #" + position + " has URI " + savedProcedureUri);
        queue.add(savedProcedureUri);
        cursor.moveToNext();
        position++;
      }
      Log.i(TAG, "Queue has been extracted from database. Here is the queue: " + queue);
    } catch (Exception e) {
      Log.e(TAG, "Exception in getting queue from database: "
          + e.toString());
      e.printStackTrace();
    } finally {
      if (cursor != null)
        cursor.deactivate();
    }
    return queue;
  }
  
  public static void updateQueueInDB(Context c, PriorityQueue<Uri> queue) {
    Log.i(TAG, "Updating queue information in the database");
    Log.i(TAG, "Queue is now: " + queue.toString());
    
    // Reset all saved procedure to have -1 for UPLOAD_QUEUE. This takes
    // everything out of the queue, then we re-add everything that is in the
    // in-memory queue.
    ContentValues cv = new ContentValues();
    cv.put(SavedProcedureSQLFormat.UPLOAD_QUEUE, -1);
    c.getContentResolver().update(SavedProcedureSQLFormat.CONTENT_URI, cv, null, null);
    
    // TODO(XXX) This loop is inefficient -- O(n^2) when it could be O(n)
    for (Uri procedureUri : queue) {
      cv = new ContentValues();
      int index = queueIndex(queue, procedureUri);
      Log.i(TAG, "In updateQueueInDB, queueIndex(" + procedureUri
          + ") returns: " + index);
      cv.put(SavedProcedureSQLFormat.UPLOAD_QUEUE, index);
      c.getContentResolver().update(procedureUri, cv, null, null);
    }
  }
  
  public static void addToQueue(Context c, PriorityQueue<Uri> queue, Uri procedureUri) {
    queue.add(procedureUri);
    setProcedureUploadStatus(c, procedureUri, UPLOAD_STATUS_WAITING);
    updateQueueInDB(c, queue);
  }
  
  public static boolean removeFromQueue(Context c, PriorityQueue<Uri> queue, Uri procedureUri) {
    return removeFromQueue(c, queue, procedureUri, QueueManager.UPLOAD_STATUS_NOT_IN_QUEUE);
  }
  
  
  public static boolean removeFromQueue(Context c, PriorityQueue<Uri> queue, Uri procedureUri, int newStatus) {
    if (QueueManager.isInQueue(queue, procedureUri)) {
      queue.remove(procedureUri);
      QueueManager.updateQueueInDB(c, queue);
      QueueManager.setProcedureUploadStatus(c, procedureUri, newStatus);
      return true;
    }
    return false;
  }

  public static boolean isInQueue(PriorityQueue<Uri> queue, Uri procedureUri) {
    return queue.contains(procedureUri);
  }

  public static int queueIndex(PriorityQueue<Uri> queue, Uri procedureUri) {
    if (isInQueue(queue, procedureUri)) {
      int index = 0;
      for (Uri uri : queue) {
        if (uri.equals(procedureUri)) {
          return index;
        }
        index++;
      }
    }
    return -1;
  }
  
  public static void setProcedureUploadStatus(Context c, Uri procedureUri, int status) {
    Log.v(TAG, "Setting upload status for " + procedureUri + " to " + status);
    ContentValues cv = new ContentValues();
    cv.put(SavedProcedureSQLFormat.UPLOAD_STATUS, status); 
    c.getContentResolver().update(procedureUri, cv, null, null); 
  }

  public static void setProceduresUploadStatus(Context c, Collection<Uri> procedureUris, int status) {
    ContentValues cv = new ContentValues();
    cv.put(SavedProcedureSQLFormat.UPLOAD_STATUS, status);
    for (Uri uri : procedureUris) {
      c.getContentResolver().update(uri, cv, null, null);
    }
  }

}




Java Source Code List

.Moca.java
org.moca.Constants.java
org.moca.ImagePreviewDialog.java
org.moca.ScalingImageAdapter.java
org.moca.SelectableImageView.java
org.moca.activity.NotificationList.java
org.moca.activity.NotificationViewer.java
org.moca.activity.PatientInfoDialog.java
org.moca.activity.ProcedureRunner.java
org.moca.activity.ProceduresList.java
org.moca.activity.SavedProcedureList.java
org.moca.activity.Settings.java
org.moca.db.EncounterDAO.java
org.moca.db.EventDAO.java
org.moca.db.EventProvider.java
org.moca.db.Event.java
org.moca.db.ImageProvider.java
org.moca.db.MocaDB.java
org.moca.db.NotificationMessage.java
org.moca.db.NotificationProvider.java
org.moca.db.PatientInfo.java
org.moca.db.PatientProvider.java
org.moca.db.PatientValidator.java
org.moca.db.ProcedureDAO.java
org.moca.db.ProcedureProvider.java
org.moca.db.SavedProcedureProvider.java
org.moca.db.SoundProvider.java
org.moca.media.AudioPlayer.java
org.moca.net.MDSCode.java
org.moca.net.MDSInterface.java
org.moca.net.MDSNotification.java
org.moca.net.MDSResult.java
org.moca.net.SMSReceive.java
org.moca.procedure.BinaryUploadElement.java
org.moca.procedure.DateElement.java
org.moca.procedure.GpsElement.java
org.moca.procedure.MultiSelectElement.java
org.moca.procedure.PatientIdElement.java
org.moca.procedure.PictureElement.java
org.moca.procedure.ProcedureElement.java
org.moca.procedure.ProcedurePage.java
org.moca.procedure.ProcedureParseException.java
org.moca.procedure.Procedure.java
org.moca.procedure.RadioElement.java
org.moca.procedure.SelectElement.java
org.moca.procedure.SoundElement.java
org.moca.procedure.TextElement.java
org.moca.procedure.TextEntryElement.java
org.moca.procedure.ValidationError.java
org.moca.procedure.branching.Criteria.java
org.moca.procedure.branching.Criterion.java
org.moca.procedure.branching.LogicAnd.java
org.moca.procedure.branching.LogicBase.java
org.moca.procedure.branching.LogicNot.java
org.moca.procedure.branching.LogicOr.java
org.moca.service.BackgroundUploader.java
org.moca.service.QueueManager.java
org.moca.service.ServiceConnector.java
org.moca.service.ServiceListener.java
org.moca.task.CheckCredentialsTask.java
org.moca.task.ImageProcessingTaskRequest.java
org.moca.task.ImageProcessingTask.java
org.moca.task.MDSSyncTask.java
org.moca.task.PatientLookupListener.java
org.moca.task.PatientLookupTask.java
org.moca.task.ResetDatabaseTask.java
org.moca.task.ValidationListener.java
org.moca.util.MocaUtil.java
org.moca.util.UserDatabase.java