Android Open Source - PicSync Upload Service






From Project

Back to project page PicSync.

License

The source code is released under:

Apache License

If you think the Android project PicSync 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 com.darrenmowat.gdcu.service;
//from w  w  w.ja  v  a  2s.  co m
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.util.Log;

import com.darrenmowat.gdcu.R;
import com.darrenmowat.gdcu.data.Preferences;
import com.darrenmowat.gdcu.otto.DataBus;
import com.darrenmowat.gdcu.otto.UploadStatusEvent;
import com.darrenmowat.gdcu.service.helpers.ThreadCallbacks;
import com.google.android.gms.auth.UserRecoverableAuthException;

public class UploadService extends Service implements ThreadCallbacks {

  public static boolean uploading = false;
  
  private WakeLock wakelock;
  private UploadThread thread;

  private String email;

  @Override
  public void onStart(Intent intent, int startId) {
    handleCommand(intent);
  }

  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    handleCommand(intent);
    // We want this service to continue running until it is explicitly
    // stopped, so return sticky.
    return START_STICKY;
  }

  @Override
  public void onDestroy() {
    if (thread != null && thread.uploading) {
      thread.stopSoon();
      // The thread will notify this once it has actually stopped running
      return;
    }
    // Ensure the thread has been stopped
    // Ensure the WakeLock has been given up
    if (wakelock != null && wakelock.isHeld()) {
      wakelock.release();
    }
    uploading = false;
    super.onDestroy();

  }

  private void handleCommand(Intent intent) {
    uploading = true;
    email = Preferences.getEmail(UploadService.this);
    if (email == null || email.trim().equals("")) {
      log("No user stored. Stopping service.");
      stopSelf();
      return;
    }

    if (wakelock == null) {
      PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
      wakelock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MediaServiceWakeLock");
    }

    if (!wakelock.isHeld()) {
      wakelock.acquire();
    }

    if (thread == null) {
      thread = new UploadThread(UploadService.this, email);
    }

    synchronized (this) {
      if (!thread.uploading) {
        thread.start();
      } else {
        thread.touch();
      }
    }

  }

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

  private void log(String msg) {
    Log.v("GDCU::UploadService", msg);
  }

  @Override
  public void onThreadFinished() {
    thread = null;
    if (wakelock != null && wakelock.isHeld()) {
      wakelock.release();
    }
    stopSelf();
  }

  @Override
  public void onThreadError(Exception e) {
    log("UploadService crashed");
    e.printStackTrace();
    thread = null;
    if (e instanceof UserRecoverableAuthException) {
      UserRecoverableAuthException userException = (UserRecoverableAuthException) e;
      Notifier.notifyAuthError(UploadService.this, email, userException.getIntent());
    } else {
      e.printStackTrace();
      String title = getResources().getString(
          R.string.pref_title_upload_status_uploads_pending);
      String msg = getResources().getString(
          R.string.pref_summary_upload_status_uploads_failed);
      DataBus.getInstance().post(new UploadStatusEvent(title, msg));
    }

    if (wakelock != null && wakelock.isHeld()) {
      wakelock.release();
    }
    stopSelf();
  }


}




Java Source Code List

com.darrenmowat.gdcu.GDCU.java
com.darrenmowat.gdcu.Keys.java
com.darrenmowat.gdcu.activity.AuthActivity.java
com.darrenmowat.gdcu.activity.RenameFolderActivity.java
com.darrenmowat.gdcu.activity.SettingsActivity.java
com.darrenmowat.gdcu.activity.WebviewActivity.java
com.darrenmowat.gdcu.data.Database.java
com.darrenmowat.gdcu.data.Preferences.java
com.darrenmowat.gdcu.drive.DriveApi.java
com.darrenmowat.gdcu.otto.DataBus.java
com.darrenmowat.gdcu.otto.UploadStatusEventProducer.java
com.darrenmowat.gdcu.otto.UploadStatusEvent.java
com.darrenmowat.gdcu.security.PackageSecurityException.java
com.darrenmowat.gdcu.security.PackageSecurity.java
com.darrenmowat.gdcu.service.GalleryAlarmListener.java
com.darrenmowat.gdcu.service.GalleryObserver.java
com.darrenmowat.gdcu.service.MediaService.java
com.darrenmowat.gdcu.service.MediaThread.java
com.darrenmowat.gdcu.service.Notifier.java
com.darrenmowat.gdcu.service.ServiceUtils.java
com.darrenmowat.gdcu.service.UploadService.java
com.darrenmowat.gdcu.service.UploadThread.java
com.darrenmowat.gdcu.service.helpers.CloudSyncer.java
com.darrenmowat.gdcu.service.helpers.Media.java
com.darrenmowat.gdcu.service.helpers.ServiceIntents.java
com.darrenmowat.gdcu.service.helpers.ThreadCallbacks.java
com.darrenmowat.gdcu.tasks.GetTokenTask.java
com.darrenmowat.gdcu.tasks.RenameFolderTask.java
com.darrenmowat.gdcu.ui.ProgressDialogFragment.java
com.darrenmowat.gdcu.utils.HexConversions.java
com.darrenmowat.gdcu.utils.MD5Utils.java
com.darrenmowat.gdcu.utils.Preconditions.java