1package eu.siacs.conversations.ui;
2
3import android.app.PendingIntent;
4import android.content.Intent;
5import android.os.Bundle;
6import android.view.View;
7import android.view.View.OnClickListener;
8import android.widget.AutoCompleteTextView;
9import android.widget.Button;
10import android.widget.CheckBox;
11import android.widget.CompoundButton;
12import android.widget.EditText;
13import android.widget.LinearLayout;
14import android.widget.CompoundButton.OnCheckedChangeListener;
15import android.widget.TextView;
16import eu.siacs.conversations.R;
17import eu.siacs.conversations.entities.Account;
18import eu.siacs.conversations.services.XmppConnectionService.OnAccountUpdate;
19import eu.siacs.conversations.ui.adapter.KnownHostsAdapter;
20import eu.siacs.conversations.utils.UIHelper;
21import eu.siacs.conversations.utils.Validator;
22import eu.siacs.conversations.xmpp.XmppConnection.Features;
23import eu.siacs.conversations.xmpp.pep.Avatar;
24
25public class EditAccountActivity extends XmppActivity {
26
27 private AutoCompleteTextView mAccountJid;
28 private EditText mPassword;
29 private EditText mPasswordConfirm;
30 private CheckBox mRegisterNew;
31 private Button mCancelButton;
32 private Button mSaveButton;
33
34 private LinearLayout mStats;
35 private TextView mServerInfoSm;
36 private TextView mServerInfoCarbons;
37 private TextView mServerInfoPep;
38 private TextView mSessionEst;
39 private TextView mOtrFingerprint;
40 private TextView mOtrFingerprintHeadline;
41
42 private String jidToEdit;
43 private Account mAccount;
44
45 private boolean mFetchingAvatar = false;
46
47 private OnClickListener mSaveButtonClickListener = new OnClickListener() {
48
49 @Override
50 public void onClick(View v) {
51 if (!Validator.isValidJid(mAccountJid.getText().toString())) {
52 mAccountJid.setError(getString(R.string.invalid_jid));
53 mAccountJid.requestFocus();
54 return;
55 }
56 boolean registerNewAccount = mRegisterNew.isChecked();
57 String[] jidParts = mAccountJid.getText().toString().split("@");
58 String username = jidParts[0];
59 String server;
60 if (jidParts.length >= 2) {
61 server = jidParts[1];
62 } else {
63 server = "";
64 }
65 String password = mPassword.getText().toString();
66 String passwordConfirm = mPasswordConfirm.getText().toString();
67 if (registerNewAccount) {
68 if (!password.equals(passwordConfirm)) {
69 mPasswordConfirm
70 .setError(getString(R.string.passwords_do_not_match));
71 mPasswordConfirm.requestFocus();
72 return;
73 }
74 }
75 if (mAccount != null) {
76 mAccount.setPassword(password);
77 mAccount.setUsername(username);
78 mAccount.setServer(server);
79 mAccount.setOption(Account.OPTION_REGISTER,registerNewAccount);
80 xmppConnectionService.updateAccount(mAccount);
81 } else {
82 if (xmppConnectionService.findAccountByJid(mAccountJid
83 .getText().toString()) != null) {
84 mAccountJid
85 .setError(getString(R.string.account_already_exists));
86 mAccountJid.requestFocus();
87 return;
88 }
89 mAccount = new Account(username, server, password);
90 mAccount.setOption(Account.OPTION_USETLS, true);
91 mAccount.setOption(Account.OPTION_USECOMPRESSION, true);
92 mAccount.setOption(Account.OPTION_REGISTER, registerNewAccount);
93 xmppConnectionService.createAccount(mAccount);
94 }
95 if (jidToEdit != null) {
96 finish();
97 } else {
98 updateSaveButton();
99 updateAccountInformation();
100 }
101
102 }
103 };
104 private OnClickListener mCancelButtonClickListener = new OnClickListener() {
105
106 @Override
107 public void onClick(View v) {
108 finish();
109 }
110 };
111 private OnAccountUpdate mOnAccountUpdateListener = new OnAccountUpdate() {
112
113 @Override
114 public void onAccountUpdate() {
115 runOnUiThread(new Runnable() {
116
117 @Override
118 public void run() {
119 if (jidToEdit == null && mAccount != null
120 && mAccount.getStatus() == Account.STATUS_ONLINE) {
121 if (!mFetchingAvatar) {
122 mFetchingAvatar = true;
123 xmppConnectionService.checkForAvatar(mAccount,
124 mAvatarFetchCallback);
125 }
126 } else {
127 updateSaveButton();
128 }
129 if (mAccount != null) {
130 updateAccountInformation();
131 }
132 }
133 });
134 }
135 };
136 private UiCallback<Avatar> mAvatarFetchCallback = new UiCallback<Avatar>() {
137
138 @Override
139 public void userInputRequried(PendingIntent pi, Avatar avatar) {
140 finishInitialSetup(avatar);
141 }
142
143 @Override
144 public void success(Avatar avatar) {
145 finishInitialSetup(avatar);
146 }
147
148 @Override
149 public void error(int errorCode, Avatar avatar) {
150 finishInitialSetup(avatar);
151 }
152 };
153 private KnownHostsAdapter mKnownHostsAdapter;
154
155 protected void finishInitialSetup(final Avatar avatar) {
156 runOnUiThread(new Runnable() {
157
158 @Override
159 public void run() {
160 Intent intent;
161 if (avatar != null) {
162 intent = new Intent(getApplicationContext(),
163 StartConversationActivity.class);
164 } else {
165 intent = new Intent(getApplicationContext(),
166 PublishProfilePictureActivity.class);
167 intent.putExtra("account", mAccount.getJid());
168 }
169 startActivity(intent);
170 finish();
171 }
172 });
173 }
174
175 protected boolean inputDataDiffersFromAccount() {
176 if (mAccount == null) {
177 return true;
178 } else {
179 return (!mAccount.getJid().equals(mAccountJid.getText().toString()))
180 || (!mAccount.getPassword().equals(
181 mPassword.getText().toString()) || mAccount
182 .isOptionSet(Account.OPTION_REGISTER) != mRegisterNew
183 .isChecked());
184 }
185 }
186
187 protected void updateSaveButton() {
188 if (mAccount != null
189 && mAccount.getStatus() == Account.STATUS_CONNECTING) {
190 this.mSaveButton.setEnabled(false);
191 this.mSaveButton.setTextColor(getSecondaryTextColor());
192 this.mSaveButton.setText(R.string.account_status_connecting);
193 } else {
194 this.mSaveButton.setEnabled(true);
195 this.mSaveButton.setTextColor(getPrimaryTextColor());
196 if (jidToEdit != null) {
197 this.mSaveButton.setText(R.string.connect);
198 } else {
199 this.mSaveButton.setText(R.string.next);
200 }
201 }
202 }
203
204 @Override
205 protected void onCreate(Bundle savedInstanceState) {
206 super.onCreate(savedInstanceState);
207 setContentView(R.layout.activity_edit_account);
208 this.mAccountJid = (AutoCompleteTextView) findViewById(R.id.account_jid);
209 this.mPassword = (EditText) findViewById(R.id.account_password);
210 this.mPasswordConfirm = (EditText) findViewById(R.id.account_password_confirm);
211 this.mRegisterNew = (CheckBox) findViewById(R.id.account_register_new);
212 this.mStats = (LinearLayout) findViewById(R.id.stats);
213 this.mSessionEst = (TextView) findViewById(R.id.session_est);
214 this.mServerInfoCarbons = (TextView) findViewById(R.id.server_info_carbons);
215 this.mServerInfoSm = (TextView) findViewById(R.id.server_info_sm);
216 this.mServerInfoPep = (TextView) findViewById(R.id.server_info_pep);
217 this.mOtrFingerprint = (TextView) findViewById(R.id.otr_fingerprint);
218 this.mOtrFingerprintHeadline = (TextView) findViewById(R.id.otr_fingerprint_headline);
219 this.mSaveButton = (Button) findViewById(R.id.save_button);
220 this.mCancelButton = (Button) findViewById(R.id.cancel_button);
221 this.mSaveButton.setOnClickListener(this.mSaveButtonClickListener);
222 this.mCancelButton.setOnClickListener(this.mCancelButtonClickListener);
223 this.mRegisterNew
224 .setOnCheckedChangeListener(new OnCheckedChangeListener() {
225
226 @Override
227 public void onCheckedChanged(CompoundButton buttonView,
228 boolean isChecked) {
229 if (isChecked) {
230 mPasswordConfirm.setVisibility(View.VISIBLE);
231 } else {
232 mPasswordConfirm.setVisibility(View.GONE);
233 }
234 updateSaveButton();
235 }
236 });
237 }
238
239 @Override
240 protected void onStart() {
241 super.onStart();
242 if (getIntent() != null) {
243 this.jidToEdit = getIntent().getStringExtra("jid");
244 if (this.jidToEdit != null) {
245 this.mRegisterNew.setVisibility(View.GONE);
246 getActionBar().setTitle(R.string.mgmt_account_edit);
247 } else {
248 getActionBar().setTitle(R.string.action_add_account);
249 }
250 }
251 }
252
253 @Override
254 protected void onStop() {
255 if (xmppConnectionServiceBound) {
256 xmppConnectionService.removeOnAccountListChangedListener();
257 }
258 super.onStop();
259 }
260
261 @Override
262 protected void onBackendConnected() {
263 this.mKnownHostsAdapter = new KnownHostsAdapter(this,
264 android.R.layout.simple_list_item_1,
265 xmppConnectionService.getKnownHosts());
266 this.xmppConnectionService
267 .setOnAccountListChangedListener(this.mOnAccountUpdateListener);
268 if (this.jidToEdit != null) {
269 this.mAccount = xmppConnectionService.findAccountByJid(jidToEdit);
270 updateAccountInformation();
271 } else if (this.xmppConnectionService.getAccounts().size() == 0) {
272 getActionBar().setDisplayHomeAsUpEnabled(false);
273 getActionBar().setDisplayShowHomeEnabled(false);
274 this.mCancelButton.setEnabled(false);
275 }
276 this.mAccountJid.setAdapter(this.mKnownHostsAdapter);
277 updateSaveButton();
278 }
279
280 private void updateAccountInformation() {
281 this.mAccountJid.setText(this.mAccount.getJid());
282 this.mPassword.setText(this.mAccount.getPassword());
283 if (this.mAccount.isOptionSet(Account.OPTION_REGISTER)) {
284 this.mRegisterNew.setVisibility(View.VISIBLE);
285 this.mRegisterNew.setChecked(true);
286 this.mPasswordConfirm.setText(this.mAccount.getPassword());
287 } else {
288 this.mRegisterNew.setVisibility(View.GONE);
289 this.mRegisterNew.setChecked(false);
290 }
291 if (this.mAccount.getStatus() == Account.STATUS_ONLINE && !this.mFetchingAvatar) {
292 this.mStats.setVisibility(View.VISIBLE);
293 this.mSessionEst.setText(UIHelper.readableTimeDifference(
294 getApplicationContext(), this.mAccount.getXmppConnection()
295 .getLastSessionEstablished()));
296 Features features = this.mAccount.getXmppConnection().getFeatures();
297 if (features.carbons()) {
298 this.mServerInfoCarbons.setText(R.string.server_info_available);
299 } else {
300 this.mServerInfoCarbons.setText(R.string.server_info_unavailable);
301 }
302 if (features.sm()) {
303 this.mServerInfoSm.setText(R.string.server_info_available);
304 } else {
305 this.mServerInfoSm.setText(R.string.server_info_unavailable);
306 }
307 if (features.pubsub()) {
308 this.mServerInfoPep.setText(R.string.server_info_available);
309 } else {
310 this.mServerInfoPep.setText(R.string.server_info_unavailable);
311 }
312 String fingerprint = this.mAccount.getOtrFingerprint(getApplicationContext());
313 if (fingerprint!=null) {
314 this.mOtrFingerprintHeadline.setVisibility(View.VISIBLE);
315 this.mOtrFingerprint.setVisibility(View.VISIBLE);
316 this.mOtrFingerprint.setText(fingerprint);
317 } else {
318 this.mOtrFingerprint.setVisibility(View.GONE);
319 this.mOtrFingerprintHeadline.setVisibility(View.GONE);
320 }
321 } else {
322 if (this.mAccount.errorStatus()) {
323 this.mAccountJid.setError(getString(this.mAccount
324 .getReadableStatusId()));
325 this.mAccountJid.requestFocus();
326 }
327 this.mStats.setVisibility(View.GONE);
328 }
329 }
330}