2013-09-09 01:19:05 +00:00
|
|
|
package org.thoughtcrime.securesms.database;
|
|
|
|
|
|
|
|
import android.content.ContentValues;
|
|
|
|
import android.content.Context;
|
2013-09-09 23:46:03 +00:00
|
|
|
import android.database.Cursor;
|
2013-09-09 01:19:05 +00:00
|
|
|
import android.database.sqlite.SQLiteOpenHelper;
|
|
|
|
|
|
|
|
import org.whispersystems.textsecure.push.IncomingPushMessage;
|
2013-09-09 23:46:03 +00:00
|
|
|
import org.whispersystems.textsecure.util.Base64;
|
2013-09-09 01:19:05 +00:00
|
|
|
import org.whispersystems.textsecure.util.Util;
|
|
|
|
|
2013-09-09 23:46:03 +00:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.List;
|
|
|
|
|
2013-09-09 01:19:05 +00:00
|
|
|
public class PushDatabase extends Database {
|
|
|
|
|
|
|
|
private static final String TABLE_NAME = "push";
|
|
|
|
public static final String ID = "_id";
|
|
|
|
public static final String TYPE = "type";
|
|
|
|
public static final String SOURCE = "source";
|
2014-02-03 03:38:06 +00:00
|
|
|
public static final String DEVICE_ID = "device_id";
|
2013-09-09 01:19:05 +00:00
|
|
|
public static final String BODY = "body";
|
|
|
|
public static final String TIMESTAMP = "timestamp";
|
|
|
|
|
|
|
|
public static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " (" + ID + " INTEGER PRIMARY KEY, " +
|
2014-02-03 03:38:06 +00:00
|
|
|
TYPE + " INTEGER, " + SOURCE + " TEXT, " + DEVICE_ID + " INTEGER, " + BODY + " TEXT, " + TIMESTAMP + " INTEGER);";
|
2013-09-09 01:19:05 +00:00
|
|
|
|
|
|
|
public PushDatabase(Context context, SQLiteOpenHelper databaseHelper) {
|
|
|
|
super(context, databaseHelper);
|
|
|
|
}
|
|
|
|
|
|
|
|
public long insert(IncomingPushMessage message) {
|
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
values.put(TYPE, message.getType());
|
|
|
|
values.put(SOURCE, message.getSource());
|
2014-02-22 18:54:43 +00:00
|
|
|
values.put(DEVICE_ID, message.getSourceDevice());
|
2013-09-09 23:46:03 +00:00
|
|
|
values.put(BODY, Base64.encodeBytes(message.getBody()));
|
2013-09-09 01:19:05 +00:00
|
|
|
values.put(TIMESTAMP, message.getTimestampMillis());
|
|
|
|
|
|
|
|
return databaseHelper.getWritableDatabase().insert(TABLE_NAME, null, values);
|
|
|
|
}
|
|
|
|
|
2013-09-09 23:46:03 +00:00
|
|
|
public Cursor getPending() {
|
|
|
|
return databaseHelper.getReadableDatabase().query(TABLE_NAME, null, null, null, null, null, null);
|
|
|
|
}
|
|
|
|
|
2013-09-09 01:19:05 +00:00
|
|
|
public void delete(long id) {
|
|
|
|
databaseHelper.getWritableDatabase().delete(TABLE_NAME, ID_WHERE, new String[] {id+""});
|
|
|
|
}
|
2013-09-09 23:46:03 +00:00
|
|
|
|
|
|
|
public Reader readerFor(Cursor cursor) {
|
|
|
|
return new Reader(cursor);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static class Reader {
|
|
|
|
private final Cursor cursor;
|
|
|
|
|
|
|
|
public Reader(Cursor cursor) {
|
|
|
|
this.cursor = cursor;
|
|
|
|
}
|
|
|
|
|
|
|
|
public IncomingPushMessage getNext() {
|
|
|
|
try {
|
|
|
|
if (cursor == null || !cursor.moveToNext())
|
|
|
|
return null;
|
|
|
|
|
|
|
|
int type = cursor.getInt(cursor.getColumnIndexOrThrow(TYPE));
|
|
|
|
String source = cursor.getString(cursor.getColumnIndexOrThrow(SOURCE));
|
2014-02-03 03:38:06 +00:00
|
|
|
int deviceId = cursor.getInt(cursor.getColumnIndexOrThrow(DEVICE_ID));
|
2013-09-09 23:46:03 +00:00
|
|
|
byte[] body = Base64.decode(cursor.getString(cursor.getColumnIndexOrThrow(BODY)));
|
|
|
|
long timestamp = cursor.getLong(cursor.getColumnIndexOrThrow(TIMESTAMP));
|
|
|
|
|
2014-02-03 03:38:06 +00:00
|
|
|
return new IncomingPushMessage(type, source, deviceId, body, timestamp);
|
2013-09-09 23:46:03 +00:00
|
|
|
} catch (IOException e) {
|
|
|
|
throw new AssertionError(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public long getCurrentId() {
|
|
|
|
return cursor.getLong(cursor.getColumnIndexOrThrow(ID));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void close() {
|
|
|
|
this.cursor.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-09 01:19:05 +00:00
|
|
|
}
|