Android Open Source - roodroid Conversations Data Source






From Project

Back to project page roodroid.

License

The source code is released under:

Copyright (c) 2011, Jonathan Perichon & Lucas Gerbeaux Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"...

If you think the Android project roodroid listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package fr.utbm.roodroid.client;
/*from   www.  jav a 2s .  c  o m*/
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import fr.utbm.roodroid.Conversation;
import fr.utbm.roodroid.Message;
import fr.utbm.roodroid.TextMessage;
import fr.utbm.roodroid.Message.MessageStatus;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

/**
 * ConversationsDataSource
 * Manage the data source for Conversations.
 * 
 * It has public methods that interact directly with the data source 
 * (eg: to add a new conversation).
 * 
 * @author Jonathan Perichon <jonathan.perichon@gmail.com>
 * @author Lucas Gerbeaux <lucas.gerbeaux@gmail.com>
 *
 */
public class ConversationsDataSource {

  private SQLiteDatabase database;
  private ConversationsHelper dbHelper;
  private String[] columnsConversations = { 
      ConversationsHelper.COL_CONVID,
      ConversationsHelper.COL_CONVCONTACTPHONENUMBER,
      ConversationsHelper.COL_CONVCONTACTNAME
  };
  
  private String[] columnsMessages = {
      ConversationsHelper.COL_MSGIDCONV,
      ConversationsHelper.COL_MSGSTATUS,
      ConversationsHelper.COL_MSGDATE,
      ConversationsHelper.COL_MSGCONTENT,
      ConversationsHelper.COL_MSGTYPE
  };

  public ConversationsDataSource() {
    dbHelper = new ConversationsHelper();
  }

  private void open() throws SQLException {
    database = dbHelper.getWritableDatabase();
  }

  private void close() {
    dbHelper.close();
  }

  public boolean addConversation(Conversation convo) {
    open();
    Cursor cursor = database.query(ConversationsHelper.TAB_CONV, new String[] {ConversationsHelper.COL_CONVID}, ConversationsHelper.COL_CONVCONTACTPHONENUMBER + "= ?", new String[] { convo.getContactPhoneNumber() }, null, null, null);
    cursor.moveToFirst();
    long convId;
    boolean isNew; 
    if (isNew = cursor.isAfterLast()) {
      ContentValues values = new ContentValues();
      values.put(ConversationsHelper.COL_CONVCONTACTPHONENUMBER, convo.getContactPhoneNumber());
      values.put(ConversationsHelper.COL_CONVCONTACTNAME, convo.getContactName());
      convId = database.insert(ConversationsHelper.TAB_CONV, null, values);
    } else {
      convId = cursor.getLong(0);
    }
    cursor.close();
    
    for (Message m : convo.getMessages()) {
      ContentValues v = new ContentValues();
      v.put(ConversationsHelper.COL_MSGIDCONV, convId);
      v.put(ConversationsHelper.COL_MSGDATE, m.getDate().getTime());
      v.put(ConversationsHelper.COL_MSGSTATUS, m.getMessageStatus().ordinal());
      v.put(ConversationsHelper.COL_MSGTYPE, m.getMessageType().ordinal());
      v.put(ConversationsHelper.COL_MSGCONTENT, m.getTextContent());
      
      database.insert(ConversationsHelper.TAB_MSG, null, v);
    }
    close();
    return isNew;
  }
  
  public void deleteConversation(String contactPhoneNumber) {
    open();
    database.execSQL("PRAGMA foreign_keys=ON;");
    database.delete(ConversationsHelper.TAB_CONV, ConversationsHelper.COL_CONVCONTACTPHONENUMBER + " = ?", new String[] { contactPhoneNumber });
    close();
  }
  
