2 * ownCloud Android client application
4 * @author tobiasKaminsky
6 * Copyright (C) 2015 ownCloud Inc.
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.
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.
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/>.
22 package com
.owncloud
.android
.ui
.adapter
;
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
;
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
;
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
;
61 import org
.apache
.commons
.codec
.binary
.Hex
;
63 public class NavigationDrawerListAdapter
extends BaseAdapter
{
65 private final static String TAG
= NavigationDrawerListAdapter
.class.getSimpleName();
67 private Context mContext
;
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
;
77 public NavigationDrawerListAdapter(Context context
, FileDisplayActivity fileDisplayActivity
,
78 ArrayList
<NavigationDrawerItem
> navigationDrawerItems
){
79 mFileDisplayActivity
= fileDisplayActivity
;
81 mNavigationDrawerItems
= navigationDrawerItems
;
85 mAll
.addAll(mNavigationDrawerItems
);
88 public void updateAccountList(){
89 AccountManager am
= (AccountManager
) mContext
.getSystemService(mContext
.ACCOUNT_SERVICE
);
90 mAccounts
= am
.getAccountsByType(MainApp
.getAccountType());
91 mCurrentAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
95 public int getCount() {
97 return mNavigationDrawerItems
.size() + 1;
99 return mNavigationDrawerItems
.size();
104 public Object
getItem(int position
) {
105 //return all.get(position);
110 public long getItemId(int position
) {
115 public View
getView(int position
, View convertView
, ViewGroup parent
) {
117 LayoutInflater inflator
= (LayoutInflater
) mContext
118 .getSystemService(Context
.LAYOUT_INFLATER_SERVICE
);
120 if (mAll
.size() > position
) {
122 if (mAll
.get(position
) instanceof NavigationDrawerItem
){
123 NavigationDrawerItem navItem
= (NavigationDrawerItem
) mAll
.get(position
);
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());
136 if (mAll
.get(position
) instanceof Account
[]){
137 final View view
= inflator
.inflate(R
.layout
.drawer_account_group
, null
);
139 final RadioGroup group
= (RadioGroup
) view
.findViewById(R
.id
.drawer_radio_group
);
141 for (Account account
: mAccounts
) {
142 RadioButton rb
= new RadioButton(mContext
);
144 rb
.setText(account
.name
);
145 rb
.setContentDescription(account
.name
);
146 rb
.setTextColor(Color
.BLACK
);
147 rb
.setEllipsize(TextUtils
.TruncateAt
.MIDDLE
);
149 rb
.setCompoundDrawablePadding(30);
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();
161 double maxRange
= java
.lang
.Integer
.MAX_VALUE
;
162 float hue
= (float) (seedMd5Int
/ maxRange
* 360);
164 int[] rgb
= BitmapUtils
.HSLtoRGB(hue
, 90.0f
, 65.0f
, 1.0f
);
166 TextDrawable text
= new TextDrawable(username
.substring(0, 1).toUpperCase(), rgb
[0], rgb
[1], rgb
[2]);
167 rb
.setCompoundDrawablesWithIntrinsicBounds(text
, null
, null
, null
);
170 } catch (Exception e
){
171 Log_OC
.d(TAG
, e
.toString());
172 rb
.setTextColor(mContext
.getResources().getColor(R
.color
.black
));
174 RadioGroup
.LayoutParams params
= new RadioGroup
.LayoutParams(
175 LayoutParams
.MATCH_PARENT
, LayoutParams
.WRAP_CONTENT
);
177 params
.setMargins(15, 5, 5, 5);
179 // Check the current account that is being used
180 if (account
.name
.equals(mCurrentAccount
.name
)) {
183 rb
.setChecked(false
);
186 group
.addView(rb
, params
);
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
);
194 AccountUtils
.setCurrentOwnCloudAccount(mContext
,rb
.getText().toString());
195 notifyDataSetChanged();
196 mFileDisplayActivity
.closeDrawer();
198 // restart the main activity
199 mFileDisplayActivity
.restart();
209 // TODO update Account List after creating a new account and on fresh installation
210 public void setShowAccounts(boolean value
){
212 mAll
.addAll(mNavigationDrawerItems
);
215 mAll
.add(1, mAccounts
);
217 mShowAccounts
= value
;