DbHandler.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* ownCloud Android client application
  2. *
  3. * @author Bartek Przybylski
  4. * Copyright (C) 2011-2012 Bartek Przybylski
  5. * Copyright (C) 2012-2013 ownCloud Inc.
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2,
  9. * as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. package com.owncloud.android.db;
  21. import com.owncloud.android.MainApp;
  22. import com.owncloud.android.lib.common.utils.Log_OC;
  23. import android.content.ContentValues;
  24. import android.content.Context;
  25. import android.database.Cursor;
  26. import android.database.sqlite.SQLiteDatabase;
  27. import android.database.sqlite.SQLiteOpenHelper;
  28. /**
  29. * Custom database helper for ownCloud
  30. */
  31. public class DbHandler {
  32. private SQLiteDatabase mDB;
  33. private OpenerHelper mHelper;
  34. private final String mDatabaseName;
  35. private final int mDatabaseVersion = 3;
  36. private final String TABLE_INSTANT_UPLOAD = "instant_upload";
  37. public static final int UPLOAD_STATUS_UPLOAD_LATER = 0;
  38. public static final int UPLOAD_STATUS_UPLOAD_FAILED = 1;
  39. public DbHandler(Context context) {
  40. mDatabaseName = MainApp.getDBName();
  41. mHelper = new OpenerHelper(context);
  42. mDB = mHelper.getWritableDatabase();
  43. }
  44. public void close() {
  45. mDB.close();
  46. }
  47. public boolean putFileForLater(String filepath, String account, String message) {
  48. ContentValues cv = new ContentValues();
  49. cv.put("path", filepath);
  50. cv.put("account", account);
  51. cv.put("attempt", UPLOAD_STATUS_UPLOAD_LATER);
  52. cv.put("message", message);
  53. long result = mDB.insert(TABLE_INSTANT_UPLOAD, null, cv);
  54. Log_OC.d(TABLE_INSTANT_UPLOAD, "putFileForLater returns with: " + result + " for file: " + filepath);
  55. return result != -1;
  56. }
  57. public int updateFileState(String filepath, Integer status, String message) {
  58. ContentValues cv = new ContentValues();
  59. cv.put("attempt", status);
  60. cv.put("message", message);
  61. int result = mDB.update(TABLE_INSTANT_UPLOAD, cv, "path=?", new String[] { filepath });
  62. Log_OC.d(TABLE_INSTANT_UPLOAD, "updateFileState returns with: " + result + " for file: " + filepath);
  63. return result;
  64. }
  65. public Cursor getAwaitingFiles() {
  66. return mDB.query(TABLE_INSTANT_UPLOAD, null, "attempt=" + UPLOAD_STATUS_UPLOAD_LATER, null, null, null, null);
  67. }
  68. public Cursor getFailedFiles() {
  69. return mDB.query(TABLE_INSTANT_UPLOAD, null, "attempt>" + UPLOAD_STATUS_UPLOAD_LATER, null, null, null, null);
  70. }
  71. public void clearFiles() {
  72. mDB.delete(TABLE_INSTANT_UPLOAD, null, null);
  73. }
  74. /**
  75. *
  76. * @param localPath
  77. * @return true when one or more pending files was removed
  78. */
  79. public boolean removeIUPendingFile(String localPath) {
  80. long result = mDB.delete(TABLE_INSTANT_UPLOAD, "path = ?", new String[] { localPath });
  81. Log_OC.d(TABLE_INSTANT_UPLOAD, "delete returns with: " + result + " for file: " + localPath);
  82. return result != 0;
  83. }
  84. private class OpenerHelper extends SQLiteOpenHelper {
  85. public OpenerHelper(Context context) {
  86. super(context, mDatabaseName, null, mDatabaseVersion);
  87. }
  88. @Override
  89. public void onCreate(SQLiteDatabase db) {
  90. db.execSQL("CREATE TABLE " + TABLE_INSTANT_UPLOAD + " (" + " _id INTEGER PRIMARY KEY, " + " path TEXT,"
  91. + " account TEXT,attempt INTEGER,message TEXT);");
  92. }
  93. @Override
  94. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  95. if (oldVersion < 2) {
  96. db.execSQL("ALTER TABLE " + TABLE_INSTANT_UPLOAD + " ADD COLUMN attempt INTEGER;");
  97. }
  98. db.execSQL("ALTER TABLE " + TABLE_INSTANT_UPLOAD + " ADD COLUMN message TEXT;");
  99. }
  100. @Override
  101. public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  102. //downgrading is the exception, so deleting and re-creating is acceptable.
  103. //otherwise exception will be thrown (cannot downgrade) and oc app will crash.
  104. db.execSQL("DROP TABLE IF EXISTS " + TABLE_INSTANT_UPLOAD + ";");
  105. onCreate(db);
  106. }
  107. }
  108. }