Messages database changed
This commit is contained in:
parent
b23e7973ed
commit
b67cec5ac1
3 changed files with 57 additions and 45 deletions
|
@ -83,7 +83,6 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneO
|
||||||
message = (EditText) view.findViewById(R.id.message);
|
message = (EditText) view.findViewById(R.id.message);
|
||||||
|
|
||||||
messagesLayout = (RelativeLayout) view.findViewById(R.id.messages);
|
messagesLayout = (RelativeLayout) view.findViewById(R.id.messages);
|
||||||
List<ChatMessage> messagesList = LinphoneActivity.instance().getChatMessages(sipUri);
|
|
||||||
|
|
||||||
messagesScrollView = (ScrollView) view.findViewById(R.id.chatScrollView);
|
messagesScrollView = (ScrollView) view.findViewById(R.id.chatScrollView);
|
||||||
messagesScrollView.post(new Runnable() {
|
messagesScrollView.post(new Runnable() {
|
||||||
|
@ -93,10 +92,7 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneO
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
previousMessageID = -1;
|
invalidate();
|
||||||
for (ChatMessage msg : messagesList) {
|
|
||||||
displayMessage(msg.getId(), msg.getMessage(), msg.getTimestamp(), msg.isIncoming(), messagesLayout);
|
|
||||||
}
|
|
||||||
|
|
||||||
LinphoneCore lc = LinphoneManager.getLcIfManagerNotDestroyedOrNull();
|
LinphoneCore lc = LinphoneManager.getLcIfManagerNotDestroyedOrNull();
|
||||||
if (lc != null)
|
if (lc != null)
|
||||||
|
@ -110,9 +106,12 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneO
|
||||||
List<ChatMessage> messagesList = LinphoneActivity.instance().getChatMessages(sipUri);
|
List<ChatMessage> messagesList = LinphoneActivity.instance().getChatMessages(sipUri);
|
||||||
|
|
||||||
previousMessageID = -1;
|
previousMessageID = -1;
|
||||||
|
ChatStorage chatStorage = LinphoneActivity.instance().getChatStorage();
|
||||||
for (ChatMessage msg : messagesList) {
|
for (ChatMessage msg : messagesList) {
|
||||||
displayMessage(msg.getId(), msg.getMessage(), msg.getTimestamp(), msg.isIncoming(), messagesLayout);
|
displayMessage(msg.getId(), msg.getMessage(), msg.getTimestamp(), msg.isIncoming(), messagesLayout);
|
||||||
|
chatStorage.markMessageAsRead(msg.getId());
|
||||||
}
|
}
|
||||||
|
LinphoneActivity.instance().updateMissedChatCount();
|
||||||
|
|
||||||
scrollToEnd();
|
scrollToEnd();
|
||||||
}
|
}
|
||||||
|
@ -137,7 +136,7 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneO
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onContextItemSelected(MenuItem item) {
|
public boolean onContextItemSelected(MenuItem item) {
|
||||||
LinphoneActivity.instance().deleteMessage(item.getItemId());
|
LinphoneActivity.instance().getChatStorage().deleteMessage(item.getItemId());
|
||||||
invalidate();
|
invalidate();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,8 +20,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.linphone.core.Log;
|
|
||||||
|
|
||||||
import android.content.ContentValues;
|
import android.content.ContentValues;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.database.Cursor;
|
import android.database.Cursor;
|
||||||
|
@ -32,6 +30,10 @@ import android.database.sqlite.SQLiteOpenHelper;
|
||||||
* @author Sylvain Berfini
|
* @author Sylvain Berfini
|
||||||
*/
|
*/
|
||||||
public class ChatStorage {
|
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 Context context;
|
||||||
private SQLiteDatabase db;
|
private SQLiteDatabase db;
|
||||||
private static final String TABLE_NAME = "chat";
|
private static final String TABLE_NAME = "chat";
|
||||||
|
@ -48,9 +50,17 @@ public class ChatStorage {
|
||||||
|
|
||||||
public void saveMessage(String from, String to, String message) {
|
public void saveMessage(String from, String to, String message) {
|
||||||
ContentValues values = new ContentValues();
|
ContentValues values = new ContentValues();
|
||||||
values.put("sender", from);
|
if (from.equals("")) {
|
||||||
values.put("receiver", to);
|
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("message", message);
|
||||||
|
values.put("read", NOT_READ);
|
||||||
values.put("time", System.currentTimeMillis());
|
values.put("time", System.currentTimeMillis());
|
||||||
db.insert(TABLE_NAME, null, values);
|
db.insert(TABLE_NAME, null, values);
|
||||||
}
|
}
|
||||||
|
@ -58,17 +68,16 @@ 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, "receiver LIKE \"" + correspondent +
|
Cursor c = db.query(TABLE_NAME, null, "remoteContact LIKE \"" + correspondent + "\"", null, null, null, "id ASC");
|
||||||
"\" OR sender LIKE \"" + correspondent + "\"", null, null, null, "id ASC");
|
|
||||||
|
|
||||||
while (c.moveToNext()) {
|
while (c.moveToNext()) {
|
||||||
String to, message, timestamp;
|
String message, timestamp;
|
||||||
int id = c.getInt(c.getColumnIndex("id"));
|
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"));
|
message = c.getString(c.getColumnIndex("message"));
|
||||||
timestamp = c.getString(c.getColumnIndex("time"));
|
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);
|
chatMessages.add(chatMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -76,30 +85,16 @@ public class ChatStorage {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void removeDiscussion(String correspondent) {
|
public void removeDiscussion(String correspondent) {
|
||||||
db.delete(TABLE_NAME, "sender LIKE \"" + correspondent + "\"", null);
|
db.delete(TABLE_NAME, "remoteContact LIKE \"" + correspondent + "\"", null);
|
||||||
db.delete(TABLE_NAME, "receiver 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, null, null, "id DESC");
|
Cursor c = db.query(TABLE_NAME, null, null, null, "remoteContact", null, "id DESC");
|
||||||
while (c.moveToNext()) {
|
while (c.moveToNext()) {
|
||||||
String from, to;
|
String remoteContact = c.getString(c.getColumnIndex("remoteContact"));
|
||||||
from = c.getString(c.getColumnIndex("sender"));
|
chatList.add(remoteContact);
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return chatList;
|
return chatList;
|
||||||
|
@ -107,12 +102,21 @@ public class ChatStorage {
|
||||||
|
|
||||||
public void deleteMessage(int id) {
|
public void deleteMessage(int id) {
|
||||||
db.delete(TABLE_NAME, "id LIKE " + id, null);
|
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 {
|
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";
|
private static final String DATABASE_NAME = "linphone-android";
|
||||||
|
|
||||||
ChatHelper(Context context) {
|
ChatHelper(Context context) {
|
||||||
|
@ -121,12 +125,13 @@ public class ChatStorage {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(SQLiteDatabase db) {
|
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
|
@Override
|
||||||
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
||||||
|
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME + ";");
|
||||||
|
onCreate(db);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -286,8 +286,9 @@ public class LinphoneActivity extends FragmentActivity implements OnClickListene
|
||||||
}
|
}
|
||||||
|
|
||||||
LinphoneService.instance().resetMessageNotifCount();
|
LinphoneService.instance().resetMessageNotifCount();
|
||||||
displayMissedChats(0);
|
LinphoneService.instance().removeMessageNotification();
|
||||||
changeCurrentFragment(FragmentsAvailable.CHAT, extras);
|
changeCurrentFragment(FragmentsAvailable.CHAT, extras);
|
||||||
|
displayMissedChats(chatStorage.getUnreadMessageCount());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -427,10 +428,18 @@ public class LinphoneActivity extends FragmentActivity implements OnClickListene
|
||||||
if (LinphoneService.isReady()) {
|
if (LinphoneService.isReady()) {
|
||||||
LinphoneUtils.findUriPictureOfContactAndSetDisplayName(from, getContentResolver());
|
LinphoneUtils.findUriPictureOfContactAndSetDisplayName(from, getContentResolver());
|
||||||
LinphoneService.instance().displayMessageNotification(from.asStringUriOnly(), from.getDisplayName(), message);
|
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) {
|
public void onMessageSent(String to, String message) {
|
||||||
if (chatStorage == null) {
|
if (chatStorage == null) {
|
||||||
chatStorage = new ChatStorage(this);
|
chatStorage = new ChatStorage(this);
|
||||||
|
@ -583,12 +592,8 @@ public class LinphoneActivity extends FragmentActivity implements OnClickListene
|
||||||
return currentFragment;
|
return currentFragment;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void deleteMessage(int id) {
|
public ChatStorage getChatStorage() {
|
||||||
if (chatStorage == null) {
|
return chatStorage;
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
chatStorage.deleteMessage(id);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -621,6 +626,9 @@ public class LinphoneActivity extends FragmentActivity implements OnClickListene
|
||||||
chatStorage.close();
|
chatStorage.close();
|
||||||
}
|
}
|
||||||
chatStorage = new ChatStorage(this);
|
chatStorage = new ChatStorage(this);
|
||||||
|
|
||||||
|
updateMissedChatCount();
|
||||||
|
displayMissedCalls(LinphoneManager.getLc().getMissedCallsCount());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Reference in a new issue