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