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="forbid_self_call">false</bool>
<bool name="disable_chat">false</bool> <bool name="disable_chat">false</bool>
<bool name="disable_chat_send_file">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> <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 java.util.List;
import org.linphone.core.LinphoneChatMessage; import org.linphone.core.LinphoneChatMessage;
import org.linphone.core.LinphoneChatRoom;
import android.content.ContentValues; import android.content.ContentValues;
import android.content.Context; import android.content.Context;
@ -39,22 +40,41 @@ public class ChatStorage {
private static final int OUTGOING = 0; private static final int OUTGOING = 0;
private static final int READ = 1; private static final int READ = 1;
private static final int NOT_READ = 0; private static final int NOT_READ = 0;
private static ChatStorage instance;
private Context context; private Context context;
private SQLiteDatabase db; private SQLiteDatabase db;
private boolean useNativeAPI;
private static final String TABLE_NAME = "chat"; private static final String TABLE_NAME = "chat";
private static final String DRAFT_TABLE_NAME = "chat_draft"; 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; context = c;
ChatHelper chatHelper = new ChatHelper(context); useNativeAPI = c.getResources().getBoolean(R.bool.use_linphone_chat_storage);
db = chatHelper.getWritableDatabase();
if (!useNativeAPI) {
ChatHelper chatHelper = new ChatHelper(context);
db = chatHelper.getWritableDatabase();
}
} }
public void close() { public void close() {
db.close(); if (!useNativeAPI) {
db.close();
}
} }
public void updateMessageStatus(String to, String message, int status) { public void updateMessageStatus(String to, String message, int status) {
if (useNativeAPI) {
return;
}
String[] whereArgs = { String.valueOf(OUTGOING), to, message }; 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"); 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) { public void updateMessageStatus(String to, int id, int status) {
if (useNativeAPI) {
return;
}
ContentValues values = new ContentValues(); ContentValues values = new ContentValues();
values.put("status", status); values.put("status", status);
@ -82,6 +106,10 @@ public class ChatStorage {
} }
public int saveTextMessage(String from, String to, String message, long time) { public int saveTextMessage(String from, String to, String message, long time) {
if (useNativeAPI) {
return -1;
}
ContentValues values = new ContentValues(); ContentValues values = new ContentValues();
if (from.equals("")) { if (from.equals("")) {
values.put("localContact", from); values.put("localContact", from);
@ -102,6 +130,10 @@ public class ChatStorage {
} }
public int saveImageMessage(String from, String to, Bitmap image, String url, long time) { public int saveImageMessage(String from, String to, Bitmap image, String url, long time) {
if (useNativeAPI) {
return -1;
}
ContentValues values = new ContentValues(); ContentValues values = new ContentValues();
if (from.equals("")) { if (from.equals("")) {
values.put("localContact", from); values.put("localContact", from);
@ -129,6 +161,11 @@ public class ChatStorage {
} }
public void saveImage(int id, Bitmap image) { public void saveImage(int id, Bitmap image) {
if (useNativeAPI) {
//TODO
return;
}
if (image == null) if (image == null)
return; return;
@ -141,6 +178,11 @@ public class ChatStorage {
} }
public int saveDraft(String to, String message) { public int saveDraft(String to, String message) {
if (useNativeAPI) {
//TODO
return -1;
}
ContentValues values = new ContentValues(); ContentValues values = new ContentValues();
values.put("remoteContact", to); values.put("remoteContact", to);
values.put("message", message); values.put("message", message);
@ -148,6 +190,11 @@ public class ChatStorage {
} }
public void updateDraft(String to, String message) { public void updateDraft(String to, String message) {
if (useNativeAPI) {
//TODO
return;
}
ContentValues values = new ContentValues(); ContentValues values = new ContentValues();
values.put("message", message); values.put("message", message);
@ -155,10 +202,20 @@ public class ChatStorage {
} }
public void deleteDraft(String to) { public void deleteDraft(String to) {
if (useNativeAPI) {
//TODO
return;
}
db.delete(DRAFT_TABLE_NAME, "remoteContact LIKE \"" + to + "\"", null); db.delete(DRAFT_TABLE_NAME, "remoteContact LIKE \"" + to + "\"", null);
} }
public String getDraft(String to) { 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"); Cursor c = db.query(DRAFT_TABLE_NAME, null, "remoteContact LIKE \"" + to + "\"", null, null, null, "id ASC");
String message = null; String message = null;
@ -176,17 +233,22 @@ public class ChatStorage {
public List<String> getDrafts() { public List<String> getDrafts() {
List<String> drafts = new ArrayList<String>(); List<String> drafts = new ArrayList<String>();
Cursor c = db.query(DRAFT_TABLE_NAME, null, null, null, null, null, "id ASC");
if (useNativeAPI) {
//TODO
} else {
Cursor c = db.query(DRAFT_TABLE_NAME, null, null, null, null, null, "id ASC");
while (c.moveToNext()) { while (c.moveToNext()) {
try { try {
String to = c.getString(c.getColumnIndex("remoteContact")); String to = c.getString(c.getColumnIndex("remoteContact"));
drafts.add(to); drafts.add(to);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
}
} }
c.close();
} }
c.close();
return drafts; return drafts;
} }
@ -194,94 +256,140 @@ public class ChatStorage {
public List<ChatMessage> getMessages(String correspondent) { public List<ChatMessage> getMessages(String correspondent) {
List<ChatMessage> chatMessages = new ArrayList<ChatMessage>(); 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 { while (c.moveToNext()) {
String message, timestamp, url; try {
int id = c.getInt(c.getColumnIndex("id")); String message, timestamp, url;
int direction = c.getInt(c.getColumnIndex("direction")); int id = c.getInt(c.getColumnIndex("id"));
message = c.getString(c.getColumnIndex("message")); int direction = c.getInt(c.getColumnIndex("direction"));
timestamp = c.getString(c.getColumnIndex("time")); message = c.getString(c.getColumnIndex("message"));
int status = c.getInt(c.getColumnIndex("status")); timestamp = c.getString(c.getColumnIndex("time"));
byte[] rawImage = c.getBlob(c.getColumnIndex("image")); int status = c.getInt(c.getColumnIndex("status"));
int read = c.getInt(c.getColumnIndex("read")); byte[] rawImage = c.getBlob(c.getColumnIndex("image"));
url = c.getString(c.getColumnIndex("url")); 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); chatMessages.add(chatMessage);
} catch (Exception e) {
e.printStackTrace();
} }
} }
c.close();
return chatMessages; return chatMessages;
} }
public String getTextMessageForId(int id) { public String getTextMessageForId(int id) {
Cursor c = db.query(TABLE_NAME, null, "id LIKE " + id, null, null, null, null);
String message = null; String message = null;
if (c.moveToFirst()) {
try { if (useNativeAPI) {
message = c.getString(c.getColumnIndex("message")); //TODO
} catch (Exception e) { } else {
e.printStackTrace(); 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; return message;
} }
public void removeDiscussion(String correspondent) { 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() { public ArrayList<String> getChatList() {
ArrayList<String> chatList = new ArrayList<String>(); ArrayList<String> chatList = new ArrayList<String>();
Cursor c = db.query(TABLE_NAME, null, null, null, "remoteContact", null, "id DESC"); if (useNativeAPI) {
while (c != null && c.moveToNext()) { //TODO
try { } else {
String remoteContact = c.getString(c.getColumnIndex("remoteContact")); Cursor c = db.query(TABLE_NAME, null, null, null, "remoteContact", null, "id DESC");
chatList.add(remoteContact); while (c != null && c.moveToNext()) {
} catch (IllegalStateException ise) { try {
String remoteContact = c.getString(c.getColumnIndex("remoteContact"));
chatList.add(remoteContact);
} catch (IllegalStateException ise) {
}
} }
c.close();
} }
c.close();
return chatList; return chatList;
} }
public void deleteMessage(int id) { 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) { public void markMessageAsRead(int id) {
ContentValues values = new ContentValues(); if (useNativeAPI) {
values.put("read", READ); //TODO
db.update(TABLE_NAME, values, "id LIKE " + id, null); } else {
ContentValues values = new ContentValues();
values.put("read", READ);
db.update(TABLE_NAME, values, "id LIKE " + id, null);
}
} }
public int getUnreadMessageCount() { public int getUnreadMessageCount() {
Cursor c = db.query(TABLE_NAME, null, "read LIKE " + NOT_READ, null, null, null, null); int count;
int count = c.getCount(); if (!useNativeAPI) {
c.close(); 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; return count;
} }
public int getUnreadMessageCount(String contact) { 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;
int count = c.getCount(); if (!useNativeAPI) {
c.close(); 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; return count;
} }
public byte[] getRawImageFromMessage(int id) { public byte[] getRawImageFromMessage(int id) {
if (useNativeAPI) {
//TODO
return null;
}
String[] columns = { "image" }; String[] columns = { "image" };
Cursor c = db.query(TABLE_NAME, columns, "id LIKE " + id + "", null, null, null, null); 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() { public ChatStorage getChatStorage() {
if (LinphoneManager.getInstance().chatStorage == null) { return ChatStorage.getInstance();
return new ChatStorage(this);
}
return LinphoneManager.getInstance().chatStorage;
} }
public void addContact(String displayName, String sipUri) 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.R.string.pref_video_enable_key;
import static org.linphone.core.LinphoneCall.State.CallEnd; import static org.linphone.core.LinphoneCall.State.CallEnd;
import static org.linphone.core.LinphoneCall.State.Error; 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 static org.linphone.core.LinphoneCall.State.IncomingReceived;
import java.io.File; import java.io.File;
@ -164,8 +163,6 @@ public class LinphoneManager implements LinphoneCoreListener {
public boolean isBluetoothScoConnected; public boolean isBluetoothScoConnected;
public boolean isUsingBluetoothAudioRoute; public boolean isUsingBluetoothAudioRoute;
private boolean mBluetoothStarted; private boolean mBluetoothStarted;
public ChatStorage chatStorage;
private static List<LinphoneSimpleListener> simpleListeners = new ArrayList<LinphoneSimpleListener>(); private static List<LinphoneSimpleListener> simpleListeners = new ArrayList<LinphoneSimpleListener>();
public static void addListener(LinphoneSimpleListener listener) { public static void addListener(LinphoneSimpleListener listener) {
@ -189,6 +186,7 @@ public class LinphoneManager implements LinphoneCoreListener {
mRingSoundFile = basePath + "/oldphone_mono.wav"; mRingSoundFile = basePath + "/oldphone_mono.wav";
mRingbackSoundFile = basePath + "/ringback.wav"; mRingbackSoundFile = basePath + "/ringback.wav";
mPauseSoundFile = basePath + "/toy_mono.wav"; mPauseSoundFile = basePath + "/toy_mono.wav";
mChatDatabaseFile = basePath + "/linphone-history.db";
sLPref = LinphonePreferenceManager.getInstance(c); sLPref = LinphonePreferenceManager.getInstance(c);
mAudioManager = ((AudioManager) c.getSystemService(Context.AUDIO_SERVICE)); mAudioManager = ((AudioManager) c.getSystemService(Context.AUDIO_SERVICE));
@ -197,8 +195,6 @@ public class LinphoneManager implements LinphoneCoreListener {
mPowerManager = (PowerManager) c.getSystemService(Context.POWER_SERVICE); mPowerManager = (PowerManager) c.getSystemService(Context.POWER_SERVICE);
mConnectivityManager = (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE); mConnectivityManager = (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE);
mR = c.getResources(); mR = c.getResources();
chatStorage = new ChatStorage(mServiceContext);
} }
private static final int LINPHONE_VOLUME_STREAM = STREAM_VOICE_CALL; 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 mRingSoundFile;
private final String mRingbackSoundFile; private final String mRingbackSoundFile;
private final String mPauseSoundFile; private final String mPauseSoundFile;
private final String mChatDatabaseFile;
private Timer mTimer = new Timer("Linphone scheduler"); private Timer mTimer = new Timer("Linphone scheduler");
@ -575,6 +572,7 @@ public class LinphoneManager implements LinphoneCoreListener {
mLc.setRing(null); mLc.setRing(null);
mLc.setRootCA(mLinphoneRootCaFile); mLc.setRootCA(mLinphoneRootCaFile);
mLc.setPlayFile(mPauseSoundFile); mLc.setPlayFile(mPauseSoundFile);
mLc.setChatDatabasePath(mChatDatabaseFile);
int availableCores = Runtime.getRuntime().availableProcessors(); int availableCores = Runtime.getRuntime().availableProcessors();
Log.w("MediaStreamer : " + availableCores + " cores detected and configured"); Log.w("MediaStreamer : " + availableCores + " cores detected and configured");
@ -1010,10 +1008,7 @@ public class LinphoneManager implements LinphoneCoreListener {
@TargetApi(Build.VERSION_CODES.HONEYCOMB) @TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void doDestroy() { private void doDestroy() {
if (chatStorage != null) { ChatStorage.getInstance().close();
chatStorage.close();
chatStorage = null;
}
try { try {
mServiceContext.unregisterReceiver(bluetoothReiceiver); mServiceContext.unregisterReceiver(bluetoothReiceiver);
@ -1147,15 +1142,12 @@ public class LinphoneManager implements LinphoneCoreListener {
String textMessage = message.getText(); String textMessage = message.getText();
String url = message.getExternalBodyUrl(); String url = message.getExternalBodyUrl();
String notificationText = null;
int id = -1; int id = -1;
if (textMessage != null && textMessage.length() > 0) { if (textMessage != null && textMessage.length() > 0) {
id = chatStorage.saveTextMessage(from.asStringUriOnly(), "", textMessage, message.getTime()); id = ChatStorage.getInstance().saveTextMessage(from.asStringUriOnly(), "", textMessage, message.getTime());
notificationText = textMessage;
} else if (url != null && url.length() > 0) { } else if (url != null && url.length() > 0) {
//Bitmap bm = ChatFragment.downloadImage(url); //Bitmap bm = ChatFragment.downloadImage(url);
id = chatStorage.saveImageMessage(from.asStringUriOnly(), "", null, message.getExternalBodyUrl(), message.getTime()); id = ChatStorage.getInstance().saveImageMessage(from.asStringUriOnly(), "", null, message.getExternalBodyUrl(), message.getTime());
notificationText = url;
} }
try { try {

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