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