avoid bundled source selection that comes with ImageCropper on Android 10

This commit is contained in:
Daniel Gultsch 2020-03-05 13:17:18 +01:00
parent 7b5d0e034e
commit b2e9a954ab
2 changed files with 40 additions and 21 deletions

View file

@ -49,6 +49,8 @@ import eu.siacs.conversations.entities.Conversation;
import eu.siacs.conversations.ui.interfaces.OnAvatarPublication; import eu.siacs.conversations.ui.interfaces.OnAvatarPublication;
import eu.siacs.conversations.ui.util.PendingItem; import eu.siacs.conversations.ui.util.PendingItem;
import static eu.siacs.conversations.ui.PublishProfilePictureActivity.REQUEST_CHOOSE_PICTURE;
public class PublishGroupChatProfilePictureActivity extends XmppActivity implements OnAvatarPublication { public class PublishGroupChatProfilePictureActivity extends XmppActivity implements OnAvatarPublication {
private final PendingItem<String> pendingConversationUuid = new PendingItem<>(); private final PendingItem<String> pendingConversationUuid = new PendingItem<>();
private ActivityPublishProfilePictureBinding binding; private ActivityPublishProfilePictureBinding binding;
@ -93,7 +95,7 @@ public class PublishGroupChatProfilePictureActivity extends XmppActivity impleme
configureActionBar(getSupportActionBar()); configureActionBar(getSupportActionBar());
this.binding.cancelButton.setOnClickListener((v) -> this.finish()); this.binding.cancelButton.setOnClickListener((v) -> this.finish());
this.binding.secondaryHint.setVisibility(View.GONE); this.binding.secondaryHint.setVisibility(View.GONE);
this.binding.accountImage.setOnClickListener((v) -> this.chooseAvatar()); this.binding.accountImage.setOnClickListener((v) -> PublishProfilePictureActivity.chooseAvatar(this));
Intent intent = getIntent(); Intent intent = getIntent();
String uuid = intent == null ? null : intent.getStringExtra("uuid"); String uuid = intent == null ? null : intent.getStringExtra("uuid");
if (uuid != null) { if (uuid != null) {
@ -113,7 +115,7 @@ public class PublishGroupChatProfilePictureActivity extends XmppActivity impleme
@Override @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) { public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) { if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data); final CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK) { if (resultCode == RESULT_OK) {
this.uri = result.getUri(); this.uri = result.getUri();
if (xmppConnectionServiceBound) { if (xmppConnectionServiceBound) {
@ -125,17 +127,13 @@ public class PublishGroupChatProfilePictureActivity extends XmppActivity impleme
Toast.makeText(this, error.getMessage(), Toast.LENGTH_SHORT).show(); Toast.makeText(this, error.getMessage(), Toast.LENGTH_SHORT).show();
} }
} }
} else if (requestCode == REQUEST_CHOOSE_PICTURE) {
if (resultCode == RESULT_OK) {
PublishProfilePictureActivity.cropUri(this, data.getData());
}
} }
} }
private void chooseAvatar() {
CropImage.activity()
.setOutputCompressFormat(Bitmap.CompressFormat.PNG)
.setAspectRatio(1, 1)
.setMinCropResultSize(Config.AVATAR_SIZE, Config.AVATAR_SIZE)
.start(this);
}
@Override @Override
public void onAvatarPublicationSucceeded() { public void onAvatarPublicationSucceeded() {
runOnUiThread(() -> { runOnUiThread(() -> {

View file

@ -1,8 +1,10 @@
package eu.siacs.conversations.ui; package eu.siacs.conversations.ui;
import android.app.Activity;
import android.content.Intent; import android.content.Intent;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.net.Uri; import android.net.Uri;
import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.StringRes; import android.support.annotation.StringRes;
import android.util.Log; import android.util.Log;
@ -26,6 +28,8 @@ import eu.siacs.conversations.utils.PhoneHelper;
public class PublishProfilePictureActivity extends XmppActivity implements XmppConnectionService.OnAccountUpdate, OnAvatarPublication { public class PublishProfilePictureActivity extends XmppActivity implements XmppConnectionService.OnAccountUpdate, OnAvatarPublication {
public static final int REQUEST_CHOOSE_PICTURE = 0x1337;
private ImageView avatar; private ImageView avatar;
private TextView hintOrWarning; private TextView hintOrWarning;
private TextView secondaryHint; private TextView secondaryHint;
@ -106,7 +110,7 @@ public class PublishProfilePictureActivity extends XmppActivity implements XmppC
} }
finish(); finish();
}); });
this.avatar.setOnClickListener(v -> chooseAvatar()); this.avatar.setOnClickListener(v -> chooseAvatar(this));
this.defaultUri = PhoneHelper.getProfilePictureUri(getApplicationContext()); this.defaultUri = PhoneHelper.getProfilePictureUri(getApplicationContext());
if (savedInstanceState != null) { if (savedInstanceState != null) {
this.avatarUri = savedInstanceState.getParcelable("uri"); this.avatarUri = savedInstanceState.getParcelable("uri");
@ -139,15 +143,28 @@ public class PublishProfilePictureActivity extends XmppActivity implements XmppC
Toast.makeText(this, error.getMessage(), Toast.LENGTH_SHORT).show(); Toast.makeText(this, error.getMessage(), Toast.LENGTH_SHORT).show();
} }
} }
} else if (requestCode == REQUEST_CHOOSE_PICTURE) {
if (resultCode == RESULT_OK) {
cropUri(this, data.getData());
}
} }
} }
private void chooseAvatar() { public static void chooseAvatar(final Activity activity) {
CropImage.activity() if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
.setOutputCompressFormat(Bitmap.CompressFormat.PNG) final Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
.setAspectRatio(1, 1) intent.setType("image/*");
.setMinCropResultSize(Config.AVATAR_SIZE, Config.AVATAR_SIZE) activity.startActivityForResult(
.start(this); Intent.createChooser(intent, activity.getString(R.string.attach_choose_picture)),
REQUEST_CHOOSE_PICTURE
);
} else {
CropImage.activity()
.setOutputCompressFormat(Bitmap.CompressFormat.PNG)
.setAspectRatio(1, 1)
.setMinCropResultSize(Config.AVATAR_SIZE, Config.AVATAR_SIZE)
.start(activity);
}
} }
@Override @Override
@ -181,10 +198,7 @@ public class PublishProfilePictureActivity extends XmppActivity implements XmppC
final Uri uri = intent != null ? intent.getData() : null; final Uri uri = intent != null ? intent.getData() : null;
if (uri != null && handledExternalUri.compareAndSet(false,true)) { if (uri != null && handledExternalUri.compareAndSet(false,true)) {
CropImage.activity(uri).setOutputCompressFormat(Bitmap.CompressFormat.PNG) cropUri(this, uri);
.setAspectRatio(1, 1)
.setMinCropResultSize(Config.AVATAR_SIZE, Config.AVATAR_SIZE)
.start(this);
return; return;
} }
@ -194,6 +208,13 @@ public class PublishProfilePictureActivity extends XmppActivity implements XmppC
configureActionBar(getSupportActionBar(), !this.mInitialAccountSetup && !handledExternalUri.get()); configureActionBar(getSupportActionBar(), !this.mInitialAccountSetup && !handledExternalUri.get());
} }
public static void cropUri(final Activity activity, final Uri uri) {
CropImage.activity(uri).setOutputCompressFormat(Bitmap.CompressFormat.PNG)
.setAspectRatio(1, 1)
.setMinCropResultSize(Config.AVATAR_SIZE, Config.AVATAR_SIZE)
.start(activity);
}
protected void loadImageIntoPreview(Uri uri) { protected void loadImageIntoPreview(Uri uri) {
Bitmap bm = null; Bitmap bm = null;