1 package com
.owncloud
.android
.ui
.adapter
;
3 import java
.util
.ArrayList
;
5 import android
.accounts
.Account
;
6 import android
.accounts
.AccountManager
;
7 import android
.content
.Context
;
8 import android
.view
.LayoutInflater
;
9 import android
.view
.View
;
10 import android
.view
.ViewGroup
;
11 import android
.widget
.BaseAdapter
;
12 import android
.widget
.RadioButton
;
13 import android
.widget
.RadioGroup
;
14 import android
.widget
.RadioGroup
.LayoutParams
;
15 import android
.widget
.TextView
;
17 import com
.owncloud
.android
.MainApp
;
18 import com
.owncloud
.android
.R
;
19 import com
.owncloud
.android
.authentication
.AccountUtils
;
20 import com
.owncloud
.android
.ui
.activity
.FileDisplayActivity
;
22 public class NavigationDrawerListAdapter
extends BaseAdapter
{
24 private final static String TAG
= "NavigationDrawerListAdapter";
25 private Context mContext
;
26 private ArrayList
<String
> mDrawerItems
= new ArrayList
<String
>();
27 ArrayList
<Object
> all
= new ArrayList
<Object
>();
28 private Account
[] mAccounts
;
29 private boolean mShowAccounts
;
30 private Account currentAccount
;
31 private FileDisplayActivity mFileDisplayActivity
;
34 public NavigationDrawerListAdapter(Context context
, FileDisplayActivity fileDisplayActivity
){
35 mFileDisplayActivity
= fileDisplayActivity
;
38 for (String string
: mContext
.getResources().getStringArray(R
.array
.drawer_items
)) {
39 mDrawerItems
.add(string
);
42 AccountManager am
= (AccountManager
) mContext
.getSystemService(mContext
.ACCOUNT_SERVICE
);
43 mAccounts
= am
.getAccountsByType(MainApp
.getAccountType());
44 currentAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
46 all
.addAll(mDrawerItems
);
50 public int getCount() {
52 return mDrawerItems
.size() + 1;
54 return mDrawerItems
.size();
59 public Object
getItem(int position
) {
60 //return all.get(position);
65 public long getItemId(int position
) {
70 public View
getView(int position
, View convertView
, ViewGroup parent
) {
72 LayoutInflater inflator
= (LayoutInflater
) mContext
73 .getSystemService(Context
.LAYOUT_INFLATER_SERVICE
);
75 if (all
.size() > position
) {
77 if (all
.get(position
) instanceof String
){
78 View view
= inflator
.inflate(R
.layout
.drawer_list_item
, null
);
79 view
.setMinimumHeight(40);
80 TextView textView
= (TextView
) view
.findViewById(R
.id
.drawer_textView
);
82 String entry
= (String
) all
.get(position
);
83 textView
.setText(entry
);
89 if (all
.get(position
) instanceof Account
[]){
90 final View view
= inflator
.inflate(R
.layout
.drawer_account_group
, null
);
92 final RadioGroup group
= (RadioGroup
) view
.findViewById(R
.id
.drawer_radio_group
);
94 for (Account account
: mAccounts
) {
95 RadioButton rb
= new RadioButton(mContext
);
96 rb
.setText(account
.name
);
97 rb
.setTextColor(mContext
.getResources().getColor(R
.color
.black
));
99 RadioGroup
.LayoutParams params
= new RadioGroup
.LayoutParams(
100 LayoutParams
.MATCH_PARENT
, LayoutParams
.WRAP_CONTENT
);
102 params
.setMargins(15, 5, 5, 5);
104 // Check the current account that is being used
105 if (account
.name
.equals(currentAccount
.name
)) {
108 rb
.setChecked(false
);
111 group
.addView(rb
, params
);
114 group
.setOnCheckedChangeListener(new RadioGroup
.OnCheckedChangeListener(){
115 public void onCheckedChanged(RadioGroup group
, int checkedId
) {
116 // checkedId is the RadioButton selected
117 RadioButton rb
= (RadioButton
) view
.findViewById(checkedId
);
119 AccountUtils
.setCurrentOwnCloudAccount(mContext
,rb
.getText().toString());
120 notifyDataSetChanged();
121 mFileDisplayActivity
.closeDrawer();
123 // restart the main activity
124 mFileDisplayActivity
.restart();
134 public void setShowAccounts(boolean value
){
136 all
.addAll(mDrawerItems
);
139 all
.add(1, mAccounts
);
141 mShowAccounts
= value
;