  public void deleteMessage(String contactPhoneNumber, Message msg) {
    open();
    Cursor cursor = database.query(ConversationsHelper.TAB_CONV, new String[] {ConversationsHelper.COL_CONVID}, ConversationsHelper.COL_CONVCONTACTPHONENUMBER + "= ?", new String[] { contactPhoneNumber }, null, null, null);
    cursor.moveToFirst();
    if (!cursor.isAfterLast()) {
      long convId = cursor.getLong(0);
      database.delete(ConversationsHelper.TAB_MSG, ConversationsHelper.COL_MSGIDCONV + " = ? AND " + ConversationsHelper.COL_MSGCONTENT + " = ? AND " + ConversationsHelper.COL_MSGDATE + " = ?", new String[] { convId+"", msg.getTextContent(), msg.getDate().getTime()+"" });
    }
    cursor.close();
    close();
  }

  public HashSet<Conversation> getAllConversations() {
    open();
    HashSet<Conversation> conversations = new HashSet<Conversation>();
    Cursor cursor = database.query(ConversationsHelper.TAB_CONV, columnsConversations, null, null, null, null, null);

    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
      conversations.add(cursorToConversation(cursor));
      cursor.moveToNext();
    }
    cursor.close();
    close();
    return conversations;
  }
  
  private Conversation cursorToConversation(Cursor cursor) {
    long id = cursor.getLong(0);
    String contactPhoneNumber = cursor.getString(1);
    String contactName = cursor.getString(2);
    
    List<Message> messages = new ArrayList<Message>();
    Cursor cursorMessages = database.query(ConversationsHelper.TAB_MSG, columnsMessages, ConversationsHelper.COL_MSGIDCONV + "=" + id, null, null, null, null);
    cursorMessages.moveToFirst();
    while (!cursorMessages.isAfterLast()) {
      Message m = cursorToMessage(cursorMessages);
      messages.add(m);
      cursorMessages.moveToNext();
    }
    
    return new Conversation(contactPhoneNumber, contactName, messages);
  }
  
  private Message cursorToMessage(Cursor cursor) {
    Date date = new Date(cursor.getLong(2));
    String content = cursor.getString(3);
    MessageStatus messageStatus = MessageStatus.RECEIVED;
    return new TextMessage(date, messageStatus, content);
  }
}




Java Source Code List

fr.utbm.roodroid.ApplicationManager.java
fr.utbm.roodroid.ConnectionBluetooth.java
fr.utbm.roodroid.ConnectionWifi.java
fr.utbm.roodroid.Connection.java
fr.utbm.roodroid.Conversation.java
fr.utbm.roodroid.Message.java
fr.utbm.roodroid.PacketClient.java
fr.utbm.roodroid.Packet.java
fr.utbm.roodroid.TCPCommandType.java
fr.utbm.roodroid.TextMessage.java
fr.utbm.roodroid.activity.AuthorizedUsernamesAdapter.java
fr.utbm.roodroid.activity.BluetoothDiscovery.java
fr.utbm.roodroid.activity.ClientBluetoothSettings.java
fr.utbm.roodroid.activity.ClientWifiSettings.java
fr.utbm.roodroid.activity.ConversationsAdapter.java
fr.utbm.roodroid.activity.ConversationsList.java
fr.utbm.roodroid.activity.LogPage.java
fr.utbm.roodroid.activity.MessagesAdapter.java
fr.utbm.roodroid.activity.MessagesList.java
fr.utbm.roodroid.activity.ProfileTypeChooser.java
fr.utbm.roodroid.activity.ServerAdvancedSettings.java
fr.utbm.roodroid.activity.ServerBluetoothMain.java
fr.utbm.roodroid.activity.ServerBluetoothSettings.java
fr.utbm.roodroid.activity.ServerWifiMain.java
fr.utbm.roodroid.activity.ServerWifiSettings.java
fr.utbm.roodroid.client.ClientBluetooth.java
fr.utbm.roodroid.client.ClientWifi.java
fr.utbm.roodroid.client.Client.java
fr.utbm.roodroid.client.ConversationsDataSource.java
fr.utbm.roodroid.client.ConversationsHelper.java
fr.utbm.roodroid.server.AuthByID.java
fr.utbm.roodroid.server.AuthByPassword.java
fr.utbm.roodroid.server.AuthMethod.java
fr.utbm.roodroid.server.AuthNone.java
fr.utbm.roodroid.server.ServerBluetooth.java
fr.utbm.roodroid.server.ServerWifi.java
fr.utbm.roodroid.server.Server.java