@Override public int onStartCommand(Intent intent,int flags,int startId){
  Log_OC.d(TAG,"Starting command with id " + startId);
  if (!intent.hasExtra(KEY_ACCOUNT) || !intent.hasExtra(KEY_UPLOAD_TYPE) || !(intent.hasExtra(KEY_LOCAL_FILE) || intent.hasExtra(KEY_FILE))) {
    Log_OC.e(TAG,"Not enough information provided in intent");
    return Service.START_NOT_STICKY;
  }
  int uploadType=intent.getIntExtra(KEY_UPLOAD_TYPE,-1);
  if (uploadType == -1) {
    Log_OC.e(TAG,"Incorrect upload type provided");
    return Service.START_NOT_STICKY;
  }
  Account account=intent.getParcelableExtra(KEY_ACCOUNT);
  if (!AccountUtils.exists(account,getApplicationContext())) {
    return Service.START_NOT_STICKY;
  }
  String[] localPaths=null, remotePaths=null, mimeTypes=null;
  OCFile[] files=null;
  if (uploadType == UPLOAD_SINGLE_FILE) {
    if (intent.hasExtra(KEY_FILE)) {
      files=new OCFile[]{intent.getParcelableExtra(KEY_FILE)};
    }
 else {
      localPaths=new String[]{intent.getStringExtra(KEY_LOCAL_FILE)};
      remotePaths=new String[]{intent.getStringExtra(KEY_REMOTE_FILE)};
      mimeTypes=new String[]{intent.getStringExtra(KEY_MIME_TYPE)};
    }
  }
 else {
    if (intent.hasExtra(KEY_FILE)) {
      files=(OCFile[])intent.getParcelableArrayExtra(KEY_FILE);
    }
 else {
      localPaths=intent.getStringArrayExtra(KEY_LOCAL_FILE);
      remotePaths=intent.getStringArrayExtra(KEY_REMOTE_FILE);
      mimeTypes=intent.getStringArrayExtra(KEY_MIME_TYPE);
    }
  }
  FileDataStorageManager storageManager=new FileDataStorageManager(account,getContentResolver());
  boolean forceOverwrite=intent.getBooleanExtra(KEY_FORCE_OVERWRITE,false);
  boolean isInstant=intent.getBooleanExtra(KEY_INSTANT_UPLOAD,false);
  int localAction=intent.getIntExtra(KEY_LOCAL_BEHAVIOUR,LOCAL_BEHAVIOUR_COPY);
  if (intent.hasExtra(KEY_FILE) && files == null) {
    Log_OC.e(TAG,"Incorrect array for OCFiles provided in upload intent");
    return Service.START_NOT_STICKY;
  }
 else   if (!intent.hasExtra(KEY_FILE)) {
    if (localPaths == null) {
      Log_OC.e(TAG,"Incorrect array for local paths provided in upload intent");
      return Service.START_NOT_STICKY;
    }
    if (remotePaths == null) {
      Log_OC.e(TAG,"Incorrect array for remote paths provided in upload intent");
      return Service.START_NOT_STICKY;
    }
    if (localPaths.length != remotePaths.length) {
      Log_OC.e(TAG,"Different number of remote paths and local paths!");
      return Service.START_NOT_STICKY;
    }
    files=new OCFile[localPaths.length];
    for (int i=0; i < localPaths.length; i++) {
      files[i]=obtainNewOCFileToUpload(remotePaths[i],localPaths[i],((mimeTypes != null) ? mimeTypes[i] : null),storageManager);
      if (files[i] == null) {
        return Service.START_NOT_STICKY;
      }
    }
  }
  OwnCloudVersion ocv=AccountUtils.getServerVersion(account);
  boolean chunked=FileUploader.chunkedUploadIsSupported(ocv);
  AbstractList<String> requestedUploads=new Vector<>();
  String uploadKey=null;
  UploadFileOperation newUpload=null;
  try {
    for (    OCFile file : files) {
      uploadKey=buildRemoteName(account,file.getRemotePath());
      newUpload=new UploadFileOperation(account,file,chunked,isInstant,forceOverwrite,localAction,getApplicationContext());
      if (isInstant) {
        newUpload.setRemoteFolderToBeCreated();
      }
      mPendingUploads.putIfAbsent(uploadKey,newUpload);
      newUpload.addDatatransferProgressListener(this);
      newUpload.addDatatransferProgressListener((FileUploaderBinder)mBinder);
      requestedUploads.add(uploadKey);
    }
  }
 catch (  IllegalArgumentException e) {
    Log_OC.e(TAG,"Not enough information provided in intent: " + e.getMessage());
    return START_NOT_STICKY;
  }
catch (  IllegalStateException e) {
    Log_OC.e(TAG,"Bad information provided in intent: " + e.getMessage());
    return START_NOT_STICKY;
  }
catch (  Exception e) {
    Log_OC.e(TAG,"Unexpected exception while processing upload intent",e);
    return START_NOT_STICKY;
  }
  if (requestedUploads.size() > 0) {
    Message msg=mServiceHandler.obtainMessage();
    msg.arg1=startId;
    msg.obj=requestedUploads;
    mServiceHandler.sendMessage(msg);
  }
  Log_OC.i(TAG,"mPendingUploads size:" + mPendingUploads.size());
  return Service.START_NOT_STICKY;
}

