1package eu.siacs.conversations.ui;
2
3import java.util.ArrayList;
4import java.util.List;
5
6import eu.siacs.conversations.R;
7import eu.siacs.conversations.entities.Account;
8import eu.siacs.conversations.services.XmppConnectionService.OnAccountUpdate;
9import eu.siacs.conversations.ui.adapter.AccountAdapter;
10import eu.siacs.conversations.xmpp.XmppConnection;
11import android.app.Activity;
12import android.app.AlertDialog;
13import android.content.DialogInterface;
14import android.content.DialogInterface.OnClickListener;
15import android.content.Intent;
16import android.os.Bundle;
17import android.os.SystemClock;
18import android.view.ActionMode;
19import android.view.Menu;
20import android.view.MenuInflater;
21import android.view.MenuItem;
22import android.view.View;
23import android.widget.AdapterView;
24import android.widget.AdapterView.OnItemClickListener;
25import android.widget.AdapterView.OnItemLongClickListener;
26import android.widget.ListView;
27import android.widget.TextView;
28
29public class ManageAccountActivity extends XmppActivity {
30
31 protected boolean isActionMode = false;
32 protected ActionMode actionMode;
33 protected Account selectedAccountForActionMode = null;
34 protected ManageAccountActivity activity = this;
35
36 protected boolean firstrun = true;
37
38 protected List<Account> accountList = new ArrayList<Account>();
39 protected ListView accountListView;
40 protected AccountAdapter mAccountAdapter;
41 protected OnAccountUpdate accountChanged = new OnAccountUpdate() {
42
43 @Override
44 public void onAccountUpdate() {
45 accountList.clear();
46 accountList.addAll(xmppConnectionService.getAccounts());
47 runOnUiThread(new Runnable() {
48
49 @Override
50 public void run() {
51 mAccountAdapter.notifyDataSetChanged();
52 }
53 });
54 }
55 };
56
57 protected ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
58
59 @Override
60 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
61 if (selectedAccountForActionMode
62 .isOptionSet(Account.OPTION_DISABLED)) {
63 menu.findItem(R.id.mgmt_account_enable).setVisible(true);
64 menu.findItem(R.id.mgmt_account_disable).setVisible(false);
65 } else {
66 menu.findItem(R.id.mgmt_account_disable).setVisible(true);
67 menu.findItem(R.id.mgmt_account_enable).setVisible(false);
68 }
69 return true;
70 }
71
72 @Override
73 public void onDestroyActionMode(ActionMode mode) {
74 // TODO Auto-generated method stub
75
76 }
77
78 @Override
79 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
80 MenuInflater inflater = mode.getMenuInflater();
81 inflater.inflate(R.menu.manageaccounts_context, menu);
82 return true;
83 }
84
85 @Override
86 public boolean onActionItemClicked(final ActionMode mode, MenuItem item) {
87 if (item.getItemId() == R.id.mgmt_account_edit) {
88 editAccount(selectedAccountForActionMode);
89 } else if (item.getItemId() == R.id.mgmt_account_disable) {
90 selectedAccountForActionMode.setOption(Account.OPTION_DISABLED,
91 true);
92 xmppConnectionService
93 .updateAccount(selectedAccountForActionMode);
94 mode.finish();
95 } else if (item.getItemId() == R.id.mgmt_account_enable) {
96 selectedAccountForActionMode.setOption(Account.OPTION_DISABLED,
97 false);
98 xmppConnectionService
99 .updateAccount(selectedAccountForActionMode);
100 mode.finish();
101 } else if (item.getItemId() == R.id.mgmt_account_publish_avatar) {
102 Intent intent = new Intent(getApplicationContext(), PublishProfilePictureActivity.class);
103 intent.putExtra("account", selectedAccountForActionMode.getJid());
104 startActivity(intent);
105 } else if (item.getItemId() == R.id.mgmt_account_delete) {
106 AlertDialog.Builder builder = new AlertDialog.Builder(activity);
107 builder.setTitle(getString(R.string.mgmt_account_are_you_sure));
108 builder.setIconAttribute(android.R.attr.alertDialogIcon);
109 builder.setMessage(getString(R.string.mgmt_account_delete_confirm_text));
110 builder.setPositiveButton(getString(R.string.delete),
111 new OnClickListener() {
112
113 @Override
114 public void onClick(DialogInterface dialog,
115 int which) {
116 xmppConnectionService
117 .deleteAccount(selectedAccountForActionMode);
118 selectedAccountForActionMode = null;
119 mode.finish();
120 }
121 });
122 builder.setNegativeButton(getString(R.string.cancel), null);
123 builder.create().show();
124 } else if (item.getItemId() == R.id.mgmt_account_announce_pgp) {
125 if (activity.hasPgp()) {
126 mode.finish();
127 announcePgp(selectedAccountForActionMode, null);
128 } else {
129 activity.showInstallPgpDialog();
130 }
131 } else if (item.getItemId() == R.id.mgmt_otr_key) {
132 AlertDialog.Builder builder = new AlertDialog.Builder(activity);
133 builder.setTitle("OTR Fingerprint");
134 String fingerprintTxt = selectedAccountForActionMode
135 .getOtrFingerprint(getApplicationContext());
136 View view = (View) getLayoutInflater().inflate(
137 R.layout.otr_fingerprint, null);
138 if (fingerprintTxt != null) {
139 TextView fingerprint = (TextView) view
140 .findViewById(R.id.otr_fingerprint);
141 TextView noFingerprintView = (TextView) view
142 .findViewById(R.id.otr_no_fingerprint);
143 fingerprint.setText(fingerprintTxt);
144 fingerprint.setVisibility(View.VISIBLE);
145 noFingerprintView.setVisibility(View.GONE);
146 }
147 builder.setView(view);
148 builder.setPositiveButton(getString(R.string.done), null);
149 builder.create().show();
150 } else if (item.getItemId() == R.id.mgmt_account_info) {
151 AlertDialog.Builder builder = new AlertDialog.Builder(activity);
152 builder.setTitle(getString(R.string.account_info));
153 if (selectedAccountForActionMode.getStatus() == Account.STATUS_ONLINE) {
154 XmppConnection xmpp = selectedAccountForActionMode
155 .getXmppConnection();
156 long connectionAge = (SystemClock.elapsedRealtime() - xmpp.lastConnect) / 60000;
157 long sessionAge = (SystemClock.elapsedRealtime() - xmpp.lastSessionStarted) / 60000;
158 long connectionAgeHours = connectionAge / 60;
159 long sessionAgeHours = sessionAge / 60;
160 View view = (View) getLayoutInflater().inflate(
161 R.layout.server_info, null);
162 TextView connection = (TextView) view
163 .findViewById(R.id.connection);
164 TextView session = (TextView) view
165 .findViewById(R.id.session);
166 TextView pcks_sent = (TextView) view
167 .findViewById(R.id.pcks_sent);
168 TextView pcks_received = (TextView) view
169 .findViewById(R.id.pcks_received);
170 TextView carbon = (TextView) view.findViewById(R.id.carbon);
171 TextView stream = (TextView) view.findViewById(R.id.stream);
172 TextView roster = (TextView) view.findViewById(R.id.roster);
173 TextView presences = (TextView) view
174 .findViewById(R.id.number_presences);
175 presences.setText(selectedAccountForActionMode
176 .countPresences() + "");
177 pcks_received.setText("" + xmpp.getReceivedStanzas());
178 pcks_sent.setText("" + xmpp.getSentStanzas());
179 if (connectionAgeHours >= 2) {
180 connection.setText(connectionAgeHours + " "
181 + getString(R.string.hours));
182 } else {
183 connection.setText(connectionAge + " "
184 + getString(R.string.mins));
185 }
186 if (xmpp.getFeatures().sm()) {
187 if (sessionAgeHours >= 2) {
188 session.setText(sessionAgeHours + " "
189 + getString(R.string.hours));
190 } else {
191 session.setText(sessionAge + " "
192 + getString(R.string.mins));
193 }
194 stream.setText(getString(R.string.yes));
195 } else {
196 stream.setText(getString(R.string.no));
197 session.setText(connection.getText());
198 }
199 if (xmpp.getFeatures().carbons()) {
200 carbon.setText(getString(R.string.yes));
201 } else {
202 carbon.setText(getString(R.string.no));
203 }
204 if (xmpp.getFeatures().rosterVersioning()) {
205 roster.setText(getString(R.string.yes));
206 } else {
207 roster.setText(getString(R.string.no));
208 }
209 builder.setView(view);
210 } else {
211 builder.setMessage(getString(R.string.mgmt_account_account_offline));
212 }
213 builder.setPositiveButton(getString(R.string.hide), null);
214 builder.create().show();
215 }
216 return true;
217 }
218
219 };
220
221 @Override
222 protected void onCreate(Bundle savedInstanceState) {
223
224 super.onCreate(savedInstanceState);
225
226 setContentView(R.layout.manage_accounts);
227
228 accountListView = (ListView) findViewById(R.id.account_list);
229 final XmppActivity activity = this;
230 this.mAccountAdapter = new AccountAdapter(this, accountList);
231 accountListView.setAdapter(this.mAccountAdapter);
232 accountListView.setOnItemClickListener(new OnItemClickListener() {
233
234 @Override
235 public void onItemClick(AdapterView<?> arg0, View view,
236 int position, long arg3) {
237 if (!isActionMode) {
238 editAccount(accountList.get(position));
239 } else {
240 selectedAccountForActionMode = accountList.get(position);
241 actionMode.invalidate();
242 }
243 }
244 });
245 accountListView
246 .setOnItemLongClickListener(new OnItemLongClickListener() {
247
248 @Override
249 public boolean onItemLongClick(AdapterView<?> arg0,
250 View view, int position, long arg3) {
251 if (!isActionMode) {
252 accountListView
253 .setChoiceMode(ListView.CHOICE_MODE_SINGLE);
254 accountListView.setItemChecked(position, true);
255 selectedAccountForActionMode = accountList
256 .get(position);
257 actionMode = activity
258 .startActionMode(mActionModeCallback);
259 return true;
260 } else {
261 return false;
262 }
263 }
264 });
265 }
266
267 @Override
268 protected void onStop() {
269 if (xmppConnectionServiceBound) {
270 xmppConnectionService.removeOnAccountListChangedListener();
271 }
272 super.onStop();
273 }
274
275 @Override
276 void onBackendConnected() {
277 xmppConnectionService.setOnAccountListChangedListener(accountChanged);
278 this.accountList.clear();
279 this.accountList.addAll(xmppConnectionService.getAccounts());
280 mAccountAdapter.notifyDataSetChanged();
281 if ((this.accountList.size() == 0) && (this.firstrun)) {
282 getActionBar().setDisplayHomeAsUpEnabled(false);
283 getActionBar().setHomeButtonEnabled(false);
284 this.firstrun = false;
285 }
286 }
287
288 @Override
289 public boolean onCreateOptionsMenu(Menu menu) {
290 getMenuInflater().inflate(R.menu.manageaccounts, menu);
291 return true;
292 }
293
294 @Override
295 public boolean onOptionsItemSelected(MenuItem item) {
296 switch (item.getItemId()) {
297 case R.id.action_add_account:
298 startActivity(new Intent(getApplicationContext(), EditAccountActivity.class));
299 break;
300 default:
301 break;
302 }
303 return super.onOptionsItemSelected(item);
304 }
305
306 @Override
307 public boolean onNavigateUp() {
308 if (xmppConnectionService.getConversations().size() == 0) {
309 Intent contactsIntent = new Intent(this,
310 StartConversationActivity.class);
311 contactsIntent.setFlags(
312 // if activity exists in stack, pop the stack and go back to it
313 Intent.FLAG_ACTIVITY_CLEAR_TOP |
314 // otherwise, make a new task for it
315 Intent.FLAG_ACTIVITY_NEW_TASK |
316 // don't use the new activity animation; finish
317 // animation runs instead
318 Intent.FLAG_ACTIVITY_NO_ANIMATION);
319 startActivity(contactsIntent);
320 finish();
321 return true;
322 } else {
323 return super.onNavigateUp();
324 }
325 }
326
327 private void editAccount(Account account) {
328 Intent intent = new Intent(this, EditAccountActivity.class);
329 intent.putExtra("jid", account.getJid());
330 startActivity(intent);
331 }
332
333 @Override
334 public void onActionModeStarted(ActionMode mode) {
335 super.onActionModeStarted(mode);
336 this.isActionMode = true;
337 }
338
339 @Override
340 public void onActionModeFinished(ActionMode mode) {
341 super.onActionModeFinished(mode);
342 this.isActionMode = false;
343 accountListView.clearChoices();
344 accountListView.requestLayout();
345 accountListView.post(new Runnable() {
346 @Override
347 public void run() {
348 accountListView.setChoiceMode(ListView.CHOICE_MODE_NONE);
349 }
350 });
351 }
352
353 @Override
354 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
355 super.onActivityResult(requestCode, resultCode, data);
356 if (resultCode == RESULT_OK) {
357 if (requestCode == REQUEST_ANNOUNCE_PGP) {
358 announcePgp(selectedAccountForActionMode, null);
359 }
360 }
361 }
362}