private void beginUpload(PictureFile pictureFile) {
    String full_path_on_device = pictureFile.getFull_path_on_device();
    Log.w(LOG_TAG, "try start upload file = " + full_path_on_device);
    File fileFromPath = new File(full_path_on_device);
    boolean exists = fileFromPath.exists();
    if (exists) {
      boolean addressExist = isAddressExist(pictureFile);
      Log.w(
          LOG_TAG,
          "file "
              + full_path_on_device
              + " EXIST. Try upload it. Address  exist = "
              + addressExist);

      if (!addressExist) {
        Log.w(LOG_TAG, "file " + full_path_on_device + " don't have geoname, try found it ");
        try {
          String newGeoname = retrieveGeoName(pictureFile);

          if (newGeoname != null && !newGeoname.isEmpty()) {
            renameFile(fileFromPath, pictureFile, newGeoname);
          }

        } catch (IOException e) {
          e.printStackTrace();
        }
        addressExist = isAddressExist(pictureFile);
        full_path_on_device = pictureFile.getFull_path_on_device();
        Log.w(LOG_TAG, "file " + full_path_on_device + " address retrieved  = " + addressExist);
      }

      if (addressExist) {
        File localFile = new File(full_path_on_device);
        String remoteName = pictureFile.getFilename();
        initUpload(pictureFile, localFile, remoteName);
        Log.w(LOG_TAG, "file " + full_path_on_device + " is READY. START UPLOAD AWS id");
      } else {
        Log.e(LOG_TAG, "file " + full_path_on_device + " don't receive geoname, skip it ");
        String filename = pictureFile.getFilename();
        addInFailed(filename);

        uploadCycleFinished();
      }

    } else {
      Log.e(LOG_TAG, "file " + full_path_on_device + " NOT EXIST. Remove it from database");
      PictureFile deletedFile = daoHelper.getFileFromPath(full_path_on_device);
      if (deletedFile != null) {
        daoHelper.pictureFileDao.delete(deletedFile);
        daoHelper.onPictureFilesUpdated();
      }
      Intent intent = new Intent(BroadcastReceiverHelper.ACTION);
      intent.putExtra(BroadcastReceiverHelper.PARAM, BroadcastReceiverHelper.PARAM_DELETED);
      String name = fileFromPath.getName();
      intent.putExtra(BroadcastReceiverHelper.FILENAME, name);
      sendBroadcast(intent);

      uploadCycleFinished();
    }
  }
  private boolean startUploadFile() {
    boolean uploadStarted;
    PictureFile file = daoHelper.selectPictureFileForUploading();
    if (file != null) {
      String filename = file.getFilename();
      boolean previouslyFailed = isPreviouslyFailed(filename);
      if (previouslyFailed) {
        file = tryFoundNewFileForUpload(filename);

        if (file != null) {
          uploadStarted = true;
          beginUpload(file);
        } else {
          uploadStarted = false;
          synchronized (failedUploadsList) {
            failedUploadsList.clear();
          }
          uploadCycleFinished();
        }

      } else {
        uploadStarted = true;
        beginUpload(file);
      }

    } else {
      uploadStarted = false;
      uploadCycleFinished();
    }

    return uploadStarted;
  }
  private String retrieveGeoName(PictureFile pictureFile) throws IOException {
    Log.w(LOG_TAG, "start retrieve geoname for pictureFile " + pictureFile.getFilename());

    String address = null;

    Context context = getApplicationContext();
    LocationHelper locationHelper = LocationHelper.getInstatce(context);
    address = locationHelper.getAddressFromLocationSync(pictureFile.getLat(), pictureFile.getLon());
    Log.w(LOG_TAG, "finish retrieve geoname for pictureFile " + pictureFile.getFilename());
    return address;
  }
  private void setUpUploadingAgain(PictureFile failedFile) {

    if (failedFile != null) {
      String filename = failedFile.getFilename();
      if (Constants.DEBUG) {
        ToastHelper.showToast(getApplicationContext(), "FAILED " + failedFile.getFilename());
      }
      Log.w(LOG_TAG, "FAILED " + filename);
      failedFile.setStatus(DAOHelper.FILE_STATUS_IN_QUEUE);
      daoHelper.pictureFileDao.update(failedFile);
      daoHelper.onPictureFilesUpdated();
    }
  }
  private void onUploadInProgress(PictureFile uploadingFile) {

    if (uploadingFile != null) {
      Integer oldStatus = uploadingFile.getStatus();
      if (oldStatus != DAOHelper.FILE_STATUS_UPLOADING
          && oldStatus != DAOHelper.FILE_STATUS_UPLOADED) {
        Log.w(LOG_TAG, "IN PROGRESS " + uploadingFile.getFilename());
        uploadingFile.setStatus(DAOHelper.FILE_STATUS_UPLOADING);
        daoHelper.pictureFileDao.update(uploadingFile);
        daoHelper.onPictureFilesUpdated();
      }
    }
  }
  private void onUploadCompleted(PictureFile uploadedFile) {

    if (uploadedFile != null) {
      if (Constants.DEBUG) {
        ToastHelper.showToast(getApplicationContext(), "COMPLETED " + uploadedFile.getFilename());
      }
      Log.w(LOG_TAG, "COMPLETED " + uploadedFile.getFilename());
      uploadedFile.setStatus(DAOHelper.FILE_STATUS_UPLOADED);
      uploadedFile.setIsDeleted(false);
      daoHelper.pictureFileDao.update(uploadedFile);
      daoHelper.onPictureFilesUpdated();
    }

    uploadCycleFinished();
  }
  private void onUploadFailed(PictureFile id) {
    setUpUploadingAgain(id);

    Intent intent = new Intent(BroadcastReceiverHelper.ACTION);
    intent.putExtra(BroadcastReceiverHelper.PARAM, BroadcastReceiverHelper.PARAM_UPLOAD_ERROR);
    String name = id.getFilename();
    intent.putExtra(BroadcastReceiverHelper.FILENAME, name);
    sendBroadcast(intent);

    String filename = id.getFilename();

    addInFailed(filename);

    uploadCycleFinished();
  }
 private void OnUploadStateChanged(
     PictureFile pictureFile,
     ServiceInnerUploadState state,
     long countBytesTransfered,
     long size) {
   if (state == ServiceInnerUploadState.IN_PROGRESS) {
     Log.i(
         LOG_TAG,
         pictureFile.getFilename() + " " + state + " " + countBytesTransfered + "/" + size);
   } else {
     Log.w(
         LOG_TAG,
         pictureFile.getFilename() + " " + state + " " + countBytesTransfered + "/" + size);
   }
 }
  private void renameFile(File oldFile, PictureFile pictureFile, String newGeoname) {

    pictureFile.setGeo_name(newGeoname);

    Double lat = pictureFile.getLat();
    Double lon = pictureFile.getLon();
    String geoname = newGeoname;
    Long time = pictureFile.getTime();

    String oldPath = pictureFile.getFull_path_on_device();
    int indexForSlash = oldPath.lastIndexOf("/");
    String parentPath = oldPath.substring(0, indexForSlash + 1);
    String timestamp = StorageHelper.formatTimeForFilename(time);
    String location = StorageHelper.formatLatLonGeoname(lat, lon, geoname);
    String username = daoHelper.getUserName();
    String newName = StorageHelper.formatFilename(username, location, timestamp);
    String newPath = parentPath + newName;

    pictureFile.setFilename(newName);
    pictureFile.setFull_path_on_device(newPath);

    File newFile = new File(newPath);
    oldFile.renameTo(newFile);

    Log.w(LOG_TAG, "Renamed " + oldPath + " to " + newPath);

    daoHelper.pictureFileDao.update(pictureFile);
    daoHelper.onPictureFilesUpdated();
  }
Exemplo n.º 10
0
  @Nullable
  private PictureFile tryFoundNewFileForUpload(String filename) {
    PictureFile file;
    Log.w(LOG_TAG, "Skip file, which previously failed for upload = " + filename);
    List<PictureFile> pictureFiles = daoHelper.selectPictureFilesForUploading();

    PictureFile newFileForUpload = null;

    for (PictureFile currentFile : pictureFiles) {
      String currentFileFilename = currentFile.getFilename();
      boolean currentPreviouslyFailed = isPreviouslyFailed(currentFileFilename);
      if (!currentPreviouslyFailed) {
        newFileForUpload = currentFile;
        break;
      }
    }
    file = newFileForUpload;
    return file;
  }
Exemplo n.º 11
0
 private boolean isAddressExist(PictureFile pictureFile) {
   boolean exist;
   String geoName = pictureFile.getGeo_name();
   exist = geoName != null && !geoName.isEmpty();
   return exist;
 }