ManageAccountActivity.java

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