2012-07-19 21:22:03 +00:00
|
|
|
package org.thoughtcrime.securesms;
|
|
|
|
|
2012-08-04 00:34:09 +00:00
|
|
|
import android.app.Activity;
|
2012-07-19 21:22:03 +00:00
|
|
|
import android.app.AlertDialog;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.DialogInterface;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.database.Cursor;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.os.Handler;
|
|
|
|
import android.support.v4.app.LoaderManager;
|
|
|
|
import android.support.v4.content.Loader;
|
|
|
|
import android.text.ClipboardManager;
|
|
|
|
import android.view.ContextMenu;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.CursorAdapter;
|
|
|
|
|
|
|
|
import com.actionbarsherlock.app.SherlockListFragment;
|
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
|
|
|
import org.thoughtcrime.securesms.database.MessageRecord;
|
|
|
|
import org.thoughtcrime.securesms.database.loaders.ConversationLoader;
|
|
|
|
import org.thoughtcrime.securesms.recipients.Recipients;
|
|
|
|
|
|
|
|
import java.sql.Date;
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
|
|
|
|
public class ConversationFragment extends SherlockListFragment
|
|
|
|
implements LoaderManager.LoaderCallbacks<Cursor>
|
|
|
|
{
|
|
|
|
|
2012-08-04 00:34:09 +00:00
|
|
|
private ConversationFragmentListener listener;
|
|
|
|
|
2012-07-19 21:22:03 +00:00
|
|
|
private MasterSecret masterSecret;
|
|
|
|
private Recipients recipients;
|
|
|
|
private long threadId;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
|
|
|
|
return inflater.inflate(R.layout.conversation_fragment, container, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onActivityCreated(Bundle bundle) {
|
|
|
|
super.onActivityCreated(bundle);
|
|
|
|
|
|
|
|
initializeResources();
|
|
|
|
initializeListAdapter();
|
|
|
|
registerForContextMenu(getListView());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreateContextMenu (ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
|
|
|
|
android.view.MenuInflater inflater = this.getSherlockActivity().getMenuInflater();
|
|
|
|
menu.clear();
|
|
|
|
|
|
|
|
inflater.inflate(R.menu.conversation_context, menu);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onContextItemSelected(android.view.MenuItem item) {
|
|
|
|
Cursor cursor = ((CursorAdapter)getListAdapter()).getCursor();
|
|
|
|
ConversationItem conversationItem = (ConversationItem)(((ConversationAdapter)getListAdapter()).newView(getActivity(), cursor, null));
|
|
|
|
MessageRecord messageRecord = conversationItem.getMessageRecord();
|
|
|
|
|
|
|
|
switch(item.getItemId()) {
|
|
|
|
case R.id.menu_context_copy: handleCopyMessage(messageRecord); return true;
|
|
|
|
case R.id.menu_context_delete_message: handleDeleteMessage(messageRecord); return true;
|
|
|
|
case R.id.menu_context_details: handleDisplayDetails(messageRecord); return true;
|
|
|
|
case R.id.menu_context_forward: handleForwardMessage(messageRecord); return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-08-04 00:34:09 +00:00
|
|
|
@Override
|
|
|
|
public void onAttach(Activity activity) {
|
|
|
|
super.onAttach(activity);
|
|
|
|
this.listener = (ConversationFragmentListener)activity;
|
|
|
|
}
|
|
|
|
|
2012-07-31 23:27:58 +00:00
|
|
|
public void reload(Recipients recipients, long threadId) {
|
|
|
|
this.recipients = recipients;
|
|
|
|
this.threadId = threadId;
|
|
|
|
|
|
|
|
initializeListAdapter();
|
|
|
|
}
|
|
|
|
|
2012-07-19 21:22:03 +00:00
|
|
|
private void handleCopyMessage(MessageRecord message) {
|
|
|
|
String body = message.getBody();
|
|
|
|
if (body == null) return;
|
|
|
|
|
|
|
|
ClipboardManager clipboard = (ClipboardManager)getActivity()
|
|
|
|
.getSystemService(Context.CLIPBOARD_SERVICE);
|
|
|
|
clipboard.setText(body);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void handleDeleteMessage(MessageRecord message) {
|
|
|
|
final long messageId = message.getId();
|
|
|
|
final String transport = message.isMms() ? "mms" : "sms";
|
|
|
|
|
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
|
|
|
|
builder.setTitle("Delete Message Confirmation");
|
|
|
|
builder.setIcon(android.R.drawable.ic_dialog_alert);
|
|
|
|
builder.setCancelable(true);
|
|
|
|
builder.setMessage("Are you sure that you want to permanently delete this message?");
|
|
|
|
|
|
|
|
builder.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
if (transport.equals("mms")) {
|
|
|
|
DatabaseFactory.getMmsDatabase(getActivity()).delete(messageId);
|
|
|
|
} else {
|
|
|
|
DatabaseFactory.getSmsDatabase(getActivity()).deleteMessage(messageId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
builder.setNegativeButton(R.string.no, null);
|
|
|
|
builder.show();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void handleDisplayDetails(MessageRecord message) {
|
|
|
|
String sender = message.getRecipients().getPrimaryRecipient().getNumber();
|
|
|
|
String transport = message.isMms() ? "mms" : "sms";
|
|
|
|
long date = message.getDate();
|
|
|
|
|
|
|
|
SimpleDateFormat dateFormatter = new SimpleDateFormat("EEE MMM d, yyyy 'at' hh:mm:ss a zzz");
|
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
|
2012-09-20 02:56:04 +00:00
|
|
|
builder.setTitle(R.string.ConversationFragment_message_details);
|
2012-07-19 21:22:03 +00:00
|
|
|
builder.setIcon(android.R.drawable.ic_dialog_info);
|
|
|
|
builder.setCancelable(false);
|
2012-09-20 02:56:04 +00:00
|
|
|
builder.setMessage(String.format(getSherlockActivity()
|
|
|
|
.getString(R.string.ConversationFragment_sender_s_transport_s_sent_received_s),
|
2012-09-08 03:03:23 +00:00
|
|
|
sender, transport.toUpperCase(),
|
|
|
|
dateFormatter.format(new Date(date))));
|
|
|
|
builder.setPositiveButton(android.R.string.ok, null);
|
2012-07-19 21:22:03 +00:00
|
|
|
builder.show();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void handleForwardMessage(MessageRecord message) {
|
|
|
|
Intent composeIntent = new Intent(getActivity(), ConversationActivity.class);
|
|
|
|
composeIntent.putExtra("forwarded_message", message.getBody());
|
|
|
|
composeIntent.putExtra("master_secret", masterSecret);
|
|
|
|
startActivity(composeIntent);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void initializeResources() {
|
|
|
|
this.masterSecret = (MasterSecret)this.getActivity().getIntent()
|
|
|
|
.getParcelableExtra("master_secret");
|
|
|
|
this.recipients = this.getActivity().getIntent().getParcelableExtra("recipients");
|
|
|
|
this.threadId = this.getActivity().getIntent().getLongExtra("thread_id", -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void initializeListAdapter() {
|
|
|
|
if (this.recipients != null && this.threadId != -1) {
|
2012-07-31 23:27:58 +00:00
|
|
|
this.setListAdapter(new ConversationAdapter(recipients, threadId, getActivity(),
|
|
|
|
masterSecret, new FailedIconClickHandler()));
|
|
|
|
getLoaderManager().initLoader(0, null, this);
|
2012-07-19 21:22:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
|
2012-07-31 23:27:58 +00:00
|
|
|
return new ConversationLoader(getActivity(), threadId);
|
2012-07-19 21:22:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onLoadFinished(Loader<Cursor> arg0, Cursor cursor) {
|
|
|
|
((CursorAdapter)getListAdapter()).changeCursor(cursor);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onLoaderReset(Loader<Cursor> arg0) {
|
|
|
|
((CursorAdapter)getListAdapter()).changeCursor(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
private class FailedIconClickHandler extends Handler {
|
|
|
|
@Override
|
|
|
|
public void handleMessage(android.os.Message message) {
|
2012-08-04 00:34:09 +00:00
|
|
|
if (listener != null) {
|
|
|
|
listener.setComposeText((String)message.obj);
|
|
|
|
}
|
2012-07-19 21:22:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-04 00:34:09 +00:00
|
|
|
public interface ConversationFragmentListener {
|
|
|
|
public void setComposeText(String text);
|
|
|
|
}
|
|
|
|
|
2012-07-19 21:22:03 +00:00
|
|
|
}
|