2b850332be5e82b590664f3407670ef3e7499b16
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / adapter / NavigationDrawerListAdapter.java
1 package com.owncloud.android.ui.adapter;
2
3 import java.util.ArrayList;
4
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;
16
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;
21
22 public class NavigationDrawerListAdapter extends BaseAdapter {
23
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;
32
33
34 public NavigationDrawerListAdapter(Context context, FileDisplayActivity fileDisplayActivity){
35 mFileDisplayActivity = fileDisplayActivity;
36 mContext = context;
37
38 for (String string : mContext.getResources().getStringArray(R.array.drawer_items)) {
39 mDrawerItems.add(string);
40 }
41
42 AccountManager am = (AccountManager) mContext.getSystemService(mContext.ACCOUNT_SERVICE);
43 mAccounts = am.getAccountsByType(MainApp.getAccountType());
44 currentAccount = AccountUtils.getCurrentOwnCloudAccount(mContext);
45
46 all.addAll(mDrawerItems);
47 }
48
49 @Override
50 public int getCount() {
51 if (mShowAccounts){
52 return mDrawerItems.size() + 1;
53 } else {
54 return mDrawerItems.size();
55 }
56 }
57
58 @Override
59 public Object getItem(int position) {
60 //return all.get(position);
61 return null;
62 }
63
64 @Override
65 public long getItemId(int position) {
66 return 0;
67 }
68
69 @Override
70 public View getView(int position, View convertView, ViewGroup parent) {
71
72 LayoutInflater inflator = (LayoutInflater) mContext
73 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
74
75 if (all.size() > position) {
76 // Normal entry
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);
81
82 String entry = (String) all.get(position);
83 textView.setText(entry);
84
85 return view;
86 }
87
88 // Account
89 if (all.get(position) instanceof Account[]){
90 final View view = inflator.inflate(R.layout.drawer_account_group, null);
91
92 final RadioGroup group = (RadioGroup) view.findViewById(R.id.drawer_radio_group);
93
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));
98
99 RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(
100 LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
101 params.weight=1.0f;
102 params.setMargins(15, 5, 5, 5);
103
104 // Check the current account that is being used
105 if (account.name.equals(currentAccount.name)) {
106 rb.setChecked(true);
107 } else {
108 rb.setChecked(false);
109 }
110
111 group.addView(rb, params);
112 }
113
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);
118
119 AccountUtils.setCurrentOwnCloudAccount(mContext,rb.getText().toString());
120 notifyDataSetChanged();
121 mFileDisplayActivity.closeDrawer();
122
123 // restart the main activity
124 mFileDisplayActivity.restart();
125 }
126 });
127
128 return view;
129 }
130 }
131 return convertView;
132 }
133
134 public void setShowAccounts(boolean value){
135 all.clear();
136 all.addAll(mDrawerItems);
137
138 if (value){
139 all.add(1, mAccounts);
140 }
141 mShowAccounts = value;
142 }
143 }