1 package eu
.alefzero
.owncloud
.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
.Context
;
13 import android
.content
.Intent
;
14 import android
.os
.Bundle
;
15 import android
.os
.Handler
;
16 import android
.util
.Log
;
17 import android
.view
.ContextMenu
;
18 import android
.view
.View
;
19 import android
.view
.ViewGroup
;
20 import android
.view
.ContextMenu
.ContextMenuInfo
;
21 import android
.widget
.AdapterView
;
22 import android
.widget
.AdapterView
.AdapterContextMenuInfo
;
23 import android
.widget
.AdapterView
.OnItemLongClickListener
;
24 import android
.widget
.CheckedTextView
;
25 import android
.widget
.ListView
;
26 import android
.widget
.SimpleAdapter
;
27 import android
.widget
.TextView
;
29 import com
.actionbarsherlock
.app
.ActionBar
;
30 import com
.actionbarsherlock
.app
.SherlockListActivity
;
31 import com
.actionbarsherlock
.view
.Menu
;
32 import com
.actionbarsherlock
.view
.MenuInflater
;
33 import com
.actionbarsherlock
.view
.MenuItem
;
35 import eu
.alefzero
.owncloud
.authenticator
.AccountAuthenticator
;
36 import eu
.alefzero
.owncloud
.AccountUtils
;
37 import eu
.alefzero
.owncloud
.R
;
39 public class AccountSelectActivity
extends SherlockListActivity
implements
40 AccountManagerCallback
<Boolean
> {
42 private final Handler mHandler
= new Handler();
45 protected void onCreate(Bundle savedInstanceState
) {
46 super.onCreate(savedInstanceState
);
48 ActionBar action_bar
= getSupportActionBar();
49 action_bar
.setDisplayShowTitleEnabled(true
);
50 action_bar
.setDisplayHomeAsUpEnabled(false
);
54 protected void onResume() {
56 populateAccountList();
60 public boolean onCreateOptionsMenu(Menu menu
) {
61 MenuInflater inflater
= getSherlock().getMenuInflater();
62 inflater
.inflate(eu
.alefzero
.owncloud
.R
.menu
.account_picker
, menu
);
67 public void onCreateContextMenu(ContextMenu menu
, View v
,
68 ContextMenuInfo menuInfo
) {
69 getMenuInflater().inflate(R
.menu
.account_picker_long_click
, menu
);
70 super.onCreateContextMenu(menu
, v
, menuInfo
);
74 protected void onListItemClick(ListView l
, View v
, int position
, long id
) {
75 String accountName
= ((TextView
) v
.findViewById(android
.R
.id
.text1
))
76 .getText().toString();
77 AccountUtils
.setCurrentOwnCloudAccount(this, accountName
);
78 Intent i
= new Intent(this, FileDisplayActivity
.class);
79 i
.addFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
);
85 public boolean onMenuItemSelected(int featureId
, MenuItem item
) {
86 if (item
.getItemId() == R
.id
.createAccount
) {
87 Intent intent
= new Intent(
88 android
.provider
.Settings
.ACTION_ADD_ACCOUNT
);
89 intent
.putExtra("authorities",
90 new String
[] { AccountAuthenticator
.AUTH_TOKEN_TYPE
});
91 startActivity(intent
);
98 public boolean onContextItemSelected(android
.view
.MenuItem item
) {
99 AdapterContextMenuInfo info
= (AdapterContextMenuInfo
) item
101 int index
= info
.position
;
102 HashMap
<String
, String
> map
= (HashMap
<String
, String
>) getListAdapter()
104 String accountName
= map
.get("NAME");
105 AccountManager am
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
);
106 Account accounts
[] = am
107 .getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
);
108 for (Account a
: accounts
) {
109 if (a
.name
.equals(accountName
)) {
110 am
.removeAccount(a
, this, mHandler
);
117 private void populateAccountList() {
118 AccountManager am
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
);
119 Account accounts
[] = am
120 .getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
);
121 LinkedList
<HashMap
<String
, String
>> ll
= new LinkedList
<HashMap
<String
, String
>>();
122 for (Account a
: accounts
) {
123 HashMap
<String
, String
> h
= new HashMap
<String
, String
>();
124 h
.put("NAME", a
.name
);
128 AccountAuthenticator
.KEY_OC_VERSION
));
132 setListAdapter(new AccountCheckedSimpleAdepter(this, ll
,
133 android
.R
.layout
.simple_list_item_single_choice
,
134 new String
[] { "NAME" }, new int[] { android
.R
.id
.text1
}));
135 registerForContextMenu(getListView());
139 public void run(AccountManagerFuture
<Boolean
> future
) {
140 if (future
.isDone()) {
141 Account a
= AccountUtils
.getCurrentOwnCloudAccount(this);
142 String accountName
= "";
144 Account
[] accounts
= AccountManager
.get(this)
145 .getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
);
146 if (accounts
.length
!= 0)
147 accountName
= accounts
[0].name
;
148 AccountUtils
.setCurrentOwnCloudAccount(this, accountName
);
150 populateAccountList();
154 private class AccountCheckedSimpleAdepter
extends SimpleAdapter
{
155 private Account mCurrentAccount
;
156 private List
<?
extends Map
<String
, ?
>> mPrivateData
;
158 public AccountCheckedSimpleAdepter(Context context
,
159 List
<?
extends Map
<String
, ?
>> data
, int resource
,
160 String
[] from
, int[] to
) {
161 super(context
, data
, resource
, from
, to
);
162 mCurrentAccount
= AccountUtils
163 .getCurrentOwnCloudAccount(AccountSelectActivity
.this);
168 public View
getView(int position
, View convertView
, ViewGroup parent
) {
169 View v
= super.getView(position
, convertView
, parent
);
170 CheckedTextView ctv
= (CheckedTextView
) v
171 .findViewById(android
.R
.id
.text1
);
172 if (mPrivateData
.get(position
).get("NAME")
173 .equals(mCurrentAccount
.name
)) {
174 ctv
.setChecked(true
);