2014-10-22 16:38:44 +00:00
|
|
|
package eu.siacs.conversations.ui;
|
|
|
|
|
2020-03-05 12:17:18 +00:00
|
|
|
import android.app.Activity;
|
2014-10-22 16:38:44 +00:00
|
|
|
import android.content.Intent;
|
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.net.Uri;
|
2020-03-05 12:17:18 +00:00
|
|
|
import android.os.Build;
|
2014-10-22 16:38:44 +00:00
|
|
|
import android.os.Bundle;
|
2017-12-16 10:00:49 +00:00
|
|
|
import android.util.Log;
|
2014-10-22 16:38:44 +00:00
|
|
|
import android.view.View;
|
|
|
|
import android.view.View.OnLongClickListener;
|
|
|
|
import android.widget.Button;
|
|
|
|
import android.widget.ImageView;
|
|
|
|
import android.widget.TextView;
|
2015-01-15 15:26:46 +00:00
|
|
|
import android.widget.Toast;
|
|
|
|
|
2021-01-23 08:25:34 +00:00
|
|
|
import androidx.annotation.StringRes;
|
|
|
|
|
2018-06-18 12:15:19 +00:00
|
|
|
import com.theartofdev.edmodo.cropper.CropImage;
|
2015-11-06 10:14:05 +00:00
|
|
|
|
2019-01-13 14:28:24 +00:00
|
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
|
|
|
|
2017-12-16 10:00:49 +00:00
|
|
|
import eu.siacs.conversations.Config;
|
2014-10-22 16:38:44 +00:00
|
|
|
import eu.siacs.conversations.R;
|
|
|
|
import eu.siacs.conversations.entities.Account;
|
2017-12-16 10:00:49 +00:00
|
|
|
import eu.siacs.conversations.services.XmppConnectionService;
|
2018-06-18 12:15:19 +00:00
|
|
|
import eu.siacs.conversations.ui.interfaces.OnAvatarPublication;
|
2014-10-22 16:38:44 +00:00
|
|
|
import eu.siacs.conversations.utils.PhoneHelper;
|
|
|
|
|
2018-06-18 12:15:19 +00:00
|
|
|
public class PublishProfilePictureActivity extends XmppActivity implements XmppConnectionService.OnAccountUpdate, OnAvatarPublication {
|
|
|
|
|
2020-03-05 12:17:18 +00:00
|
|
|
public static final int REQUEST_CHOOSE_PICTURE = 0x1337;
|
|
|
|
|
2018-06-18 12:15:19 +00:00
|
|
|
private ImageView avatar;
|
|
|
|
private TextView hintOrWarning;
|
|
|
|
private TextView secondaryHint;
|
|
|
|
private Button cancelButton;
|
|
|
|
private Button publishButton;
|
|
|
|
private Uri avatarUri;
|
|
|
|
private Uri defaultUri;
|
|
|
|
private Account account;
|
|
|
|
private boolean support = false;
|
|
|
|
private boolean publishing = false;
|
2021-01-23 08:25:34 +00:00
|
|
|
private final AtomicBoolean handledExternalUri = new AtomicBoolean(false);
|
|
|
|
private final OnLongClickListener backToDefaultListener = new OnLongClickListener() {
|
2018-06-18 12:15:19 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onLongClick(View v) {
|
|
|
|
avatarUri = defaultUri;
|
|
|
|
loadImageIntoPreview(defaultUri);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
private boolean mInitialAccountSetup;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAvatarPublicationSucceeded() {
|
|
|
|
runOnUiThread(() -> {
|
|
|
|
if (mInitialAccountSetup) {
|
|
|
|
Intent intent = new Intent(getApplicationContext(), StartConversationActivity.class);
|
2018-10-09 17:35:33 +00:00
|
|
|
StartConversationActivity.addInviteUri(intent, getIntent());
|
2018-06-18 12:15:19 +00:00
|
|
|
intent.putExtra("init", true);
|
2020-01-09 16:39:55 +00:00
|
|
|
intent.putExtra(EXTRA_ACCOUNT, account.getJid().asBareJid().toEscapedString());
|
2018-06-18 12:15:19 +00:00
|
|
|
startActivity(intent);
|
|
|
|
}
|
|
|
|
Toast.makeText(PublishProfilePictureActivity.this,
|
|
|
|
R.string.avatar_has_been_published,
|
|
|
|
Toast.LENGTH_SHORT).show();
|
|
|
|
finish();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAvatarPublicationFailed(int res) {
|
|
|
|
runOnUiThread(() -> {
|
|
|
|
hintOrWarning.setText(res);
|
2019-04-08 14:32:10 +00:00
|
|
|
hintOrWarning.setTextAppearance(this,R.style.TextAppearance_Conversations_Body1_Warning);
|
2018-06-18 12:15:19 +00:00
|
|
|
hintOrWarning.setVisibility(View.VISIBLE);
|
|
|
|
publishing = false;
|
|
|
|
togglePublishButton(true, R.string.publish);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
setContentView(R.layout.activity_publish_profile_picture);
|
|
|
|
setSupportActionBar(findViewById(R.id.toolbar));
|
|
|
|
|
|
|
|
this.avatar = findViewById(R.id.account_image);
|
|
|
|
this.cancelButton = findViewById(R.id.cancel_button);
|
|
|
|
this.publishButton = findViewById(R.id.publish_button);
|
|
|
|
this.hintOrWarning = findViewById(R.id.hint_or_warning);
|
|
|
|
this.secondaryHint = findViewById(R.id.secondary_hint);
|
|
|
|
this.publishButton.setOnClickListener(v -> {
|
|
|
|
if (avatarUri != null) {
|
|
|
|
publishing = true;
|
|
|
|
togglePublishButton(false, R.string.publishing);
|
|
|
|
xmppConnectionService.publishAvatar(account, avatarUri, this);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
this.cancelButton.setOnClickListener(v -> {
|
|
|
|
if (mInitialAccountSetup) {
|
2020-01-09 14:49:49 +00:00
|
|
|
final Intent intent = new Intent(getApplicationContext(), StartConversationActivity.class);
|
2018-06-18 12:15:19 +00:00
|
|
|
if (xmppConnectionService != null && xmppConnectionService.getAccounts().size() == 1) {
|
|
|
|
intent.putExtra("init", true);
|
|
|
|
}
|
2020-01-09 14:49:49 +00:00
|
|
|
StartConversationActivity.addInviteUri(intent, getIntent());
|
2020-01-09 16:39:55 +00:00
|
|
|
intent.putExtra(EXTRA_ACCOUNT, account.getJid().asBareJid().toEscapedString());
|
2018-06-18 12:15:19 +00:00
|
|
|
startActivity(intent);
|
|
|
|
}
|
|
|
|
finish();
|
|
|
|
});
|
2020-03-05 12:17:18 +00:00
|
|
|
this.avatar.setOnClickListener(v -> chooseAvatar(this));
|
2018-06-18 12:15:19 +00:00
|
|
|
this.defaultUri = PhoneHelper.getProfilePictureUri(getApplicationContext());
|
2019-01-13 14:28:24 +00:00
|
|
|
if (savedInstanceState != null) {
|
|
|
|
this.avatarUri = savedInstanceState.getParcelable("uri");
|
|
|
|
this.handledExternalUri.set(savedInstanceState.getBoolean("handle_external_uri",false));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onSaveInstanceState(Bundle outState) {
|
|
|
|
if (this.avatarUri != null) {
|
|
|
|
outState.putParcelable("uri", this.avatarUri);
|
|
|
|
}
|
|
|
|
outState.putBoolean("handle_external_uri", handledExternalUri.get());
|
|
|
|
super.onSaveInstanceState(outState);
|
2018-06-18 12:15:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onActivityResult(int requestCode, int resultCode, Intent data) {
|
2021-01-29 20:25:00 +00:00
|
|
|
super.onActivityResult(requestCode, resultCode, data);
|
2018-06-18 12:15:19 +00:00
|
|
|
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
|
|
|
|
CropImage.ActivityResult result = CropImage.getActivityResult(data);
|
|
|
|
if (resultCode == RESULT_OK) {
|
|
|
|
this.avatarUri = result.getUri();
|
|
|
|
if (xmppConnectionServiceBound) {
|
|
|
|
loadImageIntoPreview(this.avatarUri);
|
|
|
|
}
|
|
|
|
} else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
|
|
|
|
Exception error = result.getError();
|
|
|
|
if (error != null) {
|
|
|
|
Toast.makeText(this, error.getMessage(), Toast.LENGTH_SHORT).show();
|
|
|
|
}
|
|
|
|
}
|
2020-03-05 12:17:18 +00:00
|
|
|
} else if (requestCode == REQUEST_CHOOSE_PICTURE) {
|
|
|
|
if (resultCode == RESULT_OK) {
|
|
|
|
cropUri(this, data.getData());
|
|
|
|
}
|
2018-06-18 12:15:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-05 12:17:18 +00:00
|
|
|
public static void chooseAvatar(final Activity activity) {
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
|
|
|
final Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
|
|
|
|
intent.setType("image/*");
|
|
|
|
activity.startActivityForResult(
|
|
|
|
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);
|
|
|
|
}
|
2018-06-18 12:15:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onBackendConnected() {
|
|
|
|
this.account = extractAccount(getIntent());
|
|
|
|
if (this.account != null) {
|
|
|
|
reloadAvatar();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void reloadAvatar() {
|
|
|
|
this.support = this.account.getXmppConnection() != null && this.account.getXmppConnection().getFeatures().pep();
|
|
|
|
if (this.avatarUri == null) {
|
|
|
|
if (this.account.getAvatar() != null || this.defaultUri == null) {
|
|
|
|
loadImageIntoPreview(null);
|
|
|
|
} else {
|
|
|
|
this.avatarUri = this.defaultUri;
|
|
|
|
loadImageIntoPreview(this.defaultUri);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
loadImageIntoPreview(avatarUri);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onStart() {
|
|
|
|
super.onStart();
|
2018-10-23 19:25:38 +00:00
|
|
|
final Intent intent = getIntent();
|
|
|
|
this.mInitialAccountSetup = intent != null && intent.getBooleanExtra("setup", false);
|
|
|
|
|
2019-01-13 14:28:24 +00:00
|
|
|
final Uri uri = intent != null ? intent.getData() : null;
|
|
|
|
|
|
|
|
if (uri != null && handledExternalUri.compareAndSet(false,true)) {
|
2020-03-05 12:17:18 +00:00
|
|
|
cropUri(this, uri);
|
2019-01-13 14:28:24 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-18 12:15:19 +00:00
|
|
|
if (this.mInitialAccountSetup) {
|
|
|
|
this.cancelButton.setText(R.string.skip);
|
|
|
|
}
|
2019-01-13 14:28:24 +00:00
|
|
|
configureActionBar(getSupportActionBar(), !this.mInitialAccountSetup && !handledExternalUri.get());
|
2018-06-18 12:15:19 +00:00
|
|
|
}
|
|
|
|
|
2020-03-05 12:17:18 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2018-06-18 12:15:19 +00:00
|
|
|
protected void loadImageIntoPreview(Uri uri) {
|
|
|
|
|
|
|
|
Bitmap bm = null;
|
|
|
|
if (uri == null) {
|
2018-06-23 17:00:23 +00:00
|
|
|
bm = avatarService().get(account, (int) getResources().getDimension(R.dimen.publish_avatar_size));
|
2018-06-18 12:15:19 +00:00
|
|
|
} else {
|
|
|
|
try {
|
2018-06-23 17:00:23 +00:00
|
|
|
bm = xmppConnectionService.getFileBackend().cropCenterSquare(uri, (int) getResources().getDimension(R.dimen.publish_avatar_size));
|
2018-06-18 12:15:19 +00:00
|
|
|
} catch (Exception e) {
|
|
|
|
Log.d(Config.LOGTAG, "unable to load bitmap into image view", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bm == null) {
|
|
|
|
togglePublishButton(false, R.string.publish);
|
|
|
|
this.hintOrWarning.setVisibility(View.VISIBLE);
|
2019-04-08 14:32:10 +00:00
|
|
|
this.hintOrWarning.setTextAppearance(this,R.style.TextAppearance_Conversations_Body1_Warning);
|
2018-06-18 12:15:19 +00:00
|
|
|
this.hintOrWarning.setText(R.string.error_publish_avatar_converting);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.avatar.setImageBitmap(bm);
|
|
|
|
if (support) {
|
|
|
|
togglePublishButton(uri != null, R.string.publish);
|
|
|
|
this.hintOrWarning.setVisibility(View.INVISIBLE);
|
|
|
|
} else {
|
|
|
|
togglePublishButton(false, R.string.publish);
|
|
|
|
this.hintOrWarning.setVisibility(View.VISIBLE);
|
2019-04-08 14:32:10 +00:00
|
|
|
this.hintOrWarning.setTextAppearance(this,R.style.TextAppearance_Conversations_Body1_Warning);
|
2018-06-18 12:15:19 +00:00
|
|
|
if (account.getStatus() == Account.State.ONLINE) {
|
|
|
|
this.hintOrWarning.setText(R.string.error_publish_avatar_no_server_support);
|
|
|
|
} else {
|
|
|
|
this.hintOrWarning.setText(R.string.error_publish_avatar_offline);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (this.defaultUri == null || this.defaultUri.equals(uri)) {
|
|
|
|
this.secondaryHint.setVisibility(View.INVISIBLE);
|
|
|
|
this.avatar.setOnLongClickListener(null);
|
|
|
|
} else if (this.defaultUri != null) {
|
|
|
|
this.secondaryHint.setVisibility(View.VISIBLE);
|
|
|
|
this.avatar.setOnLongClickListener(this.backToDefaultListener);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void togglePublishButton(boolean enabled, @StringRes int res) {
|
|
|
|
final boolean status = enabled && !publishing;
|
|
|
|
this.publishButton.setText(publishing ? R.string.publishing : res);
|
|
|
|
this.publishButton.setEnabled(status);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void refreshUiReal() {
|
|
|
|
if (this.account != null) {
|
|
|
|
reloadAvatar();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAccountUpdate() {
|
|
|
|
refreshUi();
|
|
|
|
}
|
2014-10-22 16:38:44 +00:00
|
|
|
|
|
|
|
}
|