Skip to content

Commit

Permalink
Started working on #568: Improve Olympus messages activity to use Cur…
Browse files Browse the repository at this point in the history
…sorAdapter to load messages instead of SimpleAdapter
  • Loading branch information
Antonis committed Apr 21, 2017
1 parent f0f28a9 commit ccc3322
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 10 deletions.
Expand Up @@ -44,12 +44,17 @@ public static abstract class ContactEntry implements BaseColumns {
public static abstract class MessageEntry implements BaseColumns {
public static final String TABLE_NAME = "message";
public static final String COLUMN_NAME_CONTACT_ID = "contact_id";
// job id created when a message was sent, so that we can associate a response to the message with original message. We need this
// to be able to correlate a message delivery status with the message to which it is related with
public static final String COLUMN_NAME_JOB_ID = "job_id";
// message actual text
public static final String COLUMN_NAME_TEXT = "text";
// message type: 'local' or 'remote'
public static final String COLUMN_NAME_TYPE = "type";
// time sent or received
public static final String COLUMN_NAME_TIMESTAMP = "timestamp";
// Was the message properly delivered?
public static final String COLUMN_NAME_DELIVERY_STATUS = "delivery_status";
}

}
Expand Down
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 = 14;
public static final int DATABASE_VERSION = 15;
public static final String DATABASE_NAME = "Olympus.db";

private static final String TAG = "DatabaseHelper";
Expand All @@ -48,9 +48,11 @@ public class DatabaseHelper extends SQLiteOpenHelper {
"CREATE TABLE " + DatabaseContract.MessageEntry.TABLE_NAME + " (" +
DatabaseContract.MessageEntry._ID + " INTEGER PRIMARY KEY," +
DatabaseContract.MessageEntry.COLUMN_NAME_CONTACT_ID + " INTEGER, " +
DatabaseContract.MessageEntry.COLUMN_NAME_JOB_ID + " TEXT, " +
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, " +
"FOREIGN KEY (" + DatabaseContract.MessageEntry.COLUMN_NAME_CONTACT_ID + ") REFERENCES " + DatabaseContract.ContactEntry.TABLE_NAME +
"(" + DatabaseContract.ContactEntry._ID + ") " +
" );";
Expand Down
Expand Up @@ -404,6 +404,31 @@ public void addMessage(String contactName, String messageText, boolean isLocal)
db.insertOrThrow(DatabaseContract.MessageEntry.TABLE_NAME, null, values);
}

public void updateMessageStatus(String jobId, boolean isDelivered) throws SQLException
{
if (databaseHelper == null) {
throw new RuntimeException("Database hasn't been opened.");
}

// Gets the data repository in write mode
SQLiteDatabase db = databaseHelper.getWritableDatabase();

// Create a new map of values, where column names are the keys
ContentValues values = new ContentValues();

values.put(DatabaseContract.MessageEntry.COLUMN_NAME_DELIVERY_STATUS, isDelivered);

// Add the WHERE clause
String selection = DatabaseContract.MessageEntry.COLUMN_NAME_JOB_ID + " LIKE ?";
String[] selectionArgs = { jobId };

int count = db.update(
DatabaseContract.MessageEntry.TABLE_NAME,
values,
selection,
selectionArgs);
}

// Helpers for adapters
private HashMap<String, String> createContactEntry(String name, String uri)
{
Expand Down
Expand Up @@ -498,7 +498,7 @@ private void handleExternalCall()
}
}

public void onMessageSent(RCDevice device, int statusCode, String statusText)
public void onMessageSent(RCDevice device, int statusCode, String statusText, String jobId)
{

}
Expand Down
Expand Up @@ -33,6 +33,8 @@
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
Expand All @@ -41,7 +43,10 @@
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import org.restcomm.android.sdk.RCClient;
Expand All @@ -59,6 +64,8 @@ public class MessageActivity extends AppCompatActivity
private RCDevice device;
boolean serviceBound = false;
HashMap<String, Object> params = new HashMap<String, Object>();
// keep around for each jobId that creates a message the index it gets inside the ListView
HashMap<String, Integer> indexes = new HashMap<String, Integer>();
private static final String TAG = "MessageActivity";
private AlertDialog alertDialog;
private String currentPeer;
Expand Down Expand Up @@ -233,7 +240,8 @@ private void handleMessage(Intent intent)
if (DatabaseManager.getInstance().addContactIfNeded(username)) {
Toast.makeText(getApplicationContext(), "Adding '" + shortname + "\' to contacts as it doesn't exist", Toast.LENGTH_LONG).show();
}
DatabaseManager.getInstance().addMessage(shortname, message, false);
//DatabaseManager.getInstance().addMessage(shortname, message, false);
listFragment.addRemoteMessage(message, shortname);
return;
}

