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