1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
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 eu
.alefzero
.owncloud
.db
;
20 import java
.util
.Vector
;
22 import eu
.alefzero
.owncloud
.OwnCloudSession
;
24 import android
.content
.ContentValues
;
25 import android
.content
.Context
;
26 import android
.database
.Cursor
;
27 import android
.database
.sqlite
.SQLiteDatabase
;
28 import android
.database
.sqlite
.SQLiteOpenHelper
;
31 * Custom database helper for ownCloud
32 * @author Bartek Przybylski
35 public class DbHandler
{
36 private SQLiteDatabase mDB
;
37 private OpenerHepler mHelper
;
38 private final String mDatabaseName
= "ownCloud";
39 private final String TABLE_SESSIONS
= "sessions";
40 private final int mDatabaseVersion
= 1;
42 public DbHandler(Context context
) {
43 mHelper
= new OpenerHepler(context
);
44 mDB
= mHelper
.getWritableDatabase();
47 public Vector
<OwnCloudSession
> getSessionList() {
48 Cursor c
= mDB
.query(TABLE_SESSIONS
, null
, null
, null
, null
, null
, null
);
49 Vector
<OwnCloudSession
> v
= new Vector
<OwnCloudSession
>();
50 if (!c
.moveToFirst()) {
53 while (!c
.isAfterLast()) {
54 v
.add(new OwnCloudSession(c
.getString(c
.getColumnIndex("sessionName")),
55 c
.getString(c
.getColumnIndex("sessionUrl")),
56 c
.getInt(c
.getColumnIndex("_id"))));
63 public void addSession(String sessionName
, String uri
) {
64 ContentValues cv
= new ContentValues();
65 cv
.put("sessionName", sessionName
);
66 cv
.put("sessionUrl", uri
);
67 mDB
.insert(TABLE_SESSIONS
, null
, cv
);
70 public void removeSessionWithId(int sessionId
) {
71 mDB
.delete(TABLE_SESSIONS
, "_id = ?", new String
[] {String
.valueOf(sessionId
)});
74 public void changeSessionFields(int id
, String hostname
, String uri
) {
75 ContentValues cv
= new ContentValues();
76 cv
.put("sessionName", hostname
);
77 cv
.put("sessionUrl", uri
);
78 mDB
.update(TABLE_SESSIONS
, cv
, "_id = ?", new String
[] {String
.valueOf(id
)});
85 private class OpenerHepler
extends SQLiteOpenHelper
{
86 public OpenerHepler(Context context
) {
87 super(context
, mDatabaseName
, null
, mDatabaseVersion
);
91 public void onCreate(SQLiteDatabase db
) {
92 db
.execSQL("CREATE TABLE " + TABLE_SESSIONS
+ " (" +
93 " _id INTEGER PRIMARY KEY, " +
94 " sessionName TEXT, " +
95 " sessionUrl TEXT);");
99 public void onUpgrade(SQLiteDatabase db
, int oldVersion
, int newVersion
) {