update job to obey syncedfolder settings

This commit is contained in:
tobiasKaminsky 2016-10-12 21:41:44 +02:00
parent 179c695128
commit c3a8b0c450
3 changed files with 21 additions and 18 deletions

View file

@ -40,6 +40,7 @@ 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.MimetypeIconUtil;
import java.io.File;
import java.util.Date;
@ -51,12 +52,9 @@ import java.util.Date;
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public class SyncedFolderJobService extends JobService {
private static final String TAG = "SyncedFolderJobService";
private Context mContext;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Tobi why is this null?
mContext = MainApp.getAppContext();
return START_NOT_STICKY;
}
@ -64,17 +62,17 @@ public class SyncedFolderJobService extends JobService {
public boolean onStartJob(JobParameters params) {
Log_OC.d(TAG, "startJob: " + params.getJobId());
// TODO Tobi just for testing!
Context context = MainApp.getAppContext();
Account account = AccountUtils.getCurrentOwnCloudAccount(context);
PersistableBundle bundle = params.getExtras();
String filePath = bundle.getString("filePath");
String remoteFolder = bundle.getString("remoteFolder");
String remoteFolder = bundle.getString("remotePath");
Long dateTaken = bundle.getLong("dateTaken");
Boolean subfolderByDate = bundle.getInt("subfolderByDate") == 1;
Account account = AccountUtils.getOwnCloudAccountByName(context, bundle.getString("account"));
Integer uploadBehaviour = bundle.getInt("uploadBehaviour");
File file = new File(filePath);
String mimeType = MimetypeIconUtil.getBestMimeTypeByFilename(file.getAbsolutePath());
FileUploader.UploadRequester requester = new FileUploader.UploadRequester();
requester.uploadNewFile(
@ -82,8 +80,8 @@ public class SyncedFolderJobService extends JobService {
account,
filePath,
FileStorageUtils.getInstantUploadFilePath(remoteFolder, file.getName(), dateTaken, subfolderByDate),
FileUploader.LOCAL_BEHAVIOUR_FORGET,
"image/jpg",
uploadBehaviour,
mimeType,
true, // create parent folder if not existent
UploadFileOperation.CREATED_AS_INSTANT_PICTURE
);

View file

@ -11,6 +11,7 @@ import android.os.PersistableBundle;
import android.util.Log;
import com.owncloud.android.MainApp;
import com.owncloud.android.datamodel.SyncedFolder;
import com.owncloud.android.lib.common.utils.Log_OC;
import com.owncloud.android.services.SyncedFolderJobService;
import com.owncloud.android.utils.RecursiveFileObserver;
@ -24,15 +25,15 @@ class SyncedFolderObserver extends RecursiveFileObserver {
private static final int MY_BACKGROUND_JOB = 0;
public static final String TAG = "SyncedFolderObserver";
private String remoteFolder;
private SyncedFolder syncedFolder;
public SyncedFolderObserver(String path, String remoteFolder) {
super(path, FileObserver.CREATE + FileObserver.MOVED_TO);
public SyncedFolderObserver(SyncedFolder syncedFolder) {
super(syncedFolder.getLocalPath(), FileObserver.CREATE + FileObserver.MOVED_TO);
context = MainApp.getAppContext();
this.remoteFolder = remoteFolder;
Log_OC.d("SyncedFolderObserver", "Started watching: " + path);
this.syncedFolder = syncedFolder;
Log_OC.d("SyncedFolderObserver", "Started watching: " + syncedFolder.getLocalPath());
}
@ -40,15 +41,20 @@ class SyncedFolderObserver extends RecursiveFileObserver {
@Override
public void onEvent(int event, String path) {
PersistableBundle bundle = new PersistableBundle();
// TODO extract
bundle.putString("filePath", path);
bundle.putString("remoteFolder", remoteFolder);
bundle.putString("remotePath", syncedFolder.getRemotePath());
bundle.putLong("dateTaken", new Date().getTime());
bundle.putString("account", syncedFolder.getAccount());
bundle.putInt("uploadBehaviour", syncedFolder.getUploadAction());
bundle.putInt("subfolderByDate", syncedFolder.getSubfolderByDate() ? 1 : 0);
JobScheduler js = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
JobInfo job = new JobInfo.Builder(
MY_BACKGROUND_JOB,
new ComponentName(context, SyncedFolderJobService.class))
.setRequiresCharging(false)
.setRequiresCharging(syncedFolder.getChargingOnly())
// TODO change to UNMETERED after developing
.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
.setExtras(bundle)
.build();

View file

@ -24,8 +24,7 @@ public class SyncedFolderObserverService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
for (SyncedFolder syncedFolder : mProvider.getSyncedFolders()) {
SyncedFolderObserver observer = new SyncedFolderObserver(syncedFolder.getLocalPath(),
syncedFolder.getRemotePath());
SyncedFolderObserver observer = new SyncedFolderObserver(syncedFolder);
observer.startWatching();
syncedFolderObservers.add(observer);