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();
  }
  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 void addInFailed(String filename) {
   Context applicationContext = getApplicationContext();
   InternetHelper internetHelper = InternetHelper.getInstance(applicationContext);
   DAOHelper instance = DAOHelper.getInstance(applicationContext);
   boolean wifiOnly = instance.getWifiOnlyState();
   if (internetHelper.canUpload(wifiOnly)) {
     synchronized (failedUploadsList) {
       if (!failedUploadsList.contains(filename)) {
         failedUploadsList.add(filename);
       }
     }
   }
 }
  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 void uploadFiles() {

    boolean needWork = true;
    while (needWork) {

      boolean uploading = isUploading;
      boolean connected = internetHelper.isConnected();
      boolean wifiOnlyState = daoHelper.getWifiOnlyState();
      boolean wifiConnected = internetHelper.isWifiConnected();

      boolean canUploadWifi = internetHelper.canUploadWifi();
      boolean canUploadCellular = internetHelper.canUploadCellular(wifiOnlyState);
      boolean canUpload = internetHelper.canUpload(wifiOnlyState);

      Log.w(
          LOG_TAG,
          "onUpload files"
              + " isUploading = "
              + uploading
              + " conneted = "
              + connected
              + " wifionly = "
              + wifiOnlyState
              + " wifi connected = "
              + wifiConnected
              + " canUpload = "
              + canUpload
              + " canUploadWifi = "
              + canUploadWifi
              + " canUploadCellular = "
              + canUploadCellular);

      if (!uploading) {
        uploadCycleStarted();
        checkFilesForDelete();
        if (canUpload) {
          boolean uploadStarted = startUploadFile();
          while (isUploading) {
            doThreadSleepAndDelay();
          }
          if (!uploadStarted) {
            needWork = false;
          }

        } else {
          needWork = false;
          uploadCycleFinished();
        }
      } else {
        needWork = false;
      }
      Thread.yield();
    }
  }
  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();
  }
  @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;
  }
  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    Log.w(LOG_TAG, "service started");
    Context context = getApplicationContext();
    if (internetHelper == null) {
      internetHelper = InternetHelper.getInstance(context);
    }
    if (daoHelper == null) {
      daoHelper = DAOHelper.getInstance(context);
    }

    if (awsUploadHelper == null) {
      awsUploadHelper = new AWSUploadHelper(context);
    }

    synchronized (failedUploadsList) {
      failedUploadsList.clear();
    }

    return super.onStartCommand(intent, flags, startId);
  }
 private void checkFilesForDelete() {
   Log.w(LOG_TAG, "checkfilesfordelete");
   daoHelper.deleteUploadedFilesOnDevice();
 }