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
.security
.MessageDigest
;
26 import java
.util
.ArrayList
;
27 import java
.util
.Locale
;
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
;
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
;
53 public class NavigationDrawerListAdapter
extends BaseAdapter
{
55 private final static String TAG
= NavigationDrawerListAdapter
.class.getSimpleName();
57 private Context mContext
;
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
;
67 public NavigationDrawerListAdapter(Context context
, FileActivity fileActivity
,
68 ArrayList
<NavigationDrawerItem
> navigationDrawerItems
){
69 mFileActivity
= fileActivity
;
71 mNavigationDrawerItems
= navigationDrawerItems
;
75 mAll
.addAll(mNavigationDrawerItems
);
78 public void updateAccountList(){
79 AccountManager am
= (AccountManager
) mContext
.getSystemService(mContext
.ACCOUNT_SERVICE
);
80 mAccounts
= am
.getAccountsByType(MainApp
.getAccountType());
81 mCurrentAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
85 public int getCount() {
87 return mNavigationDrawerItems
.size() + 1;
89 return mNavigationDrawerItems
.size();
94 public Object
getItem(int position
) {
95 //return all.get(position);
100 public long getItemId(int position
) {
105 public View
getView(int position
, View convertView
, ViewGroup parent
) {
107 LayoutInflater inflator
= (LayoutInflater
) mContext
108 .getSystemService(Context
.LAYOUT_INFLATER_SERVICE
);
110 if (mAll
.size() > position
) {
112 if (mAll
.get(position
) instanceof NavigationDrawerItem
){
113 NavigationDrawerItem navItem
= (NavigationDrawerItem
) mAll
.get(position
);
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());
126 if (mAll
.get(position
) instanceof Account
[]){
127 final View view
= inflator
.inflate(R
.layout
.drawer_account_group
, null
);
129 final RadioGroup group
= (RadioGroup
) view
.findViewById(R
.id
.drawer_radio_group
);
131 for (Account account
: mAccounts
) {
132 RadioButton rb
= new RadioButton(mContext
);
134 rb
.setText(account
.name
);
135 rb
.setContentDescription(account
.name
);
136 rb
.setTextColor(Color
.BLACK
);
137 rb
.setEllipsize(TextUtils
.TruncateAt
.MIDDLE
);
139 rb
.setCompoundDrawablePadding(30);
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))
150 Integer seedMd5Int
= String
.format(Locale
.ROOT
, "%032x",
151 new BigInteger(1, md
.digest(seed
))).hashCode();
153 double maxRange
= java
.lang
.Integer
.MAX_VALUE
;
154 float hue
= (float) (seedMd5Int
/ maxRange
* 360);
156 int[] rgb
= BitmapUtils
.HSLtoRGB(hue
, 90.0f
, 65.0f
, 1.0f
);
158 TextDrawable text
= new TextDrawable(username
.substring(0, 1).toUpperCase(),
159 rgb
[0], rgb
[1], rgb
[2]);
160 rb
.setCompoundDrawablesWithIntrinsicBounds(text
, null
, null
, null
);
163 } catch (Exception e
){
164 Log_OC
.d(TAG
, e
.toString());
165 rb
.setTextColor(mContext
.getResources().getColor(R
.color
.black
));
167 RadioGroup
.LayoutParams params
= new RadioGroup
.LayoutParams(
168 LayoutParams
.MATCH_PARENT
, LayoutParams
.WRAP_CONTENT
);
170 params
.setMargins(15, 5, 5, 5);
172 // Check the current account that is being used
173 if (account
.name
.equals(mCurrentAccount
.name
)) {
176 rb
.setChecked(false
);
179 group
.addView(rb
, params
);
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
);
187 AccountUtils
.setCurrentOwnCloudAccount(mContext
,rb
.getText().toString());
188 notifyDataSetChanged();
189 mFileActivity
.closeDrawer();
191 // restart the main activity
192 mFileActivity
.restart();
202 // TODO update Account List after creating a new account and on fresh installation
203 public void setShowAccounts(boolean value
){
205 mAll
.addAll(mNavigationDrawerItems
);
208 mAll
.add(1, mAccounts
);
210 mShowAccounts
= value
;