1 /* ownCloud Android client application
2 * Copyright (C) 2011-2012 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
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.
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.
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/>.
18 package com
.owncloud
.android
.db
;
20 import com
.owncloud
.android
.Log_OC
;
22 import android
.content
.ContentValues
;
23 import android
.content
.Context
;
24 import android
.database
.Cursor
;
25 import android
.database
.sqlite
.SQLiteDatabase
;
26 import android
.database
.sqlite
.SQLiteOpenHelper
;
29 * Custom database helper for ownCloud
31 * @author Bartek Przybylski
34 public class DbHandler
{
35 private SQLiteDatabase mDB
;
36 private OpenerHelper mHelper
;
37 private final String mDatabaseName
= "ownCloud";
38 private final int mDatabaseVersion
= 3;
40 private final String TABLE_INSTANT_UPLOAD
= "instant_upload";
42 public static final int UPLOAD_STATUS_UPLOAD_LATER
= 0;
43 public static final int UPLOAD_STATUS_UPLOAD_FAILED
= 1;
45 public DbHandler(Context context
) {
46 mHelper
= new OpenerHelper(context
);
47 mDB
= mHelper
.getWritableDatabase();
54 public boolean putFileForLater(String filepath
, String account
, String message
) {
55 ContentValues cv
= new ContentValues();
56 cv
.put("path", filepath
);
57 cv
.put("account", account
);
58 cv
.put("attempt", UPLOAD_STATUS_UPLOAD_LATER
);
59 cv
.put("message", message
);
60 long result
= mDB
.insert(TABLE_INSTANT_UPLOAD
, null
, cv
);
61 Log_OC
.d(TABLE_INSTANT_UPLOAD
, "putFileForLater returns with: " + result
+ " for file: " + filepath
);
65 public int updateFileState(String filepath
, Integer status
, String message
) {
66 ContentValues cv
= new ContentValues();
67 cv
.put("attempt", status
);
68 cv
.put("message", message
);
69 int result
= mDB
.update(TABLE_INSTANT_UPLOAD
, cv
, "path=?", new String
[] { filepath
});
70 Log_OC
.d(TABLE_INSTANT_UPLOAD
, "updateFileState returns with: " + result
+ " for file: " + filepath
);
74 public Cursor
getAwaitingFiles() {
75 return mDB
.query(TABLE_INSTANT_UPLOAD
, null
, "attempt=" + UPLOAD_STATUS_UPLOAD_LATER
, null
, null
, null
, null
);
78 public Cursor
getFailedFiles() {
79 return mDB
.query(TABLE_INSTANT_UPLOAD
, null
, "attempt>" + UPLOAD_STATUS_UPLOAD_LATER
, null
, null
, null
, null
);
82 public void clearFiles() {
83 mDB
.delete(TABLE_INSTANT_UPLOAD
, null
, null
);
89 * @return true when one or more pending files was removed
91 public boolean removeIUPendingFile(String localPath
) {
92 long result
= mDB
.delete(TABLE_INSTANT_UPLOAD
, "path = ?", new String
[] { localPath
});
93 Log_OC
.d(TABLE_INSTANT_UPLOAD
, "delete returns with: " + result
+ " for file: " + localPath
);
98 private class OpenerHelper
extends SQLiteOpenHelper
{
99 public OpenerHelper(Context context
) {
100 super(context
, mDatabaseName
, null
, mDatabaseVersion
);
104 public void onCreate(SQLiteDatabase db
) {
105 db
.execSQL("CREATE TABLE " + TABLE_INSTANT_UPLOAD
+ " (" + " _id INTEGER PRIMARY KEY, " + " path TEXT,"
106 + " account TEXT,attempt INTEGER,message TEXT);");
110 public void onUpgrade(SQLiteDatabase db
, int oldVersion
, int newVersion
) {
111 if (oldVersion
< 2) {
112 db
.execSQL("ALTER TABLE " + TABLE_INSTANT_UPLOAD
+ " ADD COLUMN attempt INTEGER;");
114 db
.execSQL("ALTER TABLE " + TABLE_INSTANT_UPLOAD
+ " ADD COLUMN message TEXT;");