UploadDbHandler.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /* ownCloud Android client application
  2. * Copyright (C) 2011-2012 Bartek Przybylski
  3. * Copyright (C) 2012-2013 ownCloud Inc.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2,
  7. * as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. */
  18. package com.owncloud.android.db;
  19. import com.owncloud.android.MainApp;
  20. import com.owncloud.android.lib.common.utils.Log_OC;
  21. import android.content.ContentValues;
  22. import android.content.Context;
  23. import android.database.Cursor;
  24. import android.database.sqlite.SQLiteDatabase;
  25. import android.database.sqlite.SQLiteOpenHelper;
  26. /**
  27. * Database helper for storing list of files to be uploaded, including status information for each file.
  28. *
  29. * @author Bartek Przybylski
  30. * @author LukeOwncloud
  31. *
  32. */
  33. public class UploadDbHandler {
  34. private SQLiteDatabase mDB;
  35. private OpenerHelper mHelper;
  36. private final String mDatabaseName;
  37. private final int mDatabaseVersion = 4;
  38. static private final String TABLE_UPLOAD = "list_of_uploads";
  39. public enum UploadStatus {
  40. UPLOAD_STATUS_UPLOAD_LATER(0), UPLOAD_STATUS_UPLOAD_FAILED(1);
  41. private final int value;
  42. private UploadStatus(int value) {
  43. this.value = value;
  44. }
  45. public int getValue() {
  46. return value;
  47. }
  48. };
  49. public UploadDbHandler(Context context) {
  50. mDatabaseName = MainApp.getDBName();
  51. mHelper = new OpenerHelper(context);
  52. mDB = mHelper.getWritableDatabase();
  53. }
  54. public void close() {
  55. mDB.close();
  56. }
  57. /**
  58. * Store a file persistently for upload.
  59. * @param filepath local file path to file
  60. * @param account account for uploading
  61. * @param message optional message. can be null.
  62. * @return false if an error occurred, else true.
  63. */
  64. public boolean putFileForLater(String filepath, String account, String message) {
  65. ContentValues cv = new ContentValues();
  66. cv.put("path", filepath);
  67. cv.put("account", account);
  68. cv.put("attempt", UploadStatus.UPLOAD_STATUS_UPLOAD_LATER.getValue());
  69. cv.put("message", message);
  70. long result = mDB.insert(TABLE_UPLOAD, null, cv);
  71. Log_OC.d(TABLE_UPLOAD, "putFileForLater returns with: " + result + " for file: " + filepath);
  72. return result != -1;
  73. }
  74. /**
  75. * Update upload status of file.
  76. *
  77. * @param filepath local file path to file. used as identifier.
  78. * @param status new status.
  79. * @param message new message.
  80. * @return 1 if file status was updated, else 0.
  81. */
  82. public int updateFileState(String filepath, UploadStatus status, String message) {
  83. ContentValues cv = new ContentValues();
  84. cv.put("attempt", status.getValue());
  85. cv.put("message", message);
  86. int result = mDB.update(TABLE_UPLOAD, cv, "path=?", new String[] { filepath });
  87. Log_OC.d(TABLE_UPLOAD, "updateFileState returns with: " + result + " for file: " + filepath);
  88. return result;
  89. }
  90. /**
  91. * Get all files with status {@link UploadStatus}.UPLOAD_STATUS_UPLOAD_LATER.
  92. * @return
  93. */
  94. public Cursor getAwaitingFiles() {
  95. return mDB.query(TABLE_UPLOAD, null, "attempt=" + UploadStatus.UPLOAD_STATUS_UPLOAD_LATER, null, null, null, null);
  96. }
  97. //ununsed until now. uncomment if needed.
  98. // public Cursor getFailedFiles() {
  99. // return mDB.query(TABLE_INSTANT_UPLOAD, null, "attempt>" + UploadStatus.UPLOAD_STATUS_UPLOAD_LATER, null, null, null, null);
  100. // }
  101. //ununsed until now. uncomment if needed.
  102. // public void clearFiles() {
  103. // mDB.delete(TABLE_INSTANT_UPLOAD, null, null);
  104. // }
  105. /**
  106. * Remove file from upload list. Should be called when upload succeed or failed and should not be retried.
  107. * @param localPath
  108. * @return true when one or more pending files was removed
  109. */
  110. public boolean removePendingFile(String localPath) {
  111. long result = mDB.delete(TABLE_UPLOAD, "path = ?", new String[] { localPath });
  112. Log_OC.d(TABLE_UPLOAD, "delete returns with: " + result + " for file: " + localPath);
  113. return result != 0;
  114. }
  115. private class OpenerHelper extends SQLiteOpenHelper {
  116. public OpenerHelper(Context context) {
  117. super(context, mDatabaseName, null, mDatabaseVersion);
  118. }
  119. @Override
  120. public void onCreate(SQLiteDatabase db) {
  121. db.execSQL("CREATE TABLE " + TABLE_UPLOAD + " (" + " _id INTEGER PRIMARY KEY, " + " path TEXT,"
  122. + " account TEXT,attempt INTEGER,message TEXT);");
  123. }
  124. @Override
  125. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  126. if (oldVersion < 2) {
  127. db.execSQL("ALTER TABLE " + TABLE_UPLOAD + " ADD COLUMN attempt INTEGER;");
  128. }
  129. db.execSQL("ALTER TABLE " + TABLE_UPLOAD + " ADD COLUMN message TEXT;");
  130. }
  131. }
  132. }