OC-2332: rewrite getAvailableRemotePath from UpdateFileOperation, using ExistenceCehe...
[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.AuthenticatorActivity;
51 import com.owncloud.android.authentication.AccountUtils;
52 import com.owncloud.android.oc_framework.accounts.OwnCloudAccount;
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 action_bar = getSupportActionBar();
79 action_bar.setDisplayShowTitleEnabled(true);
80 action_bar.setDisplayHomeAsUpEnabled(false);
81 }
82
83 @Override
84 protected void onResume() {
85 super.onResume();
86 populateAccountList();
87 }
88
89 @Override
90 protected void onPause() {
91 super.onPause();
92 if (this.isFinishing()) {
93 Account current = AccountUtils.getCurrentOwnCloudAccount(this);
94 if ((mPreviousAccount == null && current != null) ||
95 (mPreviousAccount != null && !mPreviousAccount.equals(current))) {
96 /// the account set as default changed since this activity was created
97
98 // restart the main activity
99 Intent i = new Intent(this, FileDisplayActivity.class);
100 i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
101 startActivity(i);
102 }
103 }
104 }
105
106 @Override
107 public boolean onCreateOptionsMenu(Menu menu) {
108 // Show Create Account if Multiaccount is enabled
109 if (getResources().getBoolean(R.bool.multiaccount_support)) {
110 MenuInflater inflater = getSherlock().getMenuInflater();
111 inflater.inflate(R.menu.account_picker, menu);
112 }
113 return true;
114 }
115
116 @Override
117 public void onCreateContextMenu(ContextMenu menu, View v,
118 ContextMenuInfo menuInfo) {
119 getMenuInflater().inflate(R.menu.account_picker_long_click, menu);
120 super.onCreateContextMenu(menu, v, menuInfo);
121 }
122
123 @Override
124 protected void onListItemClick(ListView l, View v, int position, long id) {
125 String accountName = ((TextView) v.findViewById(android.R.id.text1))
126 .getText().toString();
127 AccountUtils.setCurrentOwnCloudAccount(this, accountName);
128 finish(); // immediate exit
129 }
130
131 @Override
132 public boolean onMenuItemSelected(int featureId, MenuItem item) {
133 if (item.getItemId() == R.id.createAccount) {
134 /*Intent intent = new Intent(
135 android.provider.Settings.ACTION_ADD_ACCOUNT);
136 intent.putExtra("authorities",
137 new String[] { MainApp.getAuthTokenType() });
138 startActivity(intent);*/
139 AccountManager am = AccountManager.get(getApplicationContext());
140 am.addAccount(MainApp.getAccountType(),
141 null,
142 null,
143 null,
144 this,
145 null,
146 null);
147 return true;
148 }
149 return false;
150 }
151
152 /**
153 * Called when the user clicked on an item into the context menu created at
154 * {@link #onCreateContextMenu(ContextMenu, View, ContextMenuInfo)} for every
155 * ownCloud {@link Account} , containing 'secondary actions' for them.
156 *
157 * {@inheritDoc}}
158 */
159 @SuppressWarnings("unchecked")
160 @Override
161 public boolean onContextItemSelected(android.view.MenuItem item) {
162 AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
163 int index = info.position;
164 HashMap<String, String> map = null;
165 try {
166 map = (HashMap<String, String>) getListAdapter().getItem(index);
167 } catch (ClassCastException e) {
168 Log_OC.wtf(TAG, "getitem(index) from list adapter did not return hashmap, bailing out");
169 return false;
170 }
171
172 String accountName = map.get("NAME");
173 AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);
174 Account accounts[] = am.getAccountsByType(MainApp.getAccountType());
175 for (Account a : accounts) {
176 if (a.name.equals(accountName)) {
177 if (item.getItemId() == R.id.change_password) {
178 Intent updateAccountCredentials = new Intent(this, AuthenticatorActivity.class);
179 updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACCOUNT, a);
180 updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACTION, AuthenticatorActivity.ACTION_UPDATE_TOKEN);
181 startActivity(updateAccountCredentials);
182
183 } else if (item.getItemId() == R.id.delete_account) {
184 am.removeAccount(a, this, mHandler);
185 }
186 }
187 }
188
189 return true;
190 }
191
192 private void populateAccountList() {
193 AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);
194 Account accounts[] = am
195 .getAccountsByType(MainApp.getAccountType());
196 if (am.getAccountsByType(MainApp.getAccountType()).length == 0) {
197 // Show create account screen if there isn't any account
198 am.addAccount(MainApp.getAccountType(),
199 null,
200 null,
201 null,
202 this,
203 null,
204 null);
205 }
206 else {
207 LinkedList<HashMap<String, String>> ll = new LinkedList<HashMap<String, String>>();
208 for (Account a : accounts) {
209 HashMap<String, String> h = new HashMap<String, String>();
210 h.put("NAME", a.name);
211 h.put("VER",
212 "ownCloud version: "
213 + am.getUserData(a,
214 OwnCloudAccount.Constants.KEY_OC_VERSION));
215 ll.add(h);
216 }
217
218 setListAdapter(new AccountCheckedSimpleAdepter(this, ll,
219 android.R.layout.simple_list_item_single_choice,
220 new String[] { "NAME" }, new int[] { android.R.id.text1 }));
221 registerForContextMenu(getListView());
222 }
223 }
224
225 @Override
226 public void run(AccountManagerFuture<Boolean> future) {
227 if (future.isDone()) {
228 Account a = AccountUtils.getCurrentOwnCloudAccount(this);
229 String accountName = "";
230 if (a == null) {
231 Account[] accounts = AccountManager.get(this)
232 .getAccountsByType(MainApp.getAccountType());
233 if (accounts.length != 0)
234 accountName = accounts[0].name;
235 AccountUtils.setCurrentOwnCloudAccount(this, accountName);
236 }
237 populateAccountList();
238 }
239 }
240
241 private class AccountCheckedSimpleAdepter extends SimpleAdapter {
242 private Account mCurrentAccount;
243 private List<? extends Map<String, ?>> mPrivateData;
244
245 public AccountCheckedSimpleAdepter(Context context,
246 List<? extends Map<String, ?>> data, int resource,
247 String[] from, int[] to) {
248 super(context, data, resource, from, to);
249 mCurrentAccount = AccountUtils
250 .getCurrentOwnCloudAccount(AccountSelectActivity.this);
251 mPrivateData = data;
252 }
253
254 @Override
255 public View getView(int position, View convertView, ViewGroup parent) {
256 View v = super.getView(position, convertView, parent);
257 CheckedTextView ctv = (CheckedTextView) v
258 .findViewById(android.R.id.text1);
259 if (mPrivateData.get(position).get("NAME")
260 .equals(mCurrentAccount.name)) {
261 ctv.setChecked(true);
262 }
263 return v;
264 }
265
266 }
267
268 }