ManageAccountActivity.java

  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 android.app.AlertDialog;
 11import android.content.DialogInterface;
 12import android.content.DialogInterface.OnClickListener;
 13import android.content.Intent;
 14import android.os.Bundle;
 15import android.view.ContextMenu;
 16import android.view.Menu;
 17import android.view.MenuItem;
 18import android.view.View;
 19import android.view.ContextMenu.ContextMenuInfo;
 20import android.widget.AdapterView;
 21import android.widget.AdapterView.AdapterContextMenuInfo;
 22import android.widget.AdapterView.OnItemClickListener;
 23import android.widget.ListView;
 24
 25public class ManageAccountActivity extends XmppActivity implements OnAccountUpdate {
 26
 27	protected Account selectedAccount = null;
 28
 29	protected final List<Account> accountList = new ArrayList<>();
 30	protected ListView accountListView;
 31	protected AccountAdapter mAccountAdapter;
 32
 33	@Override
 34	public void onAccountUpdate() {
 35		refreshUi();
 36	}
 37
 38	@Override
 39	protected void refreshUiReal() {
 40		synchronized (this.accountList) {
 41			accountList.clear();
 42			accountList.addAll(xmppConnectionService.getAccounts());
 43		}
 44		invalidateOptionsMenu();
 45		mAccountAdapter.notifyDataSetChanged();
 46	}
 47
 48	@Override
 49	protected void onCreate(Bundle savedInstanceState) {
 50
 51		super.onCreate(savedInstanceState);
 52
 53		setContentView(R.layout.manage_accounts);
 54
 55		accountListView = (ListView) findViewById(R.id.account_list);
 56		this.mAccountAdapter = new AccountAdapter(this, accountList);
 57		accountListView.setAdapter(this.mAccountAdapter);
 58		accountListView.setOnItemClickListener(new OnItemClickListener() {
 59
 60			@Override
 61			public void onItemClick(AdapterView<?> arg0, View view,
 62					int position, long arg3) {
 63				switchToAccount(accountList.get(position));
 64			}
 65		});
 66		registerForContextMenu(accountListView);
 67	}
 68
 69	@Override
 70	public void onCreateContextMenu(ContextMenu menu, View v,
 71			ContextMenuInfo menuInfo) {
 72		super.onCreateContextMenu(menu, v, menuInfo);
 73		ManageAccountActivity.this.getMenuInflater().inflate(
 74				R.menu.manageaccounts_context, menu);
 75		AdapterView.AdapterContextMenuInfo acmi = (AdapterContextMenuInfo) menuInfo;
 76		this.selectedAccount = accountList.get(acmi.position);
 77		if (this.selectedAccount.isOptionSet(Account.OPTION_DISABLED)) {
 78			menu.findItem(R.id.mgmt_account_disable).setVisible(false);
 79			menu.findItem(R.id.mgmt_account_announce_pgp).setVisible(false);
 80			menu.findItem(R.id.mgmt_account_publish_avatar).setVisible(false);
 81		} else {
 82			menu.findItem(R.id.mgmt_account_enable).setVisible(false);
 83		}
 84		menu.setHeaderTitle(this.selectedAccount.getJid().toBareJid().toString());
 85	}
 86
 87	@Override
 88	void onBackendConnected() {
 89		this.accountList.clear();
 90		this.accountList.addAll(xmppConnectionService.getAccounts());
 91		mAccountAdapter.notifyDataSetChanged();
 92	}
 93
 94	@Override
 95	public boolean onCreateOptionsMenu(Menu menu) {
 96		getMenuInflater().inflate(R.menu.manageaccounts, menu);
 97		MenuItem enableAll = menu.findItem(R.id.action_enable_all);
 98		if (!accountsLeftToEnable()) {
 99			enableAll.setVisible(false);
100		}
101		MenuItem disableAll = menu.findItem(R.id.action_disable_all);
102		if (!accountsLeftToDisable()) {
103			disableAll.setVisible(false);
104		}
105		return true;
106	}
107
108	@Override
109	public boolean onContextItemSelected(MenuItem item) {
110		switch (item.getItemId()) {
111		case R.id.mgmt_account_publish_avatar:
112			publishAvatar(selectedAccount);
113			return true;
114		case R.id.mgmt_account_disable:
115			disableAccount(selectedAccount);
116			return true;
117		case R.id.mgmt_account_enable:
118			enableAccount(selectedAccount);
119			return true;
120		case R.id.mgmt_account_delete:
121			deleteAccount(selectedAccount);
122			return true;
123		case R.id.mgmt_account_announce_pgp:
124			publishOpenPGPPublicKey(selectedAccount);
125			return true;
126		default:
127			return super.onContextItemSelected(item);
128		}
129	}
130
131	@Override
132	public boolean onOptionsItemSelected(MenuItem item) {
133		switch (item.getItemId()) {
134			case R.id.action_add_account:
135				startActivity(new Intent(getApplicationContext(),
136						EditAccountActivity.class));
137				break;
138			case R.id.action_disable_all:
139				disableAllAccounts();
140				break;
141			case R.id.action_enable_all:
142				enableAllAccounts();
143				break;
144			default:
145				break;
146		}
147		return super.onOptionsItemSelected(item);
148	}
149
150	@Override
151	public boolean onNavigateUp() {
152		if (xmppConnectionService.getConversations().size() == 0) {
153			Intent contactsIntent = new Intent(this,
154					StartConversationActivity.class);
155			contactsIntent.setFlags(
156			// if activity exists in stack, pop the stack and go back to it
157					Intent.FLAG_ACTIVITY_CLEAR_TOP |
158					// otherwise, make a new task for it
159							Intent.FLAG_ACTIVITY_NEW_TASK |
160							// don't use the new activity animation; finish
161							// animation runs instead
162							Intent.FLAG_ACTIVITY_NO_ANIMATION);
163			startActivity(contactsIntent);
164			finish();
165			return true;
166		} else {
167			return super.onNavigateUp();
168		}
169	}
170
171	private void publishAvatar(Account account) {
172		Intent intent = new Intent(getApplicationContext(),
173				PublishProfilePictureActivity.class);
174		intent.putExtra("account", account.getJid().toString());
175		startActivity(intent);
176	}
177
178	private void disableAllAccounts() {
179		List<Account> list = new ArrayList<>();
180		synchronized (this.accountList) {
181			for (Account account : this.accountList) {
182				if (!account.isOptionSet(Account.OPTION_DISABLED)) {
183					list.add(account);
184				}
185			}
186		}
187		for(Account account : list) {
188			disableAccount(account);
189		}
190	}
191
192	private boolean accountsLeftToDisable() {
193		synchronized (this.accountList) {
194			for (Account account : this.accountList) {
195				if (!account.isOptionSet(Account.OPTION_DISABLED)) {
196					return true;
197				}
198			}
199			return false;
200		}
201	}
202
203	private boolean accountsLeftToEnable() {
204		synchronized (this.accountList) {
205			for (Account account : this.accountList) {
206				if (account.isOptionSet(Account.OPTION_DISABLED)) {
207					return true;
208				}
209			}
210			return false;
211		}
212	}
213
214	private void enableAllAccounts() {
215		List<Account> list = new ArrayList<>();
216		synchronized (this.accountList) {
217			for (Account account : this.accountList) {
218				if (account.isOptionSet(Account.OPTION_DISABLED)) {
219					list.add(account);
220				}
221			}
222		}
223		for(Account account : list) {
224			enableAccount(account);
225		}
226	}
227
228	private void disableAccount(Account account) {
229		account.setOption(Account.OPTION_DISABLED, true);
230		xmppConnectionService.updateAccount(account);
231	}
232
233	private void enableAccount(Account account) {
234		account.setOption(Account.OPTION_DISABLED, false);
235		xmppConnectionService.updateAccount(account);
236	}
237
238	private void publishOpenPGPPublicKey(Account account) {
239		if (ManageAccountActivity.this.hasPgp()) {
240			announcePgp(account, null);
241		} else {
242			this.showInstallPgpDialog();
243		}
244	}
245
246	private void deleteAccount(final Account account) {
247		AlertDialog.Builder builder = new AlertDialog.Builder(
248				ManageAccountActivity.this);
249		builder.setTitle(getString(R.string.mgmt_account_are_you_sure));
250		builder.setIconAttribute(android.R.attr.alertDialogIcon);
251		builder.setMessage(getString(R.string.mgmt_account_delete_confirm_text));
252		builder.setPositiveButton(getString(R.string.delete),
253				new OnClickListener() {
254					@Override
255					public void onClick(DialogInterface dialog, int which) {
256						xmppConnectionService.deleteAccount(account);
257						selectedAccount = null;
258					}
259				});
260		builder.setNegativeButton(getString(R.string.cancel), null);
261		builder.create().show();
262	}
263
264	@Override
265	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
266		super.onActivityResult(requestCode, resultCode, data);
267		if (resultCode == RESULT_OK) {
268			if (requestCode == REQUEST_ANNOUNCE_PGP) {
269				announcePgp(selectedAccount, null);
270			}
271		}
272	}
273}