6190ebee24e0ed0c299594147a189133cefd1b9b
[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-2014 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 version 2,
7 * as published by the Free Software Foundation.
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
24 import android.content.Context;
25 import android.view.LayoutInflater;
26 import android.view.View;
27 import android.view.ViewGroup;
28 import android.widget.BaseAdapter;
29 import android.widget.ImageView;
30 import android.widget.ListAdapter;
31 import android.widget.ListView;
32 import android.widget.TextView;
33
34 import com.owncloud.android.R;
35 import com.owncloud.android.utils.DisplayUtils;
36
37 /**
38 * This Adapter populates a ListView with all files and directories contained
39 * in a local directory
40 *
41 * @author David A. Velasco
42 *
43 */
44 public class LocalFileListAdapter extends BaseAdapter implements ListAdapter {
45
46 private Context mContext;
47 private File mDirectory;
48 private File[] mFiles = null;
49
50 public LocalFileListAdapter(File directory, Context context) {
51 mContext = context;
52 swapDirectory(directory);
53 }
54
55 @Override
56 public boolean areAllItemsEnabled() {
57 return true;
58 }
59
60 @Override
61 public boolean isEnabled(int position) {
62 return true;
63 }
64
65 @Override
66 public int getCount() {
67 return mFiles != null ? mFiles.length : 0;
68 }
69
70 @Override
71 public Object getItem(int position) {
72 if (mFiles == null || mFiles.length <= position)
73 return null;
74 return mFiles[position];
75 }
76
77 @Override
78 public long getItemId(int position) {
79 return mFiles != null && mFiles.length <= position ? position : -1;
80 }
81
82 @Override
83 public int getItemViewType(int position) {
84 return 0;
85 }
86
87 @Override
88 public View getView(int position, View convertView, ViewGroup parent) {
89 View view = convertView;
90 if (view == null) {
91 LayoutInflater inflator = (LayoutInflater) mContext
92 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
93 view = inflator.inflate(R.layout.list_item, null);
94 }
95 if (mFiles != null && mFiles.length > position) {
96 File file = mFiles[position];
97
98 TextView fileName = (TextView) view.findViewById(R.id.Filename);
99 String name = file.getName();
100 fileName.setText(name);
101
102 ImageView fileIcon = (ImageView) view.findViewById(R.id.imageView1);
103 if (!file.isDirectory()) {
104 fileIcon.setImageResource(R.drawable.file);
105 } else {
106 fileIcon.setImageResource(R.drawable.ic_menu_archive);
107 }
108
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);
120 } else {
121 if (parentList.isItemChecked(position)) {
122 checkBoxV.setImageResource(android.R.drawable.checkbox_on_background);
123 } else {
124 checkBoxV.setImageResource(android.R.drawable.checkbox_off_background);
125 }
126 checkBoxV.setVisibility(View.VISIBLE);
127 }
128
129 } else {
130 fileSizeV.setVisibility(View.GONE);
131 lastModV.setVisibility(View.GONE);
132 checkBoxV.setVisibility(View.GONE);
133 }
134
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);
137
138 view.findViewById(R.id.sharedIcon).setVisibility(View.GONE);
139 view.findViewById(R.id.sharedWithMeIcon).setVisibility(View.GONE);
140 }
141
142 return view;
143 }
144
145 @Override
146 public int getViewTypeCount() {
147 return 1;
148 }
149
150 @Override
151 public boolean hasStableIds() {
152 return false;
153 }
154
155 @Override
156 public boolean isEmpty() {
157 return (mFiles == null || mFiles.length == 0);
158 }
159
160 /**
161 * Change the adapted directory for a new one
162 * @param directory New file to adapt. Can be NULL, meaning "no content to adapt".
163 */
164 public void swapDirectory(File directory) {
165 mDirectory = directory;
166 mFiles = (mDirectory != null ? mDirectory.listFiles() : null);
167 if (mFiles != null) {
168 Arrays.sort(mFiles, new Comparator<File>() {
169 @Override
170 public int compare(File lhs, File rhs) {
171 if (lhs.isDirectory() && !rhs.isDirectory()) {
172 return -1;
173 } else if (!lhs.isDirectory() && rhs.isDirectory()) {
174 return 1;
175 }
176 return compareNames(lhs, rhs);
177 }
178
179 private int compareNames(File lhs, File rhs) {
180 return lhs.getName().toLowerCase().compareTo(rhs.getName().toLowerCase());
181 }
182
183 });
184 }
185 notifyDataSetChanged();
186 }
187 }