2 /* ownCloud Android client application
3 * Copyright (C) 2011 Bartek Przybylski
4 * Copyright (C) 2012-2013 ownCloud Inc.
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.
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.
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/>.
20 package com
.owncloud
.android
.ui
.adapter
;
22 import java
.util
.Vector
;
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
;
35 import com
.owncloud
.android
.DisplayUtils
;
36 import com
.owncloud
.android
.Log_OC
;
37 import com
.owncloud
.android
.R
;
38 import com
.owncloud
.android
.authentication
.AccountUtils
;
39 import com
.owncloud
.android
.datamodel
.DataStorageManager
;
40 import com
.owncloud
.android
.datamodel
.OCFile
;
41 import com
.owncloud
.android
.files
.services
.FileDownloader
.FileDownloaderBinder
;
42 import com
.owncloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
43 import com
.owncloud
.android
.ui
.activity
.TransferServiceGetter
;
46 * This Adapter populates a ListView with all files and folders in an ownCloud
49 * @author Bartek Przybylski
52 public class FileListListAdapter
extends BaseAdapter
implements ListAdapter
{
53 private Context mContext
;
54 private OCFile mFile
= null
;
55 private Vector
<OCFile
> mFiles
= null
;
56 private DataStorageManager mStorageManager
;
57 private Account mAccount
;
58 private TransferServiceGetter mTransferServiceGetter
;
59 //total size of a directory (recursive)
60 private Long totalSizeOfDirectoriesRecursive
= null
;
61 private Long lastModifiedOfAllSubdirectories
= null
;
63 public FileListListAdapter(OCFile file
, DataStorageManager storage_man
,
64 Context context
, TransferServiceGetter transferServiceGetter
) {
65 mStorageManager
= storage_man
;
67 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
68 mTransferServiceGetter
= transferServiceGetter
;
69 swapDirectory(file
, mStorageManager
);
71 mFiles = mStorageManager.getDirectoryContent(mFile);*/
75 public boolean areAllItemsEnabled() {
80 public boolean isEnabled(int position
) {
85 public int getCount() {
86 return mFiles
!= null ? mFiles
.size() : 0;
90 public Object
getItem(int position
) {
91 if (mFiles
== null
|| mFiles
.size() <= position
)
93 return mFiles
.get(position
);
97 public long getItemId(int position
) {
98 if (mFiles
== null
|| mFiles
.size() <= position
)
100 return mFiles
.get(position
).getFileId();
104 public int getItemViewType(int position
) {
109 public View
getView(int position
, View convertView
, ViewGroup parent
) {
110 View view
= convertView
;
112 LayoutInflater inflator
= (LayoutInflater
) mContext
113 .getSystemService(Context
.LAYOUT_INFLATER_SERVICE
);
114 view
= inflator
.inflate(R
.layout
.list_item
, null
);
116 if (mFiles
!= null
&& mFiles
.size() > position
) {
117 OCFile file
= mFiles
.get(position
);
118 TextView fileName
= (TextView
) view
.findViewById(R
.id
.Filename
);
119 String name
= file
.getFileName();
121 fileName
.setText(name
);
122 ImageView fileIcon
= (ImageView
) view
.findViewById(R
.id
.imageView1
);
123 fileIcon
.setImageResource(DisplayUtils
.getResourceId(file
.getMimetype()));
124 ImageView localStateView
= (ImageView
) view
.findViewById(R
.id
.imageView2
);
125 FileDownloaderBinder downloaderBinder
= mTransferServiceGetter
.getFileDownloaderBinder();
126 FileUploaderBinder uploaderBinder
= mTransferServiceGetter
.getFileUploaderBinder();
127 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, file
)) {
128 localStateView
.setImageResource(R
.drawable
.downloading_file_indicator
);
129 localStateView
.setVisibility(View
.VISIBLE
);
130 } else if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, file
)) {
131 localStateView
.setImageResource(R
.drawable
.uploading_file_indicator
);
132 localStateView
.setVisibility(View
.VISIBLE
);
133 } else if (file
.isDown()) {
134 localStateView
.setImageResource(R
.drawable
.local_file_indicator
);
135 localStateView
.setVisibility(View
.VISIBLE
);
137 localStateView
.setVisibility(View
.INVISIBLE
);
141 TextView fileSizeV
= (TextView
) view
.findViewById(R
.id
.file_size
);
142 TextView lastModV
= (TextView
) view
.findViewById(R
.id
.last_mod
);
143 ImageView checkBoxV
= (ImageView
) view
.findViewById(R
.id
.custom_checkbox
);
145 if (!file
.isDirectory()) {
146 fileSizeV
.setVisibility(View
.VISIBLE
);
147 fileSizeV
.setText(DisplayUtils
.bytesToHumanReadable(file
.getFileLength()));
148 lastModV
.setVisibility(View
.VISIBLE
);
149 lastModV
.setText(DisplayUtils
.unixTimeToHumanReadable(file
.getModificationTimestamp()));
150 // this if-else is needed even thoe fav icon is visible by default
151 // because android reuses views in listview
152 if (!file
.keepInSync()) {
153 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.GONE
);
155 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.VISIBLE
);
158 ListView parentList
= (ListView
)parent
;
159 if (parentList
.getChoiceMode() == ListView
.CHOICE_MODE_NONE
) {
160 checkBoxV
.setVisibility(View
.GONE
);
162 if (parentList
.isItemChecked(position
)) {
163 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_on_background
);
165 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_off_background
);
167 checkBoxV
.setVisibility(View
.VISIBLE
);
173 fileSizeV
.setVisibility(View
.VISIBLE
);
174 fileSizeV
.setText(DisplayUtils
.bytesToHumanReadable(file
.getFileLength()));
175 lastModV
.setVisibility(View
.VISIBLE
);
176 lastModV
.setText(DisplayUtils
.unixTimeToHumanReadable(file
.getModificationTimestamp()));
177 // getDirectorySizeNumber(file,true);
178 // if (lastModifiedOfAllSubdirectories == null)
180 // lastModV.setVisibility(View.GONE);
181 // fileSizeV.setVisibility(View.GONE);
185 // lastModV.setVisibility(View.VISIBLE);
186 // lastModV.setText(DisplayUtils.unixTimeToHumanReadable(lastModifiedOfAllSubdirectories));
187 // fileSizeV.setVisibility(View.VISIBLE);
188 // fileSizeV.setText(DisplayUtils.bytesToHumanReadable((totalSizeOfDirectoriesRecursive == null) ? 0 : totalSizeOfDirectoriesRecursive));
190 checkBoxV
.setVisibility(View
.GONE
);
191 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.GONE
);
200 * - This method counts recursively all subdirectories and their files from the root directory.
201 * - It also shows a timestamp of the last modificated file inside the root directory
203 * @param OCFile : startDirectory
204 * @param boolean : counting starts from here ?
206 private void getDirectorySizeNumber(OCFile directory
,boolean startOfRecursive
) {
207 if (startOfRecursive
) {
208 totalSizeOfDirectoriesRecursive
= null
;
210 Vector
<OCFile
> files
= mStorageManager
.getDirectoryContent(directory
);
211 for (OCFile file
: files
) {
212 if(!file
.isDirectory()) {
213 if (totalSizeOfDirectoriesRecursive
== null
) {
214 totalSizeOfDirectoriesRecursive
= file
.getFileLength();
215 lastModifiedOfAllSubdirectories
= file
.getModificationTimestamp();
219 totalSizeOfDirectoriesRecursive
+= file
.getFileLength();
220 if (lastModifiedOfAllSubdirectories
< file
.getModificationTimestamp()) {
221 lastModifiedOfAllSubdirectories
= file
.getModificationTimestamp();
225 this.getDirectorySizeNumber(file
, false
);
232 public int getViewTypeCount() {
237 public boolean hasStableIds() {
242 public boolean isEmpty() {
243 return (mFiles
== null
|| mFiles
.isEmpty());
247 * Change the adapted directory for a new one
248 * @param directory New file to adapt. Can be NULL, meaning "no content to adapt".
249 * @param updatedStorageManager Optional updated storage manager; used to replace mStorageManager if is different (and not NULL)
251 public void swapDirectory(OCFile directory
, DataStorageManager updatedStorageManager
) {
253 if (updatedStorageManager
!= null
&& updatedStorageManager
!= mStorageManager
) {
254 mStorageManager
= updatedStorageManager
;
255 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
257 if (mStorageManager
!= null
) {
258 mFiles
= mStorageManager
.getDirectoryContent(mFile
);
262 notifyDataSetChanged();