5858870291b9b69e1c83a1cf33db65b136bca7cf
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / adapter / LocalFileListAdapter.java
1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 */
19 package com.owncloud.android.ui.adapter;
20
21 import java.io.File;
22 import java.util.Arrays;
23 import java.util.Comparator;
24
25 import com.owncloud.android.DisplayUtils;
26 import com.owncloud.android.R;
27
28 import android.content.Context;
29 import android.view.LayoutInflater;
30 import android.view.View;
31 import android.view.ViewGroup;
32 import android.widget.BaseAdapter;
33 import android.widget.ImageView;
34 import android.widget.ListAdapter;
35 import android.widget.ListView;
36 import android.widget.TextView;
37
38 /**
39 * This Adapter populates a ListView with all files and directories contained
40 * in a local directory
41 *
42 * @author David A. Velasco
43 *
44 */
45 public class LocalFileListAdapter extends BaseAdapter implements ListAdapter {
46
47 private Context mContext;
48 private File mDirectory;
49 private File[] mFiles = null;
50
51 public LocalFileListAdapter(File directory, Context context) {
52 mContext = context;
53 swapDirectory(directory);
54 }
55
56 @Override
57 public boolean areAllItemsEnabled() {
58 return true;
59 }
60
61 @Override
62 public boolean isEnabled(int position) {
63 return true;
64 }
65
66 @Override
67 public int getCount() {
68 return mFiles != null ? mFiles.length : 0;
69 }
70
71 @Override
72 public Object getItem(int position) {
73 if (mFiles == null || mFiles.length <= position)
74 return null;
75 return mFiles[position];
76 }
77
78 @Override
79 public long getItemId(int position) {
80 return mFiles != null && mFiles.length <= position ? position : -1;
81 }
82
83 @Override
84 public int getItemViewType(int position) {
85 return 0;
86 }
87
88 @Override
89 public View getView(int position, View convertView, ViewGroup parent) {
90 View view = convertView;
91 if (view == null) {
92 LayoutInflater inflator = (LayoutInflater) mContext
93 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
94 view = inflator.inflate(R.layout.list_item, null);
95 }
96 if (mFiles != null && mFiles.length > position) {
97 File file = mFiles[position];
98
99 TextView fileName = (TextView) view.findViewById(R.id.Filename);
100 String name = file.getName();
101 fileName.setText(name);
102
103 ImageView fileIcon = (ImageView) view.findViewById(R.id.imageView1);
104 if (!file.isDirectory()) {
105 fileIcon.setImageResource(R.drawable.file);
106 } else {
107 fileIcon.setImageResource(R.drawable.ic_menu_archive);
108 }
109
110 TextView fileSizeV = (TextView) view.findViewById(R.id.file_size);
111 TextView lastModV = (TextView) view.findViewById(R.id.last_mod);
112 ImageView checkBoxV = (ImageView) view.findViewById(R.id.custom_checkbox);
113 if (!file.isDirectory()) {
114 fileSizeV.setVisibility(View.VISIBLE);
115 fileSizeV.setText(DisplayUtils.bytesToHumanReadable(file.length()));
116 lastModV.setVisibility(View.VISIBLE);
117 lastModV.setText(DisplayUtils.unixTimeToHumanReadable(file.lastModified()));
118 ListView parentList = (ListView)parent;
119 if (parentList.getChoiceMode() == ListView.CHOICE_MODE_NONE) {
120 checkBoxV.setVisibility(View.GONE);
121 } else {
122 if (parentList.isItemChecked(position)) {
123 checkBoxV.setImageResource(android.R.drawable.checkbox_on_background);
124 } else {
125 checkBoxV.setImageResource(android.R.drawable.checkbox_off_background);
126 }
127 checkBoxV.setVisibility(View.VISIBLE);
128 }
129
130 } else {
131 fileSizeV.setVisibility(View.GONE);
132 lastModV.setVisibility(View.GONE);
133 checkBoxV.setVisibility(View.GONE);
134 }
135
136 view.findViewById(R.id.imageView2).setVisibility(View.INVISIBLE); // not GONE; the alignment changes; ugly way to keep it
137 view.findViewById(R.id.imageView3).setVisibility(View.GONE);
138 }
139
140 return view;
141 }
142
143 @Override
144 public int getViewTypeCount() {
145 return 1;
146 }
147
148 @Override
149 public boolean hasStableIds() {
150 return false;
151 }
152
153 @Override
154 public boolean isEmpty() {
155 return (mFiles == null || mFiles.length == 0);
156 }
157
158 /**
159 * Change the adapted directory for a new one
160 * @param directory New file to adapt. Can be NULL, meaning "no content to adapt".
161 */
162 public void swapDirectory(File directory) {
163 mDirectory = directory;
164 mFiles = (mDirectory != null ? mDirectory.listFiles() : null);
165 if (mFiles != null) {
166 Arrays.sort(mFiles, new Comparator<File>() {
167 @Override
168 public int compare(File lhs, File rhs) {
169 if (lhs.isDirectory() && !rhs.isDirectory()) {
170 return -1;
171 } else if (!lhs.isDirectory() && rhs.isDirectory()) {
172 return 1;
173 }
174 return compareNames(lhs, rhs);
175 }
176
177 private int compareNames(File lhs, File rhs) {
178 return lhs.getName().toLowerCase().compareTo(rhs.getName().toLowerCase());
179 }
180
181 });
182 }
183 notifyDataSetChanged();
184 }
185 }