d5db6489854a19388ec5a12b8e92e82022c6c35a
1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package com
.owncloud
.android
.ui
.adapter
;
21 import java
.util
.Arrays
;
22 import java
.util
.Comparator
;
24 import com
.owncloud
.android
.DisplayUtils
;
25 import com
.owncloud
.android
.R
;
27 import android
.content
.Context
;
28 import android
.view
.LayoutInflater
;
29 import android
.view
.View
;
30 import android
.view
.ViewGroup
;
31 import android
.widget
.BaseAdapter
;
32 import android
.widget
.ImageView
;
33 import android
.widget
.ListAdapter
;
34 import android
.widget
.ListView
;
35 import android
.widget
.TextView
;
38 * This Adapter populates a ListView with all files and directories contained
39 * in a local directory
41 * @author David A. Velasco
44 public class LocalFileListAdapter
extends BaseAdapter
implements ListAdapter
{
46 private Context mContext
;
47 private File mDirectory
;
48 private File
[] mFiles
= null
;
50 public LocalFileListAdapter(File directory
, Context context
) {
52 swapDirectory(directory
);
56 public boolean areAllItemsEnabled() {
61 public boolean isEnabled(int position
) {
66 public int getCount() {
67 return mFiles
!= null ? mFiles
.length
: 0;
71 public Object
getItem(int position
) {
72 if (mFiles
== null
|| mFiles
.length
<= position
)
74 return mFiles
[position
];
78 public long getItemId(int position
) {
79 return mFiles
!= null
&& mFiles
.length
<= position ? position
: -1;
83 public int getItemViewType(int position
) {
88 public View
getView(int position
, View convertView
, ViewGroup parent
) {
89 View view
= convertView
;
91 LayoutInflater inflator
= (LayoutInflater
) mContext
92 .getSystemService(Context
.LAYOUT_INFLATER_SERVICE
);
93 view
= inflator
.inflate(R
.layout
.list_layout
, null
);
95 if (mFiles
!= null
&& mFiles
.length
> position
) {
96 File file
= mFiles
[position
];
98 TextView fileName
= (TextView
) view
.findViewById(R
.id
.Filename
);
99 String name
= file
.getName();
100 fileName
.setText(name
);
102 ImageView fileIcon
= (ImageView
) view
.findViewById(R
.id
.imageView1
);
103 if (!file
.isDirectory()) {
104 fileIcon
.setImageResource(R
.drawable
.file
);
106 fileIcon
.setImageResource(R
.drawable
.ic_menu_archive
);
109 TextView fileSizeV
= (TextView
) view
.findViewById(R
.id
.file_size
);
110 TextView lastModV
= (TextView
) view
.findViewById(R
.id
.last_mod
);
111 ImageView checkBoxV
= (ImageView
) view
.findViewById(R
.id
.custom_checkbox
);
112 if (!file
.isDirectory()) {
113 fileSizeV
.setVisibility(View
.VISIBLE
);
114 fileSizeV
.setText(DisplayUtils
.bytesToHumanReadable(file
.length()));
115 lastModV
.setVisibility(View
.VISIBLE
);
116 lastModV
.setText(DisplayUtils
.unixTimeToHumanReadable(file
.lastModified()));
117 ListView parentList
= (ListView
)parent
;
118 if (parentList
.getChoiceMode() == ListView
.CHOICE_MODE_NONE
) {
119 checkBoxV
.setVisibility(View
.GONE
);
121 if (parentList
.isItemChecked(position
)) {
122 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_on_background
);
124 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_off_background
);
126 checkBoxV
.setVisibility(View
.VISIBLE
);
130 fileSizeV
.setVisibility(View
.GONE
);
131 lastModV
.setVisibility(View
.GONE
);
132 checkBoxV
.setVisibility(View
.GONE
);
135 view
.findViewById(R
.id
.imageView2
).setVisibility(View
.INVISIBLE
); // not GONE; the alignment changes; ugly way to keep it
136 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.GONE
);
143 public int getViewTypeCount() {
148 public boolean hasStableIds() {
153 public boolean isEmpty() {
154 return (mFiles
== null
|| mFiles
.length
== 0);
158 * Change the adapted directory for a new one
159 * @param directory New file to adapt. Can be NULL, meaning "no content to adapt".
161 public void swapDirectory(File directory
) {
162 mDirectory
= directory
;
163 mFiles
= (mDirectory
!= null ? mDirectory
.listFiles() : null
);
164 if (mFiles
!= null
) {
165 Arrays
.sort(mFiles
, new Comparator
<File
>() {
167 public int compare(File lhs
, File rhs
) {
168 if (lhs
.isDirectory() && !rhs
.isDirectory()) {
170 } else if (!lhs
.isDirectory() && rhs
.isDirectory()) {
173 return compareNames(lhs
, rhs
);
176 private int compareNames(File lhs
, File rhs
) {
177 return lhs
.getName().toLowerCase().compareTo(rhs
.getName().toLowerCase());
182 notifyDataSetChanged();