1package eu.siacs.conversations.ui;
2
3import java.util.ArrayList;
4import java.util.List;
5
6import eu.siacs.conversations.R;
7import eu.siacs.conversations.crypto.PgpEngine;
8import eu.siacs.conversations.crypto.PgpEngine.UserInputRequiredException;
9import eu.siacs.conversations.entities.Account;
10import eu.siacs.conversations.ui.EditAccount.EditAccountListener;
11import eu.siacs.conversations.xmpp.OnTLSExceptionReceived;
12import eu.siacs.conversations.xmpp.XmppConnection;
13import android.app.Activity;
14import android.app.AlertDialog;
15import android.content.Context;
16import android.content.DialogInterface;
17import android.content.DialogInterface.OnClickListener;
18import android.content.Intent;
19import android.content.IntentSender.SendIntentException;
20import android.os.Bundle;
21import android.os.SystemClock;
22import android.util.Log;
23import android.view.ActionMode;
24import android.view.LayoutInflater;
25import android.view.Menu;
26import android.view.MenuInflater;
27import android.view.MenuItem;
28import android.view.View;
29import android.view.ViewGroup;
30import android.widget.AdapterView;
31import android.widget.AdapterView.OnItemClickListener;
32import android.widget.AdapterView.OnItemLongClickListener;
33import android.widget.ArrayAdapter;
34import android.widget.ListView;
35import android.widget.TextView;
36
37public class ManageAccountActivity extends XmppActivity {
38
39 public static final int REQUEST_ANNOUNCE_PGP = 0x73731;
40
41 protected boolean isActionMode = false;
42 protected ActionMode actionMode;
43 protected Account selectedAccountForActionMode = null;
44 protected ManageAccountActivity activity = this;
45
46 protected List<Account> accountList = new ArrayList<Account>();
47 protected ListView accountListView;
48 protected ArrayAdapter<Account> accountListViewAdapter;
49 protected OnAccountListChangedListener accountChanged = new OnAccountListChangedListener() {
50
51 @Override
52 public void onAccountListChangedListener() {
53 accountList.clear();
54 accountList.addAll(xmppConnectionService.getAccounts());
55 runOnUiThread(new Runnable() {
56
57 @Override
58 public void run() {
59 accountListViewAdapter.notifyDataSetChanged();
60 }
61 });
62 }
63 };
64
65 protected OnTLSExceptionReceived tlsExceptionReceived = new OnTLSExceptionReceived() {
66
67 @Override
68 public void onTLSExceptionReceived(final String fingerprint, final Account account) {
69 activity.runOnUiThread(new Runnable() {
70
71 @Override
72 public void run() {
73 AlertDialog.Builder builder = new AlertDialog.Builder(activity);
74 builder.setTitle("Untrusted Certificate");
75 builder.setIconAttribute(android.R.attr.alertDialogIcon);
76 View view = (View) getLayoutInflater().inflate(R.layout.cert_warning, null);
77 TextView sha = (TextView) view.findViewById(R.id.sha);
78 TextView hint = (TextView) view.findViewById(R.id.hint);
79 StringBuilder humanReadableSha = new StringBuilder();
80 humanReadableSha.append(fingerprint);
81 for(int i = 2; i < 59; i += 3) {
82 Log.d("gultsch","insert into "+i);
83 if ((i==14)||(i==29)||(i==44)) {
84 humanReadableSha.insert(i, "\n");
85 } else {
86 humanReadableSha.insert(i, ":");
87 }
88
89 }
90 hint.setText(getString(R.string.untrusted_cert_hint,account.getServer()));
91 sha.setText(humanReadableSha.toString());
92 builder.setView(view);
93 builder.setNegativeButton("Don't connect", null);
94 builder.setPositiveButton("Trust certificate", new OnClickListener() {
95
96 @Override
97 public void onClick(DialogInterface dialog, int which) {
98 account.setSSLCertFingerprint(fingerprint);
99 activity.xmppConnectionService.updateAccount(account);
100 }
101 });
102 builder.create().show();
103 }
104 });
105
106 }
107 };
108
109 @Override
110 protected void onCreate(Bundle savedInstanceState) {
111
112 super.onCreate(savedInstanceState);
113
114 setContentView(R.layout.manage_accounts);
115
116 accountListView = (ListView) findViewById(R.id.account_list);
117 accountListViewAdapter = new ArrayAdapter<Account>(
118 getApplicationContext(), R.layout.account_row, this.accountList) {
119 @Override
120 public View getView(int position, View view, ViewGroup parent) {
121 Account account = getItem(position);
122 if (view == null) {
123 LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
124 view = (View) inflater.inflate(R.layout.account_row, null);
125 }
126 ((TextView) view.findViewById(R.id.account_jid))
127 .setText(account.getJid());
128 TextView statusView = (TextView) view
129 .findViewById(R.id.account_status);
130 switch (account.getStatus()) {
131 case Account.STATUS_DISABLED:
132 statusView.setText("temporarily disabled");
133 statusView.setTextColor(0xFF1da9da);
134 break;
135 case Account.STATUS_ONLINE:
136 statusView.setText("online");
137 statusView.setTextColor(0xFF83b600);
138 break;
139 case Account.STATUS_CONNECTING:
140 statusView.setText("connecting\u2026");
141 statusView.setTextColor(0xFF1da9da);
142 break;
143 case Account.STATUS_OFFLINE:
144 statusView.setText("offline");
145 statusView.setTextColor(0xFFe92727);
146 break;
147 case Account.STATUS_UNAUTHORIZED:
148 statusView.setText("unauthorized");
149 statusView.setTextColor(0xFFe92727);
150 break;
151 case Account.STATUS_SERVER_NOT_FOUND:
152 statusView.setText("server not found");
153 statusView.setTextColor(0xFFe92727);
154 break;
155 case Account.STATUS_NO_INTERNET:
156 statusView.setText("no internet");
157 statusView.setTextColor(0xFFe92727);
158 break;
159 case Account.STATUS_SERVER_REQUIRES_TLS:
160 statusView.setText("server requires TLS");
161 statusView.setTextColor(0xFFe92727);
162 break;
163 case Account.STATUS_TLS_ERROR:
164 statusView.setText("untrusted cerficate");
165 statusView.setTextColor(0xFFe92727);
166 break;
167 default:
168 break;
169 }
170
171 return view;
172 }
173 };
174 final XmppActivity activity = this;
175 accountListView.setAdapter(this.accountListViewAdapter);
176 accountListView.setOnItemClickListener(new OnItemClickListener() {
177
178 @Override
179 public void onItemClick(AdapterView<?> arg0, View view,
180 int position, long arg3) {
181 if (!isActionMode) {
182 Account account = accountList.get(position);
183 if ((account.getStatus() != Account.STATUS_ONLINE)&&(account.getStatus() != Account.STATUS_CONNECTING)&&(!account.isOptionSet(Account.OPTION_DISABLED))) {
184 activity.xmppConnectionService.reconnectAccount(accountList.get(position),true);
185 } else if (account.getStatus() == Account.STATUS_ONLINE) {
186 activity.startActivity(new Intent(activity.getApplicationContext(),NewConversationActivity.class));
187 }
188 } else {
189 selectedAccountForActionMode = accountList.get(position);
190 actionMode.invalidate();
191 }
192 }
193 });
194 accountListView.setOnItemLongClickListener(new OnItemLongClickListener() {
195
196 @Override
197 public boolean onItemLongClick(AdapterView<?> arg0, View view,
198 int position, long arg3) {
199 if (!isActionMode) {
200 accountListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
201 accountListView.setItemChecked(position,true);
202 selectedAccountForActionMode = accountList.get(position);
203 actionMode = activity.startActionMode((new ActionMode.Callback() {
204
205 @Override
206 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
207 if (selectedAccountForActionMode.isOptionSet(Account.OPTION_DISABLED)) {
208 menu.findItem(R.id.mgmt_account_enable).setVisible(true);
209 menu.findItem(R.id.mgmt_account_disable).setVisible(false);
210 } else {
211 menu.findItem(R.id.mgmt_account_disable).setVisible(true);
212 menu.findItem(R.id.mgmt_account_enable).setVisible(false);
213 }
214 return true;
215 }
216
217 @Override
218 public void onDestroyActionMode(ActionMode mode) {
219 // TODO Auto-generated method stub
220
221 }
222
223 @Override
224 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
225 MenuInflater inflater = mode.getMenuInflater();
226 inflater.inflate(R.menu.manageaccounts_context, menu);
227 return true;
228 }
229
230 @Override
231 public boolean onActionItemClicked(final ActionMode mode, MenuItem item) {
232 if (item.getItemId()==R.id.mgmt_account_edit) {
233 EditAccount dialog = new EditAccount();
234 dialog.setAccount(selectedAccountForActionMode);
235 dialog.setEditAccountListener(new EditAccountListener() {
236
237 @Override
238 public void onAccountEdited(Account account) {
239 xmppConnectionService.updateAccount(account);
240 actionMode.finish();
241 }
242 });
243 dialog.show(getFragmentManager(), "edit_account");
244 } else if (item.getItemId()==R.id.mgmt_account_disable) {
245 selectedAccountForActionMode.setOption(Account.OPTION_DISABLED, true);
246 xmppConnectionService.updateAccount(selectedAccountForActionMode);
247 mode.finish();
248 } else if (item.getItemId()==R.id.mgmt_account_enable) {
249 selectedAccountForActionMode.setOption(Account.OPTION_DISABLED, false);
250 xmppConnectionService.updateAccount(selectedAccountForActionMode);
251 mode.finish();
252 } else if (item.getItemId()==R.id.mgmt_account_delete) {
253 AlertDialog.Builder builder = new AlertDialog.Builder(activity);
254 builder.setTitle("Are you sure?");
255 builder.setIconAttribute(android.R.attr.alertDialogIcon);
256 builder.setMessage("If you delete your account your entire conversation history will be lost");
257 builder.setPositiveButton("Delete", new OnClickListener() {
258
259 @Override
260 public void onClick(DialogInterface dialog, int which) {
261 xmppConnectionService.deleteAccount(selectedAccountForActionMode);
262 selectedAccountForActionMode = null;
263 mode.finish();
264 }
265 });
266 builder.setNegativeButton("Cancel",null);
267 builder.create().show();
268 } else if (item.getItemId()==R.id.mgmt_account_announce_pgp) {
269 if (activity.hasPgp()) {
270 mode.finish();
271 try {
272 xmppConnectionService.generatePgpAnnouncement(selectedAccountForActionMode);
273 } catch (PgpEngine.UserInputRequiredException e) {
274 try {
275 startIntentSenderForResult(e.getPendingIntent().getIntentSender(), REQUEST_ANNOUNCE_PGP, null, 0, 0, 0);
276 } catch (SendIntentException e1) {
277 Log.d("gultsch","sending intent failed");
278 }
279 }
280 }
281 } else if (item.getItemId() == R.id.mgmt_account_info) {
282 AlertDialog.Builder builder = new AlertDialog.Builder(activity);
283 builder.setTitle(getString(R.string.account_info));
284 if (selectedAccountForActionMode.getStatus() == Account.STATUS_ONLINE) {
285 XmppConnection xmpp = selectedAccountForActionMode.getXmppConnection();
286 long connectionAge = (SystemClock.elapsedRealtime() - xmpp.lastConnect) / 60000;
287 long sessionAge = (SystemClock.elapsedRealtime() - xmpp.lastSessionStarted) / 60000;
288 View view = (View) getLayoutInflater().inflate(R.layout.server_info, null);
289 TextView connection = (TextView) view.findViewById(R.id.connection);
290 TextView session = (TextView) view.findViewById(R.id.session);
291 TextView pcks_sent = (TextView) view.findViewById(R.id.pcks_sent);
292 TextView pcks_received = (TextView) view.findViewById(R.id.pcks_received);
293 pcks_received.setText(""+xmpp.getReceivedStanzas());
294 pcks_sent.setText(""+xmpp.getSentStanzas());
295 connection.setText(connectionAge+" mins");
296 session.setText(sessionAge+" mins");
297 builder.setView(view);
298 } else {
299 builder.setMessage("Account is offline");
300 }
301 builder.setPositiveButton("Hide", null);
302 builder.create().show();
303 }
304 return true;
305 }
306 }));
307 return true;
308 } else {
309 return false;
310 }
311 }
312 });
313 }
314
315 @Override
316 protected void onStop() {
317 if (xmppConnectionServiceBound) {
318 xmppConnectionService.removeOnAccountListChangedListener();
319 xmppConnectionService.removeOnTLSExceptionReceivedListener();
320 }
321 super.onStop();
322 }
323
324 @Override
325 void onBackendConnected() {
326 xmppConnectionService.setOnAccountListChangedListener(accountChanged);
327 xmppConnectionService.setOnTLSExceptionReceivedListener(tlsExceptionReceived);
328 this.accountList.clear();
329 this.accountList.addAll(xmppConnectionService.getAccounts());
330 accountListViewAdapter.notifyDataSetChanged();
331 if (this.accountList.size() == 0) {
332 getActionBar().setDisplayHomeAsUpEnabled(false);
333 addAccount();
334 }
335 }
336
337 @Override
338 public boolean onCreateOptionsMenu(Menu menu) {
339 getMenuInflater().inflate(R.menu.manageaccounts, menu);
340 return true;
341 }
342
343 @Override
344 public boolean onOptionsItemSelected(MenuItem item) {
345 switch (item.getItemId()) {
346 case R.id.action_settings:
347 startActivity(new Intent(this, SettingsActivity.class));
348 break;
349 case R.id.action_add_account:
350 addAccount();
351 break;
352 default:
353 break;
354 }
355 return super.onOptionsItemSelected(item);
356 }
357
358 protected void addAccount() {
359 final Activity activity = this;
360 EditAccount dialog = new EditAccount();
361 dialog.setEditAccountListener(new EditAccountListener() {
362
363 @Override
364 public void onAccountEdited(Account account) {
365 xmppConnectionService.createAccount(account);
366 activity.getActionBar().setDisplayHomeAsUpEnabled(true);
367 }
368 });
369 dialog.show(getFragmentManager(), "add_account");
370 }
371
372
373 @Override
374 public void onActionModeStarted(ActionMode mode) {
375 super.onActionModeStarted(mode);
376 this.isActionMode = true;
377 }
378
379 @Override
380 public void onActionModeFinished(ActionMode mode) {
381 super.onActionModeFinished(mode);
382 this.isActionMode = false;
383 accountListView.clearChoices();
384 accountListView.requestLayout();
385 accountListView.post(new Runnable() {
386 @Override
387 public void run() {
388 accountListView.setChoiceMode(ListView.CHOICE_MODE_NONE);
389 }
390 });
391 }
392
393 @Override
394 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
395 super.onActivityResult(requestCode, resultCode, data);
396 if (resultCode == RESULT_OK) {
397 if (requestCode == REQUEST_ANNOUNCE_PGP) {
398 try {
399 xmppConnectionService.generatePgpAnnouncement(selectedAccountForActionMode);
400 } catch (UserInputRequiredException e) {
401 Log.d("gultsch","already came back. ignoring");
402 }
403 }
404 }
405 }
406}