AccountSelectActivity.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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.AuthenticatorActivity;
  47. import com.owncloud.android.authentication.AccountUtils;
  48. import com.owncloud.android.oc_framework.accounts.OwnCloudAccount;
  49. import com.owncloud.android.utils.Log_OC;
  50. import com.owncloud.android.MainApp;
  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. // restart the main activity
  84. Intent i = new Intent(this, FileDisplayActivity.class);
  85. i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  86. startActivity(i);
  87. }
  88. }
  89. }
  90. @Override
  91. public boolean onCreateOptionsMenu(Menu menu) {
  92. // Show Create Account if Multiaccount is enabled
  93. if (getResources().getBoolean(R.bool.multiaccount_support)) {
  94. MenuInflater inflater = getSherlock().getMenuInflater();
  95. inflater.inflate(R.menu.account_picker, menu);
  96. }
  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[] { MainApp.getAuthTokenType() });
  119. startActivity(intent);*/
  120. AccountManager am = AccountManager.get(getApplicationContext());
  121. am.addAccount(MainApp.getAccountType(),
  122. null,
  123. null,
  124. null,
  125. this,
  126. null,
  127. null);
  128. return true;
  129. }
  130. return false;
  131. }
  132. /**
  133. * Called when the user clicked on an item into the context menu created at
  134. * {@link #onCreateContextMenu(ContextMenu, View, ContextMenuInfo)} for every
  135. * ownCloud {@link Account} , containing 'secondary actions' for them.
  136. *
  137. * {@inheritDoc}}
  138. */
  139. @SuppressWarnings("unchecked")
  140. @Override
  141. public boolean onContextItemSelected(android.view.MenuItem item) {
  142. AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
  143. int index = info.position;
  144. HashMap<String, String> map = null;
  145. try {
  146. map = (HashMap<String, String>) getListAdapter().getItem(index);
  147. } catch (ClassCastException e) {
  148. Log_OC.wtf(TAG, "getitem(index) from list adapter did not return hashmap, bailing out");
  149. return false;
  150. }
  151. String accountName = map.get("NAME");
  152. AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);
  153. Account accounts[] = am.getAccountsByType(MainApp.getAccountType());
  154. for (Account a : accounts) {
  155. if (a.name.equals(accountName)) {
  156. if (item.getItemId() == R.id.change_password) {
  157. Intent updateAccountCredentials = new Intent(this, AuthenticatorActivity.class);
  158. updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACCOUNT, a);
  159. updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACTION, AuthenticatorActivity.ACTION_UPDATE_TOKEN);
  160. startActivity(updateAccountCredentials);
  161. } else if (item.getItemId() == R.id.delete_account) {
  162. am.removeAccount(a, this, mHandler);
  163. }
  164. }
  165. }
  166. return true;
  167. }
  168. private void populateAccountList() {
  169. AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);
  170. Account accounts[] = am
  171. .getAccountsByType(MainApp.getAccountType());
  172. if (am.getAccountsByType(MainApp.getAccountType()).length == 0) {
  173. // Show create account screen if there isn't any account
  174. am.addAccount(MainApp.getAccountType(),
  175. null,
  176. null,
  177. null,
  178. this,
  179. null,
  180. null);
  181. }
  182. else {
  183. LinkedList<HashMap<String, String>> ll = new LinkedList<HashMap<String, String>>();
  184. for (Account a : accounts) {
  185. HashMap<String, String> h = new HashMap<String, String>();
  186. h.put("NAME", a.name);
  187. h.put("VER",
  188. "ownCloud version: "
  189. + am.getUserData(a,
  190. OwnCloudAccount.Constants.KEY_OC_VERSION));
  191. ll.add(h);
  192. }
  193. setListAdapter(new AccountCheckedSimpleAdepter(this, ll,
  194. android.R.layout.simple_list_item_single_choice,
  195. new String[] { "NAME" }, new int[] { android.R.id.text1 }));
  196. registerForContextMenu(getListView());
  197. }
  198. }
  199. @Override
  200. public void run(AccountManagerFuture<Boolean> future) {
  201. if (future.isDone()) {
  202. Account a = AccountUtils.getCurrentOwnCloudAccount(this);
  203. String accountName = "";
  204. if (a == null) {
  205. Account[] accounts = AccountManager.get(this)
  206. .getAccountsByType(MainApp.getAccountType());
  207. if (accounts.length != 0)
  208. accountName = accounts[0].name;
  209. AccountUtils.setCurrentOwnCloudAccount(this, accountName);
  210. }
  211. populateAccountList();
  212. }
  213. }
  214. private class AccountCheckedSimpleAdepter extends SimpleAdapter {
  215. private Account mCurrentAccount;
  216. private List<? extends Map<String, ?>> mPrivateData;
  217. public AccountCheckedSimpleAdepter(Context context,
  218. List<? extends Map<String, ?>> data, int resource,
  219. String[] from, int[] to) {
  220. super(context, data, resource, from, to);
  221. mCurrentAccount = AccountUtils
  222. .getCurrentOwnCloudAccount(AccountSelectActivity.this);
  223. mPrivateData = data;
  224. }
  225. @Override
  226. public View getView(int position, View convertView, ViewGroup parent) {
  227. View v = super.getView(position, convertView, parent);
  228. CheckedTextView ctv = (CheckedTextView) v
  229. .findViewById(android.R.id.text1);
  230. if (mPrivateData.get(position).get("NAME")
  231. .equals(mCurrentAccount.name)) {
  232. ctv.setChecked(true);
  233. }
  234. return v;
  235. }
  236. }
  237. }