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 public class NavigationDrawerListAdapter
extends BaseAdapter
{
29 private final static String TAG
= "NavigationDrawerListAdapter";
30 private Context mContext
;
31 private ArrayList
<String
> mDrawerItems
= new ArrayList
<String
>();
32 ArrayList
<Object
> all
= new ArrayList
<Object
>();
33 private Account
[] mAccounts
;
34 private boolean mShowAccounts
;
35 private Account currentAccount
;
36 private FileDisplayActivity mFileDisplayActivity
;
39 public NavigationDrawerListAdapter(Context context
, FileDisplayActivity fileDisplayActivity
){
40 mFileDisplayActivity
= fileDisplayActivity
;
43 for (String string
: mContext
.getResources().getStringArray(R
.array
.drawer_items
)) {
44 mDrawerItems
.add(string
);
47 AccountManager am
= (AccountManager
) mContext
.getSystemService(mContext
.ACCOUNT_SERVICE
);
48 mAccounts
= am
.getAccountsByType(MainApp
.getAccountType());
49 currentAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
51 all
.addAll(mDrawerItems
);
55 public int getCount() {
57 return mDrawerItems
.size() + 1;
59 return mDrawerItems
.size();
64 public Object
getItem(int position
) {
65 //return all.get(position);
70 public long getItemId(int position
) {
75 public View
getView(int position
, View convertView
, ViewGroup parent
) {
77 LayoutInflater inflator
= (LayoutInflater
) mContext
78 .getSystemService(Context
.LAYOUT_INFLATER_SERVICE
);
80 if (all
.size() > position
) {
82 if (all
.get(position
) instanceof String
){
83 View view
= inflator
.inflate(R
.layout
.drawer_list_item
, null
);
84 view
.setMinimumHeight(40);
85 TextView textView
= (TextView
) view
.findViewById(R
.id
.drawer_textView
);
87 String entry
= (String
) all
.get(position
);
88 textView
.setText(entry
);
94 if (all
.get(position
) instanceof Account
[]){
95 final View view
= inflator
.inflate(R
.layout
.drawer_account_group
, null
);
97 final RadioGroup group
= (RadioGroup
) view
.findViewById(R
.id
.drawer_radio_group
);
99 for (Account account
: mAccounts
) {
100 RadioButton rb
= new RadioButton(mContext
);
101 rb
.setText(account
.name
);
104 byte[] bytesOfMessage
= account
.name
.substring(0,5).getBytes("UTF-8");
105 MessageDigest md
= MessageDigest
.getInstance("MD5");
106 byte[] digest
= md
.digest(bytesOfMessage
);
107 int result
= Math
.abs(ByteBuffer
.wrap(digest
).getInt());
109 Log_OC
.d(TAG
, "Integer: " + result
% 100000);
110 Log_OC
.d(TAG
, "length: " + digest
.length
);
113 Double hue
= (result
% 100000) / 99999.0;
115 Log_OC
.d(TAG
, "hue: " + hue
);
117 int[] rgb
= BitmapUtils
.hslToRgb(hue
, 0.9, 0.65);
118 rb
.setTextColor(Color
.rgb(rgb
[0], rgb
[1], rgb
[2]));
119 Log_OC
.d(TAG
, "Color: " + rgb
[0] + " " + rgb
[1] + rgb
[2]);
121 } catch (Exception e
){
122 Log_OC
.d(TAG
, e
.toString());
123 rb
.setTextColor(mContext
.getResources().getColor(R
.color
.black
));
125 RadioGroup
.LayoutParams params
= new RadioGroup
.LayoutParams(
126 LayoutParams
.MATCH_PARENT
, LayoutParams
.WRAP_CONTENT
);
128 params
.setMargins(15, 5, 5, 5);
130 // Check the current account that is being used
131 if (account
.name
.equals(currentAccount
.name
)) {
134 rb
.setChecked(false
);
137 group
.addView(rb
, params
);
140 group
.setOnCheckedChangeListener(new RadioGroup
.OnCheckedChangeListener(){
141 public void onCheckedChanged(RadioGroup group
, int checkedId
) {
142 // checkedId is the RadioButton selected
143 RadioButton rb
= (RadioButton
) view
.findViewById(checkedId
);
145 AccountUtils
.setCurrentOwnCloudAccount(mContext
,rb
.getText().toString());
146 notifyDataSetChanged();
147 mFileDisplayActivity
.closeDrawer();
149 // restart the main activity
150 mFileDisplayActivity
.restart();
160 public void setShowAccounts(boolean value
){
162 all
.addAll(mDrawerItems
);
165 all
.add(1, mAccounts
);
167 mShowAccounts
= value
;