f56f529c6df5373d230e93cc29025a99eb9cab42
[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 updateAccountList();
48
49 all.addAll(mDrawerItems);
50 }
51
52 public void updateAccountList(){
53 AccountManager am = (AccountManager) mContext.getSystemService(mContext.ACCOUNT_SERVICE);
54 mAccounts = am.getAccountsByType(MainApp.getAccountType());
55 currentAccount = AccountUtils.getCurrentOwnCloudAccount(mContext);
56 }
57
58 @Override
59 public int getCount() {
60 if (mShowAccounts){
61 return mDrawerItems.size() + 1;
62 } else {
63 return mDrawerItems.size();
64 }
65 }
66
67 @Override
68 public Object getItem(int position) {
69 //return all.get(position);
70 return null;
71 }
72
73 @Override
74 public long getItemId(int position) {
75 return 0;
76 }
77
78 @Override
79 public View getView(int position, View convertView, ViewGroup parent) {
80
81 LayoutInflater inflator = (LayoutInflater) mContext
82 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
83
84 if (all.size() > position) {
85 // Normal entry
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);
90
91 String entry = (String) all.get(position);
92 textView.setText(entry);
93
94 return view;
95 }
96
97 // Account
98 if (all.get(position) instanceof Account[]){
99 final View view = inflator.inflate(R.layout.drawer_account_group, null);
100
101 final RadioGroup group = (RadioGroup) view.findViewById(R.id.drawer_radio_group);
102
103 for (Account account : mAccounts) {
104 RadioButton rb = new RadioButton(mContext);
105 rb.setText(account.name);
106
107 try {
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());
112
113 Log_OC.d(TAG, "Integer: " + result % 100000);
114 Log_OC.d(TAG, "length: " + digest.length);
115
116
117 Double hue = (result % 100000) / 99999.0;
118
119 Log_OC.d(TAG, "hue: " + hue);
120
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]);
124
125 } catch (Exception e){
126 Log_OC.d(TAG, e.toString());
127 rb.setTextColor(mContext.getResources().getColor(R.color.black));
128 }
129 RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(
130 LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
131 params.weight=1.0f;
132 params.setMargins(15, 5, 5, 5);
133
134 // Check the current account that is being used
135 if (account.name.equals(currentAccount.name)) {
136 rb.setChecked(true);
137 } else {
138 rb.setChecked(false);
139 }
140
141 group.addView(rb, params);
142 }
143
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);
148
149 AccountUtils.setCurrentOwnCloudAccount(mContext,rb.getText().toString());
150 notifyDataSetChanged();
151 mFileDisplayActivity.closeDrawer();
152
153 // restart the main activity
154 mFileDisplayActivity.restart();
155 }
156 });
157
158 return view;
159 }
160 }
161 return convertView;
162 }
163
164 // TODO update Account List after creating a new account and on fresh installation
165 public void setShowAccounts(boolean value){
166 all.clear();
167 all.addAll(mDrawerItems);
168
169 if (value){
170 all.add(1, mAccounts);
171 }
172 mShowAccounts = value;
173 }
174 }