1package eu.siacs.conversations.ui;
2
3import android.content.Intent;
4import android.graphics.Bitmap;
5import android.net.Uri;
6import android.os.Bundle;
7import android.util.Log;
8import android.view.View;
9import android.view.View.OnClickListener;
10import android.widget.Button;
11import android.widget.ImageView;
12import android.widget.TextView;
13import eu.siacs.conversations.R;
14import eu.siacs.conversations.entities.Account;
15import eu.siacs.conversations.utils.PhoneHelper;
16
17public class PublishProfilePictureActivity extends XmppActivity {
18
19 private static final int REQUEST_CHOOSE_FILE = 0xac23;
20
21 private ImageView avatar;
22 private TextView explanation;
23 private Button cancelButton;
24 private Button publishButton;
25
26 private Uri avatarUri;
27
28 private Account account;
29
30 @Override
31 public void onCreate(Bundle savedInstanceState) {
32 super.onCreate(savedInstanceState);
33 setContentView(R.layout.activity_publish_profile_picture);
34 this.avatar = (ImageView) findViewById(R.id.account_image);
35 this.explanation = (TextView) findViewById(R.id.explanation);
36 this.cancelButton = (Button) findViewById(R.id.cancel_button);
37 this.publishButton = (Button) findViewById(R.id.publish_button);
38 this.publishButton.setOnClickListener(new OnClickListener() {
39
40 @Override
41 public void onClick(View v) {
42 if (avatarUri!=null) {
43 xmppConnectionService.pushAvatar(account, avatarUri);
44 finish();
45 }
46 }
47 });
48 this.cancelButton.setOnClickListener(new OnClickListener() {
49
50 @Override
51 public void onClick(View v) {
52 finish();
53 }
54 });
55 this.avatar.setOnClickListener(new OnClickListener() {
56
57 @Override
58 public void onClick(View v) {
59 Intent attachFileIntent = new Intent();
60 attachFileIntent.setType("image/*");
61 attachFileIntent.setAction(Intent.ACTION_GET_CONTENT);
62 Intent chooser = Intent.createChooser(attachFileIntent,
63 getString(R.string.attach_file));
64 startActivityForResult(chooser, REQUEST_CHOOSE_FILE);
65 }
66 });
67 }
68
69 @Override
70 protected void onActivityResult(int requestCode, int resultCode,
71 final Intent data) {
72 super.onActivityResult(requestCode, resultCode, data);
73 if (resultCode == RESULT_OK) {
74 if (requestCode == REQUEST_CHOOSE_FILE) {
75 Log.d("xmppService","bla");
76 this.avatarUri = data.getData();
77 }
78 }
79 }
80
81 @Override
82 protected void onBackendConnected() {
83 if (getIntent()!=null) {
84 String jid = getIntent().getStringExtra("account");
85 if (jid!=null) {
86 this.account = xmppConnectionService.findAccountByJid(jid);
87 if (this.avatarUri == null) {
88 avatarUri = PhoneHelper.getSefliUri(getApplicationContext());
89 }
90 loadImageIntoPreview(avatarUri);
91 String explainText = getString(R.string.publish_avatar_explanation,account.getJid());
92 this.explanation.setText(explainText);
93 }
94 }
95
96 }
97
98 protected void loadImageIntoPreview(Uri uri) {
99 Bitmap bm = xmppConnectionService.getFileBackend().cropCenterSquare(uri, 384);
100 this.avatar.setImageBitmap(bm);
101 enablePublishButton();
102 }
103
104 protected void enablePublishButton() {
105 this.publishButton.setEnabled(true);
106 this.publishButton.setTextColor(getPrimaryTextColor());
107 }
108
109}