Skip to content

Commit

Permalink
Working on #568: Improve Olympus messages activity to use CursorAdapt…
Browse files Browse the repository at this point in the history
…er to load messages instead of SimpleAdapter. Working on #567: #567: Convey delivery status in the UI of the messages
  • Loading branch information
Antonis committed May 4, 2017
1 parent a2c6fbb commit 579fe62
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 20 deletions.
Expand Up @@ -30,7 +30,7 @@

public class DatabaseHelper extends SQLiteOpenHelper {
// If you change the database schema, you must increment the database version.
public static final int DATABASE_VERSION = 15;
public static final int DATABASE_VERSION = 16;
public static final String DATABASE_NAME = "Olympus.db";

private static final String TAG = "DatabaseHelper";
Expand All @@ -52,7 +52,7 @@ public class DatabaseHelper extends SQLiteOpenHelper {
DatabaseContract.MessageEntry.COLUMN_NAME_TEXT + " TEXT NOT NULL, " +
DatabaseContract.MessageEntry.COLUMN_NAME_TYPE + " TEXT NOT NULL, " +
DatabaseContract.MessageEntry.COLUMN_NAME_TIMESTAMP + " DATETIME DEFAULT CURRENT_TIMESTAMP, " +
DatabaseContract.MessageEntry.COLUMN_NAME_DELIVERY_STATUS + " INTEGER, " +
DatabaseContract.MessageEntry.COLUMN_NAME_DELIVERY_STATUS + " INTEGER DEFAULT 0, " +
"FOREIGN KEY (" + DatabaseContract.MessageEntry.COLUMN_NAME_CONTACT_ID + ") REFERENCES " + DatabaseContract.ContactEntry.TABLE_NAME +
"(" + DatabaseContract.ContactEntry._ID + ") " +
" );";
Expand Down
Expand Up @@ -340,7 +340,8 @@ public int removeContact(String name, String uri)

// ---- Message table
// Retrieve all messages for a contact ordered by timestamp
ArrayList<Map<String, String>> retrieveMessages(String contactName)
//ArrayList<Map<String, String>> retrieveMessages(String contactName)
Cursor retrieveMessages(String contactName)
{
if (databaseHelper == null) {
throw new RuntimeException("Database hasn't been opened yet, please call open()");
Expand All @@ -361,6 +362,7 @@ ArrayList<Map<String, String>> retrieveMessages(String contactName)
Log.i(TAG, "Query String: " + sqlQuery);
Cursor cursor = db.rawQuery(sqlQuery, selectionArgs);

/*
ArrayList<Map<String, String>> messageList = new ArrayList<Map<String, String>>();
// moveToFirst() fails if cursor is empty
Expand All @@ -373,9 +375,12 @@ ArrayList<Map<String, String>> retrieveMessages(String contactName)
cursor.getString(cursor.getColumnIndexOrThrow(DatabaseContract.MessageEntry.COLUMN_NAME_TEXT))));
} while (cursor.moveToNext());
}
cursor.close();
*/

return cursor;

return messageList;
//cursor.close();
//return messageList;
}

public void addMessage(String contactName, String messageText, boolean isLocal) throws SQLException
Expand Down
Expand Up @@ -26,11 +26,19 @@
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v4.app.ListFragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import org.restcomm.android.sdk.RCDevice;

Expand All @@ -40,7 +48,7 @@


public class MessageFragment extends ListFragment {
private SimpleAdapter listViewAdapter;
private SimpleCursorAdapter listViewAdapter;
private ArrayList<Map<String, String>> messageList;
public static final String MESSAGE_CONTACT_KEY = "username";
public static final String MESSAGE_TEXT_KEY = "message-text";
Expand Down Expand Up @@ -101,15 +109,41 @@ public void onCreate(Bundle savedInstanceState)
String contactName = args.getString("contact-name");
*/
DatabaseManager.getInstance().open(getActivity().getApplicationContext());
messageList = DatabaseManager.getInstance().retrieveMessages(contactName);
Cursor cursor = DatabaseManager.getInstance().retrieveMessages(contactName);

//messageList = new ArrayList<Map<String, String>>();

String[] from = {MESSAGE_CONTACT_KEY, MESSAGE_TEXT_KEY};
int[] to = {R.id.message_username, R.id.message_text};
String[] fromColumns = { DatabaseContract.ContactEntry.COLUMN_NAME_NAME, DatabaseContract.MessageEntry.COLUMN_NAME_TEXT,
DatabaseContract.MessageEntry.COLUMN_NAME_DELIVERY_STATUS };
int[] toViews = { R.id.message_username, R.id.message_text,
R.id.message_status };

listViewAdapter = new SimpleCursorAdapter(getActivity().getApplicationContext(), R.layout.message_row_layout, cursor,
fromColumns, toViews, 0);

// Use a binder for the delivery status column which doesn't map 1-on-1 with the view: status in the db is an integer, while in the view it's a string
listViewAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
@Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex)
{
if (cursor.getColumnName(columnIndex).equals(DatabaseContract.MessageEntry.COLUMN_NAME_DELIVERY_STATUS)) {
int deliveryStatus = cursor.getInt(columnIndex);
TextView deliveryStatusView = (TextView)view;
if (deliveryStatus == 0) {
deliveryStatusView.setText("Failed");
deliveryStatusView.setTextColor(ContextCompat.getColor(getActivity(), R.color.colorError));
}
else {
deliveryStatusView.setText("Success");
deliveryStatusView.setTextColor(ContextCompat.getColor(getActivity(), R.color.colorTextSecondary));
}
return true;
}

return false;
}
});

listViewAdapter = new SimpleAdapter(getActivity().getApplicationContext(), messageList,
R.layout.message_row_layout, from, to);
setListAdapter(listViewAdapter);
}

