This wraps the android.util.logging into Log_OC which , if its enabled
[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 as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 */
19
20 package com.owncloud.android.ui.activity;
21
22 import java.util.HashMap;
23 import java.util.LinkedList;
24 import java.util.List;
25 import java.util.Map;
26
27 import android.accounts.Account;
28 import android.accounts.AccountManager;
29 import android.accounts.AccountManagerCallback;
30 import android.accounts.AccountManagerFuture;
31 import android.content.ContentResolver;
32 import android.content.Context;
33 import android.content.Intent;
34 import android.os.Bundle;
35 import android.os.Handler;
36 import android.util.Log;
37 import android.view.ContextMenu;
38 import android.view.View;
39 import android.view.ViewGroup;
40 import android.view.ContextMenu.ContextMenuInfo;
41 import android.widget.AdapterView.AdapterContextMenuInfo;
42 import android.widget.CheckedTextView;
43 import android.widget.ListView;
44 import android.widget.SimpleAdapter;
45 import android.widget.TextView;
46
47 import com.actionbarsherlock.app.ActionBar;
48 import com.actionbarsherlock.app.SherlockListActivity;
49 import com.actionbarsherlock.view.Menu;
50 import com.actionbarsherlock.view.MenuInflater;
51 import com.actionbarsherlock.view.MenuItem;
52 import com.owncloud.android.AccountUtils;
53 import com.owncloud.android.Log_OC;
54 import com.owncloud.android.authenticator.AccountAuthenticator;
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, AccountAuthenticator.AUTH_TOKEN_TYPE);
100 Bundle bundle = new Bundle();
101 bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
102 ContentResolver.requestSync(AccountUtils.getCurrentOwnCloudAccount(this), AccountAuthenticator.AUTH_TOKEN_TYPE, 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 MenuInflater inflater = getSherlock().getMenuInflater();
115 inflater.inflate(R.menu.account_picker, menu);
116 return true;
117 }
118
119 @Override
120 public void onCreateContextMenu(ContextMenu menu, View v,
121 ContextMenuInfo menuInfo) {
122 getMenuInflater().inflate(R.menu.account_picker_long_click, menu);
123 super.onCreateContextMenu(menu, v, menuInfo);
124 }
125
126 @Override
127 protected void onListItemClick(ListView l, View v, int position, long id) {
128 String accountName = ((TextView) v.findViewById(android.R.id.text1))
129 .getText().toString();
130 AccountUtils.setCurrentOwnCloudAccount(this, accountName);
131 finish(); // immediate exit
132 }
133
134 @Override
135 public boolean onMenuItemSelected(int featureId, MenuItem item) {
136 if (item.getItemId() == R.id.createAccount) {
137 Intent intent = new Intent(
138 android.provider.Settings.ACTION_ADD_ACCOUNT);
139 intent.putExtra("authorities",
140 new String[] { AccountAuthenticator.AUTH_TOKEN_TYPE });
141 startActivity(intent);
142 return true;
143 }
144 return false;
145 }
146
147 @SuppressWarnings("unchecked")
148 @Override
149 public boolean onContextItemSelected(android.view.MenuItem item) {
150 AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
151 .getMenuInfo();
152 int index = info.position;
153 HashMap<String, String> map = null;
154 try {
155 map = (HashMap<String, String>) getListAdapter().getItem(index);
156 } catch (ClassCastException e) {
157 Log_OC.wtf(TAG, "getitem(index) from list adapter did not return hashmap, bailing out");
158 return false;
159 }
160
161 String accountName = map.get("NAME");
162 AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);
163 Account accounts[] = am
164 .getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE);
165 for (Account a : accounts) {
166 if (a.name.equals(accountName)) {
167 am.removeAccount(a, this, mHandler);
168 }
169 }
170
171 return true;
172 }
173
174 private void populateAccountList() {
175 AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);
176 Account accounts[] = am
177 .getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE);
178 LinkedList<HashMap<String, String>> ll = new LinkedList<HashMap<String, String>>();
179 for (Account a : accounts) {
180 HashMap<String, String> h = new HashMap<String, String>();
181 h.put("NAME", a.name);
182 h.put("VER",
183 "ownCloud version: "
184 + am.getUserData(a,
185 AccountAuthenticator.KEY_OC_VERSION));
186 ll.add(h);
187 }
188
189 setListAdapter(new AccountCheckedSimpleAdepter(this, ll,
190 android.R.layout.simple_list_item_single_choice,
191 new String[] { "NAME" }, new int[] { android.R.id.text1 }));
192 registerForContextMenu(getListView());
193 }
194
195 @Override
196 public void run(AccountManagerFuture<Boolean> future) {
197 if (future.isDone()) {
198 Account a = AccountUtils.getCurrentOwnCloudAccount(this);
199 String accountName = "";
200 if (a == null) {
201 Account[] accounts = AccountManager.get(this)
202 .getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE);
203 if (accounts.length != 0)
204 accountName = accounts[0].name;
205 AccountUtils.setCurrentOwnCloudAccount(this, accountName);
206 }
207 populateAccountList();
208 }
209 }
210
211 private class AccountCheckedSimpleAdepter extends SimpleAdapter {
212 private Account mCurrentAccount;
213 private List<? extends Map<String, ?>> mPrivateData;
214
215 public AccountCheckedSimpleAdepter(Context context,
216 List<? extends Map<String, ?>> data, int resource,
217 String[] from, int[] to) {
218 super(context, data, resource, from, to);
219 mCurrentAccount = AccountUtils
220 .getCurrentOwnCloudAccount(AccountSelectActivity.this);
221 mPrivateData = data;
222 }
223
224 @Override
225 public View getView(int position, View convertView, ViewGroup parent) {
226 View v = super.getView(position, convertView, parent);
227 CheckedTextView ctv = (CheckedTextView) v
228 .findViewById(android.R.id.text1);
229 if (mPrivateData.get(position).get("NAME")
230 .equals(mCurrentAccount.name)) {
231 ctv.setChecked(true);
232 }
233 return v;
234 }
235
236 }
237
238 }