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