Try to fix 4g

Signed-off-by: Mario Danic <mario@lovelyhq.com>
This commit is contained in:
Mario Danic 2017-07-20 22:33:27 +02:00 committed by AndyScherzinger
parent 95d75c5bb0
commit fac7c0eafb
No known key found for this signature in database
GPG key ID: 6CADC7E3523C308B
2 changed files with 27 additions and 76 deletions

View file

@ -21,6 +21,7 @@
package com.owncloud.android.jobs;
import android.accounts.Account;
import android.content.ContentResolver;
import android.content.Context;
import android.media.ExifInterface;
@ -30,16 +31,18 @@ import android.text.TextUtils;
import com.evernote.android.job.Job;
import com.evernote.android.job.JobManager;
import com.evernote.android.job.JobRequest;
import com.evernote.android.job.util.support.PersistableBundleCompat;
import com.owncloud.android.MainApp;
import com.owncloud.android.authentication.AccountUtils;
import com.owncloud.android.datamodel.FilesystemDataProvider;
import com.owncloud.android.datamodel.MediaFolder;
import com.owncloud.android.datamodel.SyncedFolder;
import com.owncloud.android.datamodel.SyncedFolderProvider;
import com.owncloud.android.files.services.FileUploader;
import com.owncloud.android.lib.common.utils.Log_OC;
import com.owncloud.android.operations.UploadFileOperation;
import com.owncloud.android.utils.FileStorageUtils;
import com.owncloud.android.utils.FilesSyncHelper;
import com.owncloud.android.utils.MimeTypeUtil;
import java.io.File;
import java.io.IOException;
@ -59,6 +62,7 @@ public class FilesSyncJob extends Job {
final ContentResolver contentResolver = context.getContentResolver();
PowerManager powerManager = (PowerManager) context.getSystemService(context.POWER_SERVICE);
FileUploader.UploadRequester requester = new FileUploader.UploadRequester();
PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
TAG);
wakeLock.acquire();
@ -104,32 +108,30 @@ public class FilesSyncJob extends Job {
}
}
PersistableBundleCompat bundle = new PersistableBundleCompat();
boolean needsCharging = syncedFolder.getChargingOnly();
boolean needsWifi = syncedFolder.getWifiOnly();
bundle.putString(AutoUploadJob.LOCAL_PATH, file.getAbsolutePath());
bundle.putString(AutoUploadJob.REMOTE_PATH, FileStorageUtils.getInstantUploadFilePath(
currentLocale,
syncedFolder.getRemotePath(), file.getName(),
lastModificationTime,
syncedFolder.getSubfolderByDate()));
bundle.putString(AutoUploadJob.ACCOUNT, syncedFolder.getAccount());
bundle.putInt(AutoUploadJob.UPLOAD_BEHAVIOUR, syncedFolder.getUploadAction());
bundle.putBoolean(AutoUploadJob.REQUIRES_WIFI, needsWifi);
bundle.putBoolean(AutoUploadJob.REQUIRES_CHARGING, needsCharging);
String mimeType = MimeTypeUtil.getBestMimeTypeByFilename(file.getAbsolutePath());
new JobRequest.Builder(AutoUploadJob.TAG)
.setExecutionWindow(10_000L, 10_000L)
.setRequiresCharging(needsCharging)
.setRequiredNetworkType(syncedFolder.getWifiOnly() ? JobRequest.NetworkType.UNMETERED :
JobRequest.NetworkType.CONNECTED)
.setExtras(bundle)
.setRequirementsEnforced(true)
.setUpdateCurrent(false)
.build()
.schedule();
Account account = AccountUtils.getOwnCloudAccountByName(context, syncedFolder.getAccount());
requester.uploadFileWithOverwrite(
context,
account,
file.getAbsolutePath(),
FileStorageUtils.getInstantUploadFilePath(
currentLocale,
syncedFolder.getRemotePath(), file.getName(),
lastModificationTime,
syncedFolder.getSubfolderByDate()),
syncedFolder.getUploadAction(),
mimeType,
true, // create parent folder if not existent
UploadFileOperation.CREATED_AS_INSTANT_PICTURE,
needsWifi,
needsCharging,
true
);
filesystemDataProvider.updateFilesystemFileAsSentForUpload(path,
Long.toString(syncedFolder.getId()));

View file

@ -29,7 +29,6 @@ import android.provider.MediaStore;
import android.text.TextUtils;
import android.util.Log;
import com.evernote.android.job.JobManager;
import com.evernote.android.job.JobRequest;
import com.evernote.android.job.util.Device;
import com.owncloud.android.MainApp;
@ -43,7 +42,6 @@ import com.owncloud.android.datamodel.UploadsStorageManager;
import com.owncloud.android.db.OCUpload;
import com.owncloud.android.db.UploadResult;
import com.owncloud.android.files.services.FileUploader;
import com.owncloud.android.jobs.AutoUploadJob;
import com.owncloud.android.operations.UploadFileOperation;
import org.lukhnos.nnio.file.FileVisitResult;
@ -196,49 +194,6 @@ public class FilesSyncHelper {
boolean accountExists;
boolean fileExists;
for (JobRequest jobRequest : JobManager.instance().getAllJobRequestsForTag(AutoUploadJob.TAG)) {
restartedInCurrentIteration = false;
accountExists = false;
fileExists = new File(jobRequest.getExtras().getString(AutoUploadJob.LOCAL_PATH, "")).exists();
// check if accounts still exists
for (Account account : AccountUtils.getAccounts(context)) {
if (account.name.equals(jobRequest.getExtras().getString(AutoUploadJob.ACCOUNT, ""))) {
accountExists = true;
break;
}
}
if (accountExists && fileExists) {
// Handle case of charging
if (jobRequest.requiresCharging() && Device.isCharging(context)) {
if (jobRequest.requiredNetworkType().equals(JobRequest.NetworkType.CONNECTED) &&
!Device.getNetworkType(context).equals(JobRequest.NetworkType.ANY)) {
jobRequest.cancelAndEdit().build().schedule();
restartedInCurrentIteration = true;
} else if (jobRequest.requiredNetworkType().equals(JobRequest.NetworkType.UNMETERED) &&
Device.getNetworkType(context).equals(JobRequest.NetworkType.UNMETERED)) {
jobRequest.cancelAndEdit().build().schedule();
restartedInCurrentIteration = true;
}
}
// Handle case of wifi
if (!restartedInCurrentIteration) {
if (jobRequest.requiredNetworkType().equals(JobRequest.NetworkType.CONNECTED) &&
!Device.getNetworkType(context).equals(JobRequest.NetworkType.ANY)) {
jobRequest.cancelAndEdit().build().schedule();
} else if (jobRequest.requiredNetworkType().equals(JobRequest.NetworkType.UNMETERED) &&
Device.getNetworkType(context).equals(JobRequest.NetworkType.UNMETERED)) {
jobRequest.cancelAndEdit().build().schedule();
}
}
} else {
JobManager.instance().cancel(jobRequest.getJobId());
}
}
UploadsStorageManager uploadsStorageManager = new UploadsStorageManager(context.getContentResolver(), context);
OCUpload[] failedUploads = uploadsStorageManager.getFailedUploads();
@ -286,16 +241,10 @@ public class FilesSyncHelper {
}
}
} else {
uploadsStorageManager.removeUpload(failedUpload);
}
} else {
if (accountExists && fileExists) {
uploadRequester.retry(context, failedUpload);
} else {
uploadsStorageManager.removeUpload(failedUpload);
}
}
}
}
}
}