2014-02-28 06:44:02 +00:00
|
|
|
/**
|
|
|
|
* Copyright (C) 2014 Open Whisper Systems
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package org.thoughtcrime.securesms;
|
|
|
|
|
2015-10-15 21:40:45 +00:00
|
|
|
import android.content.Context;
|
2014-02-28 06:44:02 +00:00
|
|
|
import android.content.Intent;
|
2017-05-10 22:21:52 +00:00
|
|
|
import android.database.Cursor;
|
2014-02-28 06:44:02 +00:00
|
|
|
import android.net.Uri;
|
2015-10-15 21:40:45 +00:00
|
|
|
import android.os.AsyncTask;
|
2014-02-28 06:44:02 +00:00
|
|
|
import android.os.Bundle;
|
2017-07-26 16:59:15 +00:00
|
|
|
import android.os.Parcel;
|
2016-03-26 00:08:43 +00:00
|
|
|
import android.os.Process;
|
2017-05-10 22:21:52 +00:00
|
|
|
import android.provider.OpenableColumns;
|
2014-12-15 20:25:55 +00:00
|
|
|
import android.support.annotation.NonNull;
|
2015-11-21 07:18:19 +00:00
|
|
|
import android.support.annotation.Nullable;
|
2015-10-15 21:40:45 +00:00
|
|
|
import android.util.Log;
|
2014-06-29 03:40:57 +00:00
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuInflater;
|
|
|
|
import android.view.MenuItem;
|
2015-10-15 21:40:45 +00:00
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
2014-02-28 06:44:02 +00:00
|
|
|
|
2014-11-03 23:16:04 +00:00
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
2017-07-26 16:59:15 +00:00
|
|
|
import org.thoughtcrime.securesms.database.Address;
|
2015-11-18 20:54:40 +00:00
|
|
|
import org.thoughtcrime.securesms.mms.PartAuthority;
|
2015-10-15 21:40:45 +00:00
|
|
|
import org.thoughtcrime.securesms.providers.PersistentBlobProvider;
|
2014-02-28 06:44:02 +00:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipients;
|
|
|
|
import org.thoughtcrime.securesms.util.DynamicLanguage;
|
|
|
|
import org.thoughtcrime.securesms.util.DynamicTheme;
|
2016-03-26 00:08:43 +00:00
|
|
|
import org.thoughtcrime.securesms.util.FileUtils;
|
2015-11-21 08:04:19 +00:00
|
|
|
import org.thoughtcrime.securesms.util.MediaUtil;
|
2015-10-15 21:40:45 +00:00
|
|
|
import org.thoughtcrime.securesms.util.ViewUtil;
|
2014-12-15 20:25:55 +00:00
|
|
|
|
2016-03-26 00:08:43 +00:00
|
|
|
import java.io.FileInputStream;
|
2015-10-15 21:40:45 +00:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
2015-06-09 03:56:13 +00:00
|
|
|
|
2014-02-28 06:44:02 +00:00
|
|
|
/**
|
|
|
|
* An activity to quickly share content with contacts
|
|
|
|
*
|
|
|
|
* @author Jake McGinty
|
|
|
|
*/
|
2014-06-29 03:40:57 +00:00
|
|
|
public class ShareActivity extends PassphraseRequiredActionBarActivity
|
2014-02-28 06:44:02 +00:00
|
|
|
implements ShareFragment.ConversationSelectedListener
|
2014-12-15 20:25:55 +00:00
|
|
|
{
|
2015-10-15 21:40:45 +00:00
|
|
|
private static final String TAG = ShareActivity.class.getSimpleName();
|
|
|
|
|
2017-07-26 16:59:15 +00:00
|
|
|
public static final String EXTRA_THREAD_ID = "thread_id";
|
|
|
|
public static final String EXTRA_ADDRESSES_MARSHALLED = "addresses";
|
|
|
|
public static final String EXTRA_DISTRIBUTION_TYPE = "distribution_type";
|
2017-01-12 03:54:19 +00:00
|
|
|
|
2014-02-28 06:44:02 +00:00
|
|
|
private final DynamicTheme dynamicTheme = new DynamicTheme ();
|
|
|
|
private final DynamicLanguage dynamicLanguage = new DynamicLanguage();
|
|
|
|
|
2015-10-15 21:40:45 +00:00
|
|
|
private MasterSecret masterSecret;
|
|
|
|
private ViewGroup fragmentContainer;
|
|
|
|
private View progressWheel;
|
|
|
|
private Uri resolvedExtra;
|
2015-11-21 07:18:19 +00:00
|
|
|
private String mimeType;
|
2015-10-15 21:40:45 +00:00
|
|
|
private boolean isPassingAlongMedia;
|
|
|
|
|
2014-02-28 06:44:02 +00:00
|
|
|
@Override
|
2014-12-15 20:25:55 +00:00
|
|
|
protected void onPreCreate() {
|
2014-02-28 06:44:02 +00:00
|
|
|
dynamicTheme.onCreate(this);
|
|
|
|
dynamicLanguage.onCreate(this);
|
2014-12-15 20:25:55 +00:00
|
|
|
}
|
2014-02-28 06:44:02 +00:00
|
|
|
|
2014-12-15 20:25:55 +00:00
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle icicle, @NonNull MasterSecret masterSecret) {
|
2015-10-15 21:40:45 +00:00
|
|
|
this.masterSecret = masterSecret;
|
2014-02-28 06:44:02 +00:00
|
|
|
setContentView(R.layout.share_activity);
|
2015-10-15 21:40:45 +00:00
|
|
|
|
|
|
|
fragmentContainer = ViewUtil.findById(this, R.id.drawer_layout);
|
|
|
|
progressWheel = ViewUtil.findById(this, R.id.progress_wheel);
|
|
|
|
|
2014-12-15 20:25:55 +00:00
|
|
|
initFragment(R.id.drawer_layout, new ShareFragment(), masterSecret);
|
2015-10-15 21:40:45 +00:00
|
|
|
initializeMedia();
|
2014-02-28 06:44:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onNewIntent(Intent intent) {
|
2017-07-26 16:59:15 +00:00
|
|
|
Log.w(TAG, "onNewIntent()");
|
2015-10-15 21:40:45 +00:00
|
|
|
super.onNewIntent(intent);
|
|
|
|
setIntent(intent);
|
|
|
|
initializeMedia();
|
2014-02-28 06:44:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onResume() {
|
2017-07-26 16:59:15 +00:00
|
|
|
Log.w(TAG, "onResume()");
|
2014-02-28 06:44:02 +00:00
|
|
|
super.onResume();
|
|
|
|
dynamicTheme.onResume(this);
|
|
|
|
dynamicLanguage.onResume(this);
|
2014-04-09 20:26:06 +00:00
|
|
|
getSupportActionBar().setTitle(R.string.ShareActivity_share_with);
|
2014-02-28 06:44:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPause() {
|
|
|
|
super.onPause();
|
2015-10-15 21:40:45 +00:00
|
|
|
if (!isPassingAlongMedia && resolvedExtra != null) {
|
|
|
|
PersistentBlobProvider.getInstance(this).delete(resolvedExtra);
|
|
|
|
}
|
|
|
|
if (!isFinishing()) {
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void initializeMedia() {
|
|
|
|
final Context context = this;
|
|
|
|
isPassingAlongMedia = false;
|
|
|
|
|
2015-11-18 20:54:40 +00:00
|
|
|
Uri streamExtra = getIntent().getParcelableExtra(Intent.EXTRA_STREAM);
|
2015-11-21 07:18:19 +00:00
|
|
|
mimeType = getMimeType(streamExtra);
|
2017-01-12 03:54:19 +00:00
|
|
|
|
2015-11-18 20:54:40 +00:00
|
|
|
if (streamExtra != null && PartAuthority.isLocalUri(streamExtra)) {
|
|
|
|
isPassingAlongMedia = true;
|
|
|
|
resolvedExtra = streamExtra;
|
2017-01-12 03:54:19 +00:00
|
|
|
handleResolvedMedia(getIntent(), false);
|
2015-11-18 20:54:40 +00:00
|
|
|
} else {
|
|
|
|
fragmentContainer.setVisibility(View.GONE);
|
|
|
|
progressWheel.setVisibility(View.VISIBLE);
|
|
|
|
new ResolveMediaTask(context).execute(streamExtra);
|
|
|
|
}
|
2014-02-28 06:44:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onPrepareOptionsMenu(Menu menu) {
|
2014-06-29 03:40:57 +00:00
|
|
|
MenuInflater inflater = this.getMenuInflater();
|
2014-02-28 06:44:02 +00:00
|
|
|
menu.clear();
|
|
|
|
|
|
|
|
inflater.inflate(R.menu.share, menu);
|
|
|
|
super.onPrepareOptionsMenu(menu);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
super.onOptionsItemSelected(item);
|
|
|
|
switch (item.getItemId()) {
|
|
|
|
case R.id.menu_new_message: handleNewConversation(); return true;
|
|
|
|
case android.R.id.home: finish(); return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void handleNewConversation() {
|
|
|
|
Intent intent = getBaseShareIntent(NewConversationActivity.class);
|
2015-10-15 21:40:45 +00:00
|
|
|
isPassingAlongMedia = true;
|
2014-02-28 06:44:02 +00:00
|
|
|
startActivity(intent);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreateConversation(long threadId, Recipients recipients, int distributionType) {
|
2017-07-26 16:59:15 +00:00
|
|
|
createConversation(threadId, recipients.getAddresses(), distributionType);
|
2014-02-28 06:44:02 +00:00
|
|
|
}
|
|
|
|
|
2017-01-12 03:54:19 +00:00
|
|
|
private void handleResolvedMedia(Intent intent, boolean animate) {
|
2017-07-26 16:59:15 +00:00
|
|
|
long threadId = intent.getLongExtra(EXTRA_THREAD_ID, -1);
|
|
|
|
int distributionType = intent.getIntExtra(EXTRA_DISTRIBUTION_TYPE, -1);
|
|
|
|
Address[] addresses = null;
|
|
|
|
|
|
|
|
if (intent.hasExtra(EXTRA_ADDRESSES_MARSHALLED)) {
|
|
|
|
Parcel parcel = Parcel.obtain();
|
|
|
|
byte[] marshalled = intent.getByteArrayExtra(EXTRA_ADDRESSES_MARSHALLED);
|
|
|
|
parcel.unmarshall(marshalled, 0, marshalled.length);
|
|
|
|
parcel.setDataPosition(0);
|
|
|
|
addresses = parcel.createTypedArray(Address.CREATOR);
|
|
|
|
parcel.recycle();
|
|
|
|
}
|
2017-01-12 03:54:19 +00:00
|
|
|
|
2017-07-26 16:59:15 +00:00
|
|
|
boolean hasResolvedDestination = threadId != -1 && addresses != null && distributionType != -1;
|
2017-01-12 03:54:19 +00:00
|
|
|
|
|
|
|
if (!hasResolvedDestination && animate) {
|
|
|
|
ViewUtil.fadeIn(fragmentContainer, 300);
|
|
|
|
ViewUtil.fadeOut(progressWheel, 300);
|
|
|
|
} else if (!hasResolvedDestination) {
|
|
|
|
fragmentContainer.setVisibility(View.VISIBLE);
|
|
|
|
progressWheel.setVisibility(View.GONE);
|
|
|
|
} else {
|
2017-07-26 16:59:15 +00:00
|
|
|
createConversation(threadId, addresses, distributionType);
|
2017-01-12 03:54:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-26 16:59:15 +00:00
|
|
|
private void createConversation(long threadId, Address[] addresses, int distributionType) {
|
2014-02-28 06:44:02 +00:00
|
|
|
final Intent intent = getBaseShareIntent(ConversationActivity.class);
|
2017-07-26 16:59:15 +00:00
|
|
|
intent.putExtra(ConversationActivity.ADDRESSES_EXTRA, addresses);
|
2014-02-28 06:44:02 +00:00
|
|
|
intent.putExtra(ConversationActivity.THREAD_ID_EXTRA, threadId);
|
|
|
|
intent.putExtra(ConversationActivity.DISTRIBUTION_TYPE_EXTRA, distributionType);
|
|
|
|
|
2015-10-15 21:40:45 +00:00
|
|
|
isPassingAlongMedia = true;
|
2014-02-28 06:44:02 +00:00
|
|
|
startActivity(intent);
|
|
|
|
}
|
|
|
|
|
2015-10-15 21:40:45 +00:00
|
|
|
private Intent getBaseShareIntent(final @NonNull Class<?> target) {
|
2014-12-15 20:25:55 +00:00
|
|
|
final Intent intent = new Intent(this, target);
|
2015-04-02 18:23:18 +00:00
|
|
|
final String textExtra = getIntent().getStringExtra(Intent.EXTRA_TEXT);
|
2015-11-18 20:54:40 +00:00
|
|
|
intent.putExtra(ConversationActivity.TEXT_EXTRA, textExtra);
|
2015-11-21 07:18:19 +00:00
|
|
|
if (resolvedExtra != null) intent.setDataAndType(resolvedExtra, mimeType);
|
2014-02-28 06:44:02 +00:00
|
|
|
|
2014-12-15 20:25:55 +00:00
|
|
|
return intent;
|
2014-02-28 06:44:02 +00:00
|
|
|
}
|
|
|
|
|
2015-11-21 07:18:19 +00:00
|
|
|
private String getMimeType(@Nullable Uri uri) {
|
|
|
|
if (uri != null) {
|
|
|
|
final String mimeType = MediaUtil.getMimeType(getApplicationContext(), uri);
|
|
|
|
if (mimeType != null) return mimeType;
|
|
|
|
}
|
|
|
|
return MediaUtil.getCorrectedMimeType(getIntent().getType());
|
2015-11-18 20:54:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private class ResolveMediaTask extends AsyncTask<Uri, Void, Uri> {
|
|
|
|
private final Context context;
|
|
|
|
|
|
|
|
public ResolveMediaTask(Context context) {
|
|
|
|
this.context = context;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected Uri doInBackground(Uri... uris) {
|
|
|
|
try {
|
2016-03-26 00:08:43 +00:00
|
|
|
if (uris.length != 1 || uris[0] == null) {
|
2015-11-18 20:54:40 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-03-26 00:08:43 +00:00
|
|
|
InputStream inputStream;
|
2016-03-23 23:45:29 +00:00
|
|
|
|
2016-03-26 00:08:43 +00:00
|
|
|
if ("file".equals(uris[0].getScheme())) {
|
|
|
|
inputStream = openFileUri(uris[0]);
|
|
|
|
} else {
|
|
|
|
inputStream = context.getContentResolver().openInputStream(uris[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inputStream == null) {
|
2015-11-18 20:54:40 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-05-10 22:21:52 +00:00
|
|
|
Cursor cursor = getContentResolver().query(uris[0], new String[] {OpenableColumns.DISPLAY_NAME, OpenableColumns.SIZE}, null, null, null);
|
|
|
|
String fileName = null;
|
|
|
|
Long fileSize = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (cursor != null && cursor.moveToFirst()) {
|
|
|
|
try {
|
|
|
|
fileName = cursor.getString(cursor.getColumnIndexOrThrow(OpenableColumns.DISPLAY_NAME));
|
|
|
|
fileSize = cursor.getLong(cursor.getColumnIndexOrThrow(OpenableColumns.SIZE));
|
|
|
|
} catch (IllegalArgumentException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
if (cursor != null) cursor.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
return PersistentBlobProvider.getInstance(context).create(masterSecret, inputStream, mimeType, fileName, fileSize);
|
2015-11-18 20:54:40 +00:00
|
|
|
} catch (IOException ioe) {
|
|
|
|
Log.w(TAG, ioe);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(Uri uri) {
|
|
|
|
resolvedExtra = uri;
|
2017-01-12 03:54:19 +00:00
|
|
|
handleResolvedMedia(getIntent(), true);
|
2015-11-18 20:54:40 +00:00
|
|
|
}
|
2016-03-26 00:08:43 +00:00
|
|
|
|
|
|
|
private InputStream openFileUri(Uri uri) throws IOException {
|
|
|
|
FileInputStream fin = new FileInputStream(uri.getPath());
|
|
|
|
int owner = FileUtils.getFileDescriptorOwner(fin.getFD());
|
|
|
|
|
|
|
|
if (owner == -1 || owner == Process.myUid()) {
|
|
|
|
fin.close();
|
|
|
|
throw new IOException("File owned by application");
|
|
|
|
}
|
|
|
|
|
|
|
|
return fin;
|
|
|
|
}
|
2014-02-28 06:44:02 +00:00
|
|
|
}
|
2014-12-15 20:25:55 +00:00
|
|
|
}
|