1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
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.
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.
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/>.
20 package com
.owncloud
.android
.ui
.activity
;
22 import java
.util
.HashMap
;
23 import java
.util
.LinkedList
;
24 import java
.util
.List
;
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
;
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
;
56 import com
.owncloud
.android
.R
;
58 public class AccountSelectActivity
extends SherlockListActivity
implements
59 AccountManagerCallback
<Boolean
> {
61 private static final String TAG
= "AccountSelectActivity";
63 private static final String PREVIOUS_ACCOUNT_KEY
= "ACCOUNT";
65 private final Handler mHandler
= new Handler();
66 private Account mPreviousAccount
= null
;
69 protected void onCreate(Bundle savedInstanceState
) {
70 super.onCreate(savedInstanceState
);
72 if (savedInstanceState
!= null
) {
73 mPreviousAccount
= savedInstanceState
.getParcelable(PREVIOUS_ACCOUNT_KEY
);
75 mPreviousAccount
= AccountUtils
.getCurrentOwnCloudAccount(this);
78 ActionBar action_bar
= getSupportActionBar();
79 action_bar
.setDisplayShowTitleEnabled(true
);
80 action_bar
.setDisplayHomeAsUpEnabled(false
);
84 protected void onResume() {
86 populateAccountList();
90 protected void 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
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
);
104 // restart the main activity
105 Intent i
= new Intent(this, FileDisplayActivity
.class);
106 i
.addFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
);
113 public boolean onCreateOptionsMenu(Menu menu
) {
114 MenuInflater inflater
= getSherlock().getMenuInflater();
115 inflater
.inflate(R
.menu
.account_picker
, menu
);
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
);
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
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
);
147 @SuppressWarnings("unchecked")
149 public boolean onContextItemSelected(android
.view
.MenuItem item
) {
150 AdapterContextMenuInfo info
= (AdapterContextMenuInfo
) item
152 int index
= info
.position
;
153 HashMap
<String
, String
> map
= null
;
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");
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
);
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
);
185 AccountAuthenticator
.KEY_OC_VERSION
));
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());
196 public void run(AccountManagerFuture
<Boolean
> future
) {
197 if (future
.isDone()) {
198 Account a
= AccountUtils
.getCurrentOwnCloudAccount(this);
199 String accountName
= "";
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
);
207 populateAccountList();
211 private class AccountCheckedSimpleAdepter
extends SimpleAdapter
{
212 private Account mCurrentAccount
;
213 private List
<?
extends Map
<String
, ?
>> mPrivateData
;
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);
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
);