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