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
.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
;
23 import android
.widget
.AdapterView
.AdapterContextMenuInfo
;
24 import android
.widget
.AdapterView
.OnItemLongClickListener
;
25 import android
.widget
.CheckedTextView
;
26 import android
.widget
.ListView
;
27 import android
.widget
.SimpleAdapter
;
28 import android
.widget
.TextView
;
30 import com
.actionbarsherlock
.app
.ActionBar
;
31 import com
.actionbarsherlock
.app
.SherlockListActivity
;
32 import com
.actionbarsherlock
.view
.Menu
;
33 import com
.actionbarsherlock
.view
.MenuInflater
;
34 import com
.actionbarsherlock
.view
.MenuItem
;
36 import eu
.alefzero
.owncloud
.authenticator
.AccountAuthenticator
;
37 import eu
.alefzero
.owncloud
.AccountUtils
;
38 import eu
.alefzero
.owncloud
.R
;
40 public class AccountSelectActivity
extends SherlockListActivity
implements
41 AccountManagerCallback
<Boolean
> {
43 private final Handler mHandler
= new Handler();
46 protected void onCreate(Bundle savedInstanceState
) {
47 super.onCreate(savedInstanceState
);
49 ActionBar action_bar
= getSupportActionBar();
50 action_bar
.setDisplayShowTitleEnabled(true
);
51 action_bar
.setDisplayHomeAsUpEnabled(false
);
55 protected void onResume() {
57 populateAccountList();
61 public boolean onCreateOptionsMenu(Menu menu
) {
62 MenuInflater inflater
= getSherlock().getMenuInflater();
63 inflater
.inflate(eu
.alefzero
.owncloud
.R
.menu
.account_picker
, menu
);
68 public void onCreateContextMenu(ContextMenu menu
, View v
,
69 ContextMenuInfo menuInfo
) {
70 getMenuInflater().inflate(R
.menu
.account_picker_long_click
, menu
);
71 super.onCreateContextMenu(menu
, v
, menuInfo
);
75 protected void onListItemClick(ListView l
, View v
, int position
, long id
) {
76 String accountName
= ((TextView
) v
.findViewById(android
.R
.id
.text1
))
77 .getText().toString();
78 AccountUtils
.setCurrentOwnCloudAccount(this, accountName
);
80 // trigger synchronization when current account is changed
81 ContentResolver
.cancelSync(null
, "org.owncloud");
82 Bundle bundle
= new Bundle();
83 bundle
.putBoolean(ContentResolver
.SYNC_EXTRAS_MANUAL
, true
);
84 ContentResolver
.requestSync(AccountUtils
.getCurrentOwnCloudAccount(this), "org.owncloud", bundle
);
86 Intent i
= new Intent(this, FileDisplayActivity
.class);
87 i
.addFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
);
93 public boolean onMenuItemSelected(int featureId
, MenuItem item
) {
94 if (item
.getItemId() == R
.id
.createAccount
) {
95 Intent intent
= new Intent(
96 android
.provider
.Settings
.ACTION_ADD_ACCOUNT
);
97 intent
.putExtra("authorities",
98 new String
[] { AccountAuthenticator
.AUTH_TOKEN_TYPE
});
99 startActivity(intent
);
106 public boolean onContextItemSelected(android
.view
.MenuItem item
) {
107 AdapterContextMenuInfo info
= (AdapterContextMenuInfo
) item
109 int index
= info
.position
;
110 HashMap
<String
, String
> map
= (HashMap
<String
, String
>) getListAdapter()
112 String accountName
= map
.get("NAME");
113 AccountManager am
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
);
114 Account accounts
[] = am
115 .getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
);
116 for (Account a
: accounts
) {
117 if (a
.name
.equals(accountName
)) {
118 am
.removeAccount(a
, this, mHandler
);
125 private void populateAccountList() {
126 AccountManager am
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
);
127 Account accounts
[] = am
128 .getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
);
129 LinkedList
<HashMap
<String
, String
>> ll
= new LinkedList
<HashMap
<String
, String
>>();
130 for (Account a
: accounts
) {
131 HashMap
<String
, String
> h
= new HashMap
<String
, String
>();
132 h
.put("NAME", a
.name
);
136 AccountAuthenticator
.KEY_OC_VERSION
));
140 setListAdapter(new AccountCheckedSimpleAdepter(this, ll
,
141 android
.R
.layout
.simple_list_item_single_choice
,
142 new String
[] { "NAME" }, new int[] { android
.R
.id
.text1
}));
143 registerForContextMenu(getListView());
147 public void run(AccountManagerFuture
<Boolean
> future
) {
148 if (future
.isDone()) {
149 Account a
= AccountUtils
.getCurrentOwnCloudAccount(this);
150 String accountName
= "";
152 Account
[] accounts
= AccountManager
.get(this)
153 .getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
);
154 if (accounts
.length
!= 0)
155 accountName
= accounts
[0].name
;
156 AccountUtils
.setCurrentOwnCloudAccount(this, accountName
);
158 populateAccountList();
162 private class AccountCheckedSimpleAdepter
extends SimpleAdapter
{
163 private Account mCurrentAccount
;
164 private List
<?
extends Map
<String
, ?
>> mPrivateData
;
166 public AccountCheckedSimpleAdepter(Context context
,
167 List
<?
extends Map
<String
, ?
>> data
, int resource
,
168 String
[] from
, int[] to
) {
169 super(context
, data
, resource
, from
, to
);
170 mCurrentAccount
= AccountUtils
171 .getCurrentOwnCloudAccount(AccountSelectActivity
.this);
176 public View
getView(int position
, View convertView
, ViewGroup parent
) {
177 View v
= super.getView(position
, convertView
, parent
);
178 CheckedTextView ctv
= (CheckedTextView
) v
179 .findViewById(android
.R
.id
.text1
);
180 if (mPrivateData
.get(position
).get("NAME")
181 .equals(mCurrentAccount
.name
)) {
182 ctv
.setChecked(true
);