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