Add author's line in license
[pub/Android/ownCloud.git] / src / com / owncloud / android / db / DbHandler.java
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
22 import com.owncloud.android.MainApp;
23 import com.owncloud.android.lib.common.utils.Log_OC;
24
25 import android.content.ContentValues;
26 import android.content.Context;
27 import android.database.Cursor;
28 import android.database.sqlite.SQLiteDatabase;
29 import android.database.sqlite.SQLiteOpenHelper;
30
31 /**
32 * Custom database helper for ownCloud
33 */
34 public class DbHandler {
35 private SQLiteDatabase mDB;
36 private OpenerHelper mHelper;
37 private final String mDatabaseName;
38 private final int mDatabaseVersion = 3;
39
40 private final String TABLE_INSTANT_UPLOAD = "instant_upload";
41
42 public static final int UPLOAD_STATUS_UPLOAD_LATER = 0;
43 public static final int UPLOAD_STATUS_UPLOAD_FAILED = 1;
44
45 public DbHandler(Context context) {
46 mDatabaseName = MainApp.getDBName();
47 mHelper = new OpenerHelper(context);
48 mDB = mHelper.getWritableDatabase();
49 }
50
51 public void close() {
52 mDB.close();
53 }
54
55 public boolean putFileForLater(String filepath, String account, String message) {
56 ContentValues cv = new ContentValues();
57 cv.put("path", filepath);
58 cv.put("account", account);
59 cv.put("attempt", UPLOAD_STATUS_UPLOAD_LATER);
60 cv.put("message", message);
61 long result = mDB.insert(TABLE_INSTANT_UPLOAD, null, cv);
62 Log_OC.d(TABLE_INSTANT_UPLOAD, "putFileForLater returns with: " + result + " for file: " + filepath);
63 return result != -1;
64 }
65
66 public int updateFileState(String filepath, Integer status, String message) {
67 ContentValues cv = new ContentValues();
68 cv.put("attempt", status);
69 cv.put("message", message);
70 int result = mDB.update(TABLE_INSTANT_UPLOAD, cv, "path=?", new String[] { filepath });
71 Log_OC.d(TABLE_INSTANT_UPLOAD, "updateFileState returns with: " + result + " for file: " + filepath);
72 return result;
73 }
74
75 public Cursor getAwaitingFiles() {
76 return mDB.query(TABLE_INSTANT_UPLOAD, null, "attempt=" + UPLOAD_STATUS_UPLOAD_LATER, null, null, null, null);
77 }
78
79 public Cursor getFailedFiles() {
80 return mDB.query(TABLE_INSTANT_UPLOAD, null, "attempt>" + UPLOAD_STATUS_UPLOAD_LATER, null, null, null, null);
81 }
82
83 public void clearFiles() {
84 mDB.delete(TABLE_INSTANT_UPLOAD, null, null);
85 }
86
87 /**
88 *
89 * @param localPath
90 * @return true when one or more pending files was removed
91 */
92 public boolean removeIUPendingFile(String localPath) {
93 long result = mDB.delete(TABLE_INSTANT_UPLOAD, "path = ?", new String[] { localPath });
94 Log_OC.d(TABLE_INSTANT_UPLOAD, "delete returns with: " + result + " for file: " + localPath);
95 return result != 0;
96
97 }
98
99 private class OpenerHelper extends SQLiteOpenHelper {
100 public OpenerHelper(Context context) {
101 super(context, mDatabaseName, null, mDatabaseVersion);
102 }
103
104 @Override
105 public void onCreate(SQLiteDatabase db) {
106 db.execSQL("CREATE TABLE " + TABLE_INSTANT_UPLOAD + " (" + " _id INTEGER PRIMARY KEY, " + " path TEXT,"
107 + " account TEXT,attempt INTEGER,message TEXT);");
108 }
109
110 @Override
111 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
112 if (oldVersion < 2) {
113 db.execSQL("ALTER TABLE " + TABLE_INSTANT_UPLOAD + " ADD COLUMN attempt INTEGER;");
114 }
115 db.execSQL("ALTER TABLE " + TABLE_INSTANT_UPLOAD + " ADD COLUMN message TEXT;");
116 }
117
118 @Override
119 public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
120 //downgrading is the exception, so deleting and re-creating is acceptable.
121 //otherwise exception will be thrown (cannot downgrade) and oc app will crash.
122 db.execSQL("DROP TABLE IF EXISTS " + TABLE_INSTANT_UPLOAD + ";");
123 onCreate(db);
124 }
125 }
126 }