Android Open Source - security-cam Device Registration






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.tasks;
//from ww  w.  j a v  a2s  .  c  om
import static berlin.reiche.securitas.controller.tasks.DeviceRegistration.Command.REGISTER;
import static org.apache.http.HttpStatus.SC_OK;

import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;

import android.os.AsyncTask;
import android.util.Log;
import berlin.reiche.securitas.Client;
import berlin.reiche.securitas.activities.MainActivity;
import berlin.reiche.securitas.controller.ClientController;
import berlin.reiche.securitas.model.ClientModel;
import berlin.reiche.securitas.model.ClientModel.State;
import berlin.reiche.securitas.model.Model;
import berlin.reiche.securitas.util.HttpUtilities;

/**
 * This asynchronous task manages the registration ID which is required to
 * receive GCM messages. The registration ID is send to the server application.
 * 
 * @author Konrad Reiche
 * 
 */
public class DeviceRegistration extends AsyncTask<String, Void, HttpResponse> {

  /**
   * This command is used to distinguish whether the device should be
   * registered or unregistered with/from the endpoint.
   * 
   * @author Konrad Reiche
   * 
   */
  public enum Command {
    REGISTER, UNREGISTER
  };

  /**
   * Used to uniquely identify the device.
   */
  String id;

  /**
   * This command is stored for post-processing after the task has finished.
   */
  Command command;

  /**
   * The model is required for updating the state.
   */
  ClientModel model;

  /**
   * The controller is required for updating the interface.
   */
  ClientController controller;

  /**
   * The exception is post-processed after the task has finished.
   */
  IOException exception;

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

  /**
   * Default constructor.
   * 
   * @param model
   *            the model is required for updating the state.
   * @param controller
   *            the controller is required for updating the interface.
   * @param id
   *            the id for the device registration.
   * @param command
   *            whether the id should be registered or unregistered.
   */
  public DeviceRegistration(Model<State> model, ClientController controller,
      String id, Command command) {
    super();
    this.model = (ClientModel) model;
    this.controller = controller;
    this.id = id;
    this.command = command;
  }

  /**
   * Performs the HTTP request with the provided URI.
   */
  @Override
  protected HttpResponse doInBackground(String... uri) {

    Log.i(TAG, "Perform " + command + " POST request on endpoint.");
    HttpClient client = HttpUtilities.newHttpClient();
    String[][] data = { { "id", id } };
    HttpPost post = new HttpPost(uri[0]);
    HttpUtilities.setAuthorization(post, Client.getSettings());
    HttpUtilities.setRequestBody(post, data);

    try {
      return client.execute(post);
    } catch (IOException e) {
      Log.i(TAG, command + " failed, due to " + e.getMessage());
      exception = e;
    } finally {
      HttpUtilities.closeClient(client);
    }
    return null;
  }

  /**
   * Update the model and interface afterwards.
   */
  @Override
  protected void onPostExecute(HttpResponse response) {

    if (exception != null) {
      model.setRegisteredOnServer(false);
      controller.setRegisteredOnServer(false);
    }

    if (response == null) {
      controller.reportError("Response is null without an exception. "
          + "The endpoint probably ran into a problem.");
    } else if (response.getStatusLine().getStatusCode() == SC_OK) {
      boolean isRegistered = command == REGISTER;
      model.setRegisteredOnServer(isRegistered);
      controller.setRegisteredOnServer(isRegistered);
    } else {
      Log.i(TAG, response.getStatusLine().getReasonPhrase());
      controller.reportError(response.getStatusLine().getReasonPhrase());
    }
  }

}




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