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