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 {
 26
 27	protected Account selectedAccount = null;
 28
 29	protected List<Account> accountList = new ArrayList<Account>();
 30	protected ListView accountListView;
 31	protected AccountAdapter mAccountAdapter;
 32	protected OnAccountUpdate accountChanged = new OnAccountUpdate() {
 33
 34		@Override
 35		public void onAccountUpdate() {
 36			accountList.clear();
 37			accountList.addAll(xmppConnectionService.getAccounts());
 38			runOnUiThread(new Runnable() {
 39
 40				@Override
 41				public void run() {
 42					mAccountAdapter.notifyDataSetChanged();
 43				}
 44			});
 45		}
 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());
 85	}
 86
 87	@Override
 88	protected void onStop() {
 89		if (xmppConnectionServiceBound) {
 90			xmppConnectionService.removeOnAccountListChangedListener();
 91		}
 92		super.onStop();
 93	}
 94
 95	@Override
 96	void onBackendConnected() {
 97		xmppConnectionService.setOnAccountListChangedListener(accountChanged);
 98		this.accountList.clear();
 99		this.accountList.addAll(xmppConnectionService.getAccounts());
100		mAccountAdapter.notifyDataSetChanged();
101	}
102
103	@Override
104	public boolean onCreateOptionsMenu(Menu menu) {
105		getMenuInflater().inflate(R.menu.manageaccounts, menu);
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		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		default:
139			break;
140		}
141		return super.onOptionsItemSelected(item);
142	}
143
144	@Override
145	public boolean onNavigateUp() {
146		if (xmppConnectionService.getConversations().size() == 0) {
147			Intent contactsIntent = new Intent(this,
148					StartConversationActivity.class);
149			contactsIntent.setFlags(
150			// if activity exists in stack, pop the stack and go back to it
151					Intent.FLAG_ACTIVITY_CLEAR_TOP |
152					// otherwise, make a new task for it
153							Intent.FLAG_ACTIVITY_NEW_TASK |
154							// don't use the new activity animation; finish
155							// animation runs instead
156							Intent.FLAG_ACTIVITY_NO_ANIMATION);
157			startActivity(contactsIntent);
158			finish();
159			return true;
160		} else {
161			return super.onNavigateUp();
162		}
163	}
164
165	private void publishAvatar(Account account) {
166		Intent intent = new Intent(getApplicationContext(),
167				PublishProfilePictureActivity.class);
168		intent.putExtra("account", account.getJid());
169		startActivity(intent);
170	}
171
172	private void disableAccount(Account account) {
173		account.setOption(Account.OPTION_DISABLED, true);
174		xmppConnectionService.updateAccount(account);
175	}
176
177	private void enableAccount(Account account) {
178		account.setOption(Account.OPTION_DISABLED, false);
179		xmppConnectionService.updateAccount(account);
180	}
181
182	private void publishOpenPGPPublicKey(Account account) {
183		if (ManageAccountActivity.this.hasPgp()) {
184			announcePgp(account, null);
185		} else {
186			this.showInstallPgpDialog();
187		}
188	}
189
190	private void deleteAccount(final Account account) {
191		AlertDialog.Builder builder = new AlertDialog.Builder(
192				ManageAccountActivity.this);
193		builder.setTitle(getString(R.string.mgmt_account_are_you_sure));
194		builder.setIconAttribute(android.R.attr.alertDialogIcon);
195		builder.setMessage(getString(R.string.mgmt_account_delete_confirm_text));
196		builder.setPositiveButton(getString(R.string.delete),
197				new OnClickListener() {
198					@Override
199					public void onClick(DialogInterface dialog, int which) {
200						xmppConnectionService.deleteAccount(account);
201						selectedAccount = null;
202					}
203				});
204		builder.setNegativeButton(getString(R.string.cancel), null);
205		builder.create().show();
206	}
207
208	@Override
209	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
210		super.onActivityResult(requestCode, resultCode, data);
211		if (resultCode == RESULT_OK) {
212			if (requestCode == REQUEST_ANNOUNCE_PGP) {
213				announcePgp(selectedAccount, null);
214			}
215		}
216	}
217}