5793454e10eafa11ffbf9971e46244f211e8000c
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / adapter / NavigationDrawerListAdapter.java
1 package com.owncloud.android.ui.adapter;
2
3 import java.nio.ByteBuffer;
4 import java.security.MessageDigest;
5 import java.util.ArrayList;
6
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;
19
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;
26
27 public class NavigationDrawerListAdapter extends BaseAdapter {
28
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;
37
38
39 public NavigationDrawerListAdapter(Context context, FileDisplayActivity fileDisplayActivity){
40 mFileDisplayActivity = fileDisplayActivity;
41 mContext = context;
42
43 for (String string : mContext.getResources().getStringArray(R.array.drawer_items)) {
44 mDrawerItems.add(string);
45 }
46
47 AccountManager am = (AccountManager) mContext.getSystemService(mContext.ACCOUNT_SERVICE);
48 mAccounts = am.getAccountsByType(MainApp.getAccountType());
49 currentAccount = AccountUtils.getCurrentOwnCloudAccount(mContext);
50
51 all.addAll(mDrawerItems);
52 }
53
54 @Override
55 public int getCount() {
56 if (mShowAccounts){
57 return mDrawerItems.size() + 1;
58 } else {
59 return mDrawerItems.size();
60 }
61 }
62
63 @Override
64 public Object getItem(int position) {
65 //return all.get(position);
66 return null;
67 }
68
69 @Override
70 public long getItemId(int position) {
71 return 0;
72 }
73
74 @Override
75 public View getView(int position, View convertView, ViewGroup parent) {
76
77 LayoutInflater inflator = (LayoutInflater) mContext
78 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
79
80 if (all.size() > position) {
81 // Normal entry
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);
86
87 String entry = (String) all.get(position);
88 textView.setText(entry);
89
90 return view;
91 }
92
93 // Account
94 if (all.get(position) instanceof Account[]){
95 final View view = inflator.inflate(R.layout.drawer_account_group, null);
96
97 final RadioGroup group = (RadioGroup) view.findViewById(R.id.drawer_radio_group);
98
99 for (Account account : mAccounts) {
100 RadioButton rb = new RadioButton(mContext);
101 rb.setText(account.name);
102
103 try {
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());
108
109 Log_OC.d(TAG, "Integer: " + result % 100000);
110 Log_OC.d(TAG, "length: " + digest.length);
111
112
113 Double hue = (result % 100000) / 99999.0;
114
115 Log_OC.d(TAG, "hue: " + hue);
116
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]);
120
121 } catch (Exception e){
122 Log_OC.d(TAG, e.toString());
123 rb.setTextColor(mContext.getResources().getColor(R.color.black));
124 }
125 RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(
126 LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
127 params.weight=1.0f;
128 params.setMargins(15, 5, 5, 5);
129
130 // Check the current account that is being used
131 if (account.name.equals(currentAccount.name)) {
132 rb.setChecked(true);
133 } else {
134 rb.setChecked(false);
135 }
136
137 group.addView(rb, params);
138 }
139
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);
144
145 AccountUtils.setCurrentOwnCloudAccount(mContext,rb.getText().toString());
146 notifyDataSetChanged();
147 mFileDisplayActivity.closeDrawer();
148
149 // restart the main activity
150 mFileDisplayActivity.restart();
151 }
152 });
153
154 return view;
155 }
156 }
157 return convertView;
158 }
159
160 public void setShowAccounts(boolean value){
161 all.clear();
162 all.addAll(mDrawerItems);
163
164 if (value){
165 all.add(1, mAccounts);
166 }
167 mShowAccounts = value;
168 }
169 }