1 package com
.owncloud
.android
.ui
.adapter
;
3 import java
.nio
.ByteBuffer
;
4 import java
.security
.MessageDigest
;
5 import java
.util
.ArrayList
;
7 import android
.accounts
.Account
;
8 import android
.accounts
.AccountManager
;
9 import android
.content
.Context
;
10 import android
.graphics
.Color
;
11 import android
.view
.LayoutInflater
;
12 import android
.view
.View
;
13 import android
.view
.ViewGroup
;
14 import android
.widget
.BaseAdapter
;
15 import android
.widget
.RadioButton
;
16 import android
.widget
.RadioGroup
;
17 import android
.widget
.RadioGroup
.LayoutParams
;
18 import android
.widget
.TextView
;
20 import com
.owncloud
.android
.MainApp
;
21 import com
.owncloud
.android
.R
;
22 import com
.owncloud
.android
.authentication
.AccountUtils
;
23 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
24 import com
.owncloud
.android
.ui
.activity
.FileDisplayActivity
;
25 import com
.owncloud
.android
.utils
.BitmapUtils
;
27 import org
.apache
.commons
.codec
.binary
.Hex
;
29 public class NavigationDrawerListAdapter
extends BaseAdapter
{
31 private final static String TAG
= "NavigationDrawerListAdapter";
32 private Context mContext
;
33 private ArrayList
<String
> mDrawerItems
= new ArrayList
<String
>();
34 ArrayList
<Object
> all
= new ArrayList
<Object
>();
35 private Account
[] mAccounts
;
36 private boolean mShowAccounts
;
37 private Account currentAccount
;
38 private FileDisplayActivity mFileDisplayActivity
;
41 public NavigationDrawerListAdapter(Context context
, FileDisplayActivity fileDisplayActivity
){
42 mFileDisplayActivity
= fileDisplayActivity
;
45 for (String string
: mContext
.getResources().getStringArray(R
.array
.drawer_items
)) {
46 mDrawerItems
.add(string
);
51 all
.addAll(mDrawerItems
);
54 public void updateAccountList(){
55 AccountManager am
= (AccountManager
) mContext
.getSystemService(mContext
.ACCOUNT_SERVICE
);
56 mAccounts
= am
.getAccountsByType(MainApp
.getAccountType());
57 currentAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
61 public int getCount() {
63 return mDrawerItems
.size() + 1;
65 return mDrawerItems
.size();
70 public Object
getItem(int position
) {
71 //return all.get(position);
76 public long getItemId(int position
) {
81 public View
getView(int position
, View convertView
, ViewGroup parent
) {
83 LayoutInflater inflator
= (LayoutInflater
) mContext
84 .getSystemService(Context
.LAYOUT_INFLATER_SERVICE
);
86 if (all
.size() > position
) {
88 if (all
.get(position
) instanceof String
){
89 View view
= inflator
.inflate(R
.layout
.drawer_list_item
, null
);
90 view
.setMinimumHeight(40);
91 TextView textView
= (TextView
) view
.findViewById(R
.id
.drawer_textView
);
93 String entry
= (String
) all
.get(position
);
94 textView
.setText(entry
);
100 if (all
.get(position
) instanceof Account
[]){
101 final View view
= inflator
.inflate(R
.layout
.drawer_account_group
, null
);
103 final RadioGroup group
= (RadioGroup
) view
.findViewById(R
.id
.drawer_radio_group
);
105 for (Account account
: mAccounts
) {
106 RadioButton rb
= new RadioButton(mContext
);
107 rb
.setText(account
.name
);
110 // using adapted algorithm from /core/js/placeholder.js:50
111 int lastAtPos
= account
.name
.lastIndexOf("@");
112 String username
= account
.name
.substring(0, lastAtPos
);
113 byte[] seed
= username
.getBytes("UTF-8");
114 MessageDigest md
= MessageDigest
.getInstance("MD5");
115 byte[] seedMd5
= md
.digest(seed
);
116 Integer seedMd5Int
= Math
.abs(new String(Hex
.encodeHex(seedMd5
)).hashCode());
118 double maxRange
= java
.lang
.Integer
.MAX_VALUE
;
119 float hue
= (float) (seedMd5Int
/ maxRange
* 360);
121 Log_OC
.d(TAG
, "hue: " + hue
);
124 int[] rgb
= BitmapUtils
.HSLtoRGB(hue
, 90.0f
, 65.0f
, 1.0f
);
125 rb
.setTextColor(Color
.rgb(rgb
[0], rgb
[1], rgb
[2]));
126 Log_OC
.d(TAG
, "Color: " + rgb
[0] + " " + rgb
[1] + " " + rgb
[2]);
128 } catch (Exception e
){
129 Log_OC
.d(TAG
, e
.toString());
130 rb
.setTextColor(mContext
.getResources().getColor(R
.color
.black
));
132 RadioGroup
.LayoutParams params
= new RadioGroup
.LayoutParams(
133 LayoutParams
.MATCH_PARENT
, LayoutParams
.WRAP_CONTENT
);
135 params
.setMargins(15, 5, 5, 5);
137 // Check the current account that is being used
138 if (account
.name
.equals(currentAccount
.name
)) {
141 rb
.setChecked(false
);
144 group
.addView(rb
, params
);
147 group
.setOnCheckedChangeListener(new RadioGroup
.OnCheckedChangeListener(){
148 public void onCheckedChanged(RadioGroup group
, int checkedId
) {
149 // checkedId is the RadioButton selected
150 RadioButton rb
= (RadioButton
) view
.findViewById(checkedId
);
152 AccountUtils
.setCurrentOwnCloudAccount(mContext
,rb
.getText().toString());
153 notifyDataSetChanged();
154 mFileDisplayActivity
.closeDrawer();
156 // restart the main activity
157 mFileDisplayActivity
.restart();
167 // TODO update Account List after creating a new account and on fresh installation
168 public void setShowAccounts(boolean value
){
170 all
.addAll(mDrawerItems
);
173 all
.add(1, mAccounts
);
175 mShowAccounts
= value
;