Android Open Source - security-cam Idle State






From Project

Back to project page security-cam.

License

The source code is released under:

MIT License

If you think the Android project security-cam 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 berlin.reiche.securitas.controller.states;
//w  w w  .java  2 s  . co  m
import android.os.Message;
import android.util.Log;
import berlin.reiche.securitas.Client;
import berlin.reiche.securitas.controller.ClientController;
import berlin.reiche.securitas.controller.tasks.DetectionRequest;
import berlin.reiche.securitas.controller.tasks.DetectionRequest.DetectionCommand;
import berlin.reiche.securitas.controller.tasks.BitmapDownloadTask;
import berlin.reiche.securitas.controller.tasks.DeviceRegistration;
import berlin.reiche.securitas.controller.tasks.StatusTask;
import berlin.reiche.securitas.model.Protocol;
import berlin.reiche.securitas.model.ClientModel.State;

/**
 * IdleState is one of different controller states.
 * 
 * @author Konrad Reiche
 * 
 */
public class IdleState extends ControllerState<State> {

  /**
   * Tag for logging.
   */
  private static final String TAG = IdleState.class.getSimpleName();

  /**
   * Hides the super class controller field in order to avoid type casting.
   */
  ClientController controller;

  /**
   * Default constructor.
   * 
   * @param controller
   *            controller reference for issuing requests.
   */
  public IdleState(ClientController controller) {
    super(controller);
    this.controller = controller;
  }

  /**
   * Handles incoming messages from the interface based on the defined
   * {@link Protocol}.
   */
  @Override
  public void handleMessage(Message msg) {

    Protocol request = Protocol.valueOf(msg.what);
    switch (request) {
    case START_DETECTION:
      requestDetectionStart();
      break;
    case RESTORE_CLIENT_STATE:
      restoreClientState((String) msg.obj);
      break;
    case REGISTER_DEVICE:
      registerDevice(msg.obj.toString());
      break;
    case UNREGISTER_DEVICE:
      unregisterDevice(msg.obj.toString());
      break;
    case DOWNLOAD_LATEST_SNAPSHOT:
      downloadLatestSnapshot();
      break;
    case DOWNLOAD_MOTION_SNAPSHOT:
      downloadMotionSnapshot(msg.obj.toString());
      break;
    default:
      Log.e(TAG, "Illegal protocol request: " + request);
      throw new IllegalStateException();
    }
  }

  /**
   * Downloads the latest snapshot, respectively issues to create a current
   * one.
   */
  private void downloadLatestSnapshot() {
    String uri = Client.getEndpoint();
    uri += Protocol.DOWNLOAD_LATEST_SNAPSHOT.operation;
    new BitmapDownloadTask(model, controller).execute(uri);
  }

  /**
   * Downloads a specified snapshot.
   * 
   * @param filename
   *            the filename of the snapshot that should be downloaded.
   */
  private void downloadMotionSnapshot(String filename) {
    String uri = Client.getEndpoint();
    uri += Protocol.DOWNLOAD_MOTION_SNAPSHOT.operation + filename;
    new BitmapDownloadTask(model, controller).execute(uri);
  }

  /**
   * Synchronize the client with the server backend.
   * 
   * @param motionFilename
   *            If a notification was selected then the client is synchronized
   *            and the snapshot that triggered a motion event is fetched.
   */
  private void restoreClientState(String motionFilename) {
    String uri = Client.getEndpoint()
        + Protocol.RESTORE_CLIENT_STATE.operation;
    new StatusTask(model, controller, motionFilename).execute(uri);
  }

  /**
   * Issues a detection start.
   */
  private void requestDetectionStart() {
    String uri = Client.getEndpoint() + Protocol.START_DETECTION.operation;
    new DetectionRequest(model, controller, DetectionCommand.START)
        .execute(uri);
  }

  /**
   * Registers the device with the server endpoint in order to be able to
   * receive push notifications and issue a detection start in the first
   * place.
   * 
   * @param id
   *            id which is used for identifying the device in order to
   *            receive push notifications.
   */
  private void registerDevice(String id) {
    String uri = Client.getEndpoint() + Protocol.REGISTER_DEVICE.operation;
    new DeviceRegistration(model, controller, id,
        DeviceRegistration.Command.REGISTER).execute(uri);
  }

  /**
   * Undo the device registration.
   * 
   * @param id
   *            id which was used for identifying the device for receiving
   *            push notifications.
   */
  private void unregisterDevice(String id) {
    String uri = Client.getEndpoint()
        + Protocol.UNREGISTER_DEVICE.operation;
    new DeviceRegistration(model, controller, id,
        DeviceRegistration.Command.UNREGISTER).execute(uri);
  }
}




Java Source Code List

berlin.reiche.securitas.Client.java
berlin.reiche.securitas.Settings.java
berlin.reiche.securitas.activities.Action.java
berlin.reiche.securitas.activities.LauncherActivity.java
berlin.reiche.securitas.activities.MainActivity.java
berlin.reiche.securitas.activities.SettingsActivity.java
berlin.reiche.securitas.activities.package-info.java
berlin.reiche.securitas.controller.ClientController.java
berlin.reiche.securitas.controller.Controller.java
berlin.reiche.securitas.controller.GCMIntentService.java
berlin.reiche.securitas.controller.GCMReceiver.java
berlin.reiche.securitas.controller.InboxHandler.java
berlin.reiche.securitas.controller.states.ControllerState.java
berlin.reiche.securitas.controller.states.DetectionState.java
berlin.reiche.securitas.controller.states.IdleState.java
berlin.reiche.securitas.controller.states.package-info.java
berlin.reiche.securitas.controller.tasks.BitmapDownloadTask.java
berlin.reiche.securitas.controller.tasks.DetectionRequest.java
berlin.reiche.securitas.controller.tasks.DeviceRegistration.java
berlin.reiche.securitas.controller.tasks.StatusTask.java
berlin.reiche.securitas.controller.tasks.package-info.java
berlin.reiche.securitas.controller.package-info.java
berlin.reiche.securitas.model.ClientModel.java
berlin.reiche.securitas.model.Model.java
berlin.reiche.securitas.model.Protocol.java
berlin.reiche.securitas.model.package-info.java
berlin.reiche.securitas.util.FlushedInputStream.java
berlin.reiche.securitas.util.HttpUtilities.java
berlin.reiche.securitas.util.NotificationDialog.java
berlin.reiche.securitas.util.package-info.java
berlin.reiche.securitas.package-info.java