DatabaseBackend.java

  1package eu.siacs.conversations.persistance;
  2
  3import java.util.ArrayList;
  4import java.util.List;
  5import java.util.concurrent.CopyOnWriteArrayList;
  6
  7import eu.siacs.conversations.entities.Account;
  8import eu.siacs.conversations.entities.Contact;
  9import eu.siacs.conversations.entities.Conversation;
 10import eu.siacs.conversations.entities.Message;
 11import eu.siacs.conversations.entities.Roster;
 12import android.content.Context;
 13import android.database.Cursor;
 14import android.database.sqlite.SQLiteDatabase;
 15import android.database.sqlite.SQLiteOpenHelper;
 16import android.util.Log;
 17
 18public class DatabaseBackend extends SQLiteOpenHelper {
 19
 20	private static DatabaseBackend instance = null;
 21
 22	private static final String DATABASE_NAME = "history";
 23	private static final int DATABASE_VERSION = 7;
 24
 25	private static String CREATE_CONTATCS_STATEMENT = "create table "
 26			+ Contact.TABLENAME + "(" + Contact.ACCOUNT + " TEXT, "
 27			+ Contact.SERVERNAME + " TEXT, " + Contact.SYSTEMNAME + " TEXT,"
 28			+ Contact.JID + " TEXT," + Contact.KEYS + " TEXT,"
 29			+ Contact.PHOTOURI + " TEXT," + Contact.OPTIONS + " NUMBER,"
 30			+ Contact.SYSTEMACCOUNT + " NUMBER, " + "FOREIGN KEY("
 31			+ Contact.ACCOUNT + ") REFERENCES " + Account.TABLENAME + "("
 32			+ Account.UUID + ") ON DELETE CASCADE, UNIQUE(" + Contact.ACCOUNT
 33			+ ", " + Contact.JID + ") ON CONFLICT REPLACE);";
 34
 35	public DatabaseBackend(Context context) {
 36		super(context, DATABASE_NAME, null, DATABASE_VERSION);
 37	}
 38
 39	@Override
 40	public void onCreate(SQLiteDatabase db) {
 41		db.execSQL("PRAGMA foreign_keys=ON;");
 42		db.execSQL("create table " + Account.TABLENAME + "(" + Account.UUID
 43				+ " TEXT PRIMARY KEY," + Account.USERNAME + " TEXT,"
 44				+ Account.SERVER + " TEXT," + Account.PASSWORD + " TEXT,"
 45				+ Account.ROSTERVERSION + " TEXT," + Account.OPTIONS
 46				+ " NUMBER, " + Account.KEYS + " TEXT)");
 47		db.execSQL("create table " + Conversation.TABLENAME + " ("
 48				+ Conversation.UUID + " TEXT PRIMARY KEY, " + Conversation.NAME
 49				+ " TEXT, " + Conversation.CONTACT + " TEXT, "
 50				+ Conversation.ACCOUNT + " TEXT, " + Conversation.CONTACTJID
 51				+ " TEXT, " + Conversation.CREATED + " NUMBER, "
 52				+ Conversation.STATUS + " NUMBER," + Conversation.MODE
 53				+ " NUMBER," + "FOREIGN KEY(" + Conversation.ACCOUNT
 54				+ ") REFERENCES " + Account.TABLENAME + "(" + Account.UUID
 55				+ ") ON DELETE CASCADE);");
 56		db.execSQL("create table " + Message.TABLENAME + "( " + Message.UUID
 57				+ " TEXT PRIMARY KEY, " + Message.CONVERSATION + " TEXT, "
 58				+ Message.TIME_SENT + " NUMBER, " + Message.COUNTERPART
 59				+ " TEXT, " + Message.TRUE_COUNTERPART + " TEXT,"
 60				+ Message.BODY + " TEXT, " + Message.ENCRYPTION + " NUMBER, "
 61				+ Message.STATUS + " NUMBER," + Message.TYPE
 62				+ " NUMBER, FOREIGN KEY(" + Message.CONVERSATION
 63				+ ") REFERENCES " + Conversation.TABLENAME + "("
 64				+ Conversation.UUID + ") ON DELETE CASCADE);");
 65
 66		db.execSQL(CREATE_CONTATCS_STATEMENT);
 67	}
 68
 69	@Override
 70	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
 71		if (oldVersion < 2 && newVersion >= 2) {
 72			db.execSQL("update " + Account.TABLENAME + " set "
 73					+ Account.OPTIONS + " = " + Account.OPTIONS + " | 8");
 74		}
 75		if (oldVersion < 3 && newVersion >= 3) {
 76			db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN "
 77					+ Message.TYPE + " NUMBER");
 78		}
 79		if (oldVersion < 5 && newVersion >= 5) {
 80			db.execSQL("DROP TABLE " + Contact.TABLENAME);
 81			db.execSQL(CREATE_CONTATCS_STATEMENT);
 82			db.execSQL("UPDATE " + Account.TABLENAME + " SET "
 83					+ Account.ROSTERVERSION + " = NULL");
 84		}
 85		if (oldVersion < 6 && newVersion >= 6) {
 86			db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN "
 87					+ Message.TRUE_COUNTERPART + " TEXT");
 88		}
 89		if (oldVersion < 7 && newVersion >= 7) {
 90			db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN "
 91					+ Message.REMOTE_MSG_ID + " TEXT");
 92			db.execSQL("ALTER TABLE " + Contact.TABLENAME + " ADD COLUMN "
 93					+ Contact.AVATAR + " TEXT");
 94			db.execSQL("ALTER TABLE " + Account.TABLENAME + " ADD COLUMN "
 95					+ Account.AVATAR + " TEXT");
 96		}
 97	}
 98
 99	public static synchronized DatabaseBackend getInstance(Context context) {
100		if (instance == null) {
101			instance = new DatabaseBackend(context);
102		}
103		return instance;
104	}
105
106	public void createConversation(Conversation conversation) {
107		SQLiteDatabase db = this.getWritableDatabase();
108		db.insert(Conversation.TABLENAME, null, conversation.getContentValues());
109	}
110
111	public void createMessage(Message message) {
112		SQLiteDatabase db = this.getWritableDatabase();
113		db.insert(Message.TABLENAME, null, message.getContentValues());
114	}
115
116	public void createAccount(Account account) {
117		SQLiteDatabase db = this.getWritableDatabase();
118		db.insert(Account.TABLENAME, null, account.getContentValues());
119	}
120
121	public void createContact(Contact contact) {
122		SQLiteDatabase db = this.getWritableDatabase();
123		db.insert(Contact.TABLENAME, null, contact.getContentValues());
124	}
125
126	public int getConversationCount() {
127		SQLiteDatabase db = this.getReadableDatabase();
128		Cursor cursor = db.rawQuery("select count(uuid) as count from "
129				+ Conversation.TABLENAME + " where " + Conversation.STATUS
130				+ "=" + Conversation.STATUS_AVAILABLE, null);
131		cursor.moveToFirst();
132		return cursor.getInt(0);
133	}
134
135	public CopyOnWriteArrayList<Conversation> getConversations(int status) {
136		CopyOnWriteArrayList<Conversation> list = new CopyOnWriteArrayList<Conversation>();
137		SQLiteDatabase db = this.getReadableDatabase();
138		String[] selectionArgs = { "" + status };
139		Cursor cursor = db.rawQuery("select * from " + Conversation.TABLENAME
140				+ " where " + Conversation.STATUS + " = ? order by "
141				+ Conversation.CREATED + " desc", selectionArgs);
142		while (cursor.moveToNext()) {
143			list.add(Conversation.fromCursor(cursor));
144		}
145		return list;
146	}
147
148	public CopyOnWriteArrayList<Message> getMessages(
149			Conversation conversations, int limit) {
150		return getMessages(conversations, limit, -1);
151	}
152
153	public CopyOnWriteArrayList<Message> getMessages(Conversation conversation,
154			int limit, long timestamp) {
155		CopyOnWriteArrayList<Message> list = new CopyOnWriteArrayList<Message>();
156		SQLiteDatabase db = this.getReadableDatabase();
157		Cursor cursor;
158		if (timestamp == -1) {
159			String[] selectionArgs = { conversation.getUuid() };
160			cursor = db.query(Message.TABLENAME, null, Message.CONVERSATION
161					+ "=?", selectionArgs, null, null, Message.TIME_SENT
162					+ " DESC", String.valueOf(limit));
163		} else {
164			String[] selectionArgs = { conversation.getUuid(), "" + timestamp };
165			cursor = db.query(Message.TABLENAME, null, Message.CONVERSATION
166					+ "=? and " + Message.TIME_SENT + "<?", selectionArgs,
167					null, null, Message.TIME_SENT + " DESC",
168					String.valueOf(limit));
169		}
170		if (cursor.getCount() > 0) {
171			cursor.moveToLast();
172			do {
173				list.add(Message.fromCursor(cursor));
174			} while (cursor.moveToPrevious());
175		}
176		return list;
177	}
178
179	public Conversation findConversation(Account account, String contactJid) {
180		SQLiteDatabase db = this.getReadableDatabase();
181		String[] selectionArgs = { account.getUuid(), contactJid + "%" };
182		Cursor cursor = db.query(Conversation.TABLENAME, null,
183				Conversation.ACCOUNT + "=? AND " + Conversation.CONTACTJID
184						+ " like ?", selectionArgs, null, null, null);
185		if (cursor.getCount() == 0)
186			return null;
187		cursor.moveToFirst();
188		return Conversation.fromCursor(cursor);
189	}
190
191	public void updateConversation(Conversation conversation) {
192		SQLiteDatabase db = this.getWritableDatabase();
193		String[] args = { conversation.getUuid() };
194		db.update(Conversation.TABLENAME, conversation.getContentValues(),
195				Conversation.UUID + "=?", args);
196	}
197
198	public List<Account> getAccounts() {
199		List<Account> list = new ArrayList<Account>();
200		SQLiteDatabase db = this.getReadableDatabase();
201		Cursor cursor = db.query(Account.TABLENAME, null, null, null, null,
202				null, null);
203		Log.d("gultsch", "found " + cursor.getCount() + " accounts");
204		while (cursor.moveToNext()) {
205			list.add(Account.fromCursor(cursor));
206		}
207		return list;
208	}
209
210	public void updateAccount(Account account) {
211		SQLiteDatabase db = this.getWritableDatabase();
212		String[] args = { account.getUuid() };
213		db.update(Account.TABLENAME, account.getContentValues(), Account.UUID
214				+ "=?", args);
215	}
216
217	public void deleteAccount(Account account) {
218		SQLiteDatabase db = this.getWritableDatabase();
219		String[] args = { account.getUuid() };
220		db.delete(Account.TABLENAME, Account.UUID + "=?", args);
221	}
222
223	@Override
224	public SQLiteDatabase getWritableDatabase() {
225		SQLiteDatabase db = super.getWritableDatabase();
226		db.execSQL("PRAGMA foreign_keys=ON;");
227		return db;
228	}
229
230	public void updateMessage(Message message) {
231		SQLiteDatabase db = this.getWritableDatabase();
232		String[] args = { message.getUuid() };
233		db.update(Message.TABLENAME, message.getContentValues(), Message.UUID
234				+ "=?", args);
235	}
236
237	public void readRoster(Roster roster) {
238		SQLiteDatabase db = this.getReadableDatabase();
239		Cursor cursor;
240		String args[] = { roster.getAccount().getUuid() };
241		cursor = db.query(Contact.TABLENAME, null, Contact.ACCOUNT + "=?",
242				args, null, null, null);
243		while (cursor.moveToNext()) {
244			roster.initContact(Contact.fromCursor(cursor));
245		}
246	}
247
248	public void writeRoster(Roster roster) {
249		Account account = roster.getAccount();
250		SQLiteDatabase db = this.getWritableDatabase();
251		for (Contact contact : roster.getContacts()) {
252			if (contact.getOption(Contact.Options.IN_ROSTER)) {
253				db.insert(Contact.TABLENAME, null, contact.getContentValues());
254			} else {
255				String where = Contact.ACCOUNT + "=? AND " + Contact.JID + "=?";
256				String[] whereArgs = { account.getUuid(), contact.getJid() };
257				db.delete(Contact.TABLENAME, where, whereArgs);
258			}
259		}
260		account.setRosterVersion(roster.getVersion());
261		updateAccount(account);
262	}
263
264	public void deleteMessage(Message message) {
265		SQLiteDatabase db = this.getWritableDatabase();
266		String[] args = { message.getUuid() };
267		db.delete(Message.TABLENAME, Message.UUID + "=?", args);
268	}
269
270	public void deleteMessagesInConversation(Conversation conversation) {
271		SQLiteDatabase db = this.getWritableDatabase();
272		String[] args = { conversation.getUuid() };
273		db.delete(Message.TABLENAME, Message.CONVERSATION + "=?", args);
274	}
275
276	public Conversation findConversationByUuid(String conversationUuid) {
277		SQLiteDatabase db = this.getReadableDatabase();
278		String[] selectionArgs = { conversationUuid };
279		Cursor cursor = db.query(Conversation.TABLENAME, null,
280				Conversation.UUID + "=?", selectionArgs, null, null, null);
281		if (cursor.getCount() == 0) {
282			return null;
283		}
284		cursor.moveToFirst();
285		return Conversation.fromCursor(cursor);
286	}
287
288	public Message findMessageByUuid(String messageUuid) {
289		SQLiteDatabase db = this.getReadableDatabase();
290		String[] selectionArgs = { messageUuid };
291		Cursor cursor = db.query(Message.TABLENAME, null, Message.UUID + "=?",
292				selectionArgs, null, null, null);
293		if (cursor.getCount() == 0) {
294			return null;
295		}
296		cursor.moveToFirst();
297		return Message.fromCursor(cursor);
298	}
299
300	public Account findAccountByUuid(String accountUuid) {
301		SQLiteDatabase db = this.getReadableDatabase();
302		String[] selectionArgs = { accountUuid };
303		Cursor cursor = db.query(Account.TABLENAME, null, Account.UUID + "=?",
304				selectionArgs, null, null, null);
305		if (cursor.getCount() == 0) {
306			return null;
307		}
308		cursor.moveToFirst();
309		return Account.fromCursor(cursor);
310	}
311}