Android Open Source - tape Image Upload Task Service






From Project

Back to project page tape.

License

The source code is released under:

Apache License

If you think the Android project tape 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

// Copyright 2012 Square, Inc.
package com.squareup.tape.sample;
/*  w  ww.  j  a  va 2s.  c o  m*/
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import com.squareup.otto.Bus;
import com.squareup.tape.sample.ImageUploadTask.Callback;

import javax.inject.Inject;

public class ImageUploadTaskService extends Service implements Callback {
  private static final String TAG = "Tape:ImageUploadTaskService";

  @Inject ImageUploadTaskQueue queue;
  @Inject Bus bus;

  private boolean running;

  @Override public void onCreate() {
    super.onCreate();
    ((SampleApplication) getApplication()).inject(this);
    Log.i(TAG, "Service starting!");
  }

  @Override public int onStartCommand(Intent intent, int flags, int startId) {
    executeNext();
    return START_STICKY;
  }

  private void executeNext() {
    if (running) return; // Only one task at a time.

    ImageUploadTask task = queue.peek();
    if (task != null) {
      running = true;
      task.execute(this);
    } else {
      Log.i(TAG, "Service stopping!");
      stopSelf(); // No more tasks are present. Stop.
    }
  }

  @Override public void onSuccess(final String url) {
    running = false;
    queue.remove();
    bus.post(new ImageUploadSuccessEvent(url));
    executeNext();
  }

  @Override public void onFailure() {
  }

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




Java Source Code List

com.squareup.tape.FileException.java
com.squareup.tape.FileObjectQueue.java
com.squareup.tape.InMemoryObjectQueue.java
com.squareup.tape.ObjectQueue.java
com.squareup.tape.QueueFile.java
com.squareup.tape.SerializedConverter.java
com.squareup.tape.TaskInjector.java
com.squareup.tape.TaskQueue.java
com.squareup.tape.Task.java
com.squareup.tape.sample.GsonConverter.java
com.squareup.tape.sample.ImageUploadQueueSizeEvent.java
com.squareup.tape.sample.ImageUploadSuccessEvent.java
com.squareup.tape.sample.ImageUploadTaskQueue.java
com.squareup.tape.sample.ImageUploadTaskService.java
com.squareup.tape.sample.ImageUploadTask.java
com.squareup.tape.sample.SampleActivity.java
com.squareup.tape.sample.SampleApplication.java