428acd8cabdbc1eee4da9af9eb77f2e7d0af22f8
[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 java.math.BigInteger;
25 import java.security.MessageDigest;
26 import java.util.ArrayList;
27 import java.util.Locale;
28
29 import android.accounts.Account;
30 import android.accounts.AccountManager;
31 import android.content.Context;
32 import android.graphics.Color;
33 import android.text.TextUtils;
34 import android.view.LayoutInflater;
35 import android.view.View;
36 import android.view.ViewGroup;
37 import android.widget.BaseAdapter;
38 import android.widget.LinearLayout;
39 import android.widget.RadioButton;
40 import android.widget.RadioGroup;
41 import android.widget.RadioGroup.LayoutParams;
42 import android.widget.TextView;
43
44 import com.owncloud.android.MainApp;
45 import com.owncloud.android.R;
46 import com.owncloud.android.authentication.AccountUtils;
47 import com.owncloud.android.lib.common.utils.Log_OC;
48 import com.owncloud.android.ui.NavigationDrawerItem;
49 import com.owncloud.android.ui.TextDrawable;
50 import com.owncloud.android.ui.activity.FileActivity;
51 import com.owncloud.android.utils.BitmapUtils;
52
53 public class NavigationDrawerListAdapter extends BaseAdapter {
54
55 private final static String TAG = NavigationDrawerListAdapter.class.getSimpleName();
56
57 private Context mContext;
58
59 private ArrayList<NavigationDrawerItem> mNavigationDrawerItems;
60 private ArrayList<Object> mAll = new ArrayList<Object>();
61 private Account[] mAccounts;
62 private boolean mShowAccounts;
63 private Account mCurrentAccount;
64 private FileActivity mFileActivity;
65
66
67 public NavigationDrawerListAdapter(Context context, FileActivity fileActivity,
68 ArrayList<NavigationDrawerItem> navigationDrawerItems){
69 mFileActivity = fileActivity;
70 mContext = context;
71 mNavigationDrawerItems = navigationDrawerItems;
72
73 updateAccountList();
74
75 mAll.addAll(mNavigationDrawerItems);
76 }
77
78 public void updateAccountList(){
79 AccountManager am = (AccountManager) mContext.getSystemService(mContext.ACCOUNT_SERVICE);
80 mAccounts = am.getAccountsByType(MainApp.getAccountType());
81 mCurrentAccount = AccountUtils.getCurrentOwnCloudAccount(mContext);
82 }
83
84 @Override
85 public int getCount() {
86 if (mShowAccounts){
87 return mNavigationDrawerItems.size() + 1;
88 } else {
89 return mNavigationDrawerItems.size();
90 }
91 }
92
93 @Override
94 public Object getItem(int position) {
95 //return all.get(position);
96 return null;
97 }
98
99 @Override
100 public long getItemId(int position) {
101 return 0;
102 }
103
104 @Override
105 public View getView(int position, View convertView, ViewGroup parent) {
106
107 LayoutInflater inflator = (LayoutInflater) mContext
108 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
109
110 if (mAll.size() > position) {
111 // Normal entry
112 if (mAll.get(position) instanceof NavigationDrawerItem){
113 NavigationDrawerItem navItem = (NavigationDrawerItem) mAll.get(position);
114
115 View view = inflator.inflate(R.layout.drawer_list_item, null);
116 view.setMinimumHeight(40);
117 LinearLayout itemLayout = (LinearLayout) view.findViewById(R.id.itemLayout);
118 itemLayout.setContentDescription(navItem.getContentDescription());
119 TextView itemText = (TextView) view.findViewById(R.id.itemTitle);
120 itemText.setText(navItem.getTitle());
121
122 return view;
123 }
124 // TODO re-enable when "Accounts" is available in Navigation Drawer
125 // Account
126 // if (mAll.get(position) instanceof Account[]){
127 // final View view = inflator.inflate(R.layout.drawer_account_group, null);
128 //
129 // final RadioGroup group = (RadioGroup) view.findViewById(R.id.drawer_radio_group);
130 //
131 // for (Account account : mAccounts) {
132 // RadioButton rb = new RadioButton(mContext);
133 //
134 // rb.setText(account.name);
135 // rb.setContentDescription(account.name);
136 // rb.setTextColor(Color.BLACK);
137 // rb.setEllipsize(TextUtils.TruncateAt.MIDDLE);
138 // rb.setSingleLine();
139 // rb.setCompoundDrawablePadding(30);
140 //
141 //
142 // try {
143 // // using adapted algorithm from /core/js/placeholder.js:50
144 // int lastAtPos = account.name.lastIndexOf("@");
145 // String username = account.name.substring(0, lastAtPos);
146 // byte[] seed = username.getBytes("UTF-8");
147 // MessageDigest md = MessageDigest.getInstance("MD5");
148 //// Integer seedMd5Int = Math.abs(new String(Hex.encodeHex(seedMd5))
149 //// .hashCode());
150 // Integer seedMd5Int = String.format(Locale.ROOT, "%032x",
151 // new BigInteger(1, md.digest(seed))).hashCode();
152 //
153 // double maxRange = java.lang.Integer.MAX_VALUE;
154 // float hue = (float) (seedMd5Int / maxRange * 360);
155 //
156 // int[] rgb = BitmapUtils.HSLtoRGB(hue, 90.0f, 65.0f, 1.0f);
157 //
158 // TextDrawable text = new TextDrawable(username.substring(0, 1).toUpperCase(),
159 // rgb[0], rgb[1], rgb[2]);
160 // rb.setCompoundDrawablesWithIntrinsicBounds(text, null, null, null);
161 //
162 //
163 // } catch (Exception e){
164 // Log_OC.d(TAG, e.toString());
165 // rb.setTextColor(mContext.getResources().getColor(R.color.black));
166 // }
167 // RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(
168 // LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
169 // params.weight=1.0f;
170 // params.setMargins(15, 5, 5, 5);
171 //
172 // // Check the current account that is being used
173 // if (account.name.equals(mCurrentAccount.name)) {
174 // rb.setChecked(true);
175 // } else {
176 // rb.setChecked(false);
177 // }
178 //
179 // group.addView(rb, params);
180 // }
181 //
182 // group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
183 // public void onCheckedChanged(RadioGroup group, int checkedId) {
184 // // checkedId is the RadioButton selected
185 // RadioButton rb = (RadioButton) view.findViewById(checkedId);
186 //
187 // AccountUtils.setCurrentOwnCloudAccount(mContext,rb.getText().toString());
188 // notifyDataSetChanged();
189 // mFileActivity.closeDrawer();
190 //
191 // // restart the main activity
192 // mFileActivity.restart();
193 // }
194 // });
195 //
196 // return view;
197 // }
198 }
199 return convertView;
200 }
201
202 //TODO re-enable when "Accounts" is available in Navigation Drawer
203 // TODO update Account List after creating a new account and on fresh installation
204 // public void setShowAccounts(boolean value){
205 // mAll.clear();
206 // mAll.addAll(mNavigationDrawerItems);
207 //
208 // if (value){
209 // mAll.add(1, mAccounts);
210 // }
211 // mShowAccounts = value;
212 // }
213 }