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 version 2,
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 package com
.owncloud
.android
.ui
.activity
;
21 import java
.util
.HashMap
;
22 import java
.util
.LinkedList
;
23 import java
.util
.List
;
26 import android
.accounts
.Account
;
27 import android
.accounts
.AccountManager
;
28 import android
.accounts
.AccountManagerCallback
;
29 import android
.accounts
.AccountManagerFuture
;
30 import android
.content
.ContentResolver
;
31 import android
.content
.Context
;
32 import android
.content
.Intent
;
33 import android
.os
.Bundle
;
34 import android
.os
.Handler
;
35 import android
.view
.ContextMenu
;
36 import android
.view
.View
;
37 import android
.view
.ViewGroup
;
38 import android
.view
.ContextMenu
.ContextMenuInfo
;
39 import android
.widget
.AdapterView
.AdapterContextMenuInfo
;
40 import android
.widget
.CheckedTextView
;
41 import android
.widget
.ListView
;
42 import android
.widget
.SimpleAdapter
;
43 import android
.widget
.TextView
;
45 import com
.actionbarsherlock
.app
.ActionBar
;
46 import com
.actionbarsherlock
.app
.SherlockListActivity
;
47 import com
.actionbarsherlock
.view
.Menu
;
48 import com
.actionbarsherlock
.view
.MenuInflater
;
49 import com
.actionbarsherlock
.view
.MenuItem
;
50 import com
.owncloud
.android
.authentication
.AccountAuthenticator
;
51 import com
.owncloud
.android
.authentication
.AuthenticatorActivity
;
52 import com
.owncloud
.android
.authentication
.AccountUtils
;
53 import com
.owncloud
.android
.ui
.activity
.FileActivity
.AccountCreationCallback
;
54 import com
.owncloud
.android
.Log_OC
;
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
.AUTHORITY
);
100 Bundle bundle
= new Bundle();
101 bundle
.putBoolean(ContentResolver
.SYNC_EXTRAS_MANUAL
, true
);
102 ContentResolver
.requestSync(AccountUtils
.getCurrentOwnCloudAccount(this), AccountAuthenticator
.AUTHORITY
, 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.AUTHORITY });
141 startActivity(intent);*/
142 AccountManager am
= AccountManager
.get(getApplicationContext());
143 am
.addAccount(AccountAuthenticator
.ACCOUNT_TYPE
,
157 * Called when the user clicked on an item into the context menu created at
158 * {@link #onCreateContextMenu(ContextMenu, View, ContextMenuInfo)} for every
159 * ownCloud {@link Account} , containing 'secondary actions' for them.
163 @SuppressWarnings("unchecked")
165 public boolean onContextItemSelected(android
.view
.MenuItem item
) {
166 AdapterContextMenuInfo info
= (AdapterContextMenuInfo
) item
.getMenuInfo();
167 int index
= info
.position
;
168 HashMap
<String
, String
> map
= null
;
170 map
= (HashMap
<String
, String
>) getListAdapter().getItem(index
);
171 } catch (ClassCastException e
) {
172 Log_OC
.wtf(TAG
, "getitem(index) from list adapter did not return hashmap, bailing out");
176 String accountName
= map
.get("NAME");
177 AccountManager am
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
);
178 Account accounts
[] = am
.getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
);
179 for (Account a
: accounts
) {
180 if (a
.name
.equals(accountName
)) {
181 if (item
.getItemId() == R
.id
.change_password
) {
182 Intent updateAccountCredentials
= new Intent(this, AuthenticatorActivity
.class);
183 updateAccountCredentials
.putExtra(AuthenticatorActivity
.EXTRA_ACCOUNT
, a
);
184 updateAccountCredentials
.putExtra(AuthenticatorActivity
.EXTRA_ACTION
, AuthenticatorActivity
.ACTION_UPDATE_TOKEN
);
185 startActivity(updateAccountCredentials
);
187 } else if (item
.getItemId() == R
.id
.delete_account
) {
188 am
.removeAccount(a
, this, mHandler
);
196 private void populateAccountList() {
197 AccountManager am
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
);
198 Account accounts
[] = am
199 .getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
);
200 LinkedList
<HashMap
<String
, String
>> ll
= new LinkedList
<HashMap
<String
, String
>>();
201 for (Account a
: accounts
) {
202 HashMap
<String
, String
> h
= new HashMap
<String
, String
>();
203 h
.put("NAME", a
.name
);
207 AccountAuthenticator
.KEY_OC_VERSION
));
211 setListAdapter(new AccountCheckedSimpleAdepter(this, ll
,
212 android
.R
.layout
.simple_list_item_single_choice
,
213 new String
[] { "NAME" }, new int[] { android
.R
.id
.text1
}));
214 registerForContextMenu(getListView());
218 public void run(AccountManagerFuture
<Boolean
> future
) {
219 if (future
.isDone()) {
220 Account a
= AccountUtils
.getCurrentOwnCloudAccount(this);
221 String accountName
= "";
223 Account
[] accounts
= AccountManager
.get(this)
224 .getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
);
225 if (accounts
.length
!= 0)
226 accountName
= accounts
[0].name
;
227 AccountUtils
.setCurrentOwnCloudAccount(this, accountName
);
229 populateAccountList();
233 private class AccountCheckedSimpleAdepter
extends SimpleAdapter
{
234 private Account mCurrentAccount
;
235 private List
<?
extends Map
<String
, ?
>> mPrivateData
;
237 public AccountCheckedSimpleAdepter(Context context
,
238 List
<?
extends Map
<String
, ?
>> data
, int resource
,
239 String
[] from
, int[] to
) {
240 super(context
, data
, resource
, from
, to
);
241 mCurrentAccount
= AccountUtils
242 .getCurrentOwnCloudAccount(AccountSelectActivity
.this);
247 public View
getView(int position
, View convertView
, ViewGroup parent
) {
248 View v
= super.getView(position
, convertView
, parent
);
249 CheckedTextView ctv
= (CheckedTextView
) v
250 .findViewById(android
.R
.id
.text1
);
251 if (mPrivateData
.get(position
).get("NAME")
252 .equals(mCurrentAccount
.name
)) {
253 ctv
.setChecked(true
);