+ @Override
+ protected void onListItemClick(ListView l, View v, int position, long id) {
+ String accountName = ((TextView) v.findViewById(android.R.id.text1))
+ .getText().toString();
+ AccountUtils.setCurrentOwnCloudAccount(this, accountName);
+ Intent i = new Intent(this, FileDisplayActivity.class);
+ i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
+ startActivity(i);
+ finish();
+ }
+
+ @Override
+ public boolean onMenuItemSelected(int featureId, MenuItem item) {
+ if (item.getItemId() == R.id.createAccount) {
+ Intent intent = new Intent(
+ android.provider.Settings.ACTION_ADD_ACCOUNT);
+ intent.putExtra("authorities",
+ new String[] { AccountAuthenticator.AUTH_TOKEN_TYPE });
+ startActivity(intent);
+ return true;
+ }
+ return false;
+ }
+
+ @Override
+ public boolean onContextItemSelected(android.view.MenuItem item) {
+ AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
+ .getMenuInfo();
+ int index = info.position;
+ HashMap<String, String> map = (HashMap<String, String>) getListAdapter()
+ .getItem(index);
+ String accountName = map.get("NAME");
+ AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);
+ Account accounts[] = am
+ .getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE);
+ for (Account a : accounts) {
+ if (a.name.equals(accountName)) {
+ am.removeAccount(a, this, mHandler);
+ }
+ }
+
+ return false;
+ }
+
+ private void populateAccountList() {
+ AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);
+ Account accounts[] = am
+ .getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE);
+ LinkedList<HashMap<String, String>> ll = new LinkedList<HashMap<String, String>>();
+ for (Account a : accounts) {
+ HashMap<String, String> h = new HashMap<String, String>();
+ h.put("NAME", a.name);
+ h.put("VER",
+ "ownCloud version: "
+ + am.getUserData(a,
+ AccountAuthenticator.KEY_OC_VERSION));
+ ll.add(h);
+ }
+
+ setListAdapter(new AccountCheckedSimpleAdepter(this, ll,
+ android.R.layout.simple_list_item_single_choice,
+ new String[] { "NAME" }, new int[] { android.R.id.text1 }));
+ registerForContextMenu(getListView());
+ }
+
+ @Override
+ public void run(AccountManagerFuture<Boolean> future) {
+ if (future.isDone()) {
+ Account a = AccountUtils.getCurrentOwnCloudAccount(this);
+ String accountName = "";
+ if (a == null) {
+ Account[] accounts = AccountManager.get(this)
+ .getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE);
+ if (accounts.length != 0)
+ accountName = accounts[0].name;
+ AccountUtils.setCurrentOwnCloudAccount(this, accountName);
+ }
+ populateAccountList();
+ }
+ }
+
+ private class AccountCheckedSimpleAdepter extends SimpleAdapter {
+ private Account mCurrentAccount;
+ private List<? extends Map<String, ?>> mPrivateData;
+
+ public AccountCheckedSimpleAdepter(Context context,
+ List<? extends Map<String, ?>> data, int resource,
+ String[] from, int[] to) {
+ super(context, data, resource, from, to);
+ mCurrentAccount = AccountUtils
+ .getCurrentOwnCloudAccount(AccountSelectActivity.this);
+ mPrivateData = data;
+ }
+
+ @Override
+ public View getView(int position, View convertView, ViewGroup parent) {
+ View v = super.getView(position, convertView, parent);
+ CheckedTextView ctv = (CheckedTextView) v
+ .findViewById(android.R.id.text1);
+ if (mPrivateData.get(position).get("NAME")
+ .equals(mCurrentAccount.name)) {
+ ctv.setChecked(true);
+ }
+ return v;
+ }
+
+ }
+
+}