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 public class NavigationDrawerListAdapter
extends BaseAdapter
{
63 private final static String TAG
= NavigationDrawerListAdapter
.class.getSimpleName();
65 private Context mContext
;
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
;
75 public NavigationDrawerListAdapter(Context context
, FileDisplayActivity fileDisplayActivity
,
76 ArrayList
<NavigationDrawerItem
> navigationDrawerItems
){
77 mFileDisplayActivity
= fileDisplayActivity
;
79 mNavigationDrawerItems
= navigationDrawerItems
;
83 mAll
.addAll(mNavigationDrawerItems
);
86 public void updateAccountList(){
87 AccountManager am
= (AccountManager
) mContext
.getSystemService(mContext
.ACCOUNT_SERVICE
);
88 mAccounts
= am
.getAccountsByType(MainApp
.getAccountType());
89 mCurrentAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
93 public int getCount() {
95 return mNavigationDrawerItems
.size() + 1;
97 return mNavigationDrawerItems
.size();
102 public Object
getItem(int position
) {
103 //return all.get(position);
108 public long getItemId(int position
) {
113 public View
getView(int position
, View convertView
, ViewGroup parent
) {
115 LayoutInflater inflator
= (LayoutInflater
) mContext
116 .getSystemService(Context
.LAYOUT_INFLATER_SERVICE
);
118 if (mAll
.size() > position
) {
120 if (mAll
.get(position
) instanceof NavigationDrawerItem
){
121 NavigationDrawerItem navItem
= (NavigationDrawerItem
) mAll
.get(position
);
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());
134 if (mAll
.get(position
) instanceof Account
[]){
135 final View view
= inflator
.inflate(R
.layout
.drawer_account_group
, null
);
137 final RadioGroup group
= (RadioGroup
) view
.findViewById(R
.id
.drawer_radio_group
);
139 for (Account account
: mAccounts
) {
140 RadioButton rb
= new RadioButton(mContext
);
142 rb
.setText(account
.name
);
143 rb
.setContentDescription(account
.name
);
144 rb
.setTextColor(Color
.BLACK
);
145 rb
.setEllipsize(TextUtils
.TruncateAt
.MIDDLE
);
147 rb
.setCompoundDrawablePadding(30);
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();
159 double maxRange
= java
.lang
.Integer
.MAX_VALUE
;
160 float hue
= (float) (seedMd5Int
/ maxRange
* 360);
162 int[] rgb
= BitmapUtils
.HSLtoRGB(hue
, 90.0f
, 65.0f
, 1.0f
);
164 TextDrawable text
= new TextDrawable(username
.substring(0, 1).toUpperCase(), rgb
[0], rgb
[1], rgb
[2]);
165 rb
.setCompoundDrawablesWithIntrinsicBounds(text
, null
, null
, null
);
168 } catch (Exception e
){
169 Log_OC
.d(TAG
, e
.toString());
170 rb
.setTextColor(mContext
.getResources().getColor(R
.color
.black
));
172 RadioGroup
.LayoutParams params
= new RadioGroup
.LayoutParams(
173 LayoutParams
.MATCH_PARENT
, LayoutParams
.WRAP_CONTENT
);
175 params
.setMargins(15, 5, 5, 5);
177 // Check the current account that is being used
178 if (account
.name
.equals(mCurrentAccount
.name
)) {
181 rb
.setChecked(false
);
184 group
.addView(rb
, params
);
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
);
192 AccountUtils
.setCurrentOwnCloudAccount(mContext
,rb
.getText().toString());
193 notifyDataSetChanged();
194 mFileDisplayActivity
.closeDrawer();
196 // restart the main activity
197 mFileDisplayActivity
.restart();
207 // TODO update Account List after creating a new account and on fresh installation
208 public void setShowAccounts(boolean value
){
210 mAll
.addAll(mNavigationDrawerItems
);
213 mAll
.add(1, mAccounts
);
215 mShowAccounts
= value
;