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