690832a70b5e0306d92130a301a411ff22fb605a
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
;
23 import java
.util
.HashSet
;
24 import java
.util
.Iterator
;
27 import com
.owncloud
.android
.DisplayUtils
;
28 import com
.owncloud
.android
.R
;
30 import android
.content
.Context
;
31 import android
.database
.DataSetObserver
;
32 import android
.view
.LayoutInflater
;
33 import android
.view
.View
;
34 import android
.view
.ViewGroup
;
35 import android
.widget
.BaseAdapter
;
36 import android
.widget
.ImageView
;
37 import android
.widget
.ListAdapter
;
38 import android
.widget
.ListView
;
39 import android
.widget
.TextView
;
42 * This Adapter populates a ListView with all files and directories contained
43 * in a local directory
45 * @author David A. Velasco
48 public class LocalFileListAdapter
extends BaseAdapter
implements ListAdapter
{
50 private Context mContext
;
51 private File mDirectory
;
52 private File
[] mFiles
= null
;
53 private Set
<DataSetObserver
> mObservers
= new HashSet
<DataSetObserver
>();
55 public LocalFileListAdapter(File directory
, Context context
) {
57 swapDirectory(directory
);
61 public boolean areAllItemsEnabled() {
66 public boolean isEnabled(int position
) {
71 public int getCount() {
72 return mFiles
!= null ? mFiles
.length
: 0;
76 public Object
getItem(int position
) {
77 if (mFiles
== null
|| mFiles
.length
<= position
)
79 return mFiles
[position
];
83 public long getItemId(int position
) {
84 return mFiles
!= null
&& mFiles
.length
<= position ? position
: -1;
88 public int getItemViewType(int position
) {
93 public View
getView(int position
, View convertView
, ViewGroup parent
) {
94 View view
= convertView
;
96 LayoutInflater inflator
= (LayoutInflater
) mContext
97 .getSystemService(Context
.LAYOUT_INFLATER_SERVICE
);
98 view
= inflator
.inflate(R
.layout
.list_layout
, null
);
100 if (mFiles
!= null
&& mFiles
.length
> position
) {
101 File file
= mFiles
[position
];
103 TextView fileName
= (TextView
) view
.findViewById(R
.id
.Filename
);
104 String name
= file
.getName();
105 fileName
.setText(name
);
107 ImageView fileIcon
= (ImageView
) view
.findViewById(R
.id
.imageView1
);
108 if (!file
.isDirectory()) {
109 fileIcon
.setImageResource(R
.drawable
.file
);
111 fileIcon
.setImageResource(R
.drawable
.ic_menu_archive
);
114 TextView fileSizeV
= (TextView
) view
.findViewById(R
.id
.file_size
);
115 TextView lastModV
= (TextView
) view
.findViewById(R
.id
.last_mod
);
116 ImageView checkBoxV
= (ImageView
) view
.findViewById(R
.id
.custom_checkbox
);
117 if (!file
.isDirectory()) {
118 fileSizeV
.setVisibility(View
.VISIBLE
);
119 fileSizeV
.setText(DisplayUtils
.bytesToHumanReadable(file
.length()));
120 lastModV
.setVisibility(View
.VISIBLE
);
121 lastModV
.setText(DisplayUtils
.unixTimeToHumanReadable(file
.lastModified()));
122 ListView parentList
= (ListView
)parent
;
123 if (parentList
.getChoiceMode() == ListView
.CHOICE_MODE_NONE
) {
124 checkBoxV
.setVisibility(View
.GONE
);
126 if (parentList
.isItemChecked(position
)) {
127 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_on_background
);
129 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_off_background
);
131 checkBoxV
.setVisibility(View
.VISIBLE
);
135 fileSizeV
.setVisibility(View
.GONE
);
136 lastModV
.setVisibility(View
.GONE
);
137 checkBoxV
.setVisibility(View
.GONE
);
140 view
.findViewById(R
.id
.imageView2
).setVisibility(View
.INVISIBLE
); // not GONE; the alignment changes; ugly way to keep it
141 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.GONE
);
148 public int getViewTypeCount() {
153 public boolean hasStableIds() {
158 public boolean isEmpty() {
159 return (mFiles
== null
|| mFiles
.length
== 0);
163 public void registerDataSetObserver(DataSetObserver observer
) {
164 mObservers
.add(observer
);
168 public void unregisterDataSetObserver(DataSetObserver observer
) {
169 mObservers
.remove(observer
);
173 * Change the adapted directory for a new one
174 * @param directory New file to adapt.
176 public void swapDirectory(File directory
) {
177 mDirectory
= directory
;
178 mFiles
= mDirectory
.listFiles();
179 if (mFiles
!= null
) {
180 Arrays
.sort(mFiles
, new Comparator
<File
>() {
182 public int compare(File lhs
, File rhs
) {
183 if (lhs
.isDirectory() && !rhs
.isDirectory()) {
185 } else if (!lhs
.isDirectory() && rhs
.isDirectory()) {
188 return compareNames(lhs
, rhs
);
191 private int compareNames(File lhs
, File rhs
) {
192 return lhs
.getName().toLowerCase().compareTo(rhs
.getName().toLowerCase());
197 Iterator
<DataSetObserver
> it
= mObservers
.iterator();
198 while (it
.hasNext()) {
199 it
.next().onChanged();