d0ea68403d2dfc86a24ec826ac99b5023130b992
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / adapter / FileListListAdapter.java
1
2 /* ownCloud Android client application
3 * Copyright (C) 2011 Bartek Przybylski
4 * Copyright (C) 2012-2013 ownCloud Inc.
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20 package com.owncloud.android.ui.adapter;
21
22 import java.util.Vector;
23
24 import android.accounts.Account;
25 import android.content.Context;
26 import android.view.LayoutInflater;
27 import android.view.View;
28 import android.view.ViewGroup;
29 import android.widget.BaseAdapter;
30 import android.widget.ImageView;
31 import android.widget.ListAdapter;
32 import android.widget.ListView;
33 import android.widget.TextView;
34
35 import com.owncloud.android.DisplayUtils;
36 import com.owncloud.android.R;
37 import com.owncloud.android.authentication.AccountUtils;
38 import com.owncloud.android.datamodel.DataStorageManager;
39 import com.owncloud.android.datamodel.OCFile;
40 import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
41 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
42 import com.owncloud.android.ui.activity.TransferServiceGetter;
43
44 /**
45 * This Adapter populates a ListView with all files and folders in an ownCloud
46 * instance.
47 *
48 * @author Bartek Przybylski
49 *
50 */
51 public class FileListListAdapter extends BaseAdapter implements ListAdapter {
52 private Context mContext;
53 private OCFile mFile = null;
54 private Vector<OCFile> mFiles = null;
55 private DataStorageManager mStorageManager;
56 private Account mAccount;
57 private TransferServiceGetter mTransferServiceGetter;
58 //total size of a directory (recursive)
59 private Long totalSizeOfDirectoriesRecursive = null;
60 private Long lastModifiedOfAllSubdirectories = null;
61
62 public FileListListAdapter(OCFile file, DataStorageManager storage_man,
63 Context context, TransferServiceGetter transferServiceGetter) {
64 mStorageManager = storage_man;
65 mContext = context;
66 mAccount = AccountUtils.getCurrentOwnCloudAccount(mContext);
67 mTransferServiceGetter = transferServiceGetter;
68 swapDirectory(file, mStorageManager);
69 /*mFile = file;
70 mFiles = mStorageManager.getDirectoryContent(mFile);*/
71 }
72
73 @Override
74 public boolean areAllItemsEnabled() {
75 return true;
76 }
77
78 @Override
79 public boolean isEnabled(int position) {
80 return true;
81 }
82
83 @Override
84 public int getCount() {
85 return mFiles != null ? mFiles.size() : 0;
86 }
87
88 @Override
89 public Object getItem(int position) {
90 if (mFiles == null || mFiles.size() <= position)
91 return null;
92 return mFiles.get(position);
93 }
94
95 @Override
96 public long getItemId(int position) {
97 if (mFiles == null || mFiles.size() <= position)
98 return 0;
99 return mFiles.get(position).getFileId();
100 }
101
102 @Override
103 public int getItemViewType(int position) {
104 return 0;
105 }
106
107 @Override
108 public View getView(int position, View convertView, ViewGroup parent) {
109 View view = convertView;
110 if (view == null) {
111 LayoutInflater inflator = (LayoutInflater) mContext
112 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
113 view = inflator.inflate(R.layout.list_item, null);
114 }
115 if (mFiles != null && mFiles.size() > position) {
116 OCFile file = mFiles.get(position);
117 TextView fileName = (TextView) view.findViewById(R.id.Filename);
118 String name = file.getFileName();
119
120 fileName.setText(name);
121 ImageView fileIcon = (ImageView) view.findViewById(R.id.imageView1);
122 fileIcon.setImageResource(DisplayUtils.getResourceId(file.getMimetype()));
123 ImageView localStateView = (ImageView) view.findViewById(R.id.imageView2);
124 FileDownloaderBinder downloaderBinder = mTransferServiceGetter.getFileDownloaderBinder();
125 FileUploaderBinder uploaderBinder = mTransferServiceGetter.getFileUploaderBinder();
126 if (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, file)) {
127 localStateView.setImageResource(R.drawable.downloading_file_indicator);
128 localStateView.setVisibility(View.VISIBLE);
129 } else if (uploaderBinder != null && uploaderBinder.isUploading(mAccount, file)) {
130 localStateView.setImageResource(R.drawable.uploading_file_indicator);
131 localStateView.setVisibility(View.VISIBLE);
132 } else if (file.isDown()) {
133 localStateView.setImageResource(R.drawable.local_file_indicator);
134 localStateView.setVisibility(View.VISIBLE);
135 } else {
136 localStateView.setVisibility(View.INVISIBLE);
137 }
138
139
140 TextView fileSizeV = (TextView) view.findViewById(R.id.file_size);
141 TextView lastModV = (TextView) view.findViewById(R.id.last_mod);
142 ImageView checkBoxV = (ImageView) view.findViewById(R.id.custom_checkbox);
143
144 if (!file.isDirectory()) {
145 fileSizeV.setVisibility(View.VISIBLE);
146 fileSizeV.setText(DisplayUtils.bytesToHumanReadable(file.getFileLength()));
147 lastModV.setVisibility(View.VISIBLE);
148 lastModV.setText(DisplayUtils.unixTimeToHumanReadable(file.getModificationTimestamp()));
149 // this if-else is needed even thoe fav icon is visible by default
150 // because android reuses views in listview
151 if (!file.keepInSync()) {
152 view.findViewById(R.id.imageView3).setVisibility(View.GONE);
153 } else {
154 view.findViewById(R.id.imageView3).setVisibility(View.VISIBLE);
155 }
156
157 ListView parentList = (ListView)parent;
158 if (parentList.getChoiceMode() == ListView.CHOICE_MODE_NONE) {
159 checkBoxV.setVisibility(View.GONE);
160 } else {
161 if (parentList.isItemChecked(position)) {
162 checkBoxV.setImageResource(android.R.drawable.checkbox_on_background);
163 } else {
164 checkBoxV.setImageResource(android.R.drawable.checkbox_off_background);
165 }
166 checkBoxV.setVisibility(View.VISIBLE);
167 }
168
169 }
170 else {
171
172 fileSizeV.setVisibility(View.VISIBLE);
173 fileSizeV.setText(DisplayUtils.bytesToHumanReadable(file.getFileLength()));
174 lastModV.setVisibility(View.VISIBLE);
175 lastModV.setText(DisplayUtils.unixTimeToHumanReadable(file.getModificationTimestamp()));
176 // getDirectorySizeNumber(file,true);
177 // if (lastModifiedOfAllSubdirectories == null)
178 // {
179 // lastModV.setVisibility(View.GONE);
180 // fileSizeV.setVisibility(View.GONE);
181 // }
182 // else
183 // {
184 // lastModV.setVisibility(View.VISIBLE);
185 // lastModV.setText(DisplayUtils.unixTimeToHumanReadable(lastModifiedOfAllSubdirectories));
186 // fileSizeV.setVisibility(View.VISIBLE);
187 // fileSizeV.setText(DisplayUtils.bytesToHumanReadable((totalSizeOfDirectoriesRecursive == null) ? 0 : totalSizeOfDirectoriesRecursive));
188 // }
189 checkBoxV.setVisibility(View.GONE);
190 view.findViewById(R.id.imageView3).setVisibility(View.GONE);
191 }
192 }
193
194 return view;
195 }
196
197
198 /**
199 * - This method counts recursively all subdirectories and their files from the root directory.
200 * - It also shows a timestamp of the last modificated file inside the root directory
201 *
202 * @param OCFile : startDirectory
203 * @param boolean : counting starts from here ?
204 */
205 private void getDirectorySizeNumber(OCFile directory,boolean startOfRecursive) {
206 if (startOfRecursive) {
207 totalSizeOfDirectoriesRecursive = null;
208 }
209 Vector<OCFile> files = mStorageManager.getDirectoryContent(directory);
210 for (OCFile file : files) {
211 if(!file.isDirectory()) {
212 if (totalSizeOfDirectoriesRecursive == null) {
213 totalSizeOfDirectoriesRecursive = file.getFileLength();
214 lastModifiedOfAllSubdirectories = file.getModificationTimestamp();
215 continue;
216 }
217
218 totalSizeOfDirectoriesRecursive += file.getFileLength();
219 if (lastModifiedOfAllSubdirectories < file.getModificationTimestamp()) {
220 lastModifiedOfAllSubdirectories = file.getModificationTimestamp();
221 }
222 }
223 else {
224 this.getDirectorySizeNumber(file, false);
225 }
226 }
227 }
228
229
230 @Override
231 public int getViewTypeCount() {
232 return 1;
233 }
234
235 @Override
236 public boolean hasStableIds() {
237 return true;
238 }
239
240 @Override
241 public boolean isEmpty() {
242 return (mFiles == null || mFiles.isEmpty());
243 }
244
245 /**
246 * Change the adapted directory for a new one
247 * @param directory New file to adapt. Can be NULL, meaning "no content to adapt".
248 * @param updatedStorageManager Optional updated storage manager; used to replace mStorageManager if is different (and not NULL)
249 */
250 public void swapDirectory(OCFile directory, DataStorageManager updatedStorageManager) {
251 mFile = directory;
252 if (updatedStorageManager != null && updatedStorageManager != mStorageManager) {
253 mStorageManager = updatedStorageManager;
254 mAccount = AccountUtils.getCurrentOwnCloudAccount(mContext);
255 }
256 if (mStorageManager != null) {
257 mFiles = mStorageManager.getDirectoryContent(mFile);
258 } else {
259 mFiles = null;
260 }
261 notifyDataSetChanged();
262 }
263
264 }