1 /* ownCloud Android client application 
   2  *   Copyright (C) 2012 Bartek Przybylski 
   4  *   This program is free software: you can redistribute it and/or modify 
   5  *   it under the terms of the GNU General Public License as published by 
   6  *   the Free Software Foundation, either version 3 of the License, or 
   7  *   (at your option) any later version. 
   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
.util
.Log
; 
  36 import android
.view
.ContextMenu
; 
  37 import android
.view
.View
; 
  38 import android
.view
.ViewGroup
; 
  39 import android
.view
.ContextMenu
.ContextMenuInfo
; 
  40 import android
.widget
.AdapterView
.AdapterContextMenuInfo
; 
  41 import android
.widget
.CheckedTextView
; 
  42 import android
.widget
.ListView
; 
  43 import android
.widget
.SimpleAdapter
; 
  44 import android
.widget
.TextView
; 
  46 import com
.actionbarsherlock
.app
.ActionBar
; 
  47 import com
.actionbarsherlock
.app
.SherlockListActivity
; 
  48 import com
.actionbarsherlock
.view
.Menu
; 
  49 import com
.actionbarsherlock
.view
.MenuInflater
; 
  50 import com
.actionbarsherlock
.view
.MenuItem
; 
  51 import com
.owncloud
.android
.AccountUtils
; 
  52 import com
.owncloud
.android
.authenticator
.AccountAuthenticator
; 
  54 import com
.owncloud
.android
.R
; 
  56 public class AccountSelectActivity 
extends SherlockListActivity 
implements 
  57         AccountManagerCallback
<Boolean
> { 
  59     private static final String  TAG 
= "AccountSelectActivity"; 
  61     private static final String PREVIOUS_ACCOUNT_KEY 
= "ACCOUNT"; 
  63     private final Handler mHandler 
= new Handler(); 
  64     private Account mPreviousAccount 
= null
; 
  67     protected void onCreate(Bundle savedInstanceState
) { 
  68         super.onCreate(savedInstanceState
); 
  70         if (savedInstanceState 
!= null
) { 
  71             mPreviousAccount 
= savedInstanceState
.getParcelable(PREVIOUS_ACCOUNT_KEY
); 
  73             mPreviousAccount 
= AccountUtils
.getCurrentOwnCloudAccount(this); 
  76         ActionBar action_bar 
= getSupportActionBar(); 
  77         action_bar
.setDisplayShowTitleEnabled(true
); 
  78         action_bar
.setDisplayHomeAsUpEnabled(false
); 
  82     protected void onResume() { 
  84         populateAccountList(); 
  88     protected void onPause() { 
  90         if (this.isFinishing()) { 
  91             Account current 
= AccountUtils
.getCurrentOwnCloudAccount(this); 
  92             if ((mPreviousAccount 
== null 
&& current 
!= null
) ||  
  93                 (mPreviousAccount 
!= null 
&& !mPreviousAccount
.equals(current
))) { 
  94                 /// the account set as default changed since this activity was created  
  96                 // trigger synchronization 
  97                 ContentResolver
.cancelSync(null
, AccountAuthenticator
.AUTH_TOKEN_TYPE
); 
  98                 Bundle bundle 
= new Bundle(); 
  99                 bundle
.putBoolean(ContentResolver
.SYNC_EXTRAS_MANUAL
, true
); 
 100                 ContentResolver
.requestSync(AccountUtils
.getCurrentOwnCloudAccount(this), AccountAuthenticator
.AUTH_TOKEN_TYPE
, bundle
); 
 102                 // restart the main activity 
 103                 Intent i 
= new Intent(this, FileDisplayActivity
.class); 
 104                 i
.addFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
); 
 111     public boolean onCreateOptionsMenu(Menu menu
) { 
 112         MenuInflater inflater 
= getSherlock().getMenuInflater(); 
 113         inflater
.inflate(R
.menu
.account_picker
, menu
); 
 118     public void onCreateContextMenu(ContextMenu menu
, View v
, 
 119             ContextMenuInfo menuInfo
) { 
 120         getMenuInflater().inflate(R
.menu
.account_picker_long_click
, menu
); 
 121         super.onCreateContextMenu(menu
, v
, menuInfo
); 
 125     protected void onListItemClick(ListView l
, View v
, int position
, long id
) { 
 126         String accountName 
= ((TextView
) v
.findViewById(android
.R
.id
.text1
)) 
 127                 .getText().toString(); 
 128         AccountUtils
.setCurrentOwnCloudAccount(this, accountName
); 
 129         finish();   // immediate exit 
 133     public boolean onMenuItemSelected(int featureId
, MenuItem item
) { 
 134         if (item
.getItemId() == R
.id
.createAccount
) { 
 135             Intent intent 
= new Intent( 
 136                     android
.provider
.Settings
.ACTION_ADD_ACCOUNT
); 
 137             intent
.putExtra("authorities", 
 138                     new String
[] { AccountAuthenticator
.AUTH_TOKEN_TYPE 
}); 
 139             startActivity(intent
); 
 145     @SuppressWarnings("unchecked") 
 147     public boolean onContextItemSelected(android
.view
.MenuItem item
) { 
 148         AdapterContextMenuInfo info 
= (AdapterContextMenuInfo
) item
 
 150         int index 
= info
.position
; 
 151         HashMap
<String
, String
> map 
= null
; 
 153             map 
= (HashMap
<String
, String
>) getListAdapter().getItem(index
); 
 154         } catch (ClassCastException e
) { 
 155             Log
.wtf(TAG
, "getitem(index) from list adapter did not return hashmap, bailing out"); 
 159         String accountName 
= map
.get("NAME"); 
 160         AccountManager am 
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
); 
 161         Account accounts
[] = am
 
 162                 .getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
); 
 163         for (Account a 
: accounts
) { 
 164             if (a
.name
.equals(accountName
)) { 
 165                 am
.removeAccount(a
, this, mHandler
); 
 172     private void populateAccountList() { 
 173         AccountManager am 
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
); 
 174         Account accounts
[] = am
 
 175                 .getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
); 
 176         LinkedList
<HashMap
<String
, String
>> ll 
= new LinkedList
<HashMap
<String
, String
>>(); 
 177         for (Account a 
: accounts
) { 
 178             HashMap
<String
, String
> h 
= new HashMap
<String
, String
>(); 
 179             h
.put("NAME", a
.name
); 
 183                                     AccountAuthenticator
.KEY_OC_VERSION
)); 
 187         setListAdapter(new AccountCheckedSimpleAdepter(this, ll
, 
 188                 android
.R
.layout
.simple_list_item_single_choice
, 
 189                 new String
[] { "NAME" }, new int[] { android
.R
.id
.text1 
})); 
 190         registerForContextMenu(getListView()); 
 194     public void run(AccountManagerFuture
<Boolean
> future
) { 
 195         if (future
.isDone()) { 
 196             Account a 
= AccountUtils
.getCurrentOwnCloudAccount(this); 
 197             String accountName 
= ""; 
 199                 Account
[] accounts 
= AccountManager
.get(this) 
 200                         .getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
); 
 201                 if (accounts
.length 
!= 0) 
 202                     accountName 
= accounts
[0].name
; 
 203                 AccountUtils
.setCurrentOwnCloudAccount(this, accountName
); 
 205             populateAccountList(); 
 209     private class AccountCheckedSimpleAdepter 
extends SimpleAdapter 
{ 
 210         private Account mCurrentAccount
; 
 211         private List
<? 
extends Map
<String
, ?
>> mPrivateData
; 
 213         public AccountCheckedSimpleAdepter(Context context
, 
 214                 List
<? 
extends Map
<String
, ?
>> data
, int resource
, 
 215                 String
[] from
, int[] to
) { 
 216             super(context
, data
, resource
, from
, to
); 
 217             mCurrentAccount 
= AccountUtils
 
 218                     .getCurrentOwnCloudAccount(AccountSelectActivity
.this); 
 223         public View 
getView(int position
, View convertView
, ViewGroup parent
) { 
 224             View v 
= super.getView(position
, convertView
, parent
); 
 225             CheckedTextView ctv 
= (CheckedTextView
) v
 
 226                     .findViewById(android
.R
.id
.text1
); 
 227             if (mPrivateData
.get(position
).get("NAME") 
 228                     .equals(mCurrentAccount
.name
)) { 
 229                 ctv
.setChecked(true
);