Skip to content

Commit

Permalink
Fixing bug 3 [WIP]
Browse files Browse the repository at this point in the history
  • Loading branch information
davigonz committed Dec 6, 2017
1 parent 1541f62 commit 04ea076
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 40 deletions.
39 changes: 28 additions & 11 deletions src/com/owncloud/android/files/services/CameraUploadsHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ public class CameraUploadsHandler {
private static IndexedForest<CameraUploadsHandler> mPendingCameraUploads = new IndexedForest<>();
private static final long MILLISECONDS_INTERVAL_CAMERA_UPLOAD = 900000;

private CameraUploadsConfiguration mCameraUploadsConfiguration; // Camera uploads configuration, set by the user
private CameraUploadsConfiguration mCameraUploadsConfig; // Camera uploads configuration, set by the user
private Context mContext;

public CameraUploadsHandler(CameraUploadsConfiguration cameraUploadsConfiguration, Context context) {
mCameraUploadsConfiguration = cameraUploadsConfiguration;
mCameraUploadsConfig = cameraUploadsConfiguration;
mContext = context;
}

Expand All @@ -56,7 +56,7 @@ public CameraUploadsHandler(CameraUploadsConfiguration cameraUploadsConfiguratio
public void scheduleCameraUploadsSyncJob() {

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP &&
(mCameraUploadsConfiguration.isEnabledForPictures() || mCameraUploadsConfiguration.
(mCameraUploadsConfig.isEnabledForPictures() || mCameraUploadsConfig.
isEnabledForVideos())) {

// Initialize synchronization timestamps for pictures/videos, if needed
Expand All @@ -66,8 +66,8 @@ public void scheduleCameraUploadsSyncJob() {
CameraUploadsSyncJobService.class);
JobInfo.Builder builder;

int jobId = mPendingCameraUploads.buildKey(mCameraUploadsConfiguration.getUploadAccountName(),
mCameraUploadsConfiguration.getSourcePath()).hashCode();
int jobId = mPendingCameraUploads.buildKey(mCameraUploadsConfig.getUploadAccountName(),
mCameraUploadsConfig.getSourcePath()).hashCode();

builder = new JobInfo.Builder(jobId, serviceComponent);

Expand All @@ -79,7 +79,24 @@ public void scheduleCameraUploadsSyncJob() {
// Extra data
PersistableBundle extras = new PersistableBundle();

extras.putInt(Extras.EXTRA_SYNC_CAMERA_FOLDER_JOB_ID, jobId);
extras.putInt(Extras.EXTRA_CAMERA_UPLOADS_SYNC_JOB_ID, jobId);

extras.putString(Extras.EXTRA_ACCOUNT_NAME, mCameraUploadsConfig.getUploadAccountName());

if (mCameraUploadsConfig.isEnabledForPictures()) {
extras.putString(Extras.EXTRA_CAMERA_UPLOADS_PICTURES_PATH, mCameraUploadsConfig.
getUploadPathForPictures());
}

if (mCameraUploadsConfig.isEnabledForVideos()) {
extras.putString(Extras.EXTRA_CAMERA_UPLOADS_VIDEOS_PATH, mCameraUploadsConfig.
getUploadPathForVideos());
}

extras.putString(Extras.EXTRA_CAMERA_UPLOADS_SOURCE_PATH, mCameraUploadsConfig.getSourcePath());

extras.putInt(Extras.EXTRA_CAMERA_UPLOADS_BEHAVIOR_AFTER_UPLOAD, mCameraUploadsConfig.
getBehaviourAfterUpload());

builder.setExtras(extras);

Expand All @@ -101,7 +118,7 @@ public void scheduleCameraUploadsSyncJob() {
private void initializeCameraUploadSync() {

// Set synchronization timestamps not needed
if (!mCameraUploadsConfiguration.isEnabledForPictures() && !mCameraUploadsConfiguration.isEnabledForVideos()) {
if (!mCameraUploadsConfig.isEnabledForPictures() && !mCameraUploadsConfig.isEnabledForVideos()) {
return;
}

Expand All @@ -116,8 +133,8 @@ private void initializeCameraUploadSync() {

if (ocCameraUploadSync == null) { // No synchronization timestamp for pictures/videos yet

long firstPicturesTimeStamp = mCameraUploadsConfiguration.isEnabledForPictures() ? timeStamp : 0;
long firstVideosTimeStamp = mCameraUploadsConfiguration.isEnabledForVideos() ? timeStamp : 0;
long firstPicturesTimeStamp = mCameraUploadsConfig.isEnabledForPictures() ? timeStamp : 0;
long firstVideosTimeStamp = mCameraUploadsConfig.isEnabledForVideos() ? timeStamp : 0;

// Initialize synchronization timestamp for pictures or videos in database
OCCameraUploadSync firstOcCameraUploadSync = new OCCameraUploadSync(firstPicturesTimeStamp,
Expand All @@ -136,13 +153,13 @@ private void initializeCameraUploadSync() {
return;
}

if (ocCameraUploadSync.getPicturesLastSync() == 0 && mCameraUploadsConfiguration.isEnabledForPictures()) {
if (ocCameraUploadSync.getPicturesLastSync() == 0 && mCameraUploadsConfig.isEnabledForPictures()) {

// Pictures synchronization timestamp not initialized yet, initialize it
ocCameraUploadSync.setPicturesLastSync(timeStamp);
}

if (ocCameraUploadSync.getVideosLastSync() == 0 && mCameraUploadsConfiguration.isEnabledForVideos()) {
if (ocCameraUploadSync.getVideosLastSync() == 0 && mCameraUploadsConfig.isEnabledForVideos()) {

// Videos synchronization timestamp not initialized yet, initialize it
ocCameraUploadSync.setVideosLastSync(timeStamp);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import com.owncloud.android.authentication.AccountUtils;
import com.owncloud.android.datamodel.CameraUploadsSyncStorageManager;
import com.owncloud.android.db.OCCameraUploadSync;
import com.owncloud.android.db.PreferenceManager;
import com.owncloud.android.lib.common.utils.Log_OC;
import com.owncloud.android.operations.UploadFileOperation;
import com.owncloud.android.utils.Extras;
Expand Down Expand Up @@ -63,10 +62,13 @@ private static class CameraUploadsSyncJobTask extends AsyncTask<JobParameters, V

private final JobService mCameraUploadsSyncJobService;

private PreferenceManager.CameraUploadsConfiguration mConfig;
private Account mAccount;
private CameraUploadsSyncStorageManager mCameraUploadsSyncStorageManager;
private OCCameraUploadSync mOOCCameraUploadSync;
private OCCameraUploadSync mOCCameraUploadSync;
private String mCameraUploadsPicturesPath;
private String mCameraUploadsVideosPath;
private String mCameraUploadsSourcePath;
private int mCameraUploadsBehaviorAfterUpload;


public CameraUploadsSyncJobTask(JobService mCameraUploadsSyncJobService) {
Expand All @@ -76,21 +78,23 @@ public CameraUploadsSyncJobTask(JobService mCameraUploadsSyncJobService) {
@Override
protected JobParameters doInBackground(JobParameters... jobParams) {

mConfig = PreferenceManager.getCameraUploadsConfiguration(mCameraUploadsSyncJobService);
mAccount = AccountUtils.getOwnCloudAccountByName(mCameraUploadsSyncJobService, mConfig.
getUploadAccountName());
String accountName = jobParams[0].getExtras().getString(Extras.EXTRA_ACCOUNT_NAME);
mAccount = AccountUtils.getOwnCloudAccountByName(mCameraUploadsSyncJobService, accountName);
mCameraUploadsSyncStorageManager = new CameraUploadsSyncStorageManager(
mCameraUploadsSyncJobService.getContentResolver());

// Check if camera uploads have been disabled
if (!mConfig.isEnabledForPictures() && !mConfig.isEnabledForVideos()) {
mCameraUploadsPicturesPath = jobParams[0].getExtras().getString(Extras.EXTRA_CAMERA_UPLOADS_PICTURES_PATH);
mCameraUploadsVideosPath = jobParams[0].getExtras().getString(Extras.EXTRA_CAMERA_UPLOADS_VIDEOS_PATH);
mCameraUploadsSourcePath = jobParams[0].getExtras().getString(Extras.EXTRA_CAMERA_UPLOADS_SOURCE_PATH);
mCameraUploadsBehaviorAfterUpload = jobParams[0].getExtras().
getInt(Extras.EXTRA_CAMERA_UPLOADS_BEHAVIOR_AFTER_UPLOAD);

cancelPeriodicJob(jobParams);
syncFiles();

return jobParams[0];
}
if (mCameraUploadsPicturesPath == null && mCameraUploadsVideosPath == null) {

syncFiles();
cancelPeriodicJob(jobParams);
}

return jobParams[0];
}
Expand All @@ -106,7 +110,7 @@ protected void onPostExecute(JobParameters jobParameters) {
private void syncFiles() {

//Get local images and videos
String localCameraPath = mConfig.getSourcePath();
String localCameraPath = mCameraUploadsSourcePath;

File localFiles[] = new File[0];

Expand Down Expand Up @@ -155,34 +159,33 @@ private synchronized void handleFile(File localFile) {
return;
}

if (isImage && !mConfig.isEnabledForPictures()) {
if (isImage && mCameraUploadsPicturesPath == null) {
Log_OC.d(TAG, "Camera uploads disabled for images, ignoring " + fileName);
return;
}

if (isVideo && !mConfig.isEnabledForVideos()) {
if (isVideo && mCameraUploadsVideosPath == null) {
Log_OC.d(TAG, "Camera uploads disabled for videos, ignoring " + fileName);
return;
}

String remotePath = (isImage ? mConfig.getUploadPathForPictures() :
mConfig.getUploadPathForVideos()) + fileName;
String remotePath = (isImage ? mCameraUploadsPicturesPath : mCameraUploadsVideosPath) + fileName;

int createdBy = isImage ? UploadFileOperation.CREATED_AS_CAMERA_UPLOAD_PICTURE :
UploadFileOperation.CREATED_AS_CAMERA_UPLOAD_VIDEO;

String localPath = mConfig.getSourcePath() + File.separator + fileName;
String localPath = mCameraUploadsSourcePath + File.separator + fileName;

mOOCCameraUploadSync = mCameraUploadsSyncStorageManager.getCameraUploadSync(null, null,
mOCCameraUploadSync = mCameraUploadsSyncStorageManager.getCameraUploadSync(null, null,
null);

if (mOOCCameraUploadSync == null) {
if (mOCCameraUploadSync == null) {
Log_OC.d(TAG, "There's no timestamp to compare with in database yet, not continue");
}

// Check file timestamp
if (isImage && localFile.lastModified() <= mOOCCameraUploadSync.getPicturesLastSync() ||
isVideo && localFile.lastModified() <= mOOCCameraUploadSync.getVideosLastSync()) {
if (isImage && localFile.lastModified() <= mOCCameraUploadSync.getPicturesLastSync() ||
isVideo && localFile.lastModified() <= mOCCameraUploadSync.getVideosLastSync()) {
Log_OC.i(TAG, "File " + localPath + " created before period to check, ignoring");
return;
}
Expand All @@ -193,7 +196,7 @@ private synchronized void handleFile(File localFile) {
mAccount,
localPath,
remotePath,
mConfig.getBehaviourAfterUpload(),
mCameraUploadsBehaviorAfterUpload,
mimeType,
true, // create parent folder if not existent
createdBy
Expand Down Expand Up @@ -221,8 +224,8 @@ private synchronized void handleFile(File localFile) {
*/
private void updateTimestamps(boolean isImage, boolean isVideo, long fileTimestamp) {

long picturesTimestamp = mOOCCameraUploadSync.getPicturesLastSync();
long videosTimestamp = mOOCCameraUploadSync.getVideosLastSync();
long picturesTimestamp = mOCCameraUploadSync.getPicturesLastSync();
long videosTimestamp = mOCCameraUploadSync.getVideosLastSync();

if (isImage) {

Expand All @@ -241,7 +244,7 @@ private void updateTimestamps(boolean isImage, boolean isVideo, long fileTimesta
OCCameraUploadSync newOCCameraUploadSync = new OCCameraUploadSync(picturesTimestamp,
videosTimestamp);

newOCCameraUploadSync.setId(mOOCCameraUploadSync.getId());
newOCCameraUploadSync.setId(mOCCameraUploadSync.getId());

mCameraUploadsSyncStorageManager.updateCameraUploadSync(newOCCameraUploadSync);
}
Expand All @@ -252,7 +255,7 @@ private void updateTimestamps(boolean isImage, boolean isVideo, long fileTimesta
*/
private void cancelPeriodicJob(JobParameters[] jobParams) {

int jobId = jobParams[0].getExtras().getInt(Extras.EXTRA_SYNC_CAMERA_FOLDER_JOB_ID);
int jobId = jobParams[0].getExtras().getInt(Extras.EXTRA_CAMERA_UPLOADS_SYNC_JOB_ID);

JobScheduler jobScheduler = (JobScheduler)mCameraUploadsSyncJobService.getSystemService(
Context.JOB_SCHEDULER_SERVICE);
Expand Down
9 changes: 7 additions & 2 deletions src/com/owncloud/android/utils/Extras.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,10 @@ public class Extras {
public static final String EXTRA_UPLOAD_RESULT = "RESULT";

// for Camera uploads
public static final String EXTRA_SYNC_CAMERA_FOLDER_JOB_ID = "SYNC_CAMERA_FOLDER_JOB_ID";
}
public static final String EXTRA_CAMERA_UPLOADS_SYNC_JOB_ID = "EXTRA_CAMERA_UPLOADS_SYNC_JOB_ID";
public static final String EXTRA_CAMERA_UPLOADS_PICTURES_PATH = "EXTRA_CAMERA_UPLOADS_PICTURES_PATH";
public static final String EXTRA_CAMERA_UPLOADS_VIDEOS_PATH = "EXTRA_CAMERA_UPLOADS_VIDEOS_PATH";
public static final String EXTRA_CAMERA_UPLOADS_SOURCE_PATH = "EXTRA_CAMERA_UPLOADS_SOURCE_PATH";
public static final String EXTRA_CAMERA_UPLOADS_BEHAVIOR_AFTER_UPLOAD =
"EXTRA_CAMERA_UPLOADS_BEHAVIOR_AFTER_UPLOAD";
}

0 comments on commit 04ea076

Please sign in to comment.