From b67cec5ac172dd8b9ed90507e0a3b67487233c74 Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Tue, 17 Jul 2012 16:21:06 +0200 Subject: [PATCH] Messages database changed --- src/org/linphone/ChatFragment.java | 11 ++--- src/org/linphone/ChatStorage.java | 67 ++++++++++++++------------ src/org/linphone/LinphoneActivity.java | 24 ++++++--- 3 files changed, 57 insertions(+), 45 deletions(-) diff --git a/src/org/linphone/ChatFragment.java b/src/org/linphone/ChatFragment.java index 669fe6bfa..840503163 100644 --- a/src/org/linphone/ChatFragment.java +++ b/src/org/linphone/ChatFragment.java @@ -83,7 +83,6 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneO message = (EditText) view.findViewById(R.id.message); messagesLayout = (RelativeLayout) view.findViewById(R.id.messages); - List messagesList = LinphoneActivity.instance().getChatMessages(sipUri); messagesScrollView = (ScrollView) view.findViewById(R.id.chatScrollView); messagesScrollView.post(new Runnable() { @@ -93,10 +92,7 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneO } }); - previousMessageID = -1; - for (ChatMessage msg : messagesList) { - displayMessage(msg.getId(), msg.getMessage(), msg.getTimestamp(), msg.isIncoming(), messagesLayout); - } + invalidate(); LinphoneCore lc = LinphoneManager.getLcIfManagerNotDestroyedOrNull(); if (lc != null) @@ -110,9 +106,12 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneO List messagesList = LinphoneActivity.instance().getChatMessages(sipUri); previousMessageID = -1; + ChatStorage chatStorage = LinphoneActivity.instance().getChatStorage(); for (ChatMessage msg : messagesList) { displayMessage(msg.getId(), msg.getMessage(), msg.getTimestamp(), msg.isIncoming(), messagesLayout); + chatStorage.markMessageAsRead(msg.getId()); } + LinphoneActivity.instance().updateMissedChatCount(); scrollToEnd(); } @@ -137,7 +136,7 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneO @Override public boolean onContextItemSelected(MenuItem item) { - LinphoneActivity.instance().deleteMessage(item.getItemId()); + LinphoneActivity.instance().getChatStorage().deleteMessage(item.getItemId()); invalidate(); return true; } diff --git a/src/org/linphone/ChatStorage.java b/src/org/linphone/ChatStorage.java index 4cee53f23..e229bad8c 100644 --- a/src/org/linphone/ChatStorage.java +++ b/src/org/linphone/ChatStorage.java @@ -20,8 +20,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. import java.util.ArrayList; import java.util.List; -import org.linphone.core.Log; - import android.content.ContentValues; import android.content.Context; import android.database.Cursor; @@ -32,6 +30,10 @@ import android.database.sqlite.SQLiteOpenHelper; * @author Sylvain Berfini */ public class ChatStorage { + private static final int INCOMING = 1; + private static final int OUTGOING = 0; + private static final int READ = 1; + private static final int NOT_READ = 0; private Context context; private SQLiteDatabase db; private static final String TABLE_NAME = "chat"; @@ -48,9 +50,17 @@ public class ChatStorage { public void saveMessage(String from, String to, String message) { ContentValues values = new ContentValues(); - values.put("sender", from); - values.put("receiver", to); + if (from.equals("")) { + values.put("localContact", from); + values.put("remoteContact", to); + values.put("direction", OUTGOING); + } else if (to.equals("")) { + values.put("localContact", to); + values.put("remoteContact", from); + values.put("direction", INCOMING); + } values.put("message", message); + values.put("read", NOT_READ); values.put("time", System.currentTimeMillis()); db.insert(TABLE_NAME, null, values); } @@ -58,17 +68,16 @@ public class ChatStorage { public List getMessages(String correspondent) { List chatMessages = new ArrayList(); - Cursor c = db.query(TABLE_NAME, null, "receiver LIKE \"" + correspondent + - "\" OR sender LIKE \"" + correspondent + "\"", null, null, null, "id ASC"); + Cursor c = db.query(TABLE_NAME, null, "remoteContact LIKE \"" + correspondent + "\"", null, null, null, "id ASC"); while (c.moveToNext()) { - String to, message, timestamp; + String message, timestamp; int id = c.getInt(c.getColumnIndex("id")); - to = c.getString(c.getColumnIndex("receiver")); + int direction = c.getInt(c.getColumnIndex("direction")); message = c.getString(c.getColumnIndex("message")); timestamp = c.getString(c.getColumnIndex("time")); - ChatMessage chatMessage = new ChatMessage(id, message, timestamp, to.equals("")); + ChatMessage chatMessage = new ChatMessage(id, message, timestamp, direction == INCOMING); chatMessages.add(chatMessage); } @@ -76,30 +85,16 @@ public class ChatStorage { } public void removeDiscussion(String correspondent) { - db.delete(TABLE_NAME, "sender LIKE \"" + correspondent + "\"", null); - db.delete(TABLE_NAME, "receiver LIKE \"" + correspondent + "\"", null); + db.delete(TABLE_NAME, "remoteContact LIKE \"" + correspondent + "\"", null); } public ArrayList getChatList() { ArrayList chatList = new ArrayList(); - Cursor c = db.query(TABLE_NAME, null, null, null, null, null, "id DESC"); + Cursor c = db.query(TABLE_NAME, null, null, null, "remoteContact", null, "id DESC"); while (c.moveToNext()) { - String from, to; - from = c.getString(c.getColumnIndex("sender")); - to = c.getString(c.getColumnIndex("receiver")); - - if (from.equals("") && !to.equals("")) { - if (!chatList.contains(to)) { - chatList.add(to); - } - } - else if (!from.equals("") && to.equals("")) - { - if (!chatList.contains(from)) { - chatList.add(from); - } - } + String remoteContact = c.getString(c.getColumnIndex("remoteContact")); + chatList.add(remoteContact); } return chatList; @@ -107,12 +102,21 @@ public class ChatStorage { public void deleteMessage(int id) { db.delete(TABLE_NAME, "id LIKE " + id, null); - Log.d("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); + } + + public int getUnreadMessageCount() { + return db.query(TABLE_NAME, null, "read LIKE " + NOT_READ, null, null, null, null).getCount(); } class ChatHelper extends SQLiteOpenHelper { - private static final int DATABASE_VERSION = 1; + private static final int DATABASE_VERSION = 2; private static final String DATABASE_NAME = "linphone-android"; ChatHelper(Context context) { @@ -121,12 +125,13 @@ public class ChatStorage { @Override public void onCreate(SQLiteDatabase db) { - db.execSQL("CREATE TABLE " + TABLE_NAME + " (id INTEGER PRIMARY KEY AUTOINCREMENT, sender TEXT NOT NULL, receiver TEXT NOT NULL, message TEXT NOT NULL, time NUMERIC);"); + db.execSQL("CREATE TABLE " + TABLE_NAME + " (id INTEGER PRIMARY KEY AUTOINCREMENT, localContact TEXT NOT NULL, remoteContact TEXT NOT NULL, direction INTEGER, message TEXT NOT NULL, time NUMERIC, read INTEGER);"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { - + db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME + ";"); + onCreate(db); } } } \ No newline at end of file diff --git a/src/org/linphone/LinphoneActivity.java b/src/org/linphone/LinphoneActivity.java index e3dff2235..60d9405a6 100644 --- a/src/org/linphone/LinphoneActivity.java +++ b/src/org/linphone/LinphoneActivity.java @@ -286,8 +286,9 @@ public class LinphoneActivity extends FragmentActivity implements OnClickListene } LinphoneService.instance().resetMessageNotifCount(); - displayMissedChats(0); + LinphoneService.instance().removeMessageNotification(); changeCurrentFragment(FragmentsAvailable.CHAT, extras); + displayMissedChats(chatStorage.getUnreadMessageCount()); } @Override @@ -427,10 +428,18 @@ public class LinphoneActivity extends FragmentActivity implements OnClickListene if (LinphoneService.isReady()) { LinphoneUtils.findUriPictureOfContactAndSetDisplayName(from, getContentResolver()); LinphoneService.instance().displayMessageNotification(from.asStringUriOnly(), from.getDisplayName(), message); - displayMissedChats(LinphoneService.instance().getMessageNotifCount()); + displayMissedChats(chatStorage.getUnreadMessageCount()); } } + public void updateMissedChatCount() { + if (chatStorage == null) { + chatStorage = new ChatStorage(this); + } + + displayMissedChats(chatStorage.getUnreadMessageCount()); + } + public void onMessageSent(String to, String message) { if (chatStorage == null) { chatStorage = new ChatStorage(this); @@ -583,12 +592,8 @@ public class LinphoneActivity extends FragmentActivity implements OnClickListene return currentFragment; } - public void deleteMessage(int id) { - if (chatStorage == null) { - return; - } - - chatStorage.deleteMessage(id); + public ChatStorage getChatStorage() { + return chatStorage; } @Override @@ -621,6 +626,9 @@ public class LinphoneActivity extends FragmentActivity implements OnClickListene chatStorage.close(); } chatStorage = new ChatStorage(this); + + updateMissedChatCount(); + displayMissedCalls(LinphoneManager.getLc().getMissedCallsCount()); } @Override