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