2018-11-20 09:59:23 -08:00
|
|
|
package org.thoughtcrime.securesms.mediasend;
|
|
|
|
|
|
|
|
import android.arch.lifecycle.ViewModelProviders;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.net.Uri;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.support.annotation.NonNull;
|
2019-03-01 10:50:48 -08:00
|
|
|
import android.support.v4.app.Fragment;
|
|
|
|
import android.support.v4.app.FragmentManager;
|
|
|
|
import android.support.v4.app.FragmentTransaction;
|
|
|
|
import android.view.View;
|
|
|
|
import android.widget.Button;
|
|
|
|
import android.widget.TextView;
|
2018-11-20 09:59:23 -08:00
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.PassphraseRequiredActionBarActivity;
|
|
|
|
import org.thoughtcrime.securesms.R;
|
|
|
|
import org.thoughtcrime.securesms.TransportOption;
|
|
|
|
import org.thoughtcrime.securesms.database.Address;
|
2019-03-01 10:50:48 -08:00
|
|
|
import org.thoughtcrime.securesms.logging.Log;
|
2019-02-11 15:05:37 -08:00
|
|
|
import org.thoughtcrime.securesms.mms.MediaConstraints;
|
2018-11-20 09:59:23 -08:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
|
|
|
import org.thoughtcrime.securesms.scribbles.ScribbleFragment;
|
|
|
|
import org.thoughtcrime.securesms.util.DynamicLanguage;
|
|
|
|
import org.thoughtcrime.securesms.util.DynamicNoActionBarTheme;
|
|
|
|
import org.thoughtcrime.securesms.util.DynamicTheme;
|
|
|
|
import org.thoughtcrime.securesms.util.Util;
|
|
|
|
import org.whispersystems.libsignal.util.guava.Optional;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.List;
|
2019-03-01 10:50:48 -08:00
|
|
|
import java.util.Locale;
|
2018-11-20 09:59:23 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Encompasses the entire flow of sending media, starting from the selection process to the actual
|
|
|
|
* captioning and editing of the content.
|
|
|
|
*
|
|
|
|
* This activity is intended to be launched via {@link #startActivityForResult(Intent, int)}.
|
|
|
|
* It will return the {@link Media} that the user decided to send.
|
|
|
|
*/
|
|
|
|
public class MediaSendActivity extends PassphraseRequiredActionBarActivity implements MediaPickerFolderFragment.Controller,
|
|
|
|
MediaPickerItemFragment.Controller,
|
|
|
|
MediaSendFragment.Controller,
|
|
|
|
ScribbleFragment.Controller
|
|
|
|
{
|
|
|
|
public static final String EXTRA_MEDIA = "media";
|
|
|
|
public static final String EXTRA_MESSAGE = "message";
|
|
|
|
public static final String EXTRA_TRANSPORT = "transport";
|
|
|
|
|
|
|
|
private static final int MAX_PUSH = 32;
|
|
|
|
private static final int MAX_SMS = 1;
|
|
|
|
|
|
|
|
private static final String KEY_ADDRESS = "address";
|
|
|
|
private static final String KEY_BODY = "body";
|
|
|
|
private static final String KEY_MEDIA = "media";
|
|
|
|
private static final String KEY_TRANSPORT = "transport";
|
|
|
|
|
|
|
|
private static final String TAG_FOLDER_PICKER = "folder_picker";
|
|
|
|
private static final String TAG_ITEM_PICKER = "item_picker";
|
|
|
|
private static final String TAG_SEND = "send";
|
|
|
|
|
|
|
|
private final DynamicTheme dynamicTheme = new DynamicNoActionBarTheme();
|
|
|
|
private final DynamicLanguage dynamicLanguage = new DynamicLanguage();
|
|
|
|
|
|
|
|
private Recipient recipient;
|
|
|
|
private TransportOption transport;
|
|
|
|
private MediaSendViewModel viewModel;
|
|
|
|
|
2019-03-01 10:50:48 -08:00
|
|
|
private View countButton;
|
|
|
|
private TextView countButtonText;
|
|
|
|
|
2018-11-20 09:59:23 -08:00
|
|
|
/**
|
|
|
|
* Get an intent to launch the media send flow starting with the picker.
|
|
|
|
*/
|
2019-01-17 11:42:01 -08:00
|
|
|
public static Intent getIntent(@NonNull Context context, @NonNull Recipient recipient, @NonNull String body, @NonNull TransportOption transport) {
|
2018-11-20 09:59:23 -08:00
|
|
|
Intent intent = new Intent(context, MediaSendActivity.class);
|
|
|
|
intent.putExtra(KEY_ADDRESS, recipient.getAddress().serialize());
|
|
|
|
intent.putExtra(KEY_TRANSPORT, transport);
|
2019-01-17 11:42:01 -08:00
|
|
|
intent.putExtra(KEY_BODY, body);
|
2018-11-20 09:59:23 -08:00
|
|
|
return intent;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an intent to launch the media send flow with a specific list of media. Will jump right to
|
|
|
|
* the editor screen.
|
|
|
|
*/
|
|
|
|
public static Intent getIntent(@NonNull Context context,
|
|
|
|
@NonNull List<Media> media,
|
|
|
|
@NonNull Recipient recipient,
|
|
|
|
@NonNull String body,
|
|
|
|
@NonNull TransportOption transport)
|
|
|
|
{
|
2019-01-17 11:42:01 -08:00
|
|
|
Intent intent = getIntent(context, recipient, body, transport);
|
2018-11-20 09:59:23 -08:00
|
|
|
intent.putParcelableArrayListExtra(KEY_MEDIA, new ArrayList<>(media));
|
|
|
|
return intent;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPreCreate() {
|
|
|
|
dynamicTheme.onCreate(this);
|
|
|
|
dynamicLanguage.onCreate(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState, boolean ready) {
|
2019-03-01 10:50:48 -08:00
|
|
|
setContentView(R.layout.mediasend_activity);
|
2018-11-20 09:59:23 -08:00
|
|
|
setResult(RESULT_CANCELED);
|
|
|
|
|
|
|
|
if (savedInstanceState != null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-01 10:50:48 -08:00
|
|
|
countButton = findViewById(R.id.mediasend_count_button);
|
|
|
|
countButtonText = findViewById(R.id.mediasend_count_button_text);
|
|
|
|
|
2018-11-20 09:59:23 -08:00
|
|
|
viewModel = ViewModelProviders.of(this, new MediaSendViewModel.Factory(new MediaRepository())).get(MediaSendViewModel.class);
|
|
|
|
recipient = Recipient.from(this, Address.fromSerialized(getIntent().getStringExtra(KEY_ADDRESS)), true);
|
|
|
|
transport = getIntent().getParcelableExtra(KEY_TRANSPORT);
|
|
|
|
|
2019-02-11 15:05:37 -08:00
|
|
|
viewModel.setMediaConstraints(transport.isSms() ? MediaConstraints.getMmsMediaConstraints(transport.getSimSubscriptionId().or(-1))
|
|
|
|
: MediaConstraints.getPushMediaConstraints());
|
|
|
|
|
2019-03-01 10:50:48 -08:00
|
|
|
viewModel.onBodyChanged(getIntent().getStringExtra(KEY_BODY));
|
|
|
|
|
2018-11-20 09:59:23 -08:00
|
|
|
List<Media> media = getIntent().getParcelableArrayListExtra(KEY_MEDIA);
|
|
|
|
|
|
|
|
if (!Util.isEmpty(media)) {
|
2019-03-01 10:50:48 -08:00
|
|
|
viewModel.onSelectedMediaChanged(this, media);
|
|
|
|
|
|
|
|
Fragment fragment = MediaSendFragment.newInstance(transport, dynamicLanguage.getCurrentLocale());
|
|
|
|
getSupportFragmentManager().beginTransaction()
|
|
|
|
.replace(R.id.mediasend_fragment_container, fragment, TAG_SEND)
|
|
|
|
.commit();
|
2018-11-20 09:59:23 -08:00
|
|
|
} else {
|
2019-03-01 10:50:48 -08:00
|
|
|
MediaPickerFolderFragment fragment = MediaPickerFolderFragment.newInstance(recipient);
|
|
|
|
getSupportFragmentManager().beginTransaction()
|
|
|
|
.replace(R.id.mediasend_fragment_container, fragment, TAG_FOLDER_PICKER)
|
|
|
|
.commit();
|
2018-11-20 09:59:23 -08:00
|
|
|
}
|
2019-03-01 10:50:48 -08:00
|
|
|
|
|
|
|
initializeCountButtonObserver(transport, dynamicLanguage.getCurrentLocale());
|
2018-11-20 09:59:23 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onResume() {
|
|
|
|
super.onResume();
|
|
|
|
dynamicTheme.onResume(this);
|
|
|
|
dynamicLanguage.onResume(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onBackPressed() {
|
|
|
|
MediaSendFragment sendFragment = (MediaSendFragment) getSupportFragmentManager().findFragmentByTag(TAG_SEND);
|
2019-01-15 15:08:35 -08:00
|
|
|
if (sendFragment == null || !sendFragment.isVisible() || !sendFragment.handleBackPress()) {
|
2018-11-20 09:59:23 -08:00
|
|
|
super.onBackPressed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFolderSelected(@NonNull MediaFolder folder) {
|
|
|
|
viewModel.onFolderSelected(folder.getBucketId());
|
|
|
|
|
2019-03-01 10:50:48 -08:00
|
|
|
MediaPickerItemFragment fragment = MediaPickerItemFragment.newInstance(folder.getBucketId(), folder.getTitle(), transport.isSms() ? MAX_SMS :MAX_PUSH);
|
2018-11-20 09:59:23 -08:00
|
|
|
getSupportFragmentManager().beginTransaction()
|
2019-03-01 10:50:48 -08:00
|
|
|
.setCustomAnimations(R.anim.slide_from_right, R.anim.slide_to_left, R.anim.slide_from_left, R.anim.slide_to_right)
|
|
|
|
.replace(R.id.mediasend_fragment_container, fragment, TAG_ITEM_PICKER)
|
2018-11-20 09:59:23 -08:00
|
|
|
.addToBackStack(null)
|
|
|
|
.commit();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-03-01 10:50:48 -08:00
|
|
|
public void onMediaSelected(@NonNull String bucketId) {
|
|
|
|
navigateToMediaSend(transport, dynamicLanguage.getCurrentLocale());
|
2018-11-20 09:59:23 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAddMediaClicked(@NonNull String bucketId) {
|
2019-03-01 10:50:48 -08:00
|
|
|
// TODO: Get actual folder title somehow
|
2018-11-20 09:59:23 -08:00
|
|
|
MediaPickerFolderFragment folderFragment = MediaPickerFolderFragment.newInstance(recipient);
|
2019-03-01 10:50:48 -08:00
|
|
|
MediaPickerItemFragment itemFragment = MediaPickerItemFragment.newInstance(bucketId, "", transport.isSms() ? MAX_SMS : MAX_PUSH);
|
2018-11-20 09:59:23 -08:00
|
|
|
|
|
|
|
getSupportFragmentManager().beginTransaction()
|
2019-03-01 10:50:48 -08:00
|
|
|
.setCustomAnimations(R.anim.stationary, R.anim.slide_to_left, R.anim.slide_from_left, R.anim.slide_to_right)
|
|
|
|
.replace(R.id.mediasend_fragment_container, folderFragment, TAG_FOLDER_PICKER)
|
2018-11-20 09:59:23 -08:00
|
|
|
.addToBackStack(null)
|
|
|
|
.commit();
|
|
|
|
|
|
|
|
getSupportFragmentManager().beginTransaction()
|
2019-03-01 10:50:48 -08:00
|
|
|
.setCustomAnimations(R.anim.slide_from_right, R.anim.stationary, R.anim.slide_from_left, R.anim.slide_to_right)
|
|
|
|
.replace(R.id.mediasend_fragment_container, itemFragment, TAG_ITEM_PICKER)
|
2018-11-20 09:59:23 -08:00
|
|
|
.addToBackStack(null)
|
|
|
|
.commit();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onSendClicked(@NonNull List<Media> media, @NonNull String message, @NonNull TransportOption transport) {
|
|
|
|
ArrayList<Media> mediaList = new ArrayList<>(media);
|
|
|
|
Intent intent = new Intent();
|
|
|
|
|
|
|
|
intent.putParcelableArrayListExtra(EXTRA_MEDIA, mediaList);
|
|
|
|
intent.putExtra(EXTRA_MESSAGE, message);
|
|
|
|
intent.putExtra(EXTRA_TRANSPORT, transport);
|
|
|
|
setResult(RESULT_OK, intent);
|
|
|
|
finish();
|
|
|
|
|
|
|
|
overridePendingTransition(R.anim.stationary, R.anim.camera_slide_to_bottom);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onNoMediaAvailable() {
|
|
|
|
setResult(RESULT_CANCELED);
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onImageEditComplete(@NonNull Uri uri, int width, int height, long size, @NonNull Optional<String> message, @NonNull Optional<TransportOption> transport) {
|
|
|
|
throw new UnsupportedOperationException("Callback unsupported.");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onImageEditFailure() {
|
|
|
|
throw new UnsupportedOperationException("Callback unsupported.");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onTouchEventsNeeded(boolean needed) {
|
|
|
|
MediaSendFragment fragment = (MediaSendFragment) getSupportFragmentManager().findFragmentByTag(TAG_SEND);
|
|
|
|
if (fragment != null) {
|
|
|
|
fragment.onTouchEventsNeeded(needed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-01 10:50:48 -08:00
|
|
|
private void initializeCountButtonObserver(@NonNull TransportOption transport, @NonNull Locale locale) {
|
|
|
|
viewModel.getCountButtonState().observe(this, buttonState -> {
|
|
|
|
if (buttonState == null) return;
|
2018-11-20 09:59:23 -08:00
|
|
|
|
2019-03-01 10:50:48 -08:00
|
|
|
countButton.setVisibility(buttonState.getVisibility() ? View.VISIBLE : View.GONE);
|
|
|
|
countButton.setOnClickListener(v -> navigateToMediaSend(transport, locale));
|
|
|
|
countButtonText.setText(String.valueOf(buttonState.getCount()));
|
|
|
|
});
|
2018-11-20 09:59:23 -08:00
|
|
|
}
|
|
|
|
|
2019-03-01 10:50:48 -08:00
|
|
|
private void navigateToMediaSend(@NonNull TransportOption transport, @NonNull Locale locale) {
|
|
|
|
MediaSendFragment fragment = MediaSendFragment.newInstance(transport, locale);
|
|
|
|
String backstackTag = null;
|
|
|
|
|
|
|
|
if (getSupportFragmentManager().findFragmentByTag(TAG_SEND) != null) {
|
|
|
|
getSupportFragmentManager().popBackStack(TAG_SEND, FragmentManager.POP_BACK_STACK_INCLUSIVE);
|
|
|
|
backstackTag = TAG_SEND;
|
|
|
|
}
|
2018-11-20 09:59:23 -08:00
|
|
|
|
|
|
|
getSupportFragmentManager().beginTransaction()
|
2019-03-01 10:50:48 -08:00
|
|
|
.setCustomAnimations(R.anim.slide_from_right, R.anim.slide_to_left, R.anim.slide_from_left, R.anim.slide_to_right)
|
|
|
|
.replace(R.id.mediasend_fragment_container, fragment, TAG_SEND)
|
|
|
|
.addToBackStack(backstackTag)
|
2018-11-20 09:59:23 -08:00
|
|
|
.commit();
|
|
|
|
}
|
|
|
|
}
|