Expand Down Expand Up @@ -204,12 +238,15 @@ private void setActivatedPosition(int position)
// Called by Activity when when new message is sent
public int addLocalMessage(String message, String username)
{
HashMap<String, String> item = new HashMap<String, String>();
item.put(MESSAGE_CONTACT_KEY, "Me");
item.put(MESSAGE_TEXT_KEY, message);
messageList.add(item);
//HashMap<String, String> item = new HashMap<String, String>();
//item.put(MESSAGE_CONTACT_KEY, "Me");
//item.put(MESSAGE_TEXT_KEY, message);
//messageList.add(item);
int countBeforeAddition = listViewAdapter.getCount();
DatabaseManager.getInstance().addMessage(username, message, true);
Cursor cursor = DatabaseManager.getInstance().retrieveMessages(username);
// update adapter cursor to use the new one with updated rows
this.listViewAdapter.changeCursor(cursor);
this.listViewAdapter.notifyDataSetChanged();
getListView().setSelection(listViewAdapter.getCount() - 1);
return countBeforeAddition;
Expand All @@ -218,11 +255,14 @@ public int addLocalMessage(String message, String username)
// Called by Activity when when new message is sent
public void addRemoteMessage(String message, String username)
{
HashMap<String, String> item = new HashMap<String, String>();
item.put(MESSAGE_CONTACT_KEY, username);
item.put(MESSAGE_TEXT_KEY, message);
messageList.add(item);
//HashMap<String, String> item = new HashMap<String, String>();
//item.put(MESSAGE_CONTACT_KEY, username);
//item.put(MESSAGE_TEXT_KEY, message);
//messageList.add(item);
DatabaseManager.getInstance().addMessage(username, message, false);
Cursor cursor = DatabaseManager.getInstance().retrieveMessages(username);
// update adapter cursor to use the new one with updated rows
this.listViewAdapter.changeCursor(cursor);
this.listViewAdapter.notifyDataSetChanged();
getListView().setSelection(listViewAdapter.getCount() - 1);
}
Expand Down
Expand Up @@ -23,7 +23,7 @@
<TextView android:id="@+id/message_status"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:gravity="end"
android:textStyle="normal|italic"
/>
</LinearLayout>
Expand Down

0 comments on commit 579fe62

Please sign in to comment.