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
.Log_OC
;
55 import com
.owncloud
.android
.R
;
57 public class AccountSelectActivity
extends SherlockListActivity
implements
58 AccountManagerCallback
<Boolean
> {
60 private static final String TAG
= "AccountSelectActivity";
62 private static final String PREVIOUS_ACCOUNT_KEY
= "ACCOUNT";
64 private final Handler mHandler
= new Handler();
65 private Account mPreviousAccount
= null
;
68 protected void onCreate(Bundle savedInstanceState
) {
69 super.onCreate(savedInstanceState
);
71 if (savedInstanceState
!= null
) {
72 mPreviousAccount
= savedInstanceState
.getParcelable(PREVIOUS_ACCOUNT_KEY
);
74 mPreviousAccount
= AccountUtils
.getCurrentOwnCloudAccount(this);
77 ActionBar action_bar
= getSupportActionBar();
78 action_bar
.setDisplayShowTitleEnabled(true
);
79 action_bar
.setDisplayHomeAsUpEnabled(false
);
83 protected void onResume() {
85 populateAccountList();
89 protected void onPause() {
91 if (this.isFinishing()) {
92 Account current
= AccountUtils
.getCurrentOwnCloudAccount(this);
93 if ((mPreviousAccount
== null
&& current
!= null
) ||
94 (mPreviousAccount
!= null
&& !mPreviousAccount
.equals(current
))) {
95 /// the account set as default changed since this activity was created
97 // trigger synchronization
98 ContentResolver
.cancelSync(null
, AccountAuthenticator
.AUTHORITY
);
99 Bundle bundle
= new Bundle();
100 bundle
.putBoolean(ContentResolver
.SYNC_EXTRAS_MANUAL
, true
);
101 ContentResolver
.requestSync(AccountUtils
.getCurrentOwnCloudAccount(this), AccountAuthenticator
.AUTHORITY
, bundle
);
103 // restart the main activity
104 Intent i
= new Intent(this, FileDisplayActivity
.class);
105 i
.addFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
);
112 public boolean onCreateOptionsMenu(Menu menu
) {
113 MenuInflater inflater
= getSherlock().getMenuInflater();
114 inflater
.inflate(R
.menu
.account_picker
, menu
);
119 public void onCreateContextMenu(ContextMenu menu
, View v
,
120 ContextMenuInfo menuInfo
) {
121 getMenuInflater().inflate(R
.menu
.account_picker_long_click
, menu
);
122 super.onCreateContextMenu(menu
, v
, menuInfo
);
126 protected void onListItemClick(ListView l
, View v
, int position
, long id
) {
127 String accountName
= ((TextView
) v
.findViewById(android
.R
.id
.text1
))
128 .getText().toString();
129 AccountUtils
.setCurrentOwnCloudAccount(this, accountName
);
130 finish(); // immediate exit
134 public boolean onMenuItemSelected(int featureId
, MenuItem item
) {
135 if (item
.getItemId() == R
.id
.createAccount
) {
136 Intent intent
= new Intent(
137 android
.provider
.Settings
.ACTION_ADD_ACCOUNT
);
138 intent
.putExtra("authorities",
139 new String
[] { AccountAuthenticator
.AUTHORITY
});
140 startActivity(intent
);
147 * Called when the user clicked on an item into the context menu created at
148 * {@link #onCreateContextMenu(ContextMenu, View, ContextMenuInfo)} for every
149 * ownCloud {@link Account} , containing 'secondary actions' for them.
153 @SuppressWarnings("unchecked")
155 public boolean onContextItemSelected(android
.view
.MenuItem item
) {
156 AdapterContextMenuInfo info
= (AdapterContextMenuInfo
) item
.getMenuInfo();
157 int index
= info
.position
;
158 HashMap
<String
, String
> map
= null
;
160 map
= (HashMap
<String
, String
>) getListAdapter().getItem(index
);
161 } catch (ClassCastException e
) {
162 Log_OC
.wtf(TAG
, "getitem(index) from list adapter did not return hashmap, bailing out");
166 String accountName
= map
.get("NAME");
167 AccountManager am
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
);
168 Account accounts
[] = am
.getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
);
169 for (Account a
: accounts
) {
170 if (a
.name
.equals(accountName
)) {
171 if (item
.getItemId() == R
.id
.change_password
) {
172 Intent updateAccountCredentials
= new Intent(this, AuthenticatorActivity
.class);
173 updateAccountCredentials
.putExtra(AuthenticatorActivity
.EXTRA_ACCOUNT
, a
);
174 updateAccountCredentials
.putExtra(AuthenticatorActivity
.EXTRA_ACTION
, AuthenticatorActivity
.ACTION_UPDATE_TOKEN
);
175 startActivity(updateAccountCredentials
);
177 } else if (item
.getItemId() == R
.id
.delete_account
) {
178 am
.removeAccount(a
, this, mHandler
);
186 private void populateAccountList() {
187 AccountManager am
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
);
188 Account accounts
[] = am
189 .getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
);
190 LinkedList
<HashMap
<String
, String
>> ll
= new LinkedList
<HashMap
<String
, String
>>();
191 for (Account a
: accounts
) {
192 HashMap
<String
, String
> h
= new HashMap
<String
, String
>();
193 h
.put("NAME", a
.name
);
197 AccountAuthenticator
.KEY_OC_VERSION
));
201 setListAdapter(new AccountCheckedSimpleAdepter(this, ll
,
202 android
.R
.layout
.simple_list_item_single_choice
,
203 new String
[] { "NAME" }, new int[] { android
.R
.id
.text1
}));
204 registerForContextMenu(getListView());
208 public void run(AccountManagerFuture
<Boolean
> future
) {
209 if (future
.isDone()) {
210 Account a
= AccountUtils
.getCurrentOwnCloudAccount(this);
211 String accountName
= "";
213 Account
[] accounts
= AccountManager
.get(this)
214 .getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
);
215 if (accounts
.length
!= 0)
216 accountName
= accounts
[0].name
;
217 AccountUtils
.setCurrentOwnCloudAccount(this, accountName
);
219 populateAccountList();
223 private class AccountCheckedSimpleAdepter
extends SimpleAdapter
{
224 private Account mCurrentAccount
;
225 private List
<?
extends Map
<String
, ?
>> mPrivateData
;
227 public AccountCheckedSimpleAdepter(Context context
,
228 List
<?
extends Map
<String
, ?
>> data
, int resource
,
229 String
[] from
, int[] to
) {
230 super(context
, data
, resource
, from
, to
);
231 mCurrentAccount
= AccountUtils
232 .getCurrentOwnCloudAccount(AccountSelectActivity
.this);
237 public View
getView(int position
, View convertView
, ViewGroup parent
) {
238 View v
= super.getView(position
, convertView
, parent
);
239 CheckedTextView ctv
= (CheckedTextView
) v
240 .findViewById(android
.R
.id
.text1
);
241 if (mPrivateData
.get(position
).get("NAME")
242 .equals(mCurrentAccount
.name
)) {
243 ctv
.setChecked(true
);