PublishProfilePictureActivity: refresh on status change
This commit is contained in:
parent
ccf75ef45a
commit
9b95f1102c
|
@ -7,6 +7,8 @@ import android.graphics.Bitmap;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.support.annotation.NonNull;
|
import android.support.annotation.NonNull;
|
||||||
|
import android.support.annotation.StringRes;
|
||||||
|
import android.util.Log;
|
||||||
import android.view.Menu;
|
import android.view.Menu;
|
||||||
import android.view.MenuItem;
|
import android.view.MenuItem;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
|
@ -20,14 +22,16 @@ import com.soundcloud.android.crop.Crop;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
|
||||||
|
import eu.siacs.conversations.Config;
|
||||||
import eu.siacs.conversations.R;
|
import eu.siacs.conversations.R;
|
||||||
import eu.siacs.conversations.entities.Account;
|
import eu.siacs.conversations.entities.Account;
|
||||||
import eu.siacs.conversations.persistance.FileBackend;
|
import eu.siacs.conversations.persistance.FileBackend;
|
||||||
|
import eu.siacs.conversations.services.XmppConnectionService;
|
||||||
import eu.siacs.conversations.utils.FileUtils;
|
import eu.siacs.conversations.utils.FileUtils;
|
||||||
import eu.siacs.conversations.utils.PhoneHelper;
|
import eu.siacs.conversations.utils.PhoneHelper;
|
||||||
import eu.siacs.conversations.xmpp.pep.Avatar;
|
import eu.siacs.conversations.xmpp.pep.Avatar;
|
||||||
|
|
||||||
public class PublishProfilePictureActivity extends XmppActivity {
|
public class PublishProfilePictureActivity extends XmppActivity implements XmppConnectionService.OnAccountUpdate {
|
||||||
|
|
||||||
private static final int REQUEST_CHOOSE_FILE_AND_CROP = 0xac23;
|
private static final int REQUEST_CHOOSE_FILE_AND_CROP = 0xac23;
|
||||||
private static final int REQUEST_CHOOSE_FILE = 0xac24;
|
private static final int REQUEST_CHOOSE_FILE = 0xac24;
|
||||||
|
@ -40,6 +44,7 @@ public class PublishProfilePictureActivity extends XmppActivity {
|
||||||
private Uri defaultUri;
|
private Uri defaultUri;
|
||||||
private Account account;
|
private Account account;
|
||||||
private boolean support = false;
|
private boolean support = false;
|
||||||
|
private boolean publishing = false;
|
||||||
private OnLongClickListener backToDefaultListener = new OnLongClickListener() {
|
private OnLongClickListener backToDefaultListener = new OnLongClickListener() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -74,8 +79,8 @@ public class PublishProfilePictureActivity extends XmppActivity {
|
||||||
hintOrWarning.setText(errorCode);
|
hintOrWarning.setText(errorCode);
|
||||||
hintOrWarning.setTextColor(getWarningTextColor());
|
hintOrWarning.setTextColor(getWarningTextColor());
|
||||||
hintOrWarning.setVisibility(View.VISIBLE);
|
hintOrWarning.setVisibility(View.VISIBLE);
|
||||||
publishButton.setText(R.string.publish);
|
publishing = false;
|
||||||
enablePublishButton();
|
togglePublishButton(true,R.string.publish);
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -96,10 +101,9 @@ public class PublishProfilePictureActivity extends XmppActivity {
|
||||||
this.secondaryHint = findViewById(R.id.secondary_hint);
|
this.secondaryHint = findViewById(R.id.secondary_hint);
|
||||||
this.publishButton.setOnClickListener(v -> {
|
this.publishButton.setOnClickListener(v -> {
|
||||||
if (avatarUri != null) {
|
if (avatarUri != null) {
|
||||||
publishButton.setText(R.string.publishing);
|
publishing = true;
|
||||||
disablePublishButton();
|
togglePublishButton(false,R.string.publishing);
|
||||||
xmppConnectionService.publishAvatar(account, avatarUri,
|
xmppConnectionService.publishAvatar(account, avatarUri, avatarPublication);
|
||||||
avatarPublication);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
this.cancelButton.setOnClickListener(v -> {
|
this.cancelButton.setOnClickListener(v -> {
|
||||||
|
@ -212,36 +216,23 @@ public class PublishProfilePictureActivity extends XmppActivity {
|
||||||
protected void onBackendConnected() {
|
protected void onBackendConnected() {
|
||||||
this.account = extractAccount(getIntent());
|
this.account = extractAccount(getIntent());
|
||||||
if (this.account != null) {
|
if (this.account != null) {
|
||||||
if (this.account.getXmppConnection() != null) {
|
reloadAvatar();
|
||||||
this.support = this.account.getXmppConnection().getFeatures().pep();
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void reloadAvatar() {
|
||||||
|
this.support = this.account.getXmppConnection() != null && this.account.getXmppConnection().getFeatures().pep();
|
||||||
if (this.avatarUri == null) {
|
if (this.avatarUri == null) {
|
||||||
if (this.account.getAvatar() != null || this.defaultUri == null) {
|
if (this.account.getAvatar() != null || this.defaultUri == null) {
|
||||||
this.avatar.setImageBitmap(avatarService().get(account, getPixel(192)));
|
loadImageIntoPreview(null);
|
||||||
if (this.defaultUri != null) {
|
|
||||||
this.avatar.setOnLongClickListener(this.backToDefaultListener);
|
|
||||||
} else {
|
|
||||||
this.secondaryHint.setVisibility(View.INVISIBLE);
|
|
||||||
}
|
|
||||||
if (!support) {
|
|
||||||
this.hintOrWarning.setVisibility(View.VISIBLE);
|
|
||||||
this.hintOrWarning.setTextColor(getWarningTextColor());
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
this.avatarUri = this.defaultUri;
|
this.avatarUri = this.defaultUri;
|
||||||
loadImageIntoPreview(this.defaultUri);
|
loadImageIntoPreview(this.defaultUri);
|
||||||
this.secondaryHint.setVisibility(View.INVISIBLE);
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
loadImageIntoPreview(avatarUri);
|
loadImageIntoPreview(avatarUri);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onStart() {
|
protected void onStart() {
|
||||||
|
@ -255,15 +246,20 @@ public class PublishProfilePictureActivity extends XmppActivity {
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void loadImageIntoPreview(Uri uri) {
|
protected void loadImageIntoPreview(Uri uri) {
|
||||||
|
|
||||||
Bitmap bm = null;
|
Bitmap bm = null;
|
||||||
|
if (uri == null) {
|
||||||
|
bm = avatarService().get(account, getPixel(192));
|
||||||
|
} else {
|
||||||
try {
|
try {
|
||||||
bm = xmppConnectionService.getFileBackend().cropCenterSquare(uri, getPixel(192));
|
bm = xmppConnectionService.getFileBackend().cropCenterSquare(uri, getPixel(192));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
Log.d(Config.LOGTAG,"unable to load bitmap into image view",e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (bm == null) {
|
if (bm == null) {
|
||||||
disablePublishButton();
|
togglePublishButton(false,R.string.publish);
|
||||||
this.hintOrWarning.setVisibility(View.VISIBLE);
|
this.hintOrWarning.setVisibility(View.VISIBLE);
|
||||||
this.hintOrWarning.setTextColor(getWarningTextColor());
|
this.hintOrWarning.setTextColor(getWarningTextColor());
|
||||||
this.hintOrWarning.setText(R.string.error_publish_avatar_converting);
|
this.hintOrWarning.setText(R.string.error_publish_avatar_converting);
|
||||||
|
@ -271,11 +267,10 @@ public class PublishProfilePictureActivity extends XmppActivity {
|
||||||
}
|
}
|
||||||
this.avatar.setImageBitmap(bm);
|
this.avatar.setImageBitmap(bm);
|
||||||
if (support) {
|
if (support) {
|
||||||
enablePublishButton();
|
togglePublishButton(uri != null,R.string.publish);
|
||||||
this.publishButton.setText(R.string.publish);
|
|
||||||
this.hintOrWarning.setVisibility(View.INVISIBLE);
|
this.hintOrWarning.setVisibility(View.INVISIBLE);
|
||||||
} else {
|
} else {
|
||||||
disablePublishButton();
|
togglePublishButton(false,R.string.publish);
|
||||||
this.hintOrWarning.setVisibility(View.VISIBLE);
|
this.hintOrWarning.setVisibility(View.VISIBLE);
|
||||||
this.hintOrWarning.setTextColor(getWarningTextColor());
|
this.hintOrWarning.setTextColor(getWarningTextColor());
|
||||||
if (account.getStatus() == Account.State.ONLINE) {
|
if (account.getStatus() == Account.State.ONLINE) {
|
||||||
|
@ -284,7 +279,7 @@ public class PublishProfilePictureActivity extends XmppActivity {
|
||||||
this.hintOrWarning.setText(R.string.error_publish_avatar_offline);
|
this.hintOrWarning.setText(R.string.error_publish_avatar_offline);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (this.defaultUri != null && uri.equals(this.defaultUri)) {
|
if (this.defaultUri == null || this.defaultUri.equals(uri)) {
|
||||||
this.secondaryHint.setVisibility(View.INVISIBLE);
|
this.secondaryHint.setVisibility(View.INVISIBLE);
|
||||||
this.avatar.setOnLongClickListener(null);
|
this.avatar.setOnLongClickListener(null);
|
||||||
} else if (this.defaultUri != null) {
|
} else if (this.defaultUri != null) {
|
||||||
|
@ -293,17 +288,21 @@ public class PublishProfilePictureActivity extends XmppActivity {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void enablePublishButton() {
|
protected void togglePublishButton(boolean enabled, @StringRes int res) {
|
||||||
this.publishButton.setEnabled(true);
|
final boolean status = enabled && !publishing;
|
||||||
this.publishButton.setTextColor(getPrimaryTextColor());
|
this.publishButton.setText(publishing ? R.string.publishing : res);
|
||||||
}
|
this.publishButton.setEnabled(status);
|
||||||
|
this.publishButton.setTextColor(status ? getPrimaryTextColor() : getSecondaryTextColor());
|
||||||
protected void disablePublishButton() {
|
|
||||||
this.publishButton.setEnabled(false);
|
|
||||||
this.publishButton.setTextColor(getSecondaryTextColor());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void refreshUiReal() {
|
public void refreshUiReal() {
|
||||||
//nothing to do. This Activity doesn't implement any listeners
|
if (this.account != null) {
|
||||||
|
reloadAvatar();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onAccountUpdate() {
|
||||||
|
refreshUi();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue