|
@@ -23,13 +23,18 @@
|
|
|
*/
|
|
|
package com.owncloud.android.datamodel;
|
|
|
|
|
|
+import android.content.ContentProviderOperation;
|
|
|
+import android.content.ContentProviderResult;
|
|
|
import android.content.ContentResolver;
|
|
|
import android.content.ContentValues;
|
|
|
+import android.content.OperationApplicationException;
|
|
|
import android.database.Cursor;
|
|
|
import android.net.Uri;
|
|
|
+import android.os.RemoteException;
|
|
|
|
|
|
import com.nextcloud.client.account.CurrentAccountProvider;
|
|
|
import com.nextcloud.client.account.User;
|
|
|
+import com.owncloud.android.MainApp;
|
|
|
import com.owncloud.android.db.OCUpload;
|
|
|
import com.owncloud.android.db.ProviderMeta.ProviderTableMeta;
|
|
|
import com.owncloud.android.db.UploadResult;
|
|
@@ -42,6 +47,7 @@ import com.owncloud.android.operations.UploadFileOperation;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Arrays;
|
|
|
import java.util.Calendar;
|
|
|
+import java.util.List;
|
|
|
import java.util.Locale;
|
|
|
import java.util.Observable;
|
|
|
|
|
@@ -82,6 +88,52 @@ public class UploadsStorageManager extends Observable {
|
|
|
public long storeUpload(OCUpload ocUpload) {
|
|
|
Log_OC.v(TAG, "Inserting " + ocUpload.getLocalPath() + " with status=" + ocUpload.getUploadStatus());
|
|
|
|
|
|
+ ContentValues cv = getContentValues(ocUpload);
|
|
|
+ Uri result = getDB().insert(ProviderTableMeta.CONTENT_URI_UPLOADS, cv);
|
|
|
+
|
|
|
+ Log_OC.d(TAG, "storeUpload returns with: " + result + " for file: " + ocUpload.getLocalPath());
|
|
|
+ if (result == null) {
|
|
|
+ Log_OC.e(TAG, "Failed to insert item " + ocUpload.getLocalPath() + " into upload db.");
|
|
|
+ return -1;
|
|
|
+ } else {
|
|
|
+ long new_id = Long.parseLong(result.getPathSegments().get(1));
|
|
|
+ ocUpload.setUploadId(new_id);
|
|
|
+ notifyObserversNow();
|
|
|
+ return new_id;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public long[] storeUploads(final List<OCUpload> ocUploads) {
|
|
|
+ Log_OC.v(TAG, "Inserting " + ocUploads.size() + " uploads");
|
|
|
+ ArrayList<ContentProviderOperation> operations = new ArrayList<>(ocUploads.size());
|
|
|
+ for (OCUpload ocUpload : ocUploads) {
|
|
|
+ final ContentProviderOperation operation = ContentProviderOperation
|
|
|
+ .newInsert(ProviderTableMeta.CONTENT_URI_UPLOADS)
|
|
|
+ .withValues(getContentValues(ocUpload))
|
|
|
+ .build();
|
|
|
+ operations.add(operation);
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ final ContentProviderResult[] contentProviderResults = getDB().applyBatch(MainApp.getAuthority(), operations);
|
|
|
+ final long[] newIds = new long[ocUploads.size()];
|
|
|
+ for (int i = 0; i < contentProviderResults.length; i++) {
|
|
|
+ final ContentProviderResult result = contentProviderResults[i];
|
|
|
+ final long new_id = Long.parseLong(result.uri.getPathSegments().get(1));
|
|
|
+ ocUploads.get(i).setUploadId(new_id);
|
|
|
+ newIds[i] = new_id;
|
|
|
+ }
|
|
|
+ notifyObserversNow();
|
|
|
+ return newIds;
|
|
|
+ } catch (OperationApplicationException | RemoteException e) {
|
|
|
+ Log_OC.e(TAG, "Error inserting uploads", e);
|
|
|
+ }
|
|
|
+
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ @NonNull
|
|
|
+ private ContentValues getContentValues(OCUpload ocUpload) {
|
|
|
ContentValues cv = new ContentValues();
|
|
|
cv.put(ProviderTableMeta.UPLOADS_LOCAL_PATH, ocUpload.getLocalPath());
|
|
|
cv.put(ProviderTableMeta.UPLOADS_REMOTE_PATH, ocUpload.getRemotePath());
|
|
@@ -96,20 +148,10 @@ public class UploadsStorageManager extends Observable {
|
|
|
cv.put(ProviderTableMeta.UPLOADS_IS_WHILE_CHARGING_ONLY, ocUpload.isWhileChargingOnly() ? 1 : 0);
|
|
|
cv.put(ProviderTableMeta.UPLOADS_IS_WIFI_ONLY, ocUpload.isUseWifiOnly() ? 1 : 0);
|
|
|
cv.put(ProviderTableMeta.UPLOADS_FOLDER_UNLOCK_TOKEN, ocUpload.getFolderUnlockToken());
|
|
|
- Uri result = getDB().insert(ProviderTableMeta.CONTENT_URI_UPLOADS, cv);
|
|
|
-
|
|
|
- Log_OC.d(TAG, "storeUpload returns with: " + result + " for file: " + ocUpload.getLocalPath());
|
|
|
- if (result == null) {
|
|
|
- Log_OC.e(TAG, "Failed to insert item " + ocUpload.getLocalPath() + " into upload db.");
|
|
|
- return -1;
|
|
|
- } else {
|
|
|
- long new_id = Long.parseLong(result.getPathSegments().get(1));
|
|
|
- ocUpload.setUploadId(new_id);
|
|
|
- notifyObserversNow();
|
|
|
- return new_id;
|
|
|
- }
|
|
|
+ return cv;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
/**
|
|
|
* Update an upload object in DB.
|
|
|
*
|