AccountSelectActivity.java 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /* ownCloud Android client application
  2. * Copyright (C) 2012 Bartek Przybylski
  3. * Copyright (C) 2012-2013 ownCloud Inc.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2,
  7. * as published by the Free Software Foundation.
  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.view.ContextMenu;
  33. import android.view.View;
  34. import android.view.ViewGroup;
  35. import android.view.ContextMenu.ContextMenuInfo;
  36. import android.widget.AdapterView.AdapterContextMenuInfo;
  37. import android.widget.CheckedTextView;
  38. import android.widget.ListView;
  39. import android.widget.SimpleAdapter;
  40. import android.widget.TextView;
  41. import com.actionbarsherlock.app.ActionBar;
  42. import com.actionbarsherlock.app.SherlockListActivity;
  43. import com.actionbarsherlock.view.Menu;
  44. import com.actionbarsherlock.view.MenuInflater;
  45. import com.actionbarsherlock.view.MenuItem;
  46. import com.owncloud.android.authentication.AccountAuthenticator;
  47. import com.owncloud.android.authentication.AuthenticatorActivity;
  48. import com.owncloud.android.authentication.AccountUtils;
  49. import com.owncloud.android.ui.activity.FileActivity.AccountCreationCallback;
  50. import com.owncloud.android.Log_OC;
  51. import com.owncloud.android.R;
  52. public class AccountSelectActivity extends SherlockListActivity implements
  53. AccountManagerCallback<Boolean> {
  54. private static final String TAG = "AccountSelectActivity";
  55. private static final String PREVIOUS_ACCOUNT_KEY = "ACCOUNT";
  56. private final Handler mHandler = new Handler();
  57. private Account mPreviousAccount = null;
  58. @Override
  59. protected void onCreate(Bundle savedInstanceState) {
  60. super.onCreate(savedInstanceState);
  61. if (savedInstanceState != null) {
  62. mPreviousAccount = savedInstanceState.getParcelable(PREVIOUS_ACCOUNT_KEY);
  63. } else {
  64. mPreviousAccount = AccountUtils.getCurrentOwnCloudAccount(this);
  65. }
  66. ActionBar action_bar = getSupportActionBar();
  67. action_bar.setDisplayShowTitleEnabled(true);
  68. action_bar.setDisplayHomeAsUpEnabled(false);
  69. }
  70. @Override
  71. protected void onResume() {
  72. super.onResume();
  73. populateAccountList();
  74. }
  75. @Override
  76. protected void onPause() {
  77. super.onPause();
  78. if (this.isFinishing()) {
  79. Account current = AccountUtils.getCurrentOwnCloudAccount(this);
  80. if ((mPreviousAccount == null && current != null) ||
  81. (mPreviousAccount != null && !mPreviousAccount.equals(current))) {
  82. /// the account set as default changed since this activity was created
  83. // trigger synchronization
  84. ContentResolver.cancelSync(null, AccountAuthenticator.AUTHORITY);
  85. Bundle bundle = new Bundle();
  86. bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
  87. ContentResolver.requestSync(AccountUtils.getCurrentOwnCloudAccount(this), AccountAuthenticator.AUTHORITY, bundle);
  88. // restart the main activity
  89. Intent i = new Intent(this, FileDisplayActivity.class);
  90. i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  91. startActivity(i);
  92. }
  93. }
  94. }
  95. @Override
  96. public boolean onCreateOptionsMenu(Menu menu) {
  97. MenuInflater inflater = getSherlock().getMenuInflater();
  98. inflater.inflate(R.menu.account_picker, menu);
  99. return true;
  100. }
  101. @Override
  102. public void onCreateContextMenu(ContextMenu menu, View v,
  103. ContextMenuInfo menuInfo) {
  104. getMenuInflater().inflate(R.menu.account_picker_long_click, menu);
  105. super.onCreateContextMenu(menu, v, menuInfo);
  106. }
  107. @Override
  108. protected void onListItemClick(ListView l, View v, int position, long id) {
  109. String accountName = ((TextView) v.findViewById(android.R.id.text1))
  110. .getText().toString();
  111. AccountUtils.setCurrentOwnCloudAccount(this, accountName);
  112. finish(); // immediate exit
  113. }
  114. @Override
  115. public boolean onMenuItemSelected(int featureId, MenuItem item) {
  116. if (item.getItemId() == R.id.createAccount) {
  117. /*Intent intent = new Intent(
  118. android.provider.Settings.ACTION_ADD_ACCOUNT);
  119. intent.putExtra("authorities",
  120. new String[] { AccountAuthenticator.AUTHORITY });
  121. startActivity(intent);*/
  122. AccountManager am = AccountManager.get(getApplicationContext());
  123. am.addAccount(AccountAuthenticator.ACCOUNT_TYPE,
  124. null,
  125. null,
  126. null,
  127. this,
  128. null,
  129. null);
  130. return true;
  131. }
  132. return false;
  133. }
  134. /**
  135. * Called when the user clicked on an item into the context menu created at
  136. * {@link #onCreateContextMenu(ContextMenu, View, ContextMenuInfo)} for every
  137. * ownCloud {@link Account} , containing 'secondary actions' for them.
  138. *
  139. * {@inheritDoc}}
  140. */
  141. @SuppressWarnings("unchecked")
  142. @Override
  143. public boolean onContextItemSelected(android.view.MenuItem item) {
  144. AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
  145. int index = info.position;
  146. HashMap<String, String> map = null;
  147. try {
  148. map = (HashMap<String, String>) getListAdapter().getItem(index);
  149. } catch (ClassCastException e) {
  150. Log_OC.wtf(TAG, "getitem(index) from list adapter did not return hashmap, bailing out");
  151. return false;
  152. }
  153. String accountName = map.get("NAME");
  154. AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);
  155. Account accounts[] = am.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE);
  156. for (Account a : accounts) {
  157. if (a.name.equals(accountName)) {
  158. if (item.getItemId() == R.id.change_password) {
  159. Intent updateAccountCredentials = new Intent(this, AuthenticatorActivity.class);
  160. updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACCOUNT, a);
  161. updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACTION, AuthenticatorActivity.ACTION_UPDATE_TOKEN);
  162. startActivity(updateAccountCredentials);
  163. } else if (item.getItemId() == R.id.delete_account) {
  164. am.removeAccount(a, this, mHandler);
  165. }
  166. }
  167. }
  168. return true;
  169. }
  170. private void populateAccountList() {
  171. AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);
  172. Account accounts[] = am
  173. .getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE);
  174. LinkedList<HashMap<String, String>> ll = new LinkedList<HashMap<String, String>>();
  175. for (Account a : accounts) {
  176. HashMap<String, String> h = new HashMap<String, String>();
  177. h.put("NAME", a.name);
  178. h.put("VER",
  179. "ownCloud version: "
  180. + am.getUserData(a,
  181. AccountAuthenticator.KEY_OC_VERSION));
  182. ll.add(h);
  183. }
  184. setListAdapter(new AccountCheckedSimpleAdepter(this, ll,
  185. android.R.layout.simple_list_item_single_choice,
  186. new String[] { "NAME" }, new int[] { android.R.id.text1 }));
  187. registerForContextMenu(getListView());
  188. }
  189. @Override
  190. public void run(AccountManagerFuture<Boolean> future) {
  191. if (future.isDone()) {
  192. Account a = AccountUtils.getCurrentOwnCloudAccount(this);
  193. String accountName = "";
  194. if (a == null) {
  195. Account[] accounts = AccountManager.get(this)
  196. .getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE);
  197. if (accounts.length != 0)
  198. accountName = accounts[0].name;
  199. AccountUtils.setCurrentOwnCloudAccount(this, accountName);
  200. }
  201. populateAccountList();
  202. }
  203. }
  204. private class AccountCheckedSimpleAdepter extends SimpleAdapter {
  205. private Account mCurrentAccount;
  206. private List<? extends Map<String, ?>> mPrivateData;
  207. public AccountCheckedSimpleAdepter(Context context,
  208. List<? extends Map<String, ?>> data, int resource,
  209. String[] from, int[] to) {
  210. super(context, data, resource, from, to);
  211. mCurrentAccount = AccountUtils
  212. .getCurrentOwnCloudAccount(AccountSelectActivity.this);
  213. mPrivateData = data;
  214. }
  215. @Override
  216. public View getView(int position, View convertView, ViewGroup parent) {
  217. View v = super.getView(position, convertView, parent);
  218. CheckedTextView ctv = (CheckedTextView) v
  219. .findViewById(android.R.id.text1);
  220. if (mPrivateData.get(position).get("NAME")
  221. .equals(mCurrentAccount.name)) {
  222. ctv.setChecked(true);
  223. }
  224. return v;
  225. }
  226. }
  227. }