8438680793b7a5383acc2126e8a8fe93ba38f180
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / adapter / LocalFileListAdapter.java
1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 *
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.
8 *
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.
13 *
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/>.
16 *
17 */
18 package com.owncloud.android.ui.adapter;
19
20 import java.io.File;
21 import java.util.Arrays;
22 import java.util.Comparator;
23 import java.util.HashSet;
24 import java.util.Iterator;
25 import java.util.Set;
26
27 import com.owncloud.android.DisplayUtils;
28 import com.owncloud.android.R;
29
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;
40
41 /**
42 * This Adapter populates a ListView with all files and directories contained
43 * in a local directory
44 *
45 * @author David A. Velasco
46 *
47 */
48 public class LocalFileListAdapter extends BaseAdapter implements ListAdapter {
49
50 private Context mContext;
51 private File mDirectory;
52 private File[] mFiles = null;
53 private Set<DataSetObserver> mObservers = new HashSet<DataSetObserver>();
54
55 public LocalFileListAdapter(File directory, Context context) {
56 mContext = context;
57 swapDirectory(directory);
58 }
59
60 @Override
61 public boolean areAllItemsEnabled() {
62 return true;
63 }
64
65 @Override
66 public boolean isEnabled(int position) {
67 return true;
68 }
69
70 @Override
71 public int getCount() {
72 return mFiles != null ? mFiles.length : 0;
73 }
74
75 @Override
76 public Object getItem(int position) {
77 if (mFiles == null || mFiles.length <= position)
78 return null;
79 return mFiles[position];
80 }
81
82 @Override
83 public long getItemId(int position) {
84 return mFiles != null && mFiles.length <= position ? position : -1;
85 }
86
87 @Override
88 public int getItemViewType(int position) {
89 return 0;
90 }
91
92 @Override
93 public View getView(int position, View convertView, ViewGroup parent) {
94 View view = convertView;
95 if (view == null) {
96 LayoutInflater inflator = (LayoutInflater) mContext
97 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
98 view = inflator.inflate(R.layout.list_layout, null);
99 }
100 if (mFiles != null && mFiles.length > position) {
101 File file = mFiles[position];
102
103 TextView fileName = (TextView) view.findViewById(R.id.Filename);
104 String name = file.getName();
105 fileName.setText(name);
106
107 ImageView fileIcon = (ImageView) view.findViewById(R.id.imageView1);
108 if (!file.isDirectory()) {
109 fileIcon.setImageResource(R.drawable.file);
110 } else {
111 fileIcon.setImageResource(R.drawable.ic_menu_archive);
112 }
113
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);
125 } else {
126 checkBoxV.setVisibility(View.VISIBLE);
127 if (parentList.isItemChecked(position)) {
128 checkBoxV.setImageResource(android.R.drawable.checkbox_on_background);
129 } else {
130 checkBoxV.setImageResource(android.R.drawable.checkbox_off_background);
131 }
132 }
133
134 } else {
135 fileSizeV.setVisibility(View.GONE);
136 lastModV.setVisibility(View.GONE);
137 checkBoxV.setVisibility(View.GONE);
138 }
139
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);
142 }
143
144 return view;
145 }
146
147 @Override
148 public int getViewTypeCount() {
149 return 1;
150 }
151
152 @Override
153 public boolean hasStableIds() {
154 return false;
155 }
156
157 @Override
158 public boolean isEmpty() {
159 return (mFiles == null || mFiles.length == 0);
160 }
161
162 @Override
163 public void registerDataSetObserver(DataSetObserver observer) {
164 mObservers.add(observer);
165 }
166
167 @Override
168 public void unregisterDataSetObserver(DataSetObserver observer) {
169 mObservers.remove(observer);
170 }
171
172 /**
173 * Change the adapted directory for a new one
174 * @param directory New file to adapt.
175 */
176 public void swapDirectory(File directory) {
177 mDirectory = directory;
178 mFiles = mDirectory.listFiles();
179 if (mFiles != null) {
180 Arrays.sort(mFiles, new Comparator<File>() {
181 @Override
182 public int compare(File lhs, File rhs) {
183 if (lhs.isDirectory() && !rhs.isDirectory()) {
184 return -1;
185 } else if (!lhs.isDirectory() && rhs.isDirectory()) {
186 return 1;
187 }
188 return compareNames(lhs, rhs);
189 }
190
191 private int compareNames(File lhs, File rhs) {
192 return lhs.getName().toLowerCase().compareTo(rhs.getName().toLowerCase());
193 }
194
195 });
196 }
197 Iterator<DataSetObserver> it = mObservers.iterator();
198 while (it.hasNext()) {
199 it.next().onChanged();
200 }
201 }
202 }