78d0693e0beb40d4ffaa33bcd70376d210bcbea8
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / adapter / NavigationDrawerListAdapter.java
1 /**
2 * ownCloud Android client application
3 *
4 * @author tobiasKaminsky
5 * @author masensio
6 * Copyright (C) 2015 ownCloud Inc.
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2,
10 * as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
22 package com.owncloud.android.ui.adapter;
23
24 import android.accounts.Account;
25 import android.accounts.AccountManager;
26 import android.content.Context;
27 import android.view.LayoutInflater;
28 import android.view.View;
29 import android.view.ViewGroup;
30 import android.widget.BaseAdapter;
31 import android.widget.LinearLayout;
32 import android.widget.TextView;
33
34 import com.owncloud.android.MainApp;
35 import com.owncloud.android.R;
36 import com.owncloud.android.authentication.AccountUtils;
37 import com.owncloud.android.ui.NavigationDrawerItem;
38 import com.owncloud.android.ui.activity.FileActivity;
39
40 import java.util.ArrayList;
41
42 public class NavigationDrawerListAdapter extends BaseAdapter {
43
44 private final static String TAG = NavigationDrawerListAdapter.class.getSimpleName();
45
46 private Context mContext;
47
48 private ArrayList<NavigationDrawerItem> mNavigationDrawerItems;
49 private ArrayList<Object> mAll = new ArrayList<Object>();
50 private Account[] mAccounts;
51 private boolean mShowAccounts;
52 private Account mCurrentAccount;
53 private FileActivity mFileActivity;
54
55
56 public NavigationDrawerListAdapter(Context context, FileActivity fileActivity,
57 ArrayList<NavigationDrawerItem> navigationDrawerItems){
58 mFileActivity = fileActivity;
59 mContext = context;
60 mNavigationDrawerItems = navigationDrawerItems;
61
62 updateAccountList();
63
64 mAll.addAll(mNavigationDrawerItems);
65 }
66
67 public void updateAccountList(){
68 AccountManager am = (AccountManager) mContext.getSystemService(mContext.ACCOUNT_SERVICE);
69 mAccounts = am.getAccountsByType(MainApp.getAccountType());
70 mCurrentAccount = AccountUtils.getCurrentOwnCloudAccount(mContext);
71 }
72
73 @Override
74 public int getCount() {
75 if (mShowAccounts){
76 return mNavigationDrawerItems.size() + 1;
77 } else {
78 return mNavigationDrawerItems.size();
79 }
80 }
81
82 @Override
83 public Object getItem(int position) {
84 //return all.get(position);
85 return null;
86 }
87
88 @Override
89 public long getItemId(int position) {
90 return 0;
91 }
92
93 @Override
94 public View getView(int position, View convertView, ViewGroup parent) {
95
96 LayoutInflater inflator = (LayoutInflater) mContext
97 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
98
99 if (mAll.size() > position) {
100 // Normal entry
101 if (mAll.get(position) instanceof NavigationDrawerItem){
102 NavigationDrawerItem navItem = (NavigationDrawerItem) mAll.get(position);
103
104 View view = inflator.inflate(R.layout.drawer_list_item, null);
105 view.setMinimumHeight(40);
106 LinearLayout itemLayout = (LinearLayout) view.findViewById(R.id.itemLayout);
107 itemLayout.setContentDescription(navItem.getContentDescription());
108 TextView itemText = (TextView) view.findViewById(R.id.itemTitle);
109 itemText.setText(navItem.getTitle());
110
111 return view;
112 }
113 // TODO re-enable when "Accounts" is available in Navigation Drawer
114 // Account
115 // if (mAll.get(position) instanceof Account[]){
116 // final View view = inflator.inflate(R.layout.drawer_account_group, null);
117 //
118 // final RadioGroup group = (RadioGroup) view.findViewById(R.id.drawer_radio_group);
119 //
120 // for (Account account : mAccounts) {
121 // RadioButton rb = new RadioButton(mContext);
122 //
123 // rb.setText(account.name);
124 // rb.setContentDescription(account.name);
125 // rb.setTextColor(Color.BLACK);
126 // rb.setEllipsize(TextUtils.TruncateAt.MIDDLE);
127 // rb.setSingleLine();
128 // rb.setCompoundDrawablePadding(30);
129 //
130 //
131 // try {
132 // // using adapted algorithm from /core/js/placeholder.js:50
133 // int lastAtPos = account.name.lastIndexOf("@");
134 // String username = account.name.substring(0, lastAtPos);
135 // byte[] seed = username.getBytes("UTF-8");
136 // MessageDigest md = MessageDigest.getInstance("MD5");
137 //// Integer seedMd5Int = Math.abs(new String(Hex.encodeHex(seedMd5))
138 //// .hashCode());
139 // Integer seedMd5Int = String.format(Locale.ROOT, "%032x",
140 // new BigInteger(1, md.digest(seed))).hashCode();
141 //
142 // double maxRange = java.lang.Integer.MAX_VALUE;
143 // float hue = (float) (seedMd5Int / maxRange * 360);
144 //
145 // int[] rgb = BitmapUtils.HSLtoRGB(hue, 90.0f, 65.0f, 1.0f);
146 //
147 // TextDrawable text = new TextDrawable(username.substring(0, 1).toUpperCase(),
148 // rgb[0], rgb[1], rgb[2]);
149 // rb.setCompoundDrawablesWithIntrinsicBounds(text, null, null, null);
150 //
151 //
152 // } catch (Exception e){
153 // Log_OC.d(TAG, e.toString());
154 // rb.setTextColor(mContext.getResources().getColor(R.color.black));
155 // }
156 // RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(
157 // LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
158 // params.weight=1.0f;
159 // params.setMargins(15, 5, 5, 5);
160 //
161 // // Check the current account that is being used
162 // if (account.name.equals(mCurrentAccount.name)) {
163 // rb.setChecked(true);
164 // } else {
165 // rb.setChecked(false);
166 // }
167 //
168 // group.addView(rb, params);
169 // }
170 //
171 // group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
172 // public void onCheckedChanged(RadioGroup group, int checkedId) {
173 // // checkedId is the RadioButton selected
174 // RadioButton rb = (RadioButton) view.findViewById(checkedId);
175 //
176 // AccountUtils.setCurrentOwnCloudAccount(mContext,rb.getText().toString());
177 // notifyDataSetChanged();
178 // mFileActivity.closeDrawer();
179 //
180 // // restart the main activity
181 // mFileActivity.restart();
182 // }
183 // });
184 //
185 // return view;
186 // }
187 }
188 return convertView;
189 }
190
191 //TODO re-enable when "Accounts" is available in Navigation Drawer
192 // TODO update Account List after creating a new account and on fresh installation
193 // public void setShowAccounts(boolean value){
194 // mAll.clear();
195 // mAll.addAll(mNavigationDrawerItems);
196 //
197 // if (value){
198 // mAll.add(1, mAccounts);
199 // }
200 // mShowAccounts = value;
201 // }
202 }