Merge branch 'develop' into modify_password
[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 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.AUTHORITY });
140 startActivity(intent);
141 return true;
142 }
143 return false;
144 }
145
146 /**
147 * Called when the user clicked on an item into the context menu created at
148 * {@link #onCreateContextMenu(ContextMenu, View, ContextMenuInfo)} for every
149 * ownCloud {@link Account} , containing 'secondary actions' for them.
150 *
151 * {@inheritDoc}}
152 */
153 @SuppressWarnings("unchecked")
154 @Override
155 public boolean onContextItemSelected(android.view.MenuItem item) {
156 AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
157 int index = info.position;
158 HashMap<String, String> map = null;
159 try {
160 map = (HashMap<String, String>) getListAdapter().getItem(index);
161 } catch (ClassCastException e) {
162 Log_OC.wtf(TAG, "getitem(index) from list adapter did not return hashmap, bailing out");
163 return false;
164 }
165
166 String accountName = map.get("NAME");
167 AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);
168 Account accounts[] = am.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE);
169 for (Account a : accounts) {
170 if (a.name.equals(accountName)) {
171 if (item.getItemId() == R.id.change_password) {
172 Intent updateAccountCredentials = new Intent(this, AuthenticatorActivity.class);
173 updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACCOUNT, a);
174 updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACTION, AuthenticatorActivity.ACTION_UPDATE_TOKEN);
175 startActivity(updateAccountCredentials);
176
177 } else if (item.getItemId() == R.id.delete_account) {
178 am.removeAccount(a, this, mHandler);
179 }
180 }
181 }
182
183 return true;
184 }
185
186 private void populateAccountList() {
187 AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);
188 Account accounts[] = am
189 .getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE);
190 LinkedList<HashMap<String, String>> ll = new LinkedList<HashMap<String, String>>();
191 for (Account a : accounts) {
192 HashMap<String, String> h = new HashMap<String, String>();
193 h.put("NAME", a.name);
194 h.put("VER",
195 "ownCloud version: "
196 + am.getUserData(a,
197 AccountAuthenticator.KEY_OC_VERSION));
198 ll.add(h);
199 }
200
201 setListAdapter(new AccountCheckedSimpleAdepter(this, ll,
202 android.R.layout.simple_list_item_single_choice,
203 new String[] { "NAME" }, new int[] { android.R.id.text1 }));
204 registerForContextMenu(getListView());
205 }
206
207 @Override
208 public void run(AccountManagerFuture<Boolean> future) {
209 if (future.isDone()) {
210 Account a = AccountUtils.getCurrentOwnCloudAccount(this);
211 String accountName = "";
212 if (a == null) {
213 Account[] accounts = AccountManager.get(this)
214 .getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE);
215 if (accounts.length != 0)
216 accountName = accounts[0].name;
217 AccountUtils.setCurrentOwnCloudAccount(this, accountName);
218 }
219 populateAccountList();
220 }
221 }
222
223 private class AccountCheckedSimpleAdepter extends SimpleAdapter {
224 private Account mCurrentAccount;
225 private List<? extends Map<String, ?>> mPrivateData;
226
227 public AccountCheckedSimpleAdepter(Context context,
228 List<? extends Map<String, ?>> data, int resource,
229 String[] from, int[] to) {
230 super(context, data, resource, from, to);
231 mCurrentAccount = AccountUtils
232 .getCurrentOwnCloudAccount(AccountSelectActivity.this);
233 mPrivateData = data;
234 }
235
236 @Override
237 public View getView(int position, View convertView, ViewGroup parent) {
238 View v = super.getView(position, convertView, parent);
239 CheckedTextView ctv = (CheckedTextView) v
240 .findViewById(android.R.id.text1);
241 if (mPrivateData.get(position).get("NAME")
242 .equals(mCurrentAccount.name)) {
243 ctv.setChecked(true);
244 }
245 return v;
246 }
247
248 }
249
250 }