AccountSelectActivity.java 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /* ownCloud Android client application
  2. * Copyright (C) 2012 Bartek Przybylski
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. */
  18. package com.owncloud.android.ui.activity;
  19. import java.util.HashMap;
  20. import java.util.LinkedList;
  21. import java.util.List;
  22. import java.util.Map;
  23. import android.accounts.Account;
  24. import android.accounts.AccountManager;
  25. import android.accounts.AccountManagerCallback;
  26. import android.accounts.AccountManagerFuture;
  27. import android.content.ContentResolver;
  28. import android.content.Context;
  29. import android.content.Intent;
  30. import android.os.Bundle;
  31. import android.os.Handler;
  32. import android.util.Log;
  33. import android.view.ContextMenu;
  34. import android.view.View;
  35. import android.view.ViewGroup;
  36. import android.view.ContextMenu.ContextMenuInfo;
  37. import android.widget.AdapterView.AdapterContextMenuInfo;
  38. import android.widget.CheckedTextView;
  39. import android.widget.ListView;
  40. import android.widget.SimpleAdapter;
  41. import android.widget.TextView;
  42. import com.actionbarsherlock.app.ActionBar;
  43. import com.actionbarsherlock.app.SherlockListActivity;
  44. import com.actionbarsherlock.view.Menu;
  45. import com.actionbarsherlock.view.MenuInflater;
  46. import com.actionbarsherlock.view.MenuItem;
  47. import com.owncloud.android.AccountUtils;
  48. import com.owncloud.android.authenticator.AccountAuthenticator;
  49. import com.owncloud.android.R;
  50. public class AccountSelectActivity extends SherlockListActivity implements
  51. AccountManagerCallback<Boolean> {
  52. private static final String TAG = "AccountSelectActivity";
  53. private static final String PREVIOUS_ACCOUNT_KEY = "ACCOUNT";
  54. private final Handler mHandler = new Handler();
  55. private Account mPreviousAccount = null;
  56. @Override
  57. protected void onCreate(Bundle savedInstanceState) {
  58. super.onCreate(savedInstanceState);
  59. if (savedInstanceState != null) {
  60. mPreviousAccount = savedInstanceState.getParcelable(PREVIOUS_ACCOUNT_KEY);
  61. } else {
  62. mPreviousAccount = AccountUtils.getCurrentOwnCloudAccount(this);
  63. }
  64. ActionBar action_bar = getSupportActionBar();
  65. action_bar.setDisplayShowTitleEnabled(true);
  66. action_bar.setDisplayHomeAsUpEnabled(false);
  67. }
  68. @Override
  69. protected void onResume() {
  70. super.onResume();
  71. populateAccountList();
  72. }
  73. @Override
  74. protected void onPause() {
  75. super.onPause();
  76. if (this.isFinishing()) {
  77. Account current = AccountUtils.getCurrentOwnCloudAccount(this);
  78. if ((mPreviousAccount == null && current != null) ||
  79. (mPreviousAccount != null && !mPreviousAccount.equals(current))) {
  80. /// the account set as default changed since this activity was created
  81. // trigger synchronization
  82. ContentResolver.cancelSync(null, AccountAuthenticator.AUTH_TOKEN_TYPE);
  83. Bundle bundle = new Bundle();
  84. bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
  85. ContentResolver.requestSync(AccountUtils.getCurrentOwnCloudAccount(this), AccountAuthenticator.AUTH_TOKEN_TYPE, bundle);
  86. // restart the main activity
  87. Intent i = new Intent(this, FileDisplayActivity.class);
  88. i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  89. startActivity(i);
  90. }
  91. }
  92. }
  93. @Override
  94. public boolean onCreateOptionsMenu(Menu menu) {
  95. MenuInflater inflater = getSherlock().getMenuInflater();
  96. inflater.inflate(R.menu.account_picker, menu);
  97. return true;
  98. }
  99. @Override
  100. public void onCreateContextMenu(ContextMenu menu, View v,
  101. ContextMenuInfo menuInfo) {
  102. getMenuInflater().inflate(R.menu.account_picker_long_click, menu);
  103. super.onCreateContextMenu(menu, v, menuInfo);
  104. }
  105. @Override
  106. protected void onListItemClick(ListView l, View v, int position, long id) {
  107. String accountName = ((TextView) v.findViewById(android.R.id.text1))
  108. .getText().toString();
  109. AccountUtils.setCurrentOwnCloudAccount(this, accountName);
  110. finish(); // immediate exit
  111. }
  112. @Override
  113. public boolean onMenuItemSelected(int featureId, MenuItem item) {
  114. if (item.getItemId() == R.id.createAccount) {
  115. Intent intent = new Intent(
  116. android.provider.Settings.ACTION_ADD_ACCOUNT);
  117. intent.putExtra("authorities",
  118. new String[] { AccountAuthenticator.AUTH_TOKEN_TYPE });
  119. startActivity(intent);
  120. return true;
  121. }
  122. return false;
  123. }
  124. @SuppressWarnings("unchecked")
  125. @Override
  126. public boolean onContextItemSelected(android.view.MenuItem item) {
  127. AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
  128. .getMenuInfo();
  129. int index = info.position;
  130. HashMap<String, String> map = null;
  131. try {
  132. map = (HashMap<String, String>) getListAdapter().getItem(index);
  133. } catch (ClassCastException e) {
  134. Log.wtf(TAG, "getitem(index) from list adapter did not return hashmap, bailing out");
  135. return false;
  136. }
  137. String accountName = map.get("NAME");
  138. AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);
  139. Account accounts[] = am
  140. .getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE);
  141. for (Account a : accounts) {
  142. if (a.name.equals(accountName)) {
  143. am.removeAccount(a, this, mHandler);
  144. }
  145. }
  146. return true;
  147. }
  148. private void populateAccountList() {
  149. AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);
  150. Account accounts[] = am
  151. .getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE);
  152. LinkedList<HashMap<String, String>> ll = new LinkedList<HashMap<String, String>>();
  153. for (Account a : accounts) {
  154. HashMap<String, String> h = new HashMap<String, String>();
  155. h.put("NAME", a.name);
  156. h.put("VER",
  157. "ownCloud version: "
  158. + am.getUserData(a,
  159. AccountAuthenticator.KEY_OC_VERSION));
  160. ll.add(h);
  161. }
  162. setListAdapter(new AccountCheckedSimpleAdepter(this, ll,
  163. android.R.layout.simple_list_item_single_choice,
  164. new String[] { "NAME" }, new int[] { android.R.id.text1 }));
  165. registerForContextMenu(getListView());
  166. }
  167. @Override
  168. public void run(AccountManagerFuture<Boolean> future) {
  169. if (future.isDone()) {
  170. Account a = AccountUtils.getCurrentOwnCloudAccount(this);
  171. String accountName = "";
  172. if (a == null) {
  173. Account[] accounts = AccountManager.get(this)
  174. .getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE);
  175. if (accounts.length != 0)
  176. accountName = accounts[0].name;
  177. AccountUtils.setCurrentOwnCloudAccount(this, accountName);
  178. }
  179. populateAccountList();
  180. }
  181. }
  182. private class AccountCheckedSimpleAdepter extends SimpleAdapter {
  183. private Account mCurrentAccount;
  184. private List<? extends Map<String, ?>> mPrivateData;
  185. public AccountCheckedSimpleAdepter(Context context,
  186. List<? extends Map<String, ?>> data, int resource,
  187. String[] from, int[] to) {
  188. super(context, data, resource, from, to);
  189. mCurrentAccount = AccountUtils
  190. .getCurrentOwnCloudAccount(AccountSelectActivity.this);
  191. mPrivateData = data;
  192. }
  193. @Override
  194. public View getView(int position, View convertView, ViewGroup parent) {
  195. View v = super.getView(position, convertView, parent);
  196. CheckedTextView ctv = (CheckedTextView) v
  197. .findViewById(android.R.id.text1);
  198. if (mPrivateData.get(position).get("NAME")
  199. .equals(mCurrentAccount.name)) {
  200. ctv.setChecked(true);
  201. }
  202. return v;
  203. }
  204. }
  205. }