mirror of
https://github.com/signalapp/Signal-Android.git
synced 2026-04-20 16:49:40 +01:00
Wallpaper image selection and cropping.
This commit is contained in:
committed by
Greyson Parrelli
parent
b5712f4bd1
commit
a8ad1e718e
@@ -2,7 +2,6 @@ package org.thoughtcrime.securesms.wallpaper;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
@@ -20,11 +19,11 @@ import com.google.android.flexbox.JustifyContent;
|
||||
|
||||
import org.thoughtcrime.securesms.R;
|
||||
import org.thoughtcrime.securesms.util.ActivityTransitionUtil;
|
||||
import org.thoughtcrime.securesms.wallpaper.crop.WallpaperImageSelectionActivity;
|
||||
|
||||
public class ChatWallpaperSelectionFragment extends Fragment {
|
||||
|
||||
private static final short CHOOSE_PHOTO = 1;
|
||||
private static final short PREVIEW = 2;
|
||||
private static final short CHOOSE_WALLPAPER = 1;
|
||||
|
||||
private ChatWallpaperViewModel viewModel;
|
||||
|
||||
@@ -40,13 +39,12 @@ public class ChatWallpaperSelectionFragment extends Fragment {
|
||||
FlexboxLayoutManager flexboxLayoutManager = new FlexboxLayoutManager(requireContext());
|
||||
|
||||
chooseFromPhotos.setOnClickListener(unused -> {
|
||||
// Navigate to photo selection (akin to what we did for profile avatar selection.)
|
||||
//startActivityForResult(..., CHOOSE_PHOTO);
|
||||
startActivityForResult(WallpaperImageSelectionActivity.getIntent(requireContext(), viewModel.getRecipientId()), CHOOSE_WALLPAPER);
|
||||
});
|
||||
|
||||
@SuppressWarnings("CodeBlock2Expr")
|
||||
ChatWallpaperSelectionAdapter adapter = new ChatWallpaperSelectionAdapter(chatWallpaper -> {
|
||||
startActivityForResult(ChatWallpaperPreviewActivity.create(requireActivity(), chatWallpaper, viewModel.getDimInDarkTheme().getValue()), PREVIEW);
|
||||
startActivityForResult(ChatWallpaperPreviewActivity.create(requireActivity(), chatWallpaper, viewModel.getDimInDarkTheme().getValue()), CHOOSE_WALLPAPER);
|
||||
ActivityTransitionUtil.setSlideInTransition(requireActivity());
|
||||
});
|
||||
|
||||
@@ -60,17 +58,7 @@ public class ChatWallpaperSelectionFragment extends Fragment {
|
||||
|
||||
@Override
|
||||
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
|
||||
if (requestCode == CHOOSE_PHOTO && resultCode == Activity.RESULT_OK && data != null) {
|
||||
Uri uri = data.getData();
|
||||
if (uri == null || uri == Uri.EMPTY) {
|
||||
throw new AssertionError("Should never have an empty uri.");
|
||||
} else {
|
||||
ChatWallpaper wallpaper = ChatWallpaperFactory.create(uri);
|
||||
viewModel.setWallpaper(wallpaper);
|
||||
viewModel.saveWallpaperSelection();
|
||||
Navigation.findNavController(requireView()).popBackStack();
|
||||
}
|
||||
} else if (requestCode == PREVIEW && resultCode == Activity.RESULT_OK && data != null) {
|
||||
if (requestCode == CHOOSE_WALLPAPER && resultCode == Activity.RESULT_OK && data != null) {
|
||||
ChatWallpaper chatWallpaper = data.getParcelableExtra(ChatWallpaperPreviewActivity.EXTRA_CHAT_WALLPAPER);
|
||||
viewModel.setWallpaper(chatWallpaper);
|
||||
viewModel.saveWallpaperSelection();
|
||||
|
||||
@@ -1,19 +1,30 @@
|
||||
package org.thoughtcrime.securesms.wallpaper;
|
||||
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.net.Uri;
|
||||
import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
import android.widget.ImageView;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import com.bumptech.glide.load.DataSource;
|
||||
import com.bumptech.glide.load.engine.GlideException;
|
||||
import com.bumptech.glide.request.RequestListener;
|
||||
import com.bumptech.glide.request.target.Target;
|
||||
|
||||
import org.signal.core.util.logging.Log;
|
||||
import org.thoughtcrime.securesms.database.model.databaseprotos.Wallpaper;
|
||||
import org.thoughtcrime.securesms.mms.DecryptableStreamUriLoader;
|
||||
import org.thoughtcrime.securesms.mms.GlideApp;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
final class UriChatWallpaper implements ChatWallpaper, Parcelable {
|
||||
|
||||
private static final String TAG = Log.tag(UriChatWallpaper.class);
|
||||
|
||||
private final Uri uri;
|
||||
private final float dimLevelInDarkTheme;
|
||||
|
||||
@@ -30,7 +41,20 @@ final class UriChatWallpaper implements ChatWallpaper, Parcelable {
|
||||
@Override
|
||||
public void loadInto(@NonNull ImageView imageView) {
|
||||
GlideApp.with(imageView)
|
||||
.load(uri)
|
||||
.load(new DecryptableStreamUriLoader.DecryptableUri(uri))
|
||||
.addListener(new RequestListener<Drawable>() {
|
||||
@Override
|
||||
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
|
||||
Log.w(TAG, "Failed to load wallpaper " + uri);
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
|
||||
Log.i(TAG, "Loaded wallpaper " + uri);
|
||||
return false;
|
||||
}
|
||||
})
|
||||
.into(imageView);
|
||||
}
|
||||
|
||||
@@ -59,7 +83,7 @@ final class UriChatWallpaper implements ChatWallpaper, Parcelable {
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
UriChatWallpaper that = (UriChatWallpaper) o;
|
||||
return Float.compare(that.dimLevelInDarkTheme, dimLevelInDarkTheme) == 0 &&
|
||||
uri.equals(that.uri);
|
||||
uri.equals(that.uri);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,210 @@
|
||||
package org.thoughtcrime.securesms.wallpaper.crop;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.graphics.Point;
|
||||
import android.graphics.PorterDuff;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.util.DisplayMetrics;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.annotation.StyleRes;
|
||||
import androidx.appcompat.app.ActionBar;
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
import androidx.appcompat.widget.SwitchCompat;
|
||||
import androidx.appcompat.widget.Toolbar;
|
||||
import androidx.core.content.ContextCompat;
|
||||
import androidx.lifecycle.ViewModelProviders;
|
||||
|
||||
import org.signal.core.util.logging.Log;
|
||||
import org.thoughtcrime.securesms.BaseActivity;
|
||||
import org.thoughtcrime.securesms.R;
|
||||
import org.thoughtcrime.securesms.imageeditor.ImageEditorView;
|
||||
import org.thoughtcrime.securesms.imageeditor.model.EditorElement;
|
||||
import org.thoughtcrime.securesms.imageeditor.model.EditorModel;
|
||||
import org.thoughtcrime.securesms.imageeditor.renderers.FaceBlurRenderer;
|
||||
import org.thoughtcrime.securesms.recipients.RecipientId;
|
||||
import org.thoughtcrime.securesms.scribbles.UriGlideRenderer;
|
||||
import org.thoughtcrime.securesms.util.AsynchronousCallback;
|
||||
import org.thoughtcrime.securesms.util.DynamicTheme;
|
||||
import org.thoughtcrime.securesms.util.views.SimpleProgressDialog;
|
||||
import org.thoughtcrime.securesms.wallpaper.ChatWallpaper;
|
||||
import org.thoughtcrime.securesms.wallpaper.ChatWallpaperPreviewActivity;
|
||||
|
||||
import java.util.Locale;
|
||||
import java.util.Objects;
|
||||
|
||||
public final class WallpaperCropActivity extends BaseActivity {
|
||||
|
||||
private static final String TAG = Log.tag(WallpaperCropActivity.class);
|
||||
|
||||
private static final String EXTRA_RECIPIENT_ID = "RECIPIENT_ID";
|
||||
private static final String EXTRA_IMAGE_URI = "IMAGE_URI";
|
||||
|
||||
private final DynamicTheme dynamicTheme = new DynamicWallpaperTheme();
|
||||
|
||||
private ImageEditorView imageEditor;
|
||||
private WallpaperCropViewModel viewModel;
|
||||
|
||||
public static Intent newIntent(@NonNull Context context,
|
||||
@Nullable RecipientId recipientId,
|
||||
@NonNull Uri imageUri)
|
||||
{
|
||||
Intent intent = new Intent(context, WallpaperCropActivity.class);
|
||||
intent.putExtra(EXTRA_RECIPIENT_ID, recipientId);
|
||||
intent.putExtra(EXTRA_IMAGE_URI, Objects.requireNonNull(imageUri));
|
||||
return intent;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
dynamicTheme.onCreate(this);
|
||||
setContentView(R.layout.chat_wallpaper_crop_activity);
|
||||
|
||||
RecipientId recipientId = getIntent().getParcelableExtra(EXTRA_RECIPIENT_ID);
|
||||
Uri inputImage = Objects.requireNonNull(getIntent().getParcelableExtra(EXTRA_IMAGE_URI));
|
||||
|
||||
Log.i(TAG, "Cropping wallpaper for " + (recipientId == null ? "default wallpaper" : recipientId));
|
||||
|
||||
WallpaperCropViewModel.Factory factory = new WallpaperCropViewModel.Factory(recipientId);
|
||||
viewModel = ViewModelProviders.of(this, factory).get(WallpaperCropViewModel.class);
|
||||
|
||||
imageEditor = findViewById(R.id.image_editor);
|
||||
View receivedBubble = findViewById(R.id.preview_bubble_1);
|
||||
TextView bubble2Text = findViewById(R.id.chat_wallpaper_bubble2_text);
|
||||
View setWallPaper = findViewById(R.id.preview_set_wallpaper);
|
||||
SwitchCompat blur = findViewById(R.id.preview_blur);
|
||||
|
||||
setupImageEditor(inputImage);
|
||||
|
||||
setWallPaper.setOnClickListener(v -> setWallpaper());
|
||||
|
||||
Toolbar toolbar = findViewById(R.id.toolbar);
|
||||
setSupportActionBar(toolbar);
|
||||
ActionBar supportActionBar = Objects.requireNonNull(getSupportActionBar());
|
||||
supportActionBar.setHomeAsUpIndicator(ContextCompat.getDrawable(this, R.drawable.ic_arrow_left_24));
|
||||
supportActionBar.setDisplayHomeAsUpEnabled(true);
|
||||
|
||||
blur.setOnCheckedChangeListener((v, checked) -> viewModel.setBlur(checked));
|
||||
|
||||
viewModel.getBlur()
|
||||
.observe(this, blurred -> {
|
||||
setBlurred(blurred);
|
||||
if (blurred != blur.isChecked()) {
|
||||
blur.setChecked(blurred);
|
||||
}
|
||||
});
|
||||
|
||||
viewModel.getRecipient()
|
||||
.observe(this, r -> {
|
||||
if (r.getId().isUnknown()) {
|
||||
bubble2Text.setText(R.string.WallpaperCropActivity__set_wallpaper_for_all_chats);
|
||||
} else {
|
||||
bubble2Text.setText(getString(R.string.WallpaperCropActivity__set_wallpaper_for_s, r.getDisplayName(this)));
|
||||
receivedBubble.getBackground().setColorFilter(r.getColor().toConversationColor(this), PorterDuff.Mode.SRC_IN);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
dynamicTheme.onResume(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
|
||||
if (super.onOptionsItemSelected(item)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
int itemId = item.getItemId();
|
||||
|
||||
if (itemId == android.R.id.home) {
|
||||
finish();
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private void setWallpaper() {
|
||||
EditorModel model = imageEditor.getModel();
|
||||
|
||||
Point size = new Point(imageEditor.getWidth(), imageEditor.getHeight());
|
||||
|
||||
AlertDialog dialog = SimpleProgressDialog.show(this);
|
||||
viewModel.render(this, model, size,
|
||||
new AsynchronousCallback.MainThread<ChatWallpaper, WallpaperCropViewModel.Error>() {
|
||||
@Override public void onComplete(@Nullable ChatWallpaper result) {
|
||||
dialog.dismiss();
|
||||
setResult(RESULT_OK, new Intent().putExtra(ChatWallpaperPreviewActivity.EXTRA_CHAT_WALLPAPER, result));
|
||||
finish();
|
||||
}
|
||||
|
||||
@Override public void onError(@Nullable WallpaperCropViewModel.Error error) {
|
||||
dialog.dismiss();
|
||||
Toast.makeText(WallpaperCropActivity.this, R.string.WallpaperCropActivity__error_setting_wallpaper, Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}.toWorkerCallback());
|
||||
}
|
||||
|
||||
private void setupImageEditor(@NonNull Uri imageUri) {
|
||||
DisplayMetrics displayMetrics = new DisplayMetrics();
|
||||
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
|
||||
int height = displayMetrics.heightPixels;
|
||||
int width = displayMetrics.widthPixels;
|
||||
float ratio = width / (float) height;
|
||||
|
||||
EditorModel editorModel = EditorModel.createForWallpaperEditing(ratio);
|
||||
|
||||
EditorElement image = new EditorElement(new UriGlideRenderer(imageUri, true, width, height, UriGlideRenderer.WEAK_BLUR));
|
||||
image.getFlags()
|
||||
.setSelectable(false)
|
||||
.persist();
|
||||
|
||||
editorModel.addElement(image);
|
||||
|
||||
imageEditor.setModel(editorModel);
|
||||
|
||||
imageEditor.setSizeChangedListener((newWidth, newHeight) -> {
|
||||
float newRatio = newWidth / (float) newHeight;
|
||||
Log.i(TAG, String.format(Locale.US, "Output size (%d, %d) (ratio %.2f)", newWidth, newHeight, newRatio));
|
||||
|
||||
editorModel.setFixedRatio(newRatio);
|
||||
});
|
||||
}
|
||||
|
||||
private void setBlurred(boolean blurred) {
|
||||
imageEditor.getModel().clearFaceRenderers();
|
||||
|
||||
if (blurred) {
|
||||
EditorElement mainImage = imageEditor.getModel().getMainImage();
|
||||
|
||||
if (mainImage != null) {
|
||||
EditorElement element = new EditorElement(new FaceBlurRenderer(), EditorModel.Z_MASK);
|
||||
|
||||
element.getFlags()
|
||||
.setEditable(false)
|
||||
.setSelectable(false)
|
||||
.persist();
|
||||
|
||||
mainImage.addElement(element);
|
||||
imageEditor.invalidate();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static final class DynamicWallpaperTheme extends DynamicTheme {
|
||||
protected @StyleRes int getTheme() {
|
||||
return R.style.Signal_DayNight_WallpaperCropper;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package org.thoughtcrime.securesms.wallpaper.crop;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.annotation.WorkerThread;
|
||||
|
||||
import org.signal.core.util.logging.Log;
|
||||
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
||||
import org.thoughtcrime.securesms.dependencies.ApplicationDependencies;
|
||||
import org.thoughtcrime.securesms.keyvalue.SignalStore;
|
||||
import org.thoughtcrime.securesms.recipients.RecipientId;
|
||||
import org.thoughtcrime.securesms.wallpaper.ChatWallpaper;
|
||||
import org.thoughtcrime.securesms.wallpaper.WallpaperStorage;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
final class WallpaperCropRepository {
|
||||
|
||||
private static final String TAG = Log.tag(WallpaperCropRepository.class);
|
||||
|
||||
@Nullable private final RecipientId recipientId;
|
||||
private final Context context;
|
||||
|
||||
public WallpaperCropRepository(@Nullable RecipientId recipientId) {
|
||||
this.context = ApplicationDependencies.getApplication();
|
||||
this.recipientId = recipientId;
|
||||
}
|
||||
|
||||
@WorkerThread
|
||||
@NonNull ChatWallpaper setWallPaper(byte[] bytes) throws IOException {
|
||||
try (InputStream inputStream = new ByteArrayInputStream(bytes)) {
|
||||
ChatWallpaper wallpaper = WallpaperStorage.save(context, inputStream);
|
||||
|
||||
if (recipientId != null) {
|
||||
Log.i(TAG, "Setting image wallpaper for " + recipientId);
|
||||
DatabaseFactory.getRecipientDatabase(context).setWallpaper(recipientId, wallpaper);
|
||||
} else {
|
||||
Log.i(TAG, "Setting image wallpaper for default");
|
||||
SignalStore.wallpaper().setWallpaper(context, wallpaper);
|
||||
}
|
||||
|
||||
return wallpaper;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
package org.thoughtcrime.securesms.wallpaper.crop;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.Point;
|
||||
|
||||
import androidx.annotation.MainThread;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.lifecycle.LiveData;
|
||||
import androidx.lifecycle.MutableLiveData;
|
||||
import androidx.lifecycle.Transformations;
|
||||
import androidx.lifecycle.ViewModel;
|
||||
import androidx.lifecycle.ViewModelProvider;
|
||||
|
||||
import org.signal.core.util.concurrent.SignalExecutors;
|
||||
import org.signal.core.util.logging.Log;
|
||||
import org.thoughtcrime.securesms.imageeditor.model.EditorModel;
|
||||
import org.thoughtcrime.securesms.recipients.Recipient;
|
||||
import org.thoughtcrime.securesms.recipients.RecipientId;
|
||||
import org.thoughtcrime.securesms.util.AsynchronousCallback;
|
||||
import org.thoughtcrime.securesms.util.BitmapUtil;
|
||||
import org.thoughtcrime.securesms.util.livedata.LiveDataUtil;
|
||||
import org.thoughtcrime.securesms.wallpaper.ChatWallpaper;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Objects;
|
||||
|
||||
final class WallpaperCropViewModel extends ViewModel {
|
||||
|
||||
private static final String TAG = Log.tag(WallpaperCropViewModel.class);
|
||||
|
||||
private final @NonNull WallpaperCropRepository repository;
|
||||
private final @NonNull MutableLiveData<Boolean> blur;
|
||||
private final @NonNull LiveData<Recipient> recipient;
|
||||
|
||||
public WallpaperCropViewModel(@Nullable RecipientId recipientId,
|
||||
@NonNull WallpaperCropRepository repository)
|
||||
{
|
||||
this.repository = repository;
|
||||
this.blur = new MutableLiveData<>(false);
|
||||
this.recipient = recipientId != null ? Recipient.live(recipientId).getLiveData() : LiveDataUtil.just(Recipient.UNKNOWN);
|
||||
}
|
||||
|
||||
void render(@NonNull Context context,
|
||||
@NonNull EditorModel model,
|
||||
@NonNull Point size,
|
||||
@NonNull AsynchronousCallback.WorkerThread<ChatWallpaper, Error> callback)
|
||||
{
|
||||
SignalExecutors.BOUNDED.execute(
|
||||
() -> {
|
||||
Bitmap bitmap = model.render(context, size);
|
||||
try {
|
||||
ChatWallpaper chatWallpaper = repository.setWallPaper(BitmapUtil.toWebPByteArray(bitmap));
|
||||
callback.onComplete(chatWallpaper);
|
||||
} catch (IOException e) {
|
||||
Log.w(TAG, e);
|
||||
callback.onError(Error.SAVING);
|
||||
} finally {
|
||||
bitmap.recycle();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
LiveData<Boolean> getBlur() {
|
||||
return Transformations.distinctUntilChanged(blur);
|
||||
}
|
||||
|
||||
LiveData<Recipient> getRecipient() {
|
||||
return recipient;
|
||||
}
|
||||
|
||||
@MainThread
|
||||
void setBlur(boolean blur) {
|
||||
this.blur.setValue(blur);
|
||||
}
|
||||
|
||||
public static class Factory implements ViewModelProvider.Factory {
|
||||
|
||||
private final RecipientId recipientId;
|
||||
|
||||
public Factory(@Nullable RecipientId recipientId) {
|
||||
this.recipientId = recipientId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
|
||||
|
||||
WallpaperCropRepository wallpaperCropRepository = new WallpaperCropRepository(recipientId);
|
||||
|
||||
return Objects.requireNonNull(modelClass.cast(new WallpaperCropViewModel(recipientId, wallpaperCropRepository)));
|
||||
}
|
||||
}
|
||||
|
||||
enum Error {
|
||||
SAVING
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
package org.thoughtcrime.securesms.wallpaper.crop;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.appcompat.app.AppCompatDelegate;
|
||||
|
||||
import org.thoughtcrime.securesms.R;
|
||||
import org.thoughtcrime.securesms.mediasend.Media;
|
||||
import org.thoughtcrime.securesms.mediasend.MediaFolder;
|
||||
import org.thoughtcrime.securesms.mediasend.MediaPickerFolderFragment;
|
||||
import org.thoughtcrime.securesms.mediasend.MediaPickerItemFragment;
|
||||
import org.thoughtcrime.securesms.recipients.RecipientId;
|
||||
|
||||
public final class WallpaperImageSelectionActivity extends AppCompatActivity
|
||||
implements MediaPickerFolderFragment.Controller,
|
||||
MediaPickerItemFragment.Controller
|
||||
{
|
||||
private static final String EXTRA_RECIPIENT_ID = "RECIPIENT_ID";
|
||||
private static final int CROP = 901;
|
||||
|
||||
public static Intent getIntent(@NonNull Context context,
|
||||
@Nullable RecipientId recipientId)
|
||||
{
|
||||
Intent intent = new Intent(context, WallpaperImageSelectionActivity.class);
|
||||
intent.putExtra(EXTRA_RECIPIENT_ID, recipientId);
|
||||
return intent;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void attachBaseContext(@NonNull Context newBase) {
|
||||
getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_YES);
|
||||
super.attachBaseContext(newBase);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.wallpaper_image_selection_activity);
|
||||
|
||||
getSupportFragmentManager().beginTransaction()
|
||||
.replace(R.id.fragment_container, MediaPickerFolderFragment.newInstance(getString(R.string.WallpaperImageSelectionActivity__choose_wallpaper_image), true))
|
||||
.commit();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFolderSelected(@NonNull MediaFolder folder) {
|
||||
getSupportFragmentManager().beginTransaction()
|
||||
.replace(R.id.fragment_container, MediaPickerItemFragment.newInstance(folder.getBucketId(), folder.getTitle(), 1, false, true))
|
||||
.addToBackStack(null)
|
||||
.commit();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCameraSelected() {
|
||||
throw new AssertionError("Unexpected, Camera disabled");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMediaSelected(@NonNull Media media) {
|
||||
startActivityForResult(WallpaperCropActivity.newIntent(this, getRecipientId(), media.getUri()), CROP);
|
||||
}
|
||||
|
||||
private RecipientId getRecipientId() {
|
||||
return getIntent().getParcelableExtra(EXTRA_RECIPIENT_ID);
|
||||
}
|
||||
|
||||
@Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
|
||||
super.onActivityResult(requestCode, resultCode, data);
|
||||
|
||||
if (requestCode == CROP && resultCode == RESULT_OK) {
|
||||
setResult(RESULT_OK, data);
|
||||
finish();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user