Started to move the messages storage into liblinphone

This commit is contained in:
Sylvain Berfini 2013-08-08 16:24:43 +02:00
parent 4f888a5919
commit 84670ba428
5 changed files with 175 additions and 77 deletions

View file

@ -62,6 +62,7 @@
<bool name="forbid_self_call">false</bool>
<bool name="disable_chat">false</bool>
<bool name="disable_chat_send_file">false</bool>
<bool name="use_linphone_chat_storage">false</bool>
<bool name="hash_images_as_name_before_upload">true</bool>

View file

@ -22,6 +22,7 @@ import java.util.ArrayList;
import java.util.List;
import org.linphone.core.LinphoneChatMessage;
import org.linphone.core.LinphoneChatRoom;
import android.content.ContentValues;
import android.content.Context;
@ -39,22 +40,41 @@ public class ChatStorage {
private static final int OUTGOING = 0;
private static final int READ = 1;
private static final int NOT_READ = 0;
private static ChatStorage instance;
private Context context;
private SQLiteDatabase db;
private boolean useNativeAPI;
private static final String TABLE_NAME = "chat";
private static final String DRAFT_TABLE_NAME = "chat_draft";
public ChatStorage(Context c) {
public synchronized static final ChatStorage getInstance() {
if (instance == null)
instance = new ChatStorage(LinphoneService.instance().getApplicationContext());
return instance;
}
private ChatStorage(Context c) {
context = c;
ChatHelper chatHelper = new ChatHelper(context);
db = chatHelper.getWritableDatabase();
useNativeAPI = c.getResources().getBoolean(R.bool.use_linphone_chat_storage);
if (!useNativeAPI) {
ChatHelper chatHelper = new ChatHelper(context);
db = chatHelper.getWritableDatabase();
}
}
public void close() {
db.close();
if (!useNativeAPI) {
db.close();
}
}
public void updateMessageStatus(String to, String message, int status) {
if (useNativeAPI) {
return;
}
String[] whereArgs = { String.valueOf(OUTGOING), to, message };
Cursor c = db.query(TABLE_NAME, null, "direction LIKE ? AND remoteContact LIKE ? AND message LIKE ?", whereArgs, null, null, "id DESC");
@ -75,6 +95,10 @@ public class ChatStorage {
}
public void updateMessageStatus(String to, int id, int status) {
if (useNativeAPI) {
return;
}
ContentValues values = new ContentValues();
values.put("status", status);
@ -82,6 +106,10 @@ public class ChatStorage {
}
public int saveTextMessage(String from, String to, String message, long time) {
if (useNativeAPI) {
return -1;
}
ContentValues values = new ContentValues();
if (from.equals("")) {
values.put("localContact", from);
@ -102,6 +130,10 @@ public class ChatStorage {
}
public int saveImageMessage(String from, String to, Bitmap image, String url, long time) {
if (useNativeAPI) {
return -1;
}
ContentValues values = new ContentValues();
if (from.equals("")) {
values.put("localContact", from);
@ -129,6 +161,11 @@ public class ChatStorage {
}
public void saveImage(int id, Bitmap image) {
if (useNativeAPI) {
//TODO
return;
}
if (image == null)
return;
@ -141,6 +178,11 @@ public class ChatStorage {
}
public int saveDraft(String to, String message) {
if (useNativeAPI) {
//TODO
return -1;
}
ContentValues values = new ContentValues();
values.put("remoteContact", to);
values.put("message", message);
@ -148,6 +190,11 @@ public class ChatStorage {
}
public void updateDraft(String to, String message) {
if (useNativeAPI) {
//TODO
return;
}
ContentValues values = new ContentValues();
values.put("message", message);
@ -155,10 +202,20 @@ public class ChatStorage {
}
public void deleteDraft(String to) {
if (useNativeAPI) {
//TODO
return;
}
db.delete(DRAFT_TABLE_NAME, "remoteContact LIKE \"" + to + "\"", null);
}
public String getDraft(String to) {
if (useNativeAPI) {
//TODO
return "";
}
Cursor c = db.query(DRAFT_TABLE_NAME, null, "remoteContact LIKE \"" + to + "\"", null, null, null, "id ASC");
String message = null;
@ -176,17 +233,22 @@ public class ChatStorage {
public List<String> getDrafts() {
List<String> drafts = new ArrayList<String>();
Cursor c = db.query(DRAFT_TABLE_NAME, null, null, null, null, null, "id ASC");
while (c.moveToNext()) {
try {
String to = c.getString(c.getColumnIndex("remoteContact"));
drafts.add(to);
} catch (Exception e) {
e.printStackTrace();
if (useNativeAPI) {
//TODO
} else {
Cursor c = db.query(DRAFT_TABLE_NAME, null, null, null, null, null, "id ASC");
while (c.moveToNext()) {
try {
String to = c.getString(c.getColumnIndex("remoteContact"));
drafts.add(to);
} catch (Exception e) {
e.printStackTrace();
}
}
c.close();
}
c.close();
return drafts;
}
@ -194,94 +256,140 @@ public class ChatStorage {
public List<ChatMessage> getMessages(String correspondent) {
List<ChatMessage> chatMessages = new ArrayList<ChatMessage>();
Cursor c = db.query(TABLE_NAME, null, "remoteContact LIKE \"" + correspondent + "\"", null, null, null, "id ASC");
if (!useNativeAPI) {
Cursor c = db.query(TABLE_NAME, null, "remoteContact LIKE \"" + correspondent + "\"", null, null, null, "id ASC");
while (c.moveToNext()) {
try {
String message, timestamp, url;
int id = c.getInt(c.getColumnIndex("id"));
int direction = c.getInt(c.getColumnIndex("direction"));
message = c.getString(c.getColumnIndex("message"));
timestamp = c.getString(c.getColumnIndex("time"));
int status = c.getInt(c.getColumnIndex("status"));
byte[] rawImage = c.getBlob(c.getColumnIndex("image"));
int read = c.getInt(c.getColumnIndex("read"));
url = c.getString(c.getColumnIndex("url"));
while (c.moveToNext()) {
try {
String message, timestamp, url;
int id = c.getInt(c.getColumnIndex("id"));
int direction = c.getInt(c.getColumnIndex("direction"));
message = c.getString(c.getColumnIndex("message"));
timestamp = c.getString(c.getColumnIndex("time"));
int status = c.getInt(c.getColumnIndex("status"));
byte[] rawImage = c.getBlob(c.getColumnIndex("image"));
int read = c.getInt(c.getColumnIndex("read"));
url = c.getString(c.getColumnIndex("url"));
ChatMessage chatMessage = new ChatMessage(id, message, rawImage, timestamp, direction == INCOMING, status, read == READ);
chatMessage.setUrl(url);
ChatMessage chatMessage = new ChatMessage(id, message, rawImage, timestamp, direction == INCOMING, status, read == READ);
chatMessage.setUrl(url);
chatMessages.add(chatMessage);
} catch (Exception e) {
e.printStackTrace();
}
}
c.close();
} else {
LinphoneChatRoom room = LinphoneManager.getLc().createChatRoom(correspondent);
for (LinphoneChatMessage message : room.getHistory()) {
ChatMessage chatMessage = new ChatMessage(message.hashCode(), message.getText(), null, String.valueOf(message.getTime()), true, 0, true);
chatMessage.setUrl(message.getExternalBodyUrl());
chatMessages.add(chatMessage);
} catch (Exception e) {
e.printStackTrace();
}
}
c.close();
return chatMessages;
}
public String getTextMessageForId(int id) {
Cursor c = db.query(TABLE_NAME, null, "id LIKE " + id, null, null, null, null);
String message = null;
if (c.moveToFirst()) {
try {
message = c.getString(c.getColumnIndex("message"));
} catch (Exception e) {
e.printStackTrace();
if (useNativeAPI) {
//TODO
} else {
Cursor c = db.query(TABLE_NAME, null, "id LIKE " + id, null, null, null, null);
if (c.moveToFirst()) {
try {
message = c.getString(c.getColumnIndex("message"));
} catch (Exception e) {
e.printStackTrace();
}
}
c.close();
}
c.close();
return message;
}
public void removeDiscussion(String correspondent) {
db.delete(TABLE_NAME, "remoteContact LIKE \"" + correspondent + "\"", null);
if (useNativeAPI) {
//TODO
} else {
db.delete(TABLE_NAME, "remoteContact LIKE \"" + correspondent + "\"", null);
}
}
public ArrayList<String> getChatList() {
ArrayList<String> chatList = new ArrayList<String>();
Cursor c = db.query(TABLE_NAME, null, null, null, "remoteContact", null, "id DESC");
while (c != null && c.moveToNext()) {
try {
String remoteContact = c.getString(c.getColumnIndex("remoteContact"));
chatList.add(remoteContact);
} catch (IllegalStateException ise) {
if (useNativeAPI) {
//TODO
} else {
Cursor c = db.query(TABLE_NAME, null, null, null, "remoteContact", null, "id DESC");
while (c != null && c.moveToNext()) {
try {
String remoteContact = c.getString(c.getColumnIndex("remoteContact"));
chatList.add(remoteContact);
} catch (IllegalStateException ise) {
}
}
c.close();
}
c.close();
return chatList;
}
public void deleteMessage(int id) {
db.delete(TABLE_NAME, "id LIKE " + id, null);
if (useNativeAPI) {
//TODO
} else {
db.delete(TABLE_NAME, "id LIKE " + id, null);
}
}
public void markMessageAsRead(int id) {
ContentValues values = new ContentValues();
values.put("read", READ);
db.update(TABLE_NAME, values, "id LIKE " + id, null);
if (useNativeAPI) {
//TODO
} else {
ContentValues values = new ContentValues();
values.put("read", READ);
db.update(TABLE_NAME, values, "id LIKE " + id, null);
}
}
public int getUnreadMessageCount() {
Cursor c = db.query(TABLE_NAME, null, "read LIKE " + NOT_READ, null, null, null, null);
int count = c.getCount();
c.close();
int count;
if (!useNativeAPI) {
Cursor c = db.query(TABLE_NAME, null, "read LIKE " + NOT_READ, null, null, null, null);
count = c.getCount();
c.close();
} else {
//TODO
count = -1;
}
return count;
}
public int getUnreadMessageCount(String contact) {
Cursor c = db.query(TABLE_NAME, null, "remoteContact LIKE \"" + contact + "\" AND read LIKE " + NOT_READ, null, null, null, null);
int count = c.getCount();
c.close();
int count;
if (!useNativeAPI) {
Cursor c = db.query(TABLE_NAME, null, "remoteContact LIKE \"" + contact + "\" AND read LIKE " + NOT_READ, null, null, null, null);
count = c.getCount();
c.close();
} else {
//TODO
count = -1;
}
return count;
}
public byte[] getRawImageFromMessage(int id) {
if (useNativeAPI) {
//TODO
return null;
}
String[] columns = { "image" };
Cursor c = db.query(TABLE_NAME, columns, "id LIKE " + id + "", null, null, null, null);

View file

@ -1181,10 +1181,7 @@ public class LinphoneActivity extends FragmentActivity implements
}
public ChatStorage getChatStorage() {
if (LinphoneManager.getInstance().chatStorage == null) {
return new ChatStorage(this);
}
return LinphoneManager.getInstance().chatStorage;
return ChatStorage.getInstance();
}
public void addContact(String displayName, String sipUri)

View file

@ -29,7 +29,6 @@ import static org.linphone.R.string.pref_codec_speex32_key;
import static org.linphone.R.string.pref_video_enable_key;
import static org.linphone.core.LinphoneCall.State.CallEnd;
import static org.linphone.core.LinphoneCall.State.Error;
import static org.linphone.core.LinphoneCall.State.CallReleased;
import static org.linphone.core.LinphoneCall.State.IncomingReceived;
import java.io.File;
@ -165,8 +164,6 @@ public class LinphoneManager implements LinphoneCoreListener {
public boolean isUsingBluetoothAudioRoute;
private boolean mBluetoothStarted;
public ChatStorage chatStorage;
private static List<LinphoneSimpleListener> simpleListeners = new ArrayList<LinphoneSimpleListener>();
public static void addListener(LinphoneSimpleListener listener) {
if (!simpleListeners.contains(listener)) {
@ -189,6 +186,7 @@ public class LinphoneManager implements LinphoneCoreListener {
mRingSoundFile = basePath + "/oldphone_mono.wav";
mRingbackSoundFile = basePath + "/ringback.wav";
mPauseSoundFile = basePath + "/toy_mono.wav";
mChatDatabaseFile = basePath + "/linphone-history.db";
sLPref = LinphonePreferenceManager.getInstance(c);
mAudioManager = ((AudioManager) c.getSystemService(Context.AUDIO_SERVICE));
@ -197,8 +195,6 @@ public class LinphoneManager implements LinphoneCoreListener {
mPowerManager = (PowerManager) c.getSystemService(Context.POWER_SERVICE);
mConnectivityManager = (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE);
mR = c.getResources();
chatStorage = new ChatStorage(mServiceContext);
}
private static final int LINPHONE_VOLUME_STREAM = STREAM_VOICE_CALL;
@ -211,6 +207,7 @@ public class LinphoneManager implements LinphoneCoreListener {
private final String mRingSoundFile;
private final String mRingbackSoundFile;
private final String mPauseSoundFile;
private final String mChatDatabaseFile;
private Timer mTimer = new Timer("Linphone scheduler");
@ -575,6 +572,7 @@ public class LinphoneManager implements LinphoneCoreListener {
mLc.setRing(null);
mLc.setRootCA(mLinphoneRootCaFile);
mLc.setPlayFile(mPauseSoundFile);
mLc.setChatDatabasePath(mChatDatabaseFile);
int availableCores = Runtime.getRuntime().availableProcessors();
Log.w("MediaStreamer : " + availableCores + " cores detected and configured");
@ -1010,10 +1008,7 @@ public class LinphoneManager implements LinphoneCoreListener {
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void doDestroy() {
if (chatStorage != null) {
chatStorage.close();
chatStorage = null;
}
ChatStorage.getInstance().close();
try {
mServiceContext.unregisterReceiver(bluetoothReiceiver);
@ -1147,15 +1142,12 @@ public class LinphoneManager implements LinphoneCoreListener {
String textMessage = message.getText();
String url = message.getExternalBodyUrl();
String notificationText = null;
int id = -1;
if (textMessage != null && textMessage.length() > 0) {
id = chatStorage.saveTextMessage(from.asStringUriOnly(), "", textMessage, message.getTime());
notificationText = textMessage;
id = ChatStorage.getInstance().saveTextMessage(from.asStringUriOnly(), "", textMessage, message.getTime());
} else if (url != null && url.length() > 0) {
//Bitmap bm = ChatFragment.downloadImage(url);
id = chatStorage.saveImageMessage(from.asStringUriOnly(), "", null, message.getExternalBodyUrl(), message.getTime());
notificationText = url;
id = ChatStorage.getInstance().saveImageMessage(from.asStringUriOnly(), "", null, message.getExternalBodyUrl(), message.getTime());
}
try {

@ -1 +1 @@
Subproject commit 9d3fb120064b0d42048e3e14c0f0e1d6f4857a46
Subproject commit 389922acce2412e6e4f57a74d3e7ea41e8e58771