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
;
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
;
35 import com
.owncloud
.android
.AccountUtils
;
36 import com
.owncloud
.android
.authenticator
.AccountAuthenticator
;
38 import com
.owncloud
.android
.R
;
40 public class AccountSelectActivity
extends SherlockListActivity
implements
41 AccountManagerCallback
<Boolean
> {
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
);
128 public boolean onContextItemSelected(android
.view
.MenuItem item
) {
129 AdapterContextMenuInfo info
= (AdapterContextMenuInfo
) item
131 int index
= info
.position
;
132 HashMap
<String
, String
> map
= (HashMap
<String
, String
>) getListAdapter()
134 String accountName
= map
.get("NAME");
135 AccountManager am
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
);
136 Account accounts
[] = am
137 .getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
);
138 for (Account a
: accounts
) {
139 if (a
.name
.equals(accountName
)) {
140 am
.removeAccount(a
, this, mHandler
);
147 private void populateAccountList() {
148 AccountManager am
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
);
149 Account accounts
[] = am
150 .getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
);
151 LinkedList
<HashMap
<String
, String
>> ll
= new LinkedList
<HashMap
<String
, String
>>();
152 for (Account a
: accounts
) {
153 HashMap
<String
, String
> h
= new HashMap
<String
, String
>();
154 h
.put("NAME", a
.name
);
158 AccountAuthenticator
.KEY_OC_VERSION
));
162 setListAdapter(new AccountCheckedSimpleAdepter(this, ll
,
163 android
.R
.layout
.simple_list_item_single_choice
,
164 new String
[] { "NAME" }, new int[] { android
.R
.id
.text1
}));
165 registerForContextMenu(getListView());
169 public void run(AccountManagerFuture
<Boolean
> future
) {
170 if (future
.isDone()) {
171 Account a
= AccountUtils
.getCurrentOwnCloudAccount(this);
172 String accountName
= "";
174 Account
[] accounts
= AccountManager
.get(this)
175 .getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
);
176 if (accounts
.length
!= 0)
177 accountName
= accounts
[0].name
;
178 AccountUtils
.setCurrentOwnCloudAccount(this, accountName
);
180 populateAccountList();
184 private class AccountCheckedSimpleAdepter
extends SimpleAdapter
{
185 private Account mCurrentAccount
;
186 private List
<?
extends Map
<String
, ?
>> mPrivateData
;
188 public AccountCheckedSimpleAdepter(Context context
,
189 List
<?
extends Map
<String
, ?
>> data
, int resource
,
190 String
[] from
, int[] to
) {
191 super(context
, data
, resource
, from
, to
);
192 mCurrentAccount
= AccountUtils
193 .getCurrentOwnCloudAccount(AccountSelectActivity
.this);
198 public View
getView(int position
, View convertView
, ViewGroup parent
) {
199 View v
= super.getView(position
, convertView
, parent
);
200 CheckedTextView ctv
= (CheckedTextView
) v
201 .findViewById(android
.R
.id
.text1
);
202 if (mPrivateData
.get(position
).get("NAME")
203 .equals(mCurrentAccount
.name
)) {
204 ctv
.setChecked(true
);