2017-11-16 20:22:09 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014-2017 Open Whisper Systems
|
2014-02-28 06:44:02 +00:00
|
|
|
*
|
|
|
|
* 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;
|
|
|
|
|
2017-11-16 20:22:09 +00:00
|
|
|
import android.annotation.SuppressLint;
|
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;
|
2017-05-10 22:21:52 +00:00
|
|
|
import android.provider.OpenableColumns;
|
2019-03-11 17:54:54 +00:00
|
|
|
import android.view.MenuItem;
|
2015-10-15 21:40:45 +00:00
|
|
|
import android.view.View;
|
2017-11-16 20:22:09 +00:00
|
|
|
import android.widget.ImageView;
|
2014-02-28 06:44:02 +00:00
|
|
|
|
2020-08-27 05:49:16 +00:00
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.annotation.Nullable;
|
2020-09-18 04:48:20 +00:00
|
|
|
import androidx.appcompat.app.ActionBar;
|
2020-08-27 05:49:16 +00:00
|
|
|
import androidx.appcompat.widget.Toolbar;
|
|
|
|
|
2021-05-18 06:11:38 +00:00
|
|
|
import org.session.libsession.utilities.Address;
|
2021-10-04 07:51:19 +00:00
|
|
|
import org.session.libsession.utilities.DistributionTypes;
|
|
|
|
import org.session.libsession.utilities.ViewUtil;
|
|
|
|
import org.session.libsession.utilities.recipients.Recipient;
|
2021-05-17 23:12:33 +00:00
|
|
|
import org.session.libsignal.utilities.Log;
|
2021-10-04 07:51:19 +00:00
|
|
|
import org.thoughtcrime.securesms.components.SearchToolbar;
|
2021-07-09 03:25:57 +00:00
|
|
|
import org.thoughtcrime.securesms.contacts.ContactSelectionListFragment;
|
|
|
|
import org.thoughtcrime.securesms.contacts.ContactSelectionListLoader.DisplayMode;
|
2021-10-04 07:51:19 +00:00
|
|
|
import org.thoughtcrime.securesms.conversation.v2.ConversationActivityV2;
|
2021-10-04 23:58:02 +00:00
|
|
|
import org.thoughtcrime.securesms.dependencies.DatabaseComponent;
|
2015-11-18 20:54:40 +00:00
|
|
|
import org.thoughtcrime.securesms.mms.PartAuthority;
|
2019-02-26 01:47:30 +00:00
|
|
|
import org.thoughtcrime.securesms.providers.BlobProvider;
|
2015-11-21 08:04:19 +00:00
|
|
|
import org.thoughtcrime.securesms.util.MediaUtil;
|
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
|
|
|
|
2019-07-24 02:30:23 +00:00
|
|
|
import network.loki.messenger.R;
|
|
|
|
|
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
|
2020-04-15 02:52:23 +00:00
|
|
|
implements ContactSelectionListFragment.OnContactSelectedListener
|
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-08-01 15:56:00 +00:00
|
|
|
public static final String EXTRA_THREAD_ID = "thread_id";
|
|
|
|
public static final String EXTRA_ADDRESS_MARSHALLED = "address_marshalled";
|
|
|
|
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
|
|
|
|
2017-11-16 20:22:09 +00:00
|
|
|
private ContactSelectionListFragment contactsFragment;
|
|
|
|
private SearchToolbar searchToolbar;
|
|
|
|
private ImageView searchAction;
|
|
|
|
private View progressWheel;
|
|
|
|
private Uri resolvedExtra;
|
2021-07-12 05:44:46 +00:00
|
|
|
private CharSequence resolvedPlaintext;
|
2017-11-16 20:22:09 +00:00
|
|
|
private String mimeType;
|
|
|
|
private boolean isPassingAlongMedia;
|
2015-10-15 21:40:45 +00:00
|
|
|
|
2014-12-15 20:25:55 +00:00
|
|
|
@Override
|
2018-02-02 03:22:48 +00:00
|
|
|
protected void onCreate(Bundle icicle, boolean ready) {
|
2017-11-16 20:22:09 +00:00
|
|
|
if (!getIntent().hasExtra(ContactSelectionListFragment.DISPLAY_MODE)) {
|
2020-04-15 02:52:23 +00:00
|
|
|
getIntent().putExtra(ContactSelectionListFragment.DISPLAY_MODE, DisplayMode.FLAG_ALL);
|
2017-11-16 20:22:09 +00:00
|
|
|
}
|
|
|
|
|
2017-11-16 23:21:46 +00:00
|
|
|
getIntent().putExtra(ContactSelectionListFragment.REFRESHABLE, false);
|
|
|
|
|
2017-11-16 20:22:09 +00:00
|
|
|
setContentView(R.layout.share_activity);
|
2015-10-15 21:40:45 +00:00
|
|
|
|
2017-11-16 20:22:09 +00:00
|
|
|
initializeToolbar();
|
|
|
|
initializeResources();
|
|
|
|
initializeSearch();
|
2015-10-15 21:40:45 +00:00
|
|
|
initializeMedia();
|
2014-02-28 06:44:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onNewIntent(Intent intent) {
|
2018-08-02 13:25:33 +00:00
|
|
|
Log.i(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 onPause() {
|
|
|
|
super.onPause();
|
2015-10-15 21:40:45 +00:00
|
|
|
if (!isPassingAlongMedia && resolvedExtra != null) {
|
2019-02-26 01:47:30 +00:00
|
|
|
BlobProvider.getInstance().delete(this, resolvedExtra);
|
2019-02-12 00:14:27 +00:00
|
|
|
|
|
|
|
if (!isFinishing()) {
|
|
|
|
finish();
|
|
|
|
}
|
2015-10-15 21:40:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-11 17:54:54 +00:00
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
switch (item.getItemId()) {
|
|
|
|
case android.R.id.home:
|
|
|
|
onBackPressed();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
|
|
}
|
|
|
|
|
2017-11-16 20:22:09 +00:00
|
|
|
@Override
|
|
|
|
public void onBackPressed() {
|
|
|
|
if (searchToolbar.isVisible()) searchToolbar.collapse();
|
|
|
|
else super.onBackPressed();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void initializeToolbar() {
|
|
|
|
Toolbar toolbar = findViewById(R.id.toolbar);
|
|
|
|
setSupportActionBar(toolbar);
|
2020-09-18 04:48:20 +00:00
|
|
|
ActionBar actionBar = getSupportActionBar();
|
|
|
|
actionBar.setDisplayHomeAsUpEnabled(true);
|
|
|
|
actionBar.setHomeButtonEnabled(true);
|
2017-11-16 20:22:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void initializeResources() {
|
|
|
|
progressWheel = findViewById(R.id.progress_wheel);
|
|
|
|
searchToolbar = findViewById(R.id.search_toolbar);
|
|
|
|
searchAction = findViewById(R.id.search_action);
|
|
|
|
contactsFragment = (ContactSelectionListFragment) getSupportFragmentManager().findFragmentById(R.id.contact_selection_list_fragment);
|
|
|
|
contactsFragment.setOnContactSelectedListener(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void initializeSearch() {
|
|
|
|
searchAction.setOnClickListener(v -> searchToolbar.display(searchAction.getX() + (searchAction.getWidth() / 2),
|
|
|
|
searchAction.getY() + (searchAction.getHeight() / 2)));
|
|
|
|
|
|
|
|
searchToolbar.setListener(new SearchToolbar.SearchListener() {
|
|
|
|
@Override
|
|
|
|
public void onSearchTextChange(String text) {
|
|
|
|
if (contactsFragment != null) {
|
|
|
|
contactsFragment.setQueryFilter(text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-04-07 01:15:24 +00:00
|
|
|
public void onSearchClosed() {
|
2017-11-16 20:22:09 +00:00
|
|
|
if (contactsFragment != null) {
|
|
|
|
contactsFragment.resetQueryFilter();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-10-15 21:40:45 +00:00
|
|
|
private void initializeMedia() {
|
|
|
|
final Context context = this;
|
|
|
|
isPassingAlongMedia = false;
|
|
|
|
|
2015-11-18 20:54:40 +00:00
|
|
|
Uri streamExtra = getIntent().getParcelableExtra(Intent.EXTRA_STREAM);
|
2021-07-12 05:44:46 +00:00
|
|
|
CharSequence charSequenceExtra = getIntent().getCharSequenceExtra(Intent.EXTRA_TEXT);
|
|
|
|
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);
|
2021-07-12 05:44:46 +00:00
|
|
|
} else if (charSequenceExtra != null && mimeType != null && mimeType.startsWith("text/")) {
|
|
|
|
resolvedPlaintext = charSequenceExtra;
|
|
|
|
handleResolvedMedia(getIntent(), false);
|
2015-11-18 20:54:40 +00:00
|
|
|
} else {
|
2017-11-16 20:22:09 +00:00
|
|
|
contactsFragment.getView().setVisibility(View.GONE);
|
2015-11-18 20:54:40 +00:00
|
|
|
progressWheel.setVisibility(View.VISIBLE);
|
2017-10-23 20:03:32 +00:00
|
|
|
new ResolveMediaTask(context).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, streamExtra);
|
2015-11-18 20:54:40 +00:00
|
|
|
}
|
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);
|
2017-08-01 15:56:00 +00:00
|
|
|
Address address = null;
|
2017-07-26 16:59:15 +00:00
|
|
|
|
2017-08-01 15:56:00 +00:00
|
|
|
if (intent.hasExtra(EXTRA_ADDRESS_MARSHALLED)) {
|
2017-07-26 16:59:15 +00:00
|
|
|
Parcel parcel = Parcel.obtain();
|
2017-08-01 15:56:00 +00:00
|
|
|
byte[] marshalled = intent.getByteArrayExtra(EXTRA_ADDRESS_MARSHALLED);
|
2017-07-26 16:59:15 +00:00
|
|
|
parcel.unmarshall(marshalled, 0, marshalled.length);
|
|
|
|
parcel.setDataPosition(0);
|
2017-08-01 15:56:00 +00:00
|
|
|
address = parcel.readParcelable(getClassLoader());
|
2017-07-26 16:59:15 +00:00
|
|
|
parcel.recycle();
|
|
|
|
}
|
2017-01-12 03:54:19 +00:00
|
|
|
|
2017-08-01 15:56:00 +00:00
|
|
|
boolean hasResolvedDestination = threadId != -1 && address != null && distributionType != -1;
|
2017-01-12 03:54:19 +00:00
|
|
|
|
|
|
|
if (!hasResolvedDestination && animate) {
|
2017-11-16 20:22:09 +00:00
|
|
|
ViewUtil.fadeIn(contactsFragment.getView(), 300);
|
2017-01-12 03:54:19 +00:00
|
|
|
ViewUtil.fadeOut(progressWheel, 300);
|
|
|
|
} else if (!hasResolvedDestination) {
|
2017-11-16 20:22:09 +00:00
|
|
|
contactsFragment.getView().setVisibility(View.VISIBLE);
|
2017-01-12 03:54:19 +00:00
|
|
|
progressWheel.setVisibility(View.GONE);
|
|
|
|
} else {
|
2017-08-01 15:56:00 +00:00
|
|
|
createConversation(threadId, address, distributionType);
|
2017-01-12 03:54:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-01 15:56:00 +00:00
|
|
|
private void createConversation(long threadId, Address address, int distributionType) {
|
2021-06-30 04:57:53 +00:00
|
|
|
final Intent intent = getBaseShareIntent(ConversationActivityV2.class);
|
2021-06-30 23:31:30 +00:00
|
|
|
intent.putExtra(ConversationActivityV2.ADDRESS, address);
|
2021-06-30 04:57:53 +00:00
|
|
|
intent.putExtra(ConversationActivityV2.THREAD_ID, threadId);
|
2014-02-28 06:44:02 +00:00
|
|
|
|
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) {
|
2019-04-17 14:21:30 +00:00
|
|
|
final Intent intent = new Intent(this, target);
|
2018-11-20 17:59:23 +00:00
|
|
|
|
2021-07-12 05:44:46 +00:00
|
|
|
if (resolvedExtra != null) {
|
|
|
|
intent.setDataAndType(resolvedExtra, mimeType);
|
|
|
|
} else if (resolvedPlaintext != null) {
|
|
|
|
intent.putExtra(Intent.EXTRA_TEXT, resolvedPlaintext);
|
2021-07-12 05:52:46 +00:00
|
|
|
intent.setType("text/plain");
|
2021-07-12 05:44:46 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2017-11-16 20:22:09 +00:00
|
|
|
@Override
|
|
|
|
public void onContactSelected(String number) {
|
2021-02-17 05:09:36 +00:00
|
|
|
Recipient recipient = Recipient.from(this, Address.fromExternal(this, number), true);
|
2021-10-04 23:58:02 +00:00
|
|
|
long existingThread = DatabaseComponent.get(this).threadDatabase().getThreadIdIfExistsFor(recipient);
|
2021-03-02 01:24:09 +00:00
|
|
|
createConversation(existingThread, recipient.getAddress(), DistributionTypes.DEFAULT);
|
2017-11-16 20:22:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onContactDeselected(String number) {
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressLint("StaticFieldLeak")
|
2015-11-18 20:54:40 +00:00
|
|
|
private class ResolveMediaTask extends AsyncTask<Uri, Void, Uri> {
|
|
|
|
private final Context context;
|
|
|
|
|
2017-11-16 20:22:09 +00:00
|
|
|
ResolveMediaTask(Context context) {
|
2015-11-18 20:54:40 +00:00
|
|
|
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())) {
|
2020-12-07 06:40:47 +00:00
|
|
|
inputStream = new FileInputStream(uris[0].getPath());
|
2016-03-26 00:08:43 +00:00
|
|
|
} 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();
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:47:30 +00:00
|
|
|
return BlobProvider.getInstance()
|
|
|
|
.forData(inputStream, fileSize == null ? 0 : fileSize)
|
|
|
|
.withMimeType(mimeType)
|
|
|
|
.withFileName(fileName)
|
2019-03-19 22:34:04 +00:00
|
|
|
.createForMultipleSessionsOnDisk(context, e -> Log.w(TAG, "Failed to write to disk.", e));
|
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
|
|
|
}
|
2014-02-28 06:44:02 +00:00
|
|
|
}
|
2014-12-15 20:25:55 +00:00
|
|
|
}
|