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
);
49 all
.addAll(mDrawerItems
);
52 public void updateAccountList(){
53 AccountManager am
= (AccountManager
) mContext
.getSystemService(mContext
.ACCOUNT_SERVICE
);
54 mAccounts
= am
.getAccountsByType(MainApp
.getAccountType());
55 currentAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
59 public int getCount() {
61 return mDrawerItems
.size() + 1;
63 return mDrawerItems
.size();
68 public Object
getItem(int position
) {
69 //return all.get(position);
74 public long getItemId(int position
) {
79 public View
getView(int position
, View convertView
, ViewGroup parent
) {
81 LayoutInflater inflator
= (LayoutInflater
) mContext
82 .getSystemService(Context
.LAYOUT_INFLATER_SERVICE
);
84 if (all
.size() > position
) {
86 if (all
.get(position
) instanceof String
){
87 View view
= inflator
.inflate(R
.layout
.drawer_list_item
, null
);
88 view
.setMinimumHeight(40);
89 TextView textView
= (TextView
) view
.findViewById(R
.id
.drawer_textView
);
91 String entry
= (String
) all
.get(position
);
92 textView
.setText(entry
);
98 if (all
.get(position
) instanceof Account
[]){
99 final View view
= inflator
.inflate(R
.layout
.drawer_account_group
, null
);
101 final RadioGroup group
= (RadioGroup
) view
.findViewById(R
.id
.drawer_radio_group
);
103 for (Account account
: mAccounts
) {
104 RadioButton rb
= new RadioButton(mContext
);
105 rb
.setText(account
.name
);
108 byte[] bytesOfMessage
= account
.name
.substring(0,5).getBytes("UTF-8");
109 MessageDigest md
= MessageDigest
.getInstance("MD5");
110 byte[] digest
= md
.digest(bytesOfMessage
);
111 int result
= Math
.abs(ByteBuffer
.wrap(digest
).getInt());
113 Log_OC
.d(TAG
, "Integer: " + result
% 100000);
114 Log_OC
.d(TAG
, "length: " + digest
.length
);
117 Double hue
= (result
% 100000) / 99999.0;
119 Log_OC
.d(TAG
, "hue: " + hue
);
121 int[] rgb
= BitmapUtils
.hslToRgb(hue
, 0.9, 0.65);
122 rb
.setTextColor(Color
.rgb(rgb
[0], rgb
[1], rgb
[2]));
123 Log_OC
.d(TAG
, "Color: " + rgb
[0] + " " + rgb
[1] + rgb
[2]);
125 } catch (Exception e
){
126 Log_OC
.d(TAG
, e
.toString());
127 rb
.setTextColor(mContext
.getResources().getColor(R
.color
.black
));
129 RadioGroup
.LayoutParams params
= new RadioGroup
.LayoutParams(
130 LayoutParams
.MATCH_PARENT
, LayoutParams
.WRAP_CONTENT
);
132 params
.setMargins(15, 5, 5, 5);
134 // Check the current account that is being used
135 if (account
.name
.equals(currentAccount
.name
)) {
138 rb
.setChecked(false
);
141 group
.addView(rb
, params
);
144 group
.setOnCheckedChangeListener(new RadioGroup
.OnCheckedChangeListener(){
145 public void onCheckedChanged(RadioGroup group
, int checkedId
) {
146 // checkedId is the RadioButton selected
147 RadioButton rb
= (RadioButton
) view
.findViewById(checkedId
);
149 AccountUtils
.setCurrentOwnCloudAccount(mContext
,rb
.getText().toString());
150 notifyDataSetChanged();
151 mFileDisplayActivity
.closeDrawer();
153 // restart the main activity
154 mFileDisplayActivity
.restart();
164 // TODO update Account List after creating a new account and on fresh installation
165 public void setShowAccounts(boolean value
){
167 all
.addAll(mDrawerItems
);
170 all
.add(1, mAccounts
);
172 mShowAccounts
= value
;