1 package com
.owncloud
.android
.ui
.activity
;
3 import java
.util
.HashMap
;
4 import java
.util
.LinkedList
;
8 import android
.accounts
.Account
;
9 import android
.accounts
.AccountManager
;
10 import android
.accounts
.AccountManagerCallback
;
11 import android
.accounts
.AccountManagerFuture
;
12 import android
.content
.ContentResolver
;
13 import android
.content
.Context
;
14 import android
.content
.Intent
;
15 import android
.os
.Bundle
;
16 import android
.os
.Handler
;
17 import android
.util
.Log
;
18 import android
.view
.ContextMenu
;
19 import android
.view
.View
;
20 import android
.view
.ViewGroup
;
21 import android
.view
.ContextMenu
.ContextMenuInfo
;
22 import android
.widget
.AdapterView
.AdapterContextMenuInfo
;
23 import android
.widget
.CheckedTextView
;
24 import android
.widget
.ListView
;
25 import android
.widget
.SimpleAdapter
;
26 import android
.widget
.TextView
;
28 import com
.actionbarsherlock
.app
.ActionBar
;
29 import com
.actionbarsherlock
.app
.SherlockListActivity
;
30 import com
.actionbarsherlock
.view
.Menu
;
31 import com
.actionbarsherlock
.view
.MenuInflater
;
32 import com
.actionbarsherlock
.view
.MenuItem
;
33 import com
.owncloud
.android
.AccountUtils
;
34 import com
.owncloud
.android
.authenticator
.AccountAuthenticator
;
36 import com
.owncloud
.android
.R
;
38 public class AccountSelectActivity
extends SherlockListActivity
implements
39 AccountManagerCallback
<Boolean
> {
41 private static final String TAG
= "AccountSelectActivity";
43 private static final String PREVIOUS_ACCOUNT_KEY
= "ACCOUNT";
45 private final Handler mHandler
= new Handler();
46 private Account mPreviousAccount
= null
;
49 protected void onCreate(Bundle savedInstanceState
) {
50 super.onCreate(savedInstanceState
);
52 if (savedInstanceState
!= null
) {
53 mPreviousAccount
= savedInstanceState
.getParcelable(PREVIOUS_ACCOUNT_KEY
);
55 mPreviousAccount
= AccountUtils
.getCurrentOwnCloudAccount(this);
58 ActionBar action_bar
= getSupportActionBar();
59 action_bar
.setDisplayShowTitleEnabled(true
);
60 action_bar
.setDisplayHomeAsUpEnabled(false
);
64 protected void onResume() {
66 populateAccountList();
70 protected void onPause() {
72 if (this.isFinishing()) {
73 Account current
= AccountUtils
.getCurrentOwnCloudAccount(this);
74 if ((mPreviousAccount
== null
&& current
!= null
) ||
75 (mPreviousAccount
!= null
&& !mPreviousAccount
.equals(current
))) {
76 /// the account set as default changed since this activity was created
78 // trigger synchronization
79 ContentResolver
.cancelSync(null
, AccountAuthenticator
.AUTH_TOKEN_TYPE
);
80 Bundle bundle
= new Bundle();
81 bundle
.putBoolean(ContentResolver
.SYNC_EXTRAS_MANUAL
, true
);
82 ContentResolver
.requestSync(AccountUtils
.getCurrentOwnCloudAccount(this), AccountAuthenticator
.AUTH_TOKEN_TYPE
, bundle
);
84 // restart the main activity
85 Intent i
= new Intent(this, FileDisplayActivity
.class);
86 i
.addFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
);
93 public boolean onCreateOptionsMenu(Menu menu
) {
94 MenuInflater inflater
= getSherlock().getMenuInflater();
95 inflater
.inflate(R
.menu
.account_picker
, menu
);
100 public void onCreateContextMenu(ContextMenu menu
, View v
,
101 ContextMenuInfo menuInfo
) {
102 getMenuInflater().inflate(R
.menu
.account_picker_long_click
, menu
);
103 super.onCreateContextMenu(menu
, v
, menuInfo
);
107 protected void onListItemClick(ListView l
, View v
, int position
, long id
) {
108 String accountName
= ((TextView
) v
.findViewById(android
.R
.id
.text1
))
109 .getText().toString();
110 AccountUtils
.setCurrentOwnCloudAccount(this, accountName
);
111 finish(); // immediate exit
115 public boolean onMenuItemSelected(int featureId
, MenuItem item
) {
116 if (item
.getItemId() == R
.id
.createAccount
) {
117 Intent intent
= new Intent(
118 android
.provider
.Settings
.ACTION_ADD_ACCOUNT
);
119 intent
.putExtra("authorities",
120 new String
[] { AccountAuthenticator
.AUTH_TOKEN_TYPE
});
121 startActivity(intent
);
127 @SuppressWarnings("unchecked")
129 public boolean onContextItemSelected(android
.view
.MenuItem item
) {
130 AdapterContextMenuInfo info
= (AdapterContextMenuInfo
) item
132 int index
= info
.position
;
133 HashMap
<String
, String
> map
= null
;
135 map
= (HashMap
<String
, String
>) getListAdapter().getItem(index
);
136 } catch (ClassCastException e
) {
137 Log
.wtf(TAG
, "getitem(index) from list adapter did not return hashmap, bailing out");
141 String accountName
= map
.get("NAME");
142 AccountManager am
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
);
143 Account accounts
[] = am
144 .getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
);
145 for (Account a
: accounts
) {
146 if (a
.name
.equals(accountName
)) {
147 am
.removeAccount(a
, this, mHandler
);
154 private void populateAccountList() {
155 AccountManager am
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
);
156 Account accounts
[] = am
157 .getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
);
158 LinkedList
<HashMap
<String
, String
>> ll
= new LinkedList
<HashMap
<String
, String
>>();
159 for (Account a
: accounts
) {
160 HashMap
<String
, String
> h
= new HashMap
<String
, String
>();
161 h
.put("NAME", a
.name
);
165 AccountAuthenticator
.KEY_OC_VERSION
));
169 setListAdapter(new AccountCheckedSimpleAdepter(this, ll
,
170 android
.R
.layout
.simple_list_item_single_choice
,
171 new String
[] { "NAME" }, new int[] { android
.R
.id
.text1
}));
172 registerForContextMenu(getListView());
176 public void run(AccountManagerFuture
<Boolean
> future
) {
177 if (future
.isDone()) {
178 Account a
= AccountUtils
.getCurrentOwnCloudAccount(this);
179 String accountName
= "";
181 Account
[] accounts
= AccountManager
.get(this)
182 .getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
);
183 if (accounts
.length
!= 0)
184 accountName
= accounts
[0].name
;
185 AccountUtils
.setCurrentOwnCloudAccount(this, accountName
);
187 populateAccountList();
191 private class AccountCheckedSimpleAdepter
extends SimpleAdapter
{
192 private Account mCurrentAccount
;
193 private List
<?
extends Map
<String
, ?
>> mPrivateData
;
195 public AccountCheckedSimpleAdepter(Context context
,
196 List
<?
extends Map
<String
, ?
>> data
, int resource
,
197 String
[] from
, int[] to
) {
198 super(context
, data
, resource
, from
, to
);
199 mCurrentAccount
= AccountUtils
200 .getCurrentOwnCloudAccount(AccountSelectActivity
.this);
205 public View
getView(int position
, View convertView
, ViewGroup parent
) {
206 View v
= super.getView(position
, convertView
, parent
);
207 CheckedTextView ctv
= (CheckedTextView
) v
208 .findViewById(android
.R
.id
.text1
);
209 if (mPrivateData
.get(position
).get("NAME")
210 .equals(mCurrentAccount
.name
)) {
211 ctv
.setChecked(true
);