Android Open Source - security-cam Bitmap Download Task






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;
/* w w w.  j  ava  2 s.  co m*/
import java.io.IOException;
import java.io.InputStream;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import berlin.reiche.securitas.Client;
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.FlushedInputStream;
import berlin.reiche.securitas.util.HttpUtilities;

/**
 * Asynchronous task for downloading snapshots from the backend.
 * 
 * @author Konrad Reiche
 * 
 */
public class BitmapDownloadTask extends AsyncTask<String, Void, Bitmap> {

  /**
   * The model is used for setting the bitmap after the snapshot has been
   * received.
   */
  ClientModel model;

  /**
   * The controller is used to communicate with the interface if there are
   * changes.
   */
  ClientController controller;

  /**
   * The exception is stored in a field, this way the exception can be
   * processed after the background task has been finished.
   */
  IOException exception;

  /**
   * Default constructor.
   * 
   * @param model
   *            the model is used for setting the bitmap after the snapshot
   *            has been received.
   * @param controller
   *            the controller is used to communicate with the interface if
   *            there are changes.
   */
  public BitmapDownloadTask(Model<State> model, ClientController controller) {
    super();
    this.model = (ClientModel) model;
    this.controller = controller;
  }

  /**
   * Issues a HTTP GET request and reads the response which is used to
   * construct the final {@link Bitmap}. A customized flushed input stream is
   * used in order to avoid a bug in the Android library.
   */
  @Override
  protected Bitmap doInBackground(String... url) {

    HttpClient client = HttpUtilities.newHttpClient();
    HttpGet get = new HttpGet(url[0]);
    HttpUtilities.setAuthorization(get, Client.getSettings());

    try {
      HttpResponse response = client.execute(get);
      HttpEntity entity = response.getEntity();
      InputStream inputStream = entity.getContent();
      inputStream = new FlushedInputStream(inputStream);
      return BitmapFactory.decodeStream(inputStream);
    } catch (IOException e) {
      exception = e;
    } finally {
      HttpUtilities.closeClient(client);
    }
    return null;
  }

  /**
   * Sets the snapshot and reports back to interface.
   */
  @Override
  protected void onPostExecute(Bitmap result) {
    if (exception != null) {
      controller.reportError(exception.getMessage());
    }
    model.setSnapshot(result);
    controller.setSnapshot(result);
  }

}




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