f9a73a3938832d406ec24b7cb48309a5db4f05f1
[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 import org.apache.commons.codec.binary.Hex;
28
29 public class NavigationDrawerListAdapter extends BaseAdapter {
30
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;
39
40
41 public NavigationDrawerListAdapter(Context context, FileDisplayActivity fileDisplayActivity){
42 mFileDisplayActivity = fileDisplayActivity;
43 mContext = context;
44
45 for (String string : mContext.getResources().getStringArray(R.array.drawer_items)) {
46 mDrawerItems.add(string);
47 }
48
49 updateAccountList();
50
51 all.addAll(mDrawerItems);
52 }
53
54 public void updateAccountList(){
55 AccountManager am = (AccountManager) mContext.getSystemService(mContext.ACCOUNT_SERVICE);
56 mAccounts = am.getAccountsByType(MainApp.getAccountType());
57 currentAccount = AccountUtils.getCurrentOwnCloudAccount(mContext);
58 }
59
60 @Override
61 public int getCount() {
62 if (mShowAccounts){
63 return mDrawerItems.size() + 1;
64 } else {
65 return mDrawerItems.size();
66 }
67 }
68
69 @Override
70 public Object getItem(int position) {
71 //return all.get(position);
72 return null;
73 }
74
75 @Override
76 public long getItemId(int position) {
77 return 0;
78 }
79
80 @Override
81 public View getView(int position, View convertView, ViewGroup parent) {
82
83 LayoutInflater inflator = (LayoutInflater) mContext
84 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
85
86 if (all.size() > position) {
87 // Normal entry
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);
92
93 String entry = (String) all.get(position);
94 textView.setText(entry);
95
96 return view;
97 }
98
99 // Account
100 if (all.get(position) instanceof Account[]){
101 final View view = inflator.inflate(R.layout.drawer_account_group, null);
102
103 final RadioGroup group = (RadioGroup) view.findViewById(R.id.drawer_radio_group);
104
105 for (Account account : mAccounts) {
106 RadioButton rb = new RadioButton(mContext);
107 rb.setText(account.name);
108
109 try {
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());
117
118 double maxRange = java.lang.Integer.MAX_VALUE;
119 float hue = (float) (seedMd5Int / maxRange * 360);
120
121 Log_OC.d(TAG, "hue: " + hue);
122
123
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]);
127
128 } catch (Exception e){
129 Log_OC.d(TAG, e.toString());
130 rb.setTextColor(mContext.getResources().getColor(R.color.black));
131 }
132 RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(
133 LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
134 params.weight=1.0f;
135 params.setMargins(15, 5, 5, 5);
136
137 // Check the current account that is being used
138 if (account.name.equals(currentAccount.name)) {
139 rb.setChecked(true);
140 } else {
141 rb.setChecked(false);
142 }
143
144 group.addView(rb, params);
145 }
146
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);
151
152 AccountUtils.setCurrentOwnCloudAccount(mContext,rb.getText().toString());
153 notifyDataSetChanged();
154 mFileDisplayActivity.closeDrawer();
155
156 // restart the main activity
157 mFileDisplayActivity.restart();
158 }
159 });
160
161 return view;
162 }
163 }
164 return convertView;
165 }
166
167 // TODO update Account List after creating a new account and on fresh installation
168 public void setShowAccounts(boolean value){
169 all.clear();
170 all.addAll(mDrawerItems);
171
172 if (value){
173 all.add(1, mAccounts);
174 }
175 mShowAccounts = value;
176 }
177 }