OC-1770: When deleting the account, log in screen is not shown
[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 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
19 package com.owncloud.android.ui.activity;
20
21 import java.util.HashMap;
22 import java.util.LinkedList;
23 import java.util.List;
24 import java.util.Map;
25
26 import android.accounts.Account;
27 import android.accounts.AccountManager;
28 import android.accounts.AccountManagerCallback;
29 import android.accounts.AccountManagerFuture;
30 import android.content.ContentResolver;
31 import android.content.Context;
32 import android.content.Intent;
33 import android.os.Bundle;
34 import android.os.Handler;
35 import android.view.ContextMenu;
36 import android.view.View;
37 import android.view.ViewGroup;
38 import android.view.ContextMenu.ContextMenuInfo;
39 import android.widget.AdapterView.AdapterContextMenuInfo;
40 import android.widget.CheckedTextView;
41 import android.widget.ListView;
42 import android.widget.SimpleAdapter;
43 import android.widget.TextView;
44
45 import com.actionbarsherlock.app.ActionBar;
46 import com.actionbarsherlock.app.SherlockListActivity;
47 import com.actionbarsherlock.view.Menu;
48 import com.actionbarsherlock.view.MenuInflater;
49 import com.actionbarsherlock.view.MenuItem;
50 import com.owncloud.android.authentication.AccountAuthenticator;
51 import com.owncloud.android.authentication.AuthenticatorActivity;
52 import com.owncloud.android.authentication.AccountUtils;
53 import com.owncloud.android.Log_OC;
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.AUTHORITY);
99 Bundle bundle = new Bundle();
100 bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
101 ContentResolver.requestSync(AccountUtils.getCurrentOwnCloudAccount(this), AccountAuthenticator.AUTHORITY, 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 // Show Create Account if Multiaccount is enabled
114 if (getResources().getBoolean(R.bool.multiaccount_support)) {
115 MenuInflater inflater = getSherlock().getMenuInflater();
116 inflater.inflate(R.menu.account_picker, menu);
117 }
118 return true;
119 }
120
121 @Override
122 public void onCreateContextMenu(ContextMenu menu, View v,
123 ContextMenuInfo menuInfo) {
124 getMenuInflater().inflate(R.menu.account_picker_long_click, menu);
125 super.onCreateContextMenu(menu, v, menuInfo);
126 }
127
128 @Override
129 protected void onListItemClick(ListView l, View v, int position, long id) {
130 String accountName = ((TextView) v.findViewById(android.R.id.text1))
131 .getText().toString();
132 AccountUtils.setCurrentOwnCloudAccount(this, accountName);
133 finish(); // immediate exit
134 }
135
136 @Override
137 public boolean onMenuItemSelected(int featureId, MenuItem item) {
138 if (item.getItemId() == R.id.createAccount) {
139 /*Intent intent = new Intent(
140 android.provider.Settings.ACTION_ADD_ACCOUNT);
141 intent.putExtra("authorities",
142 new String[] { AccountAuthenticator.AUTHORITY });
143 startActivity(intent);*/
144 AccountManager am = AccountManager.get(getApplicationContext());
145 am.addAccount(AccountAuthenticator.ACCOUNT_TYPE,
146 null,
147 null,
148 null,
149 this,
150 null,
151 null);
152 return true;
153 }
154 return false;
155 }
156
157 /**
158 * Called when the user clicked on an item into the context menu created at
159 * {@link #onCreateContextMenu(ContextMenu, View, ContextMenuInfo)} for every
160 * ownCloud {@link Account} , containing 'secondary actions' for them.
161 *
162 * {@inheritDoc}}
163 */
164 @SuppressWarnings("unchecked")
165 @Override
166 public boolean onContextItemSelected(android.view.MenuItem item) {
167 AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
168 int index = info.position;
169 HashMap<String, String> map = null;
170 try {
171 map = (HashMap<String, String>) getListAdapter().getItem(index);
172 } catch (ClassCastException e) {
173 Log_OC.wtf(TAG, "getitem(index) from list adapter did not return hashmap, bailing out");
174 return false;
175 }
176
177 String accountName = map.get("NAME");
178 AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);
179 Account accounts[] = am.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE);
180 for (Account a : accounts) {
181 if (a.name.equals(accountName)) {
182 if (item.getItemId() == R.id.change_password) {
183 Intent updateAccountCredentials = new Intent(this, AuthenticatorActivity.class);
184 updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACCOUNT, a);
185 updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACTION, AuthenticatorActivity.ACTION_UPDATE_TOKEN);
186 startActivity(updateAccountCredentials);
187
188 } else if (item.getItemId() == R.id.delete_account) {
189 am.removeAccount(a, this, mHandler);
190 }
191 }
192 }
193
194 return true;
195 }
196
197 @Override
198 public void onContentChanged() {
199 // TODO Auto-generated method stub
200 super.onContentChanged();
201 AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);
202 if (am.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE).length == 0) {
203 // Show create account screen
204 am.addAccount(AccountAuthenticator.ACCOUNT_TYPE,
205 null,
206 null,
207 null,
208 this,
209 null,
210 null);
211 }
212 }
213
214 private void populateAccountList() {
215 AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);
216 Account accounts[] = am
217 .getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE);
218 if (am.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE).length == 0) {
219 // Show create account screen if there isn't any account
220 am.addAccount(AccountAuthenticator.ACCOUNT_TYPE,
221 null,
222 null,
223 null,
224 this,
225 null,
226 null);
227 }
228 else {
229 LinkedList<HashMap<String, String>> ll = new LinkedList<HashMap<String, String>>();
230 for (Account a : accounts) {
231 HashMap<String, String> h = new HashMap<String, String>();
232 h.put("NAME", a.name);
233 h.put("VER",
234 "ownCloud version: "
235 + am.getUserData(a,
236 AccountAuthenticator.KEY_OC_VERSION));
237 ll.add(h);
238 }
239
240 setListAdapter(new AccountCheckedSimpleAdepter(this, ll,
241 android.R.layout.simple_list_item_single_choice,
242 new String[] { "NAME" }, new int[] { android.R.id.text1 }));
243 registerForContextMenu(getListView());
244 }
245 }
246
247 @Override
248 public void run(AccountManagerFuture<Boolean> future) {
249 if (future.isDone()) {
250 Account a = AccountUtils.getCurrentOwnCloudAccount(this);
251 String accountName = "";
252 if (a == null) {
253 Account[] accounts = AccountManager.get(this)
254 .getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE);
255 if (accounts.length != 0)
256 accountName = accounts[0].name;
257 AccountUtils.setCurrentOwnCloudAccount(this, accountName);
258 }
259 populateAccountList();
260 }
261 }
262
263 private class AccountCheckedSimpleAdepter extends SimpleAdapter {
264 private Account mCurrentAccount;
265 private List<? extends Map<String, ?>> mPrivateData;
266
267 public AccountCheckedSimpleAdepter(Context context,
268 List<? extends Map<String, ?>> data, int resource,
269 String[] from, int[] to) {
270 super(context, data, resource, from, to);
271 mCurrentAccount = AccountUtils
272 .getCurrentOwnCloudAccount(AccountSelectActivity.this);
273 mPrivateData = data;
274 }
275
276 @Override
277 public View getView(int position, View convertView, ViewGroup parent) {
278 View v = super.getView(position, convertView, parent);
279 CheckedTextView ctv = (CheckedTextView) v
280 .findViewById(android.R.id.text1);
281 if (mPrivateData.get(position).get("NAME")
282 .equals(mCurrentAccount.name)) {
283 ctv.setChecked(true);
284 }
285 return v;
286 }
287
288 }
289
290 }