Handle notifications and receiving push when locked.

This commit is contained in:
Moxie Marlinspike
2013-09-09 16:46:03 -07:00
parent 0dd36c64a4
commit 8f6590b738
9 changed files with 226 additions and 53 deletions

View File

@@ -2,12 +2,16 @@ package org.thoughtcrime.securesms.database;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteOpenHelper;
import org.spongycastle.util.encoders.Base64;
import org.whispersystems.textsecure.push.IncomingPushMessage;
import org.whispersystems.textsecure.util.Base64;
import org.whispersystems.textsecure.util.Util;
import java.io.IOException;
import java.util.List;
public class PushDatabase extends Database {
private static final String TABLE_NAME = "push";
@@ -30,13 +34,55 @@ public class PushDatabase extends Database {
values.put(TYPE, message.getType());
values.put(SOURCE, message.getSource());
values.put(DESTINATIONS, Util.join(message.getDestinations(), ","));
values.put(BODY, Base64.encode(message.getBody()));
values.put(BODY, Base64.encodeBytes(message.getBody()));
values.put(TIMESTAMP, message.getTimestampMillis());
return databaseHelper.getWritableDatabase().insert(TABLE_NAME, null, values);
}
public Cursor getPending() {
return databaseHelper.getReadableDatabase().query(TABLE_NAME, null, null, null, null, null, null);
}
public void delete(long id) {
databaseHelper.getWritableDatabase().delete(TABLE_NAME, ID_WHERE, new String[] {id+""});
}
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));
List<String> destinations = Util.split(cursor.getString(cursor.getColumnIndexOrThrow(DESTINATIONS)), ",");
byte[] body = Base64.decode(cursor.getString(cursor.getColumnIndexOrThrow(BODY)));
long timestamp = cursor.getLong(cursor.getColumnIndexOrThrow(TIMESTAMP));
return new IncomingPushMessage(type, source, destinations, body, timestamp);
} catch (IOException e) {
throw new AssertionError(e);
}
}
public long getCurrentId() {
return cursor.getLong(cursor.getColumnIndexOrThrow(ID));
}
public void close() {
this.cursor.close();
}
}
}