@ Override public int onStartCommand ( Intent intent , int flags , int startId ) { Log_OC . d ( TAG , "Starting command with id " + startId ) ; if ( ! intent . hasExtra ( KEY_ACCOUNT ) || ! intent . hasExtra ( KEY_UPLOAD_TYPE ) || ! ( intent . hasExtra ( KEY_LOCAL_FILE ) || intent . hasExtra ( KEY_FILE ) ) ) { Log_OC . e ( TAG , "Not enough information provided in intent" ) ; return Service . START_NOT_STICKY ; } int uploadType = intent . getIntExtra ( KEY_UPLOAD_TYPE , - 1 ) ; if ( uploadType == - 1 ) { Log_OC . e ( TAG , "Incorrect upload type provided" ) ; return Service . START_NOT_STICKY ; } Account account = intent . getParcelableExtra ( KEY_ACCOUNT ) ; if ( ! AccountUtils . exists ( account , getApplicationContext ( ) ) ) { return Service . START_NOT_STICKY ; } String [ ] localPaths = null , remotePaths = null , mimeTypes = null ; OCFile [ ] files = null ; if ( uploadType == UPLOAD_SINGLE_FILE ) { if ( intent . hasExtra ( KEY_FILE ) ) { files = new OCFile [ ] { intent . getParcelableExtra ( KEY_FILE ) } ; } else { localPaths = new String [ ] { intent . getStringExtra ( KEY_LOCAL_FILE ) } ; remotePaths = new String [ ] { intent . getStringExtra ( KEY_REMOTE_FILE ) } ; mimeTypes = new String [ ] { intent . getStringExtra ( KEY_MIME_TYPE ) } ; } } else { if ( intent . hasExtra ( KEY_FILE ) ) { files = ( OCFile [ ] ) intent . getParcelableArrayExtra ( KEY_FILE ) ; } else { localPaths = intent . getStringArrayExtra ( KEY_LOCAL_FILE ) ; remotePaths = intent . getStringArrayExtra ( KEY_REMOTE_FILE ) ; mimeTypes = intent . getStringArrayExtra ( KEY_MIME_TYPE ) ; } } FileDataStorageManager storageManager = new FileDataStorageManager ( account , getContentResolver ( ) ) ; boolean forceOverwrite = intent . getBooleanExtra ( KEY_FORCE_OVERWRITE , false ) ; boolean isInstant = intent . getBooleanExtra ( KEY_INSTANT_UPLOAD , false ) ; int localAction = intent . getIntExtra ( KEY_LOCAL_BEHAVIOUR , LOCAL_BEHAVIOUR_COPY ) ; if ( intent . hasExtra ( KEY_FILE ) && files == null ) { Log_OC . e ( TAG , "Incorrect array for OCFiles provided in upload intent" ) ; return Service . START_NOT_STICKY ; } else if ( ! intent . hasExtra ( KEY_FILE ) ) { if ( localPaths == null ) { Log_OC . e ( TAG , "Incorrect array for local paths provided in upload intent" ) ; return Service . START_NOT_STICKY ; } if ( remotePaths == null ) { Log_OC . e ( TAG , "Incorrect array for remote paths provided in upload intent" ) ; return Service . START_NOT_STICKY ; } if ( localPaths . length != remotePaths . length ) { Log_OC . e ( TAG , "Different number of remote paths and local paths!" ) ; return Service . START_NOT_STICKY ; } files = new OCFile [ localPaths . length ] ; for ( int i = 0 ; i < localPaths . length ; i ++ ) { files [ i ] = obtainNewOCFileToUpload ( remotePaths [ i ] , localPaths [ i ] , ( ( mimeTypes != null ) ? mimeTypes [ i ] : null ) , storageManager ) ; if ( files [ i ] == null ) { return Service . START_NOT_STICKY ; } } } OwnCloudVersion ocv = AccountUtils . getServerVersion ( account ) ; boolean chunked = FileUploader . chunkedUploadIsSupported ( ocv ) ; AbstractList < String > requestedUploads = new Vector < > ( ) ; String uploadKey = null ; UploadFileOperation newUpload = null ; try { for ( OCFile file : files ) { uploadKey = buildRemoteName ( account , file . getRemotePath ( ) ) ; newUpload = new UploadFileOperation ( account , file , chunked , isInstant , forceOverwrite , localAction , getApplicationContext ( ) ) ; if ( isInstant ) { newUpload . setRemoteFolderToBeCreated ( ) ; } mPendingUploads . putIfAbsent ( uploadKey , newUpload ) ; newUpload . addDatatransferProgressListener ( this ) ; newUpload . addDatatransferProgressListener ( ( FileUploaderBinder ) mBinder ) ; requestedUploads . add ( uploadKey ) ; } } catch ( IllegalArgumentException e ) { Log_OC . e ( TAG , "Not enough information provided in intent: " + e . getMessage ( ) ) ; return START_NOT_STICKY ; } catch ( IllegalStateException e ) { Log_OC . e ( TAG , "Bad information provided in intent: " + e . getMessage ( ) ) ; return START_NOT_STICKY ; } catch ( Exception e ) { Log_OC . e ( TAG , "Unexpected exception while processing upload intent" , e ) ; return START_NOT_STICKY ; } if ( requestedUploads . size ( ) > 0 ) { Message msg = mServiceHandler . obtainMessage ( ) ; msg . arg1 = startId ; msg . obj = requestedUploads ; mServiceHandler . sendMessage ( msg ) ; } Log_OC . i ( TAG , "mPendingUploads size:" + mPendingUploads . size ( ) ) ; return Service . START_NOT_STICKY ; }
entry point to add one or several files to the queue of uploads . new uploads are added calling to startservice ( ) , resulting in a call to this method . this ensures the service will keep on working although the caller activity goes away .
