2 * ownCloud Android client application
4 * @author tobiasKaminsky
6 * Copyright (C) 2015 ownCloud Inc.
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 package com
.owncloud
.android
.ui
.adapter
;
24 import android
.accounts
.Account
;
25 import android
.accounts
.AccountManager
;
26 import android
.content
.Context
;
27 import android
.view
.LayoutInflater
;
28 import android
.view
.View
;
29 import android
.view
.ViewGroup
;
30 import android
.widget
.BaseAdapter
;
31 import android
.widget
.LinearLayout
;
32 import android
.widget
.TextView
;
34 import com
.owncloud
.android
.MainApp
;
35 import com
.owncloud
.android
.R
;
36 import com
.owncloud
.android
.authentication
.AccountUtils
;
37 import com
.owncloud
.android
.ui
.NavigationDrawerItem
;
38 import com
.owncloud
.android
.ui
.activity
.FileActivity
;
40 import java
.util
.ArrayList
;
42 public class NavigationDrawerListAdapter
extends BaseAdapter
{
44 private final static String TAG
= NavigationDrawerListAdapter
.class.getSimpleName();
46 private Context mContext
;
48 private ArrayList
<NavigationDrawerItem
> mNavigationDrawerItems
;
49 private ArrayList
<Object
> mAll
= new ArrayList
<Object
>();
50 private Account
[] mAccounts
;
51 private boolean mShowAccounts
;
52 private Account mCurrentAccount
;
53 private FileActivity mFileActivity
;
56 public NavigationDrawerListAdapter(Context context
, FileActivity fileActivity
,
57 ArrayList
<NavigationDrawerItem
> navigationDrawerItems
){
58 mFileActivity
= fileActivity
;
60 mNavigationDrawerItems
= navigationDrawerItems
;
64 mAll
.addAll(mNavigationDrawerItems
);
67 public void updateAccountList(){
68 AccountManager am
= (AccountManager
) mContext
.getSystemService(mContext
.ACCOUNT_SERVICE
);
69 mAccounts
= am
.getAccountsByType(MainApp
.getAccountType());
70 mCurrentAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
74 public int getCount() {
76 return mNavigationDrawerItems
.size() + 1;
78 return mNavigationDrawerItems
.size();
83 public Object
getItem(int position
) {
84 //return all.get(position);
89 public long getItemId(int position
) {
94 public View
getView(int position
, View convertView
, ViewGroup parent
) {
96 LayoutInflater inflator
= (LayoutInflater
) mContext
97 .getSystemService(Context
.LAYOUT_INFLATER_SERVICE
);
99 if (mAll
.size() > position
) {
101 if (mAll
.get(position
) instanceof NavigationDrawerItem
){
102 NavigationDrawerItem navItem
= (NavigationDrawerItem
) mAll
.get(position
);
104 View view
= inflator
.inflate(R
.layout
.drawer_list_item
, null
);
105 view
.setMinimumHeight(40);
106 LinearLayout itemLayout
= (LinearLayout
) view
.findViewById(R
.id
.itemLayout
);
107 itemLayout
.setContentDescription(navItem
.getContentDescription());
108 TextView itemText
= (TextView
) view
.findViewById(R
.id
.itemTitle
);
109 itemText
.setText(navItem
.getTitle());
113 // TODO re-enable when "Accounts" is available in Navigation Drawer
115 // if (mAll.get(position) instanceof Account[]){
116 // final View view = inflator.inflate(R.layout.drawer_account_group, null);
118 // final RadioGroup group = (RadioGroup) view.findViewById(R.id.drawer_radio_group);
120 // for (Account account : mAccounts) {
121 // RadioButton rb = new RadioButton(mContext);
123 // rb.setText(account.name);
124 // rb.setContentDescription(account.name);
125 // rb.setTextColor(Color.BLACK);
126 // rb.setEllipsize(TextUtils.TruncateAt.MIDDLE);
127 // rb.setSingleLine();
128 // rb.setCompoundDrawablePadding(30);
132 // // using adapted algorithm from /core/js/placeholder.js:50
133 // int lastAtPos = account.name.lastIndexOf("@");
134 // String username = account.name.substring(0, lastAtPos);
135 // byte[] seed = username.getBytes("UTF-8");
136 // MessageDigest md = MessageDigest.getInstance("MD5");
137 //// Integer seedMd5Int = Math.abs(new String(Hex.encodeHex(seedMd5))
139 // Integer seedMd5Int = String.format(Locale.ROOT, "%032x",
140 // new BigInteger(1, md.digest(seed))).hashCode();
142 // double maxRange = java.lang.Integer.MAX_VALUE;
143 // float hue = (float) (seedMd5Int / maxRange * 360);
145 // int[] rgb = BitmapUtils.HSLtoRGB(hue, 90.0f, 65.0f, 1.0f);
147 // TextDrawable text = new TextDrawable(username.substring(0, 1).toUpperCase(),
148 // rgb[0], rgb[1], rgb[2]);
149 // rb.setCompoundDrawablesWithIntrinsicBounds(text, null, null, null);
152 // } catch (Exception e){
153 // Log_OC.d(TAG, e.toString());
154 // rb.setTextColor(mContext.getResources().getColor(R.color.black));
156 // RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(
157 // LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
158 // params.weight=1.0f;
159 // params.setMargins(15, 5, 5, 5);
161 // // Check the current account that is being used
162 // if (account.name.equals(mCurrentAccount.name)) {
163 // rb.setChecked(true);
165 // rb.setChecked(false);
168 // group.addView(rb, params);
171 // group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
172 // public void onCheckedChanged(RadioGroup group, int checkedId) {
173 // // checkedId is the RadioButton selected
174 // RadioButton rb = (RadioButton) view.findViewById(checkedId);
176 // AccountUtils.setCurrentOwnCloudAccount(mContext,rb.getText().toString());
177 // notifyDataSetChanged();
178 // mFileActivity.closeDrawer();
180 // // restart the main activity
181 // mFileActivity.restart();
191 //TODO re-enable when "Accounts" is available in Navigation Drawer
192 // TODO update Account List after creating a new account and on fresh installation
193 // public void setShowAccounts(boolean value){
195 // mAll.addAll(mNavigationDrawerItems);
198 // mAll.add(1, mAccounts);
200 // mShowAccounts = value;