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	public void onClickTglAccountState(Account account, boolean enable) {
172		if (enable) {
173			enableAccount(account);
174		} else {
175			disableAccount(account);
176		}
177	}
178
179	private void publishAvatar(Account account) {
180		Intent intent = new Intent(getApplicationContext(),
181				PublishProfilePictureActivity.class);
182		intent.putExtra("account", account.getJid().toString());
183		startActivity(intent);
184	}
185
186	private void disableAllAccounts() {
187		List<Account> list = new ArrayList<>();
188		synchronized (this.accountList) {
189			for (Account account : this.accountList) {
190				if (!account.isOptionSet(Account.OPTION_DISABLED)) {
191					list.add(account);
192				}
193			}
194		}
195		for(Account account : list) {
196			disableAccount(account);
197		}
198	}
199
200	private boolean accountsLeftToDisable() {
201		synchronized (this.accountList) {
202			for (Account account : this.accountList) {
203				if (!account.isOptionSet(Account.OPTION_DISABLED)) {
204					return true;
205				}
206			}
207			return false;
208		}
209	}
210
211	private boolean accountsLeftToEnable() {
212		synchronized (this.accountList) {
213			for (Account account : this.accountList) {
214				if (account.isOptionSet(Account.OPTION_DISABLED)) {
215					return true;
216				}
217			}
218			return false;
219		}
220	}
221
222	private void enableAllAccounts() {
223		List<Account> list = new ArrayList<>();
224		synchronized (this.accountList) {
225			for (Account account : this.accountList) {
226				if (account.isOptionSet(Account.OPTION_DISABLED)) {
227					list.add(account);
228				}
229			}
230		}
231		for(Account account : list) {
232			enableAccount(account);
233		}
234	}
235
236	private void disableAccount(Account account) {
237		account.setOption(Account.OPTION_DISABLED, true);
238		xmppConnectionService.updateAccount(account);
239	}
240
241	private void enableAccount(Account account) {
242		account.setOption(Account.OPTION_DISABLED, false);
243		xmppConnectionService.updateAccount(account);
244	}
245
246	private void publishOpenPGPPublicKey(Account account) {
247		if (ManageAccountActivity.this.hasPgp()) {
248			announcePgp(account, null);
249		} else {
250			this.showInstallPgpDialog();
251		}
252	}
253
254	private void deleteAccount(final Account account) {
255		AlertDialog.Builder builder = new AlertDialog.Builder(
256				ManageAccountActivity.this);
257		builder.setTitle(getString(R.string.mgmt_account_are_you_sure));
258		builder.setIconAttribute(android.R.attr.alertDialogIcon);
259		builder.setMessage(getString(R.string.mgmt_account_delete_confirm_text));
260		builder.setPositiveButton(getString(R.string.delete),
261				new OnClickListener() {
262					@Override
263					public void onClick(DialogInterface dialog, int which) {
264						xmppConnectionService.deleteAccount(account);
265						selectedAccount = null;
266					}
267				});
268		builder.setNegativeButton(getString(R.string.cancel), null);
269		builder.create().show();
270	}
271
272	@Override
273	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
274		super.onActivityResult(requestCode, resultCode, data);
275		if (resultCode == RESULT_OK) {
276			if (requestCode == REQUEST_ANNOUNCE_PGP) {
277				announcePgp(selectedAccount, null);
278			}
279		}
280	}
281}