UploadsStorageManager.java 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. /*
  2. * ownCloud Android client application
  3. *
  4. * @author LukeOwncloud
  5. * @author David A. Velasco
  6. * @author masensio
  7. * Copyright (C) 2016 ownCloud Inc.
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. package com.owncloud.android.datamodel;
  22. import android.accounts.Account;
  23. import android.content.ContentResolver;
  24. import android.content.ContentValues;
  25. import android.content.Context;
  26. import android.database.Cursor;
  27. import android.net.Uri;
  28. import com.owncloud.android.authentication.AccountUtils;
  29. import com.owncloud.android.db.OCUpload;
  30. import com.owncloud.android.db.ProviderMeta.ProviderTableMeta;
  31. import com.owncloud.android.db.UploadResult;
  32. import com.owncloud.android.files.services.FileUploader;
  33. import com.owncloud.android.lib.common.operations.RemoteOperationResult;
  34. import com.owncloud.android.lib.common.utils.Log_OC;
  35. import com.owncloud.android.operations.UploadFileOperation;
  36. import java.util.Calendar;
  37. import java.util.Observable;
  38. /**
  39. * Database helper for storing list of files to be uploaded, including status
  40. * information for each file.
  41. */
  42. public class UploadsStorageManager extends Observable {
  43. private ContentResolver mContentResolver;
  44. private Context mContext;
  45. private static final String AND = " AND ";
  46. static private final String TAG = UploadsStorageManager.class.getSimpleName();
  47. public enum UploadStatus {
  48. /**
  49. * Upload currently in progress or scheduled to be executed.
  50. */
  51. UPLOAD_IN_PROGRESS(0),
  52. /**
  53. * Last upload failed.
  54. */
  55. UPLOAD_FAILED(1),
  56. /**
  57. * Upload was successful.
  58. */
  59. UPLOAD_SUCCEEDED(2);
  60. private final int value;
  61. UploadStatus(int value) {
  62. this.value = value;
  63. }
  64. public int getValue() {
  65. return value;
  66. }
  67. public static UploadStatus fromValue(int value) {
  68. switch (value) {
  69. case 0:
  70. return UPLOAD_IN_PROGRESS;
  71. case 1:
  72. return UPLOAD_FAILED;
  73. case 2:
  74. return UPLOAD_SUCCEEDED;
  75. }
  76. return null;
  77. }
  78. }
  79. public UploadsStorageManager(ContentResolver contentResolver, Context context) {
  80. if (contentResolver == null) {
  81. throw new IllegalArgumentException("Cannot create an instance with a NULL contentResolver");
  82. }
  83. mContentResolver = contentResolver;
  84. mContext = context;
  85. }
  86. /**
  87. * Stores an upload object in DB.
  88. *
  89. * @param ocUpload Upload object to store
  90. * @return upload id, -1 if the insert process fails.
  91. */
  92. public long storeUpload(OCUpload ocUpload) {
  93. Log_OC.v(TAG, "Inserting " + ocUpload.getLocalPath() + " with status=" + ocUpload.getUploadStatus());
  94. ContentValues cv = new ContentValues();
  95. cv.put(ProviderTableMeta.UPLOADS_LOCAL_PATH, ocUpload.getLocalPath());
  96. cv.put(ProviderTableMeta.UPLOADS_REMOTE_PATH, ocUpload.getRemotePath());
  97. cv.put(ProviderTableMeta.UPLOADS_ACCOUNT_NAME, ocUpload.getAccountName());
  98. cv.put(ProviderTableMeta.UPLOADS_FILE_SIZE, ocUpload.getFileSize());
  99. cv.put(ProviderTableMeta.UPLOADS_STATUS, ocUpload.getUploadStatus().value);
  100. cv.put(ProviderTableMeta.UPLOADS_LOCAL_BEHAVIOUR, ocUpload.getLocalAction());
  101. cv.put(ProviderTableMeta.UPLOADS_FORCE_OVERWRITE, ocUpload.isForceOverwrite() ? 1 : 0);
  102. cv.put(ProviderTableMeta.UPLOADS_IS_CREATE_REMOTE_FOLDER, ocUpload.isCreateRemoteFolder() ? 1 : 0);
  103. cv.put(ProviderTableMeta.UPLOADS_LAST_RESULT, ocUpload.getLastResult().getValue());
  104. cv.put(ProviderTableMeta.UPLOADS_CREATED_BY, ocUpload.getCreadtedBy());
  105. cv.put(ProviderTableMeta.UPLOADS_IS_WHILE_CHARGING_ONLY, ocUpload.isWhileChargingOnly() ? 1 : 0);
  106. cv.put(ProviderTableMeta.UPLOADS_IS_WIFI_ONLY, ocUpload.isUseWifiOnly() ? 1 : 0);
  107. cv.put(ProviderTableMeta.UPLOADS_FOLDER_UNLOCK_TOKEN, ocUpload.getFolderUnlockToken());
  108. Uri result = getDB().insert(ProviderTableMeta.CONTENT_URI_UPLOADS, cv);
  109. Log_OC.d(TAG, "storeUpload returns with: " + result + " for file: " + ocUpload.getLocalPath());
  110. if (result == null) {
  111. Log_OC.e(TAG, "Failed to insert item " + ocUpload.getLocalPath() + " into upload db.");
  112. return -1;
  113. } else {
  114. long new_id = Long.parseLong(result.getPathSegments().get(1));
  115. ocUpload.setUploadId(new_id);
  116. notifyObserversNow();
  117. return new_id;
  118. }
  119. }
  120. /**
  121. * Update an upload object in DB.
  122. *
  123. * @param ocUpload Upload object with state to update
  124. * @return num of updated uploads.
  125. */
  126. public int updateUpload(OCUpload ocUpload) {
  127. Log_OC.v(TAG, "Updating " + ocUpload.getLocalPath() + " with status=" + ocUpload.getUploadStatus());
  128. ContentValues cv = new ContentValues();
  129. cv.put(ProviderTableMeta.UPLOADS_LOCAL_PATH, ocUpload.getLocalPath());
  130. cv.put(ProviderTableMeta.UPLOADS_REMOTE_PATH, ocUpload.getRemotePath());
  131. cv.put(ProviderTableMeta.UPLOADS_ACCOUNT_NAME, ocUpload.getAccountName());
  132. cv.put(ProviderTableMeta.UPLOADS_STATUS, ocUpload.getUploadStatus().value);
  133. cv.put(ProviderTableMeta.UPLOADS_LAST_RESULT, ocUpload.getLastResult().getValue());
  134. cv.put(ProviderTableMeta.UPLOADS_UPLOAD_END_TIMESTAMP, ocUpload.getUploadEndTimestamp());
  135. cv.put(ProviderTableMeta.UPLOADS_FILE_SIZE, ocUpload.getFileSize());
  136. cv.put(ProviderTableMeta.UPLOADS_FOLDER_UNLOCK_TOKEN, ocUpload.getFolderUnlockToken());
  137. int result = getDB().update(ProviderTableMeta.CONTENT_URI_UPLOADS,
  138. cv,
  139. ProviderTableMeta._ID + "=?",
  140. new String[]{String.valueOf(ocUpload.getUploadId())}
  141. );
  142. Log_OC.d(TAG, "updateUpload returns with: " + result + " for file: " + ocUpload.getLocalPath());
  143. if (result != 1) {
  144. Log_OC.e(TAG, "Failed to update item " + ocUpload.getLocalPath() + " into upload db.");
  145. } else {
  146. notifyObserversNow();
  147. }
  148. return result;
  149. }
  150. private int updateUploadInternal(Cursor c, UploadStatus status, UploadResult result, String remotePath,
  151. String localPath) {
  152. int r = 0;
  153. while (c.moveToNext()) {
  154. // read upload object and update
  155. OCUpload upload = createOCUploadFromCursor(c);
  156. String path = c.getString(c.getColumnIndex(ProviderTableMeta.UPLOADS_LOCAL_PATH));
  157. Log_OC.v(
  158. TAG,
  159. "Updating " + path + " with status:" + status + " and result:"
  160. + (result == null ? "null" : result.toString()) + " (old:"
  161. + upload.toFormattedString() + ")");
  162. upload.setUploadStatus(status);
  163. upload.setLastResult(result);
  164. upload.setRemotePath(remotePath);
  165. if (localPath != null) {
  166. upload.setLocalPath(localPath);
  167. }
  168. if (status == UploadStatus.UPLOAD_SUCCEEDED) {
  169. upload.setUploadEndTimestamp(Calendar.getInstance().getTimeInMillis());
  170. }
  171. // store update upload object to db
  172. r = updateUpload(upload);
  173. }
  174. return r;
  175. }
  176. /**
  177. * Update upload status of file uniquely referenced by id.
  178. *
  179. * @param id upload id.
  180. * @param status new status.
  181. * @param result new result of upload operation
  182. * @param remotePath path of the file to upload in the ownCloud storage
  183. * @param localPath path of the file to upload in the device storage
  184. * @return 1 if file status was updated, else 0.
  185. */
  186. private int updateUploadStatus(long id, UploadStatus status, UploadResult result, String remotePath,
  187. String localPath) {
  188. //Log_OC.v(TAG, "Updating "+filepath+" with uploadStatus="+status +" and result="+result);
  189. int returnValue = 0;
  190. Cursor c = getDB().query(
  191. ProviderTableMeta.CONTENT_URI_UPLOADS,
  192. null,
  193. ProviderTableMeta._ID + "=?",
  194. new String[]{String.valueOf(id)},
  195. null
  196. );
  197. if (c != null) {
  198. if (c.getCount() != 1) {
  199. Log_OC.e(TAG, c.getCount() + " items for id=" + id
  200. + " available in UploadDb. Expected 1. Failed to update upload db.");
  201. } else {
  202. returnValue = updateUploadInternal(c, status, result, remotePath, localPath);
  203. }
  204. c.close();
  205. } else {
  206. Log_OC.e(TAG, "Cursor is null");
  207. }
  208. return returnValue;
  209. }
  210. /**
  211. * Should be called when some value of this DB was changed. All observers
  212. * are informed.
  213. */
  214. public void notifyObserversNow() {
  215. Log_OC.d(TAG, "notifyObserversNow");
  216. setChanged();
  217. notifyObservers();
  218. }
  219. /**
  220. * Remove an upload from the uploads list, known its target account and remote path.
  221. *
  222. * @param upload Upload instance to remove from persisted storage.
  223. *
  224. * @return true when the upload was stored and could be removed.
  225. */
  226. public int removeUpload(OCUpload upload) {
  227. int result = getDB().delete(
  228. ProviderTableMeta.CONTENT_URI_UPLOADS,
  229. ProviderTableMeta._ID + "=?",
  230. new String[]{Long.toString(upload.getUploadId())}
  231. );
  232. Log_OC.d(TAG, "delete returns " + result + " for upload " + upload);
  233. if (result > 0) {
  234. notifyObserversNow();
  235. }
  236. return result;
  237. }
  238. /**
  239. * Remove an upload from the uploads list, known its target account and remote path.
  240. *
  241. * @param accountName Name of the OC account target of the upload to remove.
  242. * @param remotePath Absolute path in the OC account target of the upload to remove.
  243. * @return true when one or more upload entries were removed
  244. */
  245. public int removeUpload(String accountName, String remotePath) {
  246. int result = getDB().delete(
  247. ProviderTableMeta.CONTENT_URI_UPLOADS,
  248. ProviderTableMeta.UPLOADS_ACCOUNT_NAME + "=? AND " + ProviderTableMeta.UPLOADS_REMOTE_PATH + "=?",
  249. new String[]{accountName, remotePath}
  250. );
  251. Log_OC.d(TAG, "delete returns " + result + " for file " + remotePath + " in " + accountName);
  252. if (result > 0) {
  253. notifyObserversNow();
  254. }
  255. return result;
  256. }
  257. /**
  258. * Remove all the uploads of a given account from the uploads list.
  259. *
  260. * @param accountName Name of the OC account target of the uploads to remove.
  261. * @return true when one or more upload entries were removed
  262. */
  263. public int removeUploads(String accountName) {
  264. int result = getDB().delete(
  265. ProviderTableMeta.CONTENT_URI_UPLOADS,
  266. ProviderTableMeta.UPLOADS_ACCOUNT_NAME + "=?",
  267. new String[]{accountName}
  268. );
  269. Log_OC.d(TAG, "delete returns " + result + " for uploads in " + accountName);
  270. if (result > 0) {
  271. notifyObserversNow();
  272. }
  273. return result;
  274. }
  275. public OCUpload[] getAllStoredUploads() {
  276. return getUploads(null, null);
  277. }
  278. private OCUpload[] getUploads(String selection, String[] selectionArgs) {
  279. OCUpload[] list;
  280. Cursor c = getDB().query(
  281. ProviderTableMeta.CONTENT_URI_UPLOADS,
  282. null,
  283. selection,
  284. selectionArgs,
  285. null
  286. );
  287. if (c != null) {
  288. list = new OCUpload[c.getCount()];
  289. if (c.moveToFirst()) {
  290. do {
  291. OCUpload upload = createOCUploadFromCursor(c);
  292. if (upload == null) {
  293. Log_OC.e(TAG, "OCUpload could not be created from cursor");
  294. } else {
  295. list[c.getPosition()] = upload;
  296. }
  297. } while (c.moveToNext());
  298. }
  299. c.close();
  300. } else {
  301. list = new OCUpload[0];
  302. }
  303. return list;
  304. }
  305. private OCUpload createOCUploadFromCursor(Cursor c) {
  306. OCUpload upload = null;
  307. if (c != null) {
  308. String localPath = c.getString(c.getColumnIndex(ProviderTableMeta.UPLOADS_LOCAL_PATH));
  309. String remotePath = c.getString(c.getColumnIndex(ProviderTableMeta.UPLOADS_REMOTE_PATH));
  310. String accountName = c.getString(c.getColumnIndex(ProviderTableMeta.UPLOADS_ACCOUNT_NAME));
  311. upload = new OCUpload(localPath, remotePath, accountName);
  312. upload.setFileSize(c.getLong(c.getColumnIndex(ProviderTableMeta.UPLOADS_FILE_SIZE)));
  313. upload.setUploadId(c.getLong(c.getColumnIndex(ProviderTableMeta._ID)));
  314. upload.setUploadStatus(
  315. UploadStatus.fromValue(c.getInt(c.getColumnIndex(ProviderTableMeta.UPLOADS_STATUS)))
  316. );
  317. upload.setLocalAction(c.getInt(c.getColumnIndex((ProviderTableMeta.UPLOADS_LOCAL_BEHAVIOUR))));
  318. upload.setForceOverwrite(c.getInt(
  319. c.getColumnIndex(ProviderTableMeta.UPLOADS_FORCE_OVERWRITE)) == 1);
  320. upload.setCreateRemoteFolder(c.getInt(
  321. c.getColumnIndex(ProviderTableMeta.UPLOADS_IS_CREATE_REMOTE_FOLDER)) == 1);
  322. upload.setUploadEndTimestamp(c.getLong(c.getColumnIndex(ProviderTableMeta.UPLOADS_UPLOAD_END_TIMESTAMP)));
  323. upload.setLastResult(UploadResult.fromValue(
  324. c.getInt(c.getColumnIndex(ProviderTableMeta.UPLOADS_LAST_RESULT))));
  325. upload.setCreatedBy(c.getInt(c.getColumnIndex(ProviderTableMeta.UPLOADS_CREATED_BY)));
  326. upload.setUseWifiOnly(c.getInt(c.getColumnIndex(ProviderTableMeta.UPLOADS_IS_WIFI_ONLY)) == 1);
  327. upload.setWhileChargingOnly(c.getInt(c.getColumnIndex(ProviderTableMeta.UPLOADS_IS_WHILE_CHARGING_ONLY))
  328. == 1);
  329. upload.setFolderUnlockToken(c.getString(c.getColumnIndex(ProviderTableMeta.UPLOADS_FOLDER_UNLOCK_TOKEN)));
  330. }
  331. return upload;
  332. }
  333. public OCUpload[] getCurrentAndPendingUploadsForCurrentAccount() {
  334. Account account = AccountUtils.getCurrentOwnCloudAccount(mContext);
  335. if (account != null) {
  336. return getUploads(
  337. ProviderTableMeta.UPLOADS_STATUS + "==" + UploadStatus.UPLOAD_IN_PROGRESS.value +
  338. " OR " + ProviderTableMeta.UPLOADS_LAST_RESULT +
  339. "==" + UploadResult.DELAYED_FOR_WIFI.getValue() +
  340. " OR " + ProviderTableMeta.UPLOADS_LAST_RESULT +
  341. "==" + UploadResult.LOCK_FAILED.getValue() +
  342. " OR " + ProviderTableMeta.UPLOADS_LAST_RESULT +
  343. "==" + UploadResult.DELAYED_FOR_CHARGING.getValue() +
  344. " OR " + ProviderTableMeta.UPLOADS_LAST_RESULT +
  345. "==" + UploadResult.DELAYED_IN_POWER_SAVE_MODE.getValue() +
  346. " AND " + ProviderTableMeta.UPLOADS_ACCOUNT_NAME + "== ?",
  347. new String[]{account.name}
  348. );
  349. } else {
  350. return new OCUpload[0];
  351. }
  352. }
  353. /**
  354. * Get all failed uploads.
  355. */
  356. public OCUpload[] getFailedUploads() {
  357. return getUploads(
  358. ProviderTableMeta.UPLOADS_STATUS + "==" + UploadStatus.UPLOAD_FAILED.value, null);
  359. }
  360. public OCUpload[] getFinishedUploadsForCurrentAccount() {
  361. Account account = AccountUtils.getCurrentOwnCloudAccount(mContext);
  362. if (account != null) {
  363. return getUploads(ProviderTableMeta.UPLOADS_STATUS + "==" + UploadStatus.UPLOAD_SUCCEEDED.value + AND +
  364. ProviderTableMeta.UPLOADS_ACCOUNT_NAME + "== ?", new String[]{account.name});
  365. } else {
  366. return new OCUpload[0];
  367. }
  368. }
  369. /**
  370. * Get all uploads which where successfully completed.
  371. */
  372. public OCUpload[] getFinishedUploads() {
  373. return getUploads(ProviderTableMeta.UPLOADS_STATUS + "==" + UploadStatus.UPLOAD_SUCCEEDED.value, null);
  374. }
  375. public OCUpload[] getFailedButNotDelayedUploadsForCurrentAccount() {
  376. Account account = AccountUtils.getCurrentOwnCloudAccount(mContext);
  377. if (account != null) {
  378. return getUploads(ProviderTableMeta.UPLOADS_STATUS + "==" + UploadStatus.UPLOAD_FAILED.value +
  379. AND + ProviderTableMeta.UPLOADS_LAST_RESULT +
  380. "<>" + UploadResult.DELAYED_FOR_WIFI.getValue() +
  381. AND + ProviderTableMeta.UPLOADS_LAST_RESULT +
  382. "<>" + UploadResult.LOCK_FAILED.getValue() +
  383. AND + ProviderTableMeta.UPLOADS_LAST_RESULT +
  384. "<>" + UploadResult.DELAYED_FOR_CHARGING.getValue() +
  385. AND + ProviderTableMeta.UPLOADS_LAST_RESULT +
  386. "<>" + UploadResult.DELAYED_IN_POWER_SAVE_MODE.getValue() +
  387. AND + ProviderTableMeta.UPLOADS_ACCOUNT_NAME + "== ?",
  388. new String[]{account.name}
  389. );
  390. } else {
  391. return new OCUpload[0];
  392. }
  393. }
  394. /**
  395. * Get all failed uploads, except for those that were not performed due to lack of Wifi connection.
  396. *
  397. * @return Array of failed uploads, except for those that were not performed due to lack of Wifi connection.
  398. */
  399. public OCUpload[] getFailedButNotDelayedUploads() {
  400. return getUploads(ProviderTableMeta.UPLOADS_STATUS + "==" + UploadStatus.UPLOAD_FAILED.value + AND +
  401. ProviderTableMeta.UPLOADS_LAST_RESULT + "<>" + UploadResult.LOCK_FAILED.getValue() +
  402. AND + ProviderTableMeta.UPLOADS_LAST_RESULT +
  403. "<>" + UploadResult.DELAYED_FOR_WIFI.getValue() +
  404. AND + ProviderTableMeta.UPLOADS_LAST_RESULT +
  405. "<>" + UploadResult.DELAYED_FOR_CHARGING.getValue() +
  406. AND + ProviderTableMeta.UPLOADS_LAST_RESULT +
  407. "<>" + UploadResult.DELAYED_IN_POWER_SAVE_MODE.getValue(),
  408. null
  409. );
  410. }
  411. private ContentResolver getDB() {
  412. return mContentResolver;
  413. }
  414. public long clearFailedButNotDelayedUploads() {
  415. Account account = AccountUtils.getCurrentOwnCloudAccount(mContext);
  416. long result = 0;
  417. if (account != null) {
  418. result = getDB().delete(
  419. ProviderTableMeta.CONTENT_URI_UPLOADS,
  420. ProviderTableMeta.UPLOADS_STATUS + "==" + UploadStatus.UPLOAD_FAILED.value +
  421. AND + ProviderTableMeta.UPLOADS_LAST_RESULT +
  422. "<>" + UploadResult.LOCK_FAILED.getValue() +
  423. AND + ProviderTableMeta.UPLOADS_LAST_RESULT +
  424. "<>" + UploadResult.DELAYED_FOR_WIFI.getValue() +
  425. AND + ProviderTableMeta.UPLOADS_LAST_RESULT +
  426. "<>" + UploadResult.DELAYED_FOR_CHARGING.getValue() +
  427. AND + ProviderTableMeta.UPLOADS_LAST_RESULT +
  428. "<>" + UploadResult.DELAYED_IN_POWER_SAVE_MODE.getValue() +
  429. AND + ProviderTableMeta.UPLOADS_ACCOUNT_NAME + "== ?",
  430. new String[]{account.name}
  431. );
  432. }
  433. Log_OC.d(TAG, "delete all failed uploads but those delayed for Wifi");
  434. if (result > 0) {
  435. notifyObserversNow();
  436. }
  437. return result;
  438. }
  439. public long clearSuccessfulUploads() {
  440. Account account = AccountUtils.getCurrentOwnCloudAccount(mContext);
  441. long result = 0;
  442. if (account != null) {
  443. result = getDB().delete(
  444. ProviderTableMeta.CONTENT_URI_UPLOADS,
  445. ProviderTableMeta.UPLOADS_STATUS + "==" + UploadStatus.UPLOAD_SUCCEEDED.value + AND +
  446. ProviderTableMeta.UPLOADS_ACCOUNT_NAME + "== ?", new String[]{account.name}
  447. );
  448. }
  449. Log_OC.d(TAG, "delete all successful uploads");
  450. if (result > 0) {
  451. notifyObserversNow();
  452. }
  453. return result;
  454. }
  455. /**
  456. * Updates the persistent upload database with upload result.
  457. */
  458. public void updateDatabaseUploadResult(RemoteOperationResult uploadResult, UploadFileOperation upload) {
  459. // result: success or fail notification
  460. Log_OC.d(TAG, "updateDatabaseUploadResult uploadResult: " + uploadResult + " upload: " + upload);
  461. if (uploadResult.isCancelled()) {
  462. removeUpload(
  463. upload.getAccount().name,
  464. upload.getRemotePath()
  465. );
  466. } else {
  467. String localPath = (FileUploader.LOCAL_BEHAVIOUR_MOVE == upload.getLocalBehaviour())
  468. ? upload.getStoragePath() : null;
  469. if (uploadResult.isSuccess()) {
  470. updateUploadStatus(
  471. upload.getOCUploadId(),
  472. UploadStatus.UPLOAD_SUCCEEDED,
  473. UploadResult.UPLOADED,
  474. upload.getRemotePath(),
  475. localPath
  476. );
  477. } else {
  478. updateUploadStatus(
  479. upload.getOCUploadId(),
  480. UploadStatus.UPLOAD_FAILED,
  481. UploadResult.fromOperationResult(uploadResult),
  482. upload.getRemotePath(),
  483. localPath
  484. );
  485. }
  486. }
  487. }
  488. /**
  489. * Updates the persistent upload database with an upload now in progress.
  490. */
  491. public void updateDatabaseUploadStart(UploadFileOperation upload) {
  492. String localPath = (FileUploader.LOCAL_BEHAVIOUR_MOVE == upload.getLocalBehaviour())
  493. ? upload.getStoragePath() : null;
  494. updateUploadStatus(
  495. upload.getOCUploadId(),
  496. UploadStatus.UPLOAD_IN_PROGRESS,
  497. UploadResult.UNKNOWN,
  498. upload.getRemotePath(),
  499. localPath
  500. );
  501. }
  502. /**
  503. * Changes the status of any in progress upload from UploadStatus.UPLOAD_IN_PROGRESS
  504. * to UploadStatus.UPLOAD_FAILED
  505. *
  506. * @return Number of uploads which status was changed.
  507. */
  508. public int failInProgressUploads(UploadResult fail) {
  509. Log_OC.v(TAG, "Updating state of any killed upload");
  510. ContentValues cv = new ContentValues();
  511. cv.put(ProviderTableMeta.UPLOADS_STATUS, UploadStatus.UPLOAD_FAILED.getValue());
  512. cv.put(
  513. ProviderTableMeta.UPLOADS_LAST_RESULT,
  514. fail != null ? fail.getValue() : UploadResult.UNKNOWN.getValue()
  515. );
  516. cv.put(ProviderTableMeta.UPLOADS_UPLOAD_END_TIMESTAMP, Calendar.getInstance().getTimeInMillis());
  517. int result = getDB().update(
  518. ProviderTableMeta.CONTENT_URI_UPLOADS,
  519. cv,
  520. ProviderTableMeta.UPLOADS_STATUS + "=?",
  521. new String[]{String.valueOf(UploadStatus.UPLOAD_IN_PROGRESS.getValue())}
  522. );
  523. if (result == 0) {
  524. Log_OC.v(TAG, "No upload was killed");
  525. } else {
  526. Log_OC.w(TAG, Integer.toString(result) + " uploads where abruptly interrupted");
  527. notifyObserversNow();
  528. }
  529. return result;
  530. }
  531. public int removeAccountUploads(Account account) {
  532. Log_OC.v(TAG, "Delete all uploads for account " + account.name);
  533. return getDB().delete(
  534. ProviderTableMeta.CONTENT_URI_UPLOADS,
  535. ProviderTableMeta.UPLOADS_ACCOUNT_NAME + "=?",
  536. new String[]{account.name});
  537. }
  538. }