2014-08-03 18:28:13 +00:00
|
|
|
package eu.siacs.conversations.ui;
|
|
|
|
|
2014-08-04 23:36:17 +00:00
|
|
|
import android.app.PendingIntent;
|
2014-08-03 18:28:13 +00:00
|
|
|
import android.content.Intent;
|
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.net.Uri;
|
|
|
|
import android.os.Bundle;
|
2014-08-04 23:36:17 +00:00
|
|
|
import android.view.MenuItem;
|
2014-08-03 18:28:13 +00:00
|
|
|
import android.view.View;
|
|
|
|
import android.view.View.OnClickListener;
|
2014-08-09 07:38:52 +00:00
|
|
|
import android.view.View.OnLongClickListener;
|
2014-08-03 18:28:13 +00:00
|
|
|
import android.widget.Button;
|
|
|
|
import android.widget.ImageView;
|
|
|
|
import android.widget.TextView;
|
|
|
|
import eu.siacs.conversations.R;
|
2014-08-03 18:42:05 +00:00
|
|
|
import eu.siacs.conversations.entities.Account;
|
2014-08-03 18:28:13 +00:00
|
|
|
import eu.siacs.conversations.utils.PhoneHelper;
|
2014-08-04 23:36:17 +00:00
|
|
|
import eu.siacs.conversations.xmpp.pep.Avatar;
|
2014-08-03 18:28:13 +00:00
|
|
|
|
|
|
|
public class PublishProfilePictureActivity extends XmppActivity {
|
2014-08-04 23:36:17 +00:00
|
|
|
|
2014-08-03 18:28:13 +00:00
|
|
|
private static final int REQUEST_CHOOSE_FILE = 0xac23;
|
2014-08-04 23:36:17 +00:00
|
|
|
|
2014-08-03 18:28:13 +00:00
|
|
|
private ImageView avatar;
|
2014-08-04 23:36:17 +00:00
|
|
|
private TextView accountTextView;
|
2014-08-05 10:21:08 +00:00
|
|
|
private TextView hintOrWarning;
|
2014-08-09 07:38:52 +00:00
|
|
|
private TextView secondaryHint;
|
2014-08-03 18:28:13 +00:00
|
|
|
private Button cancelButton;
|
|
|
|
private Button publishButton;
|
2014-08-04 23:36:17 +00:00
|
|
|
|
2014-08-03 18:28:13 +00:00
|
|
|
private Uri avatarUri;
|
2014-08-09 07:38:52 +00:00
|
|
|
private Uri defaultUri;
|
2014-08-04 23:36:17 +00:00
|
|
|
|
2014-08-03 18:42:05 +00:00
|
|
|
private Account account;
|
2014-08-09 08:20:16 +00:00
|
|
|
|
|
|
|
private boolean support = false;
|
2014-08-04 23:36:17 +00:00
|
|
|
|
|
|
|
private UiCallback<Avatar> avatarPublication = new UiCallback<Avatar>() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void success(Avatar object) {
|
2014-08-05 10:21:08 +00:00
|
|
|
runOnUiThread(new Runnable() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
});
|
2014-08-04 23:36:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-08-05 10:21:08 +00:00
|
|
|
public void error(final int errorCode, Avatar object) {
|
|
|
|
runOnUiThread(new Runnable() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
hintOrWarning.setText(errorCode);
|
|
|
|
hintOrWarning.setTextColor(getWarningTextColor());
|
|
|
|
publishButton.setText(R.string.publish_avatar);
|
|
|
|
enablePublishButton();
|
|
|
|
}
|
|
|
|
});
|
2014-08-04 23:36:17 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void userInputRequried(PendingIntent pi, Avatar object) {
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-08-09 07:38:52 +00:00
|
|
|
private OnLongClickListener backToDefaultListener = new OnLongClickListener() {
|
2014-08-09 07:44:19 +00:00
|
|
|
|
2014-08-09 07:38:52 +00:00
|
|
|
@Override
|
|
|
|
public boolean onLongClick(View v) {
|
|
|
|
avatarUri = defaultUri;
|
|
|
|
loadImageIntoPreview(defaultUri);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-08-03 18:28:13 +00:00
|
|
|
@Override
|
|
|
|
public void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
setContentView(R.layout.activity_publish_profile_picture);
|
|
|
|
this.avatar = (ImageView) findViewById(R.id.account_image);
|
|
|
|
this.cancelButton = (Button) findViewById(R.id.cancel_button);
|
|
|
|
this.publishButton = (Button) findViewById(R.id.publish_button);
|
2014-08-04 23:36:17 +00:00
|
|
|
this.accountTextView = (TextView) findViewById(R.id.account);
|
2014-08-05 10:21:08 +00:00
|
|
|
this.hintOrWarning = (TextView) findViewById(R.id.hint_or_warning);
|
2014-08-09 07:38:52 +00:00
|
|
|
this.secondaryHint = (TextView) findViewById(R.id.secondary_hint);
|
2014-08-03 18:28:13 +00:00
|
|
|
this.publishButton.setOnClickListener(new OnClickListener() {
|
2014-08-04 23:36:17 +00:00
|
|
|
|
2014-08-03 18:28:13 +00:00
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
2014-08-04 23:36:17 +00:00
|
|
|
if (avatarUri != null) {
|
2014-08-05 10:21:08 +00:00
|
|
|
publishButton.setText(R.string.publishing);
|
2014-08-04 23:36:17 +00:00
|
|
|
disablePublishButton();
|
|
|
|
xmppConnectionService.publishAvatar(account, avatarUri,
|
|
|
|
avatarPublication);
|
2014-08-03 18:28:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
this.cancelButton.setOnClickListener(new OnClickListener() {
|
2014-08-04 23:36:17 +00:00
|
|
|
|
2014-08-03 18:28:13 +00:00
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
this.avatar.setOnClickListener(new OnClickListener() {
|
2014-08-04 23:36:17 +00:00
|
|
|
|
2014-08-03 18:28:13 +00:00
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
Intent attachFileIntent = new Intent();
|
|
|
|
attachFileIntent.setType("image/*");
|
|
|
|
attachFileIntent.setAction(Intent.ACTION_GET_CONTENT);
|
|
|
|
Intent chooser = Intent.createChooser(attachFileIntent,
|
|
|
|
getString(R.string.attach_file));
|
|
|
|
startActivityForResult(chooser, REQUEST_CHOOSE_FILE);
|
|
|
|
}
|
|
|
|
});
|
2014-08-09 07:38:52 +00:00
|
|
|
this.defaultUri = PhoneHelper.getSefliUri(getApplicationContext());
|
2014-08-03 18:28:13 +00:00
|
|
|
}
|
2014-08-04 23:36:17 +00:00
|
|
|
|
2014-08-03 18:28:13 +00:00
|
|
|
@Override
|
|
|
|
protected void onActivityResult(int requestCode, int resultCode,
|
|
|
|
final Intent data) {
|
|
|
|
super.onActivityResult(requestCode, resultCode, data);
|
|
|
|
if (resultCode == RESULT_OK) {
|
|
|
|
if (requestCode == REQUEST_CHOOSE_FILE) {
|
|
|
|
this.avatarUri = data.getData();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-05 10:21:08 +00:00
|
|
|
|
2014-08-04 23:36:17 +00:00
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem menuItem) {
|
|
|
|
super.onOptionsItemSelected(menuItem);
|
|
|
|
switch (menuItem.getItemId()) {
|
|
|
|
case android.R.id.home:
|
|
|
|
finish();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2014-08-03 18:28:13 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onBackendConnected() {
|
2014-08-04 23:36:17 +00:00
|
|
|
if (getIntent() != null) {
|
2014-08-03 18:42:05 +00:00
|
|
|
String jid = getIntent().getStringExtra("account");
|
2014-08-04 23:36:17 +00:00
|
|
|
if (jid != null) {
|
2014-08-03 18:42:05 +00:00
|
|
|
this.account = xmppConnectionService.findAccountByJid(jid);
|
2014-08-09 08:20:16 +00:00
|
|
|
if (this.account.getXmppConnection() != null) {
|
|
|
|
this.support = this.account.getXmppConnection().getFeatures().pubsub();
|
|
|
|
}
|
2014-08-03 18:42:05 +00:00
|
|
|
if (this.avatarUri == null) {
|
2014-08-12 19:03:45 +00:00
|
|
|
if (this.account.getAvatar() != null || this.defaultUri == null) {
|
2014-08-05 11:00:06 +00:00
|
|
|
this.avatar.setImageBitmap(this.account.getImage(
|
|
|
|
getApplicationContext(), 384));
|
2014-08-09 07:38:52 +00:00
|
|
|
if (this.defaultUri != null) {
|
2014-08-12 19:03:45 +00:00
|
|
|
this.avatar
|
|
|
|
.setOnLongClickListener(this.backToDefaultListener);
|
|
|
|
} else {
|
|
|
|
this.secondaryHint.setVisibility(View.INVISIBLE);
|
2014-08-05 11:00:06 +00:00
|
|
|
}
|
2014-08-12 19:03:45 +00:00
|
|
|
if (!support) {
|
|
|
|
this.hintOrWarning.setTextColor(getWarningTextColor());
|
|
|
|
this.hintOrWarning.setText(R.string.error_publish_avatar_no_server_support);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.avatarUri = this.defaultUri;
|
|
|
|
loadImageIntoPreview(this.defaultUri);
|
|
|
|
this.secondaryHint.setVisibility(View.INVISIBLE);
|
2014-08-05 11:00:06 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
loadImageIntoPreview(avatarUri);
|
2014-08-03 18:42:05 +00:00
|
|
|
}
|
2014-08-04 23:36:17 +00:00
|
|
|
this.accountTextView.setText(this.account.getJid());
|
2014-08-03 18:42:05 +00:00
|
|
|
}
|
2014-08-03 18:28:13 +00:00
|
|
|
}
|
2014-08-04 23:36:17 +00:00
|
|
|
|
2014-08-03 18:28:13 +00:00
|
|
|
}
|
2014-08-04 23:36:17 +00:00
|
|
|
|
2014-08-03 18:28:13 +00:00
|
|
|
protected void loadImageIntoPreview(Uri uri) {
|
2014-08-04 23:36:17 +00:00
|
|
|
Bitmap bm = xmppConnectionService.getFileBackend().cropCenterSquare(
|
|
|
|
uri, 384);
|
2014-08-03 18:28:13 +00:00
|
|
|
this.avatar.setImageBitmap(bm);
|
2014-08-09 08:20:16 +00:00
|
|
|
if (support) {
|
|
|
|
enablePublishButton();
|
|
|
|
this.publishButton.setText(R.string.publish_avatar);
|
|
|
|
this.hintOrWarning.setText(R.string.publish_avatar_explanation);
|
|
|
|
this.hintOrWarning.setTextColor(getPrimaryTextColor());
|
|
|
|
} else {
|
|
|
|
disablePublishButton();
|
|
|
|
this.hintOrWarning.setTextColor(getWarningTextColor());
|
|
|
|
this.hintOrWarning.setText(R.string.error_publish_avatar_no_server_support);
|
|
|
|
}
|
2014-08-09 07:38:52 +00:00
|
|
|
if (this.defaultUri != null && uri.equals(this.defaultUri)) {
|
|
|
|
this.secondaryHint.setVisibility(View.INVISIBLE);
|
|
|
|
this.avatar.setOnLongClickListener(null);
|
2014-08-12 19:03:45 +00:00
|
|
|
} else if (this.defaultUri != null ) {
|
2014-08-09 07:38:52 +00:00
|
|
|
this.secondaryHint.setVisibility(View.VISIBLE);
|
2014-08-09 07:44:19 +00:00
|
|
|
this.avatar.setOnLongClickListener(this.backToDefaultListener);
|
2014-08-09 07:38:52 +00:00
|
|
|
}
|
2014-08-03 18:28:13 +00:00
|
|
|
}
|
2014-08-04 23:36:17 +00:00
|
|
|
|
2014-08-03 18:28:13 +00:00
|
|
|
protected void enablePublishButton() {
|
|
|
|
this.publishButton.setEnabled(true);
|
|
|
|
this.publishButton.setTextColor(getPrimaryTextColor());
|
|
|
|
}
|
2014-08-05 10:21:08 +00:00
|
|
|
|
2014-08-04 23:36:17 +00:00
|
|
|
protected void disablePublishButton() {
|
|
|
|
this.publishButton.setEnabled(false);
|
|
|
|
this.publishButton.setTextColor(getSecondaryTextColor());
|
|
|
|
}
|
2014-08-03 18:28:13 +00:00
|
|
|
|
|
|
|
}
|