Expand All @@ -256,13 +264,14 @@ public void onClick(View view)
HashMap<String, String> sendParams = new HashMap<String, String>();
String connectionPeer = (String) params.get(RCConnection.ParameterKeys.CONNECTION_PEER);
sendParams.put(RCConnection.ParameterKeys.CONNECTION_PEER, connectionPeer);
if (device.sendMessage(txtMessage.getText().toString(), sendParams)) {
RCDevice.MessageStatus messageStatus = device.sendMessage(txtMessage.getText().toString(), sendParams);
if (messageStatus.status) {
// also output the message in the wall
listFragment.addLocalMessage(txtMessage.getText().toString(), connectionPeer.replaceAll("^sip:", "").replaceAll("@.*$", ""));
int index = listFragment.addLocalMessage(txtMessage.getText().toString(), connectionPeer.replaceAll("^sip:", "").replaceAll("@.*$", ""));
indexes.put(messageStatus.jobId, index);
txtMessage.setText("");
//txtWall.append("Me: " + txtMessage.getText().toString() + "\n\n");
}
else {
} else {
showOkAlert("RCDevice Error", "No Wifi connectivity");
}
}
Expand Down Expand Up @@ -308,12 +317,26 @@ public void onConnectivityUpdate(RCDevice device, RCConnectivityStatus connectiv
handleConnectivityUpdate(connectivityStatus, null);
}

public void onMessageSent(RCDevice device, int statusCode, String statusText)
public void onMessageSent(RCDevice device, int statusCode, String statusText, String jobId)
{
Log.i(TAG, "onMessageSent(): statusCode: " + statusCode + ", statusText: " + statusText);

int index = indexes.get(jobId);
ListView listView = listFragment.getFragmentListView();
View parent = listView.getChildAt(index - 1);
TextView statusTextView = (TextView)parent.findViewById(R.id.message_status);

if (statusCode != RCClient.ErrorCodes.SUCCESS.ordinal()) {
showOkAlert("RCDevice Error", statusText);
//listView.getAdapter().getItem(index);
statusTextView.setText("Failed");
statusTextView.setTextColor(ContextCompat.getColor(this, R.color.colorError));
}
else {
statusTextView.setText("Delivered");
statusTextView.setTextColor(ContextCompat.getColor(this, R.color.colorTextSecondary));
}

indexes.remove(jobId);
}

public void onReleased(RCDevice device, int statusCode, String statusText)
Expand Down
Expand Up @@ -202,15 +202,17 @@ private void setActivatedPosition(int position)
}

// Called by Activity when when new message is sent
public void addLocalMessage(String message, String username)
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);
int countBeforeAddition = listViewAdapter.getCount();
DatabaseManager.getInstance().addMessage(username, message, true);
this.listViewAdapter.notifyDataSetChanged();
getListView().setSelection(listViewAdapter.getCount() - 1);
return countBeforeAddition;
}

// Called by Activity when when new message is sent
Expand All @@ -225,6 +227,11 @@ public void addRemoteMessage(String message, String username)
getListView().setSelection(listViewAdapter.getCount() - 1);
}

public ListView getFragmentListView()
{
return getListView();
}

// Helper methods
private void showOkAlert(final String title, final String detail)
{
Expand Down
Expand Up @@ -20,6 +20,12 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<TextView android:id="@+id/message_status"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:textStyle="normal|italic"
/>
</LinearLayout>

<!--
Expand Down

0 comments on commit ccc3322

Please sign in to comment.