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;
|
2014-06-12 01:03:01 +00:00
|
|
|
import android.app.ProgressDialog;
|
2012-07-19 21:22:03 +00:00
|
|
|
import android.content.Context;
|
|
|
|
import android.content.DialogInterface;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.database.Cursor;
|
2014-06-12 01:03:01 +00:00
|
|
|
import android.media.MediaScannerConnection;
|
|
|
|
import android.os.AsyncTask;
|
2012-07-19 21:22:03 +00:00
|
|
|
import android.os.Bundle;
|
2014-06-12 01:03:01 +00:00
|
|
|
import android.os.Environment;
|
2012-07-19 21:22:03 +00:00
|
|
|
import android.os.Handler;
|
|
|
|
import android.support.v4.app.LoaderManager;
|
|
|
|
import android.support.v4.content.Loader;
|
|
|
|
import android.text.ClipboardManager;
|
2014-06-12 01:03:01 +00:00
|
|
|
import android.util.Log;
|
2012-07-19 21:22:03 +00:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
2014-04-03 23:20:23 +00:00
|
|
|
import android.support.v4.widget.CursorAdapter;
|
2014-06-12 01:03:01 +00:00
|
|
|
import android.webkit.MimeTypeMap;
|
2014-06-12 17:22:57 +00:00
|
|
|
import android.widget.AdapterView;
|
2014-03-26 22:11:56 +00:00
|
|
|
import android.widget.ListView;
|
2014-06-12 01:03:01 +00:00
|
|
|
import android.widget.Toast;
|
2012-07-19 21:22:03 +00:00
|
|
|
|
|
|
|
import com.actionbarsherlock.app.SherlockListFragment;
|
2014-06-12 17:22:57 +00:00
|
|
|
import com.actionbarsherlock.view.ActionMode;
|
|
|
|
import com.actionbarsherlock.view.Menu;
|
|
|
|
import com.actionbarsherlock.view.MenuInflater;
|
|
|
|
import com.actionbarsherlock.view.MenuItem;
|
2012-07-19 21:22:03 +00:00
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
|
|
|
import org.thoughtcrime.securesms.database.loaders.ConversationLoader;
|
2014-06-12 01:03:01 +00:00
|
|
|
import org.thoughtcrime.securesms.database.model.MediaMmsMessageRecord;
|
2012-10-28 23:04:24 +00:00
|
|
|
import org.thoughtcrime.securesms.database.model.MessageRecord;
|
2014-06-12 01:03:01 +00:00
|
|
|
import org.thoughtcrime.securesms.mms.Slide;
|
2014-03-07 20:39:16 +00:00
|
|
|
import org.thoughtcrime.securesms.recipients.RecipientFactory;
|
2012-07-19 21:22:03 +00:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipients;
|
2013-10-13 10:53:41 +00:00
|
|
|
import org.thoughtcrime.securesms.sms.MessageSender;
|
2014-03-01 00:32:00 +00:00
|
|
|
import org.thoughtcrime.securesms.util.Dialogs;
|
2014-03-07 20:39:16 +00:00
|
|
|
import org.thoughtcrime.securesms.util.DirectoryHelper;
|
|
|
|
import org.whispersystems.textsecure.crypto.MasterSecret;
|
2014-06-12 01:03:01 +00:00
|
|
|
import org.whispersystems.textsecure.util.Util;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
import java.lang.ref.WeakReference;
|
2012-07-19 21:22:03 +00:00
|
|
|
import java.sql.Date;
|
|
|
|
import java.text.SimpleDateFormat;
|
2014-06-12 01:03:01 +00:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.concurrent.ExecutionException;
|
2012-07-19 21:22:03 +00:00
|
|
|
|
|
|
|
public class ConversationFragment extends SherlockListFragment
|
|
|
|
implements LoaderManager.LoaderCallbacks<Cursor>
|
|
|
|
{
|
2014-06-12 01:03:01 +00:00
|
|
|
private static final String TAG = ConversationFragment.class.getSimpleName();
|
2012-07-19 21:22:03 +00:00
|
|
|
|
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;
|
2014-06-12 17:22:57 +00:00
|
|
|
private ActionMode actionMode;
|
2012-07-19 21:22:03 +00:00
|
|
|
|
|
|
|
@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();
|
2014-06-12 17:22:57 +00:00
|
|
|
initializeContextualActionBar();
|
2012-07-19 21:22:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-06-12 17:22:57 +00:00
|
|
|
public void onAttach(Activity activity) {
|
|
|
|
super.onAttach(activity);
|
|
|
|
this.listener = (ConversationFragmentListener)activity;
|
|
|
|
}
|
2012-07-19 21:22:03 +00:00
|
|
|
|
2014-06-12 17:22:57 +00:00
|
|
|
private void initializeResources() {
|
|
|
|
String recipientIds = this.getActivity().getIntent().getStringExtra("recipients");
|
2013-10-13 10:53:41 +00:00
|
|
|
|
2014-06-12 17:22:57 +00:00
|
|
|
this.masterSecret = this.getActivity().getIntent().getParcelableExtra("master_secret");
|
|
|
|
this.recipients = RecipientFactory.getRecipientsForIds(getActivity(), recipientIds, true);
|
|
|
|
this.threadId = this.getActivity().getIntent().getLongExtra("thread_id", -1);
|
|
|
|
}
|
2014-06-12 01:03:01 +00:00
|
|
|
|
2014-06-12 17:22:57 +00:00
|
|
|
private void initializeListAdapter() {
|
|
|
|
if (this.recipients != null && this.threadId != -1) {
|
|
|
|
this.setListAdapter(new ConversationAdapter(getActivity(), masterSecret,
|
|
|
|
new FailedIconClickHandler(),
|
|
|
|
(!this.recipients.isSingleRecipient()) || this.recipients.isGroupRecipient(),
|
|
|
|
DirectoryHelper.isPushDestination(getActivity(), this.recipients)));
|
|
|
|
getListView().setRecyclerListener((ConversationAdapter)getListAdapter());
|
|
|
|
getLoaderManager().initLoader(0, null, this);
|
2013-10-13 10:53:41 +00:00
|
|
|
}
|
2014-06-12 17:22:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void initializeContextualActionBar() {
|
|
|
|
getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
|
|
|
|
@Override
|
|
|
|
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
|
|
|
|
if (actionMode != null) {
|
|
|
|
view.setSelected(true);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
actionMode = getSherlockActivity().startActionMode(actionModeCallback);
|
|
|
|
view.setSelected(true);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
|
|
|
|
if (actionMode != null) {
|
|
|
|
view.setSelected(true);
|
|
|
|
setCorrectMenuVisibility(getMessageRecord(), actionMode.getMenu());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private void setCorrectMenuVisibility(MessageRecord messageRecord, Menu menu) {
|
|
|
|
MenuItem resend = menu.findItem(R.id.menu_context_resend);
|
|
|
|
MenuItem saveAttachment = menu.findItem(R.id.menu_context_save_attachment);
|
|
|
|
|
|
|
|
if (messageRecord.isFailed()) resend.setVisible(true);
|
|
|
|
else resend.setVisible(false);
|
2014-06-12 01:03:01 +00:00
|
|
|
|
|
|
|
if (messageRecord.isMms() && !messageRecord.isMmsNotification()) {
|
|
|
|
try {
|
|
|
|
if (((MediaMmsMessageRecord)messageRecord).getSlideDeck().get().containsMediaSlide()) {
|
2014-06-12 17:22:57 +00:00
|
|
|
saveAttachment.setVisible(true);
|
|
|
|
} else {
|
|
|
|
saveAttachment.setVisible(false);
|
2014-06-12 01:03:01 +00:00
|
|
|
}
|
|
|
|
} catch (InterruptedException ie) {
|
|
|
|
Log.w(TAG, ie);
|
|
|
|
} catch (ExecutionException ee) {
|
|
|
|
Log.w(TAG, ee);
|
|
|
|
}
|
2014-06-12 17:22:57 +00:00
|
|
|
} else {
|
|
|
|
saveAttachment.setVisible(false);
|
2014-06-12 01:03:01 +00:00
|
|
|
}
|
2012-07-19 21:22:03 +00:00
|
|
|
}
|
|
|
|
|
2013-10-13 10:53:41 +00:00
|
|
|
private MessageRecord getMessageRecord() {
|
|
|
|
Cursor cursor = ((CursorAdapter)getListAdapter()).getCursor();
|
|
|
|
ConversationItem conversationItem = (ConversationItem)(((ConversationAdapter)getListAdapter()).newView(getActivity(), cursor, null));
|
|
|
|
return conversationItem.getMessageRecord();
|
|
|
|
}
|
|
|
|
|
2012-07-31 23:27:58 +00:00
|
|
|
public void reload(Recipients recipients, long threadId) {
|
|
|
|
this.recipients = recipients;
|
|
|
|
this.threadId = threadId;
|
|
|
|
|
|
|
|
initializeListAdapter();
|
|
|
|
}
|
|
|
|
|
2014-03-26 22:11:56 +00:00
|
|
|
public void scrollToBottom() {
|
|
|
|
final ListView list = getListView();
|
|
|
|
list.post(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
list.setSelection(getListAdapter().getCount() - 1);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2012-07-19 21:22:03 +00:00
|
|
|
private void handleCopyMessage(MessageRecord message) {
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
String body = message.getDisplayBody().toString();
|
2012-07-19 21:22:03 +00:00
|
|
|
if (body == null) return;
|
|
|
|
|
|
|
|
ClipboardManager clipboard = (ClipboardManager)getActivity()
|
|
|
|
.getSystemService(Context.CLIPBOARD_SERVICE);
|
|
|
|
clipboard.setText(body);
|
|
|
|
}
|
|
|
|
|
2012-10-28 23:04:24 +00:00
|
|
|
private void handleDeleteMessage(final MessageRecord message) {
|
2012-07-19 21:22:03 +00:00
|
|
|
final long messageId = message.getId();
|
|
|
|
|
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
|
2012-09-22 19:53:56 +00:00
|
|
|
builder.setTitle(R.string.ConversationFragment_confirm_message_delete);
|
2014-03-01 00:32:00 +00:00
|
|
|
builder.setIcon(Dialogs.resolveIcon(getActivity(), R.attr.dialog_alert_icon));
|
2012-07-19 21:22:03 +00:00
|
|
|
builder.setCancelable(true);
|
2012-09-22 19:53:56 +00:00
|
|
|
builder.setMessage(R.string.ConversationFragment_are_you_sure_you_want_to_permanently_delete_this_message);
|
2012-07-19 21:22:03 +00:00
|
|
|
|
|
|
|
builder.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
2012-10-28 23:04:24 +00:00
|
|
|
if (message.isMms()) {
|
2012-07-19 21:22:03 +00:00
|
|
|
DatabaseFactory.getMmsDatabase(getActivity()).delete(messageId);
|
|
|
|
} else {
|
|
|
|
DatabaseFactory.getSmsDatabase(getActivity()).deleteMessage(messageId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
builder.setNegativeButton(R.string.no, null);
|
|
|
|
builder.show();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void handleDisplayDetails(MessageRecord message) {
|
2013-01-06 21:13:14 +00:00
|
|
|
long dateReceived = message.getDateReceived();
|
|
|
|
long dateSent = message.getDateSent();
|
2012-07-19 21:22:03 +00:00
|
|
|
|
2014-02-21 07:00:38 +00:00
|
|
|
String transport;
|
|
|
|
|
2014-03-07 20:39:16 +00:00
|
|
|
if (message.isPending()) transport = "pending";
|
|
|
|
else if (message.isPush()) transport = "push";
|
|
|
|
else if (message.isMms()) transport = "mms";
|
|
|
|
else transport = "sms";
|
2014-01-19 02:17:08 +00:00
|
|
|
|
2012-07-19 21:22:03 +00:00
|
|
|
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);
|
2014-03-01 00:32:00 +00:00
|
|
|
builder.setIcon(Dialogs.resolveIcon(getActivity(), R.attr.dialog_info_icon));
|
2014-03-06 04:11:36 +00:00
|
|
|
builder.setCancelable(true);
|
2013-01-06 21:13:14 +00:00
|
|
|
|
|
|
|
if (dateReceived == dateSent || message.isOutgoing()) {
|
|
|
|
builder.setMessage(String.format(getSherlockActivity()
|
2014-03-12 16:57:12 +00:00
|
|
|
.getString(R.string.ConversationFragment_transport_s_sent_received_s),
|
2014-02-27 02:57:17 +00:00
|
|
|
transport.toUpperCase(),
|
2013-01-06 21:13:14 +00:00
|
|
|
dateFormatter.format(new Date(dateSent))));
|
|
|
|
} else {
|
|
|
|
builder.setMessage(String.format(getSherlockActivity()
|
|
|
|
.getString(R.string.ConversationFragment_sender_s_transport_s_sent_s_received_s),
|
2014-02-27 02:57:17 +00:00
|
|
|
message.getIndividualRecipient().getNumber(),
|
|
|
|
transport.toUpperCase(),
|
2013-01-06 21:13:14 +00:00
|
|
|
dateFormatter.format(new Date(dateSent)),
|
|
|
|
dateFormatter.format(new Date(dateReceived))));
|
|
|
|
}
|
|
|
|
|
2012-09-08 03:03:23 +00:00
|
|
|
builder.setPositiveButton(android.R.string.ok, null);
|
2012-07-19 21:22:03 +00:00
|
|
|
builder.show();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void handleForwardMessage(MessageRecord message) {
|
2014-02-28 06:44:02 +00:00
|
|
|
Intent composeIntent = new Intent(getActivity(), ShareActivity.class);
|
|
|
|
composeIntent.putExtra(ConversationActivity.DRAFT_TEXT_EXTRA, message.getDisplayBody().toString());
|
|
|
|
composeIntent.putExtra(ShareActivity.MASTER_SECRET_EXTRA, masterSecret);
|
2012-07-19 21:22:03 +00:00
|
|
|
startActivity(composeIntent);
|
|
|
|
}
|
|
|
|
|
2013-10-13 10:53:41 +00:00
|
|
|
private void handleResendMessage(MessageRecord message) {
|
|
|
|
long messageId = message.getId();
|
|
|
|
final Activity activity = getActivity();
|
|
|
|
MessageSender.resend(activity, messageId, message.isMms());
|
|
|
|
}
|
|
|
|
|
2014-06-12 01:03:01 +00:00
|
|
|
private void handleSaveAttachment(final MessageRecord message) {
|
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
|
|
|
|
builder.setTitle(R.string.ConversationFragment_save_to_sd_card);
|
|
|
|
builder.setIcon(Dialogs.resolveIcon(getActivity(), R.attr.dialog_alert_icon));
|
|
|
|
builder.setCancelable(true);
|
|
|
|
builder.setMessage(R.string.ConversationFragment_this_media_has_been_stored_in_an_encrypted_database_warning);
|
|
|
|
builder.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
SaveAttachmentTask saveTask = new SaveAttachmentTask(getActivity());
|
|
|
|
saveTask.execute((MediaMmsMessageRecord) message);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
builder.setNegativeButton(R.string.no, null);
|
|
|
|
builder.show();
|
|
|
|
}
|
|
|
|
|
2012-07-19 21:22:03 +00:00
|
|
|
@Override
|
|
|
|
public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
|
2013-04-26 01:59:49 +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);
|
|
|
|
}
|
|
|
|
|
2014-06-12 17:22:57 +00:00
|
|
|
private ActionMode.Callback actionModeCallback = new ActionMode.Callback() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
|
|
|
|
MenuInflater inflater = mode.getMenuInflater();
|
|
|
|
inflater.inflate(R.menu.conversation_context, menu);
|
|
|
|
|
|
|
|
MessageRecord messageRecord = getMessageRecord();
|
|
|
|
setCorrectMenuVisibility(messageRecord, menu);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDestroyActionMode(ActionMode mode) {
|
|
|
|
if (getListView() != null && getListView().getChildCount() > 0) {
|
|
|
|
for (int i = 0; i < getListView().getChildCount(); i++){
|
|
|
|
getListView().getChildAt(i).setSelected(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
actionMode = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
|
|
|
|
MessageRecord messageRecord = getMessageRecord();
|
|
|
|
|
|
|
|
switch(item.getItemId()) {
|
|
|
|
case R.id.menu_context_copy:
|
|
|
|
handleCopyMessage(messageRecord);
|
|
|
|
actionMode.finish();
|
|
|
|
return true;
|
|
|
|
case R.id.menu_context_delete_message:
|
|
|
|
handleDeleteMessage(messageRecord);
|
|
|
|
actionMode.finish();
|
|
|
|
return true;
|
|
|
|
case R.id.menu_context_details:
|
|
|
|
handleDisplayDetails(messageRecord);
|
|
|
|
actionMode.finish();
|
|
|
|
return true;
|
|
|
|
case R.id.menu_context_forward:
|
|
|
|
handleForwardMessage(messageRecord);
|
|
|
|
actionMode.finish();
|
|
|
|
return true;
|
|
|
|
case R.id.menu_context_resend:
|
|
|
|
handleResendMessage(messageRecord);
|
|
|
|
actionMode.finish();
|
|
|
|
return true;
|
|
|
|
case R.id.menu_context_save_attachment:
|
|
|
|
handleSaveAttachment(messageRecord);
|
|
|
|
actionMode.finish();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-06-12 01:03:01 +00:00
|
|
|
private class SaveAttachmentTask extends AsyncTask<MediaMmsMessageRecord, Void, Integer> {
|
|
|
|
|
|
|
|
private static final int SUCCESS = 0;
|
|
|
|
private static final int FAILURE = 1;
|
|
|
|
private static final int WRITE_ACCESS_FAILURE = 2;
|
|
|
|
|
|
|
|
private final WeakReference<Context> contextReference;
|
|
|
|
private ProgressDialog progressDialog;
|
|
|
|
|
|
|
|
public SaveAttachmentTask(Context context) {
|
|
|
|
this.contextReference = new WeakReference<Context>(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPreExecute() {
|
|
|
|
Context context = contextReference.get();
|
|
|
|
|
|
|
|
if (context != null) {
|
|
|
|
progressDialog = ProgressDialog.show(context,
|
|
|
|
context.getString(R.string.ConversationFragment_saving_attachment),
|
|
|
|
context.getString(R.string.ConversationFragment_saving_attachment_to_sd_card),
|
|
|
|
true, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected Integer doInBackground(MediaMmsMessageRecord... messageRecord) {
|
|
|
|
try {
|
|
|
|
Context context = contextReference.get();
|
|
|
|
|
|
|
|
if (!Environment.getExternalStorageDirectory().canWrite()) {
|
|
|
|
return WRITE_ACCESS_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (context == null) {
|
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
Slide slide = getAttachment(messageRecord[0]);
|
|
|
|
|
|
|
|
if (slide == null) {
|
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
|
2014-07-11 10:58:21 +00:00
|
|
|
File mediaFile = constructOutputFile(slide, messageRecord[0].getDateReceived());
|
2014-06-12 01:03:01 +00:00
|
|
|
InputStream inputStream = slide.getPartDataInputStream();
|
|
|
|
OutputStream outputStream = new FileOutputStream(mediaFile);
|
|
|
|
|
|
|
|
Util.copy(inputStream, outputStream);
|
|
|
|
|
|
|
|
MediaScannerConnection.scanFile(context, new String[] {mediaFile.getAbsolutePath()},
|
|
|
|
new String[] {slide.getContentType()}, null);
|
|
|
|
|
|
|
|
return SUCCESS;
|
|
|
|
} catch (IOException ioe) {
|
|
|
|
Log.w(TAG, ioe);
|
|
|
|
return FAILURE;
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
throw new AssertionError(e);
|
|
|
|
} catch (ExecutionException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(Integer result) {
|
|
|
|
Context context = contextReference.get();
|
|
|
|
if (context == null) return;
|
|
|
|
|
|
|
|
switch (result) {
|
|
|
|
case FAILURE:
|
|
|
|
Toast.makeText(context, R.string.ConversationFragment_error_while_saving_attachment_to_sd_card,
|
|
|
|
Toast.LENGTH_LONG).show();
|
|
|
|
break;
|
|
|
|
case SUCCESS:
|
|
|
|
Toast.makeText(context, R.string.ConversationFragment_success_exclamation,
|
|
|
|
Toast.LENGTH_LONG).show();
|
|
|
|
break;
|
|
|
|
case WRITE_ACCESS_FAILURE:
|
|
|
|
Toast.makeText(context, R.string.ConversationFragment_unable_to_write_to_sd_card_exclamation,
|
|
|
|
Toast.LENGTH_LONG).show();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (progressDialog != null)
|
|
|
|
progressDialog.dismiss();
|
|
|
|
}
|
|
|
|
|
|
|
|
private Slide getAttachment(MediaMmsMessageRecord record)
|
|
|
|
throws ExecutionException, InterruptedException
|
|
|
|
{
|
|
|
|
List<Slide> slides = record.getSlideDeck().get().getSlides();
|
|
|
|
|
|
|
|
for (Slide slide : slides) {
|
|
|
|
if (slide.hasImage() || slide.hasVideo() || slide.hasAudio()) {
|
|
|
|
return slide;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2014-07-11 10:58:21 +00:00
|
|
|
private File constructOutputFile(Slide slide, long timestamp) throws IOException {
|
2014-06-12 01:03:01 +00:00
|
|
|
File sdCard = Environment.getExternalStorageDirectory();
|
|
|
|
File outputDirectory;
|
|
|
|
|
|
|
|
if (slide.hasVideo()) {
|
|
|
|
outputDirectory = new File(sdCard.getAbsoluteFile() + File.separator + "Movies");
|
|
|
|
} else if (slide.hasAudio()) {
|
|
|
|
outputDirectory = new File(sdCard.getAbsolutePath() + File.separator + "Music");
|
|
|
|
} else {
|
|
|
|
outputDirectory = new File(sdCard.getAbsolutePath() + File.separator + "Pictures");
|
|
|
|
}
|
|
|
|
|
|
|
|
outputDirectory.mkdirs();
|
|
|
|
|
2014-07-11 10:58:21 +00:00
|
|
|
MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
|
|
|
|
String extension = mimeTypeMap.getExtensionFromMimeType(slide.getContentType());
|
2014-07-31 16:50:22 +00:00
|
|
|
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd-HHmmss");
|
|
|
|
String base = "textsecure-" + dateFormatter.format(timestamp);
|
2014-06-12 01:03:01 +00:00
|
|
|
|
|
|
|
if (extension == null)
|
|
|
|
extension = "attach";
|
|
|
|
|
2014-07-31 16:50:22 +00:00
|
|
|
int i = 0;
|
2014-08-13 01:21:11 +00:00
|
|
|
File file = new File(outputDirectory, base + "." + extension);
|
|
|
|
while (file.exists()) {
|
|
|
|
file = new File(outputDirectory, base + "-" + (++i) + "." + extension);
|
|
|
|
}
|
2014-07-31 16:50:22 +00:00
|
|
|
|
|
|
|
return file;
|
2014-06-12 01:03:01 +00:00
|
|
|
}
|
|
|
|
}
|
2014-03-06 04:11:36 +00:00
|
|
|
}
|