Updated references to reordered classes in owncloud-android-library
[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.Context;
31 import android.content.Intent;
32 import android.os.Bundle;
33 import android.os.Handler;
34 import android.view.ContextMenu;
35 import android.view.View;
36 import android.view.ViewGroup;
37 import android.view.ContextMenu.ContextMenuInfo;
38 import android.widget.AdapterView.AdapterContextMenuInfo;
39 import android.widget.CheckedTextView;
40 import android.widget.ListView;
41 import android.widget.SimpleAdapter;
42 import android.widget.TextView;
43
44 import com.actionbarsherlock.app.ActionBar;
45 import com.actionbarsherlock.app.SherlockListActivity;
46 import com.actionbarsherlock.view.Menu;
47 import com.actionbarsherlock.view.MenuInflater;
48 import com.actionbarsherlock.view.MenuItem;
49 import com.owncloud.android.authentication.AuthenticatorActivity;
50 import com.owncloud.android.authentication.AccountUtils;
51 import com.owncloud.android.lib.common.accounts.AccountUtils.Constants;
52 import com.owncloud.android.utils.DisplayUtils;
53 import com.owncloud.android.utils.Log_OC;
54 import com.owncloud.android.MainApp;
55 import com.owncloud.android.R;
56
57
58 public class AccountSelectActivity extends SherlockListActivity implements
59 AccountManagerCallback<Boolean> {
60
61 private static final String TAG = "AccountSelectActivity";
62
63 private static final String PREVIOUS_ACCOUNT_KEY = "ACCOUNT";
64
65 private final Handler mHandler = new Handler();
66 private Account mPreviousAccount = null;
67
68 @Override
69 protected void onCreate(Bundle savedInstanceState) {
70 super.onCreate(savedInstanceState);
71
72 if (savedInstanceState != null) {
73 mPreviousAccount = savedInstanceState.getParcelable(PREVIOUS_ACCOUNT_KEY);
74 } else {
75 mPreviousAccount = AccountUtils.getCurrentOwnCloudAccount(this);
76 }
77
78 ActionBar actionBar = getSupportActionBar();
79 actionBar.setIcon(DisplayUtils.getSeasonalIconId());
80 actionBar.setDisplayShowTitleEnabled(true);
81 actionBar.setDisplayHomeAsUpEnabled(false);
82 }
83
84 @Override
85 protected void onResume() {
86 super.onResume();
87 populateAccountList();
88 }
89
90 @Override
91 protected void onPause() {
92 super.onPause();
93 if (this.isFinishing()) {
94 Account current = AccountUtils.getCurrentOwnCloudAccount(this);
95 if ((mPreviousAccount == null && current != null) ||
96 (mPreviousAccount != null && !mPreviousAccount.equals(current))) {
97 /// the account set as default changed since this activity was created
98
99 // restart the main activity
100 Intent i = new Intent(this, FileDisplayActivity.class);
101 i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
102 startActivity(i);
103 }
104 }
105 }
106
107 @Override
108 public boolean onCreateOptionsMenu(Menu menu) {
109 // Show Create Account if Multiaccount is enabled
110 if (getResources().getBoolean(R.bool.multiaccount_support)) {
111 MenuInflater inflater = getSherlock().getMenuInflater();
112 inflater.inflate(R.menu.account_picker, menu);
113 }
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[] { MainApp.getAuthTokenType() });
139 startActivity(intent);*/
140 AccountManager am = AccountManager.get(getApplicationContext());
141 am.addAccount(MainApp.getAccountType(),
142 null,
143 null,
144 null,
145 this,
146 null,
147 null);
148 return true;
149 }
150 return false;
151 }
152
153 /**
154 * Called when the user clicked on an item into the context menu created at
155 * {@link #onCreateContextMenu(ContextMenu, View, ContextMenuInfo)} for every
156 * ownCloud {@link Account} , containing 'secondary actions' for them.
157 *
158 * {@inheritDoc}}
159 */
160 @SuppressWarnings("unchecked")
161 @Override
162 public boolean onContextItemSelected(android.view.MenuItem item) {
163 AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
164 int index = info.position;
165 HashMap<String, String> map = null;
166 try {
167 map = (HashMap<String, String>) getListAdapter().getItem(index);
168 } catch (ClassCastException e) {
169 Log_OC.wtf(TAG, "getitem(index) from list adapter did not return hashmap, bailing out");
170 return false;
171 }
172
173 String accountName = map.get("NAME");
174 AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);
175 Account accounts[] = am.getAccountsByType(MainApp.getAccountType());
176 for (Account a : accounts) {
177 if (a.name.equals(accountName)) {
178 if (item.getItemId() == R.id.change_password) {
179 Intent updateAccountCredentials = new Intent(this, AuthenticatorActivity.class);
180 updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACCOUNT, a);
181 updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACTION, AuthenticatorActivity.ACTION_UPDATE_TOKEN);
182 startActivity(updateAccountCredentials);
183
184 } else if (item.getItemId() == R.id.delete_account) {
185 am.removeAccount(a, this, mHandler);
186 }
187 }
188 }
189
190 return true;
191 }
192
193 private void populateAccountList() {
194 AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);
195 Account accounts[] = am
196 .getAccountsByType(MainApp.getAccountType());
197 if (am.getAccountsByType(MainApp.getAccountType()).length == 0) {
198 // Show create account screen if there isn't any account
199 am.addAccount(MainApp.getAccountType(),
200 null,
201 null,
202 null,
203 this,
204 null,
205 null);
206 }
207 else {
208 LinkedList<HashMap<String, String>> ll = new LinkedList<HashMap<String, String>>();
209 for (Account a : accounts) {
210 HashMap<String, String> h = new HashMap<String, String>();
211 h.put("NAME", a.name);
212 h.put("VER",
213 "ownCloud version: "
214 + am.getUserData(a,
215 Constants.KEY_OC_VERSION));
216 ll.add(h);
217 }
218
219 setListAdapter(new AccountCheckedSimpleAdepter(this, ll,
220 android.R.layout.simple_list_item_single_choice,
221 new String[] { "NAME" }, new int[] { android.R.id.text1 }));
222 registerForContextMenu(getListView());
223 }
224 }
225
226 @Override
227 public void run(AccountManagerFuture<Boolean> future) {
228 if (future.isDone()) {
229 Account a = AccountUtils.getCurrentOwnCloudAccount(this);
230 String accountName = "";
231 if (a == null) {
232 Account[] accounts = AccountManager.get(this)
233 .getAccountsByType(MainApp.getAccountType());
234 if (accounts.length != 0)
235 accountName = accounts[0].name;
236 AccountUtils.setCurrentOwnCloudAccount(this, accountName);
237 }
238 populateAccountList();
239 }
240 }
241
242 private class AccountCheckedSimpleAdepter extends SimpleAdapter {
243 private Account mCurrentAccount;
244 private List<? extends Map<String, ?>> mPrivateData;
245
246 public AccountCheckedSimpleAdepter(Context context,
247 List<? extends Map<String, ?>> data, int resource,
248 String[] from, int[] to) {
249 super(context, data, resource, from, to);
250 mCurrentAccount = AccountUtils
251 .getCurrentOwnCloudAccount(AccountSelectActivity.this);
252 mPrivateData = data;
253 }
254
255 @Override
256 public View getView(int position, View convertView, ViewGroup parent) {
257 View v = super.getView(position, convertView, parent);
258 CheckedTextView ctv = (CheckedTextView) v
259 .findViewById(android.R.id.text1);
260 if (mPrivateData.get(position).get("NAME")
261 .equals(mCurrentAccount.name)) {
262 ctv.setChecked(true);
263 }
264 return v;
265 }
266
267 }
268
269 }