1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
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.
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.
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/>.
18 package de
.mobilcom
.debitel
.cloud
.android
.ui
.adapter
;
20 import android
.accounts
.Account
;
21 import android
.content
.Context
;
22 import android
.view
.LayoutInflater
;
23 import android
.view
.View
;
24 import android
.view
.ViewGroup
;
25 import android
.widget
.BaseAdapter
;
26 import android
.widget
.ImageView
;
27 import android
.widget
.ListAdapter
;
28 import android
.widget
.ListView
;
29 import android
.widget
.TextView
;
31 import de
.mobilcom
.debitel
.cloud
.android
.DisplayUtils
;
32 import de
.mobilcom
.debitel
.cloud
.android
.R
;
33 import de
.mobilcom
.debitel
.cloud
.android
.authentication
.AccountUtils
;
34 import de
.mobilcom
.debitel
.cloud
.android
.datamodel
.DataStorageManager
;
35 import de
.mobilcom
.debitel
.cloud
.android
.datamodel
.OCFile
;
36 import de
.mobilcom
.debitel
.cloud
.android
.files
.services
.FileDownloader
.FileDownloaderBinder
;
37 import de
.mobilcom
.debitel
.cloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
38 import de
.mobilcom
.debitel
.cloud
.android
.ui
.activity
.TransferServiceGetter
;
40 import java
.util
.Vector
;
44 * This Adapter populates a ListView with all files and folders in an ownCloud
47 * @author Bartek Przybylski
50 public class FileListListAdapter
extends BaseAdapter
implements ListAdapter
{
51 private Context mContext
;
52 private OCFile mFile
= null
;
53 private Vector
<OCFile
> mFiles
= null
;
54 private DataStorageManager mStorageManager
;
55 private Account mAccount
;
56 private TransferServiceGetter mTransferServiceGetter
;
57 //total size of a directory (recursive)
58 private Long totalSizeOfDirectoriesRecursive
= null
;
59 private Long lastModifiedOfAllSubdirectories
= null
;
61 public FileListListAdapter(Context context
, TransferServiceGetter transferServiceGetter
) {
63 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
64 mTransferServiceGetter
= transferServiceGetter
;
68 public boolean areAllItemsEnabled() {
73 public boolean isEnabled(int position
) {
78 public int getCount() {
79 return mFiles
!= null ? mFiles
.size() : 0;
83 public Object
getItem(int position
) {
84 if (mFiles
== null
|| mFiles
.size() <= position
)
86 return mFiles
.get(position
);
90 public long getItemId(int position
) {
91 if (mFiles
== null
|| mFiles
.size() <= position
)
93 return mFiles
.get(position
).getFileId();
97 public int getItemViewType(int position
) {
102 public View
getView(int position
, View convertView
, ViewGroup parent
) {
103 View view
= convertView
;
105 LayoutInflater inflator
= (LayoutInflater
) mContext
106 .getSystemService(Context
.LAYOUT_INFLATER_SERVICE
);
107 view
= inflator
.inflate(R
.layout
.list_item
, null
);
110 if (mFiles
!= null
&& mFiles
.size() > position
) {
111 OCFile file
= mFiles
.get(position
);
112 TextView fileName
= (TextView
) view
.findViewById(R
.id
.Filename
);
113 String name
= file
.getFileName();
115 fileName
.setText(name
);
116 ImageView fileIcon
= (ImageView
) view
.findViewById(R
.id
.imageView1
);
117 fileIcon
.setImageResource(DisplayUtils
.getResourceId(file
.getMimetype()));
118 ImageView localStateView
= (ImageView
) view
.findViewById(R
.id
.imageView2
);
119 FileDownloaderBinder downloaderBinder
= mTransferServiceGetter
.getFileDownloaderBinder();
120 FileUploaderBinder uploaderBinder
= mTransferServiceGetter
.getFileUploaderBinder();
121 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, file
)) {
122 localStateView
.setImageResource(R
.drawable
.downloading_file_indicator
);
123 localStateView
.setVisibility(View
.VISIBLE
);
124 } else if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, file
)) {
125 localStateView
.setImageResource(R
.drawable
.uploading_file_indicator
);
126 localStateView
.setVisibility(View
.VISIBLE
);
127 } else if (file
.isDown()) {
128 localStateView
.setImageResource(R
.drawable
.local_file_indicator
);
129 localStateView
.setVisibility(View
.VISIBLE
);
131 localStateView
.setVisibility(View
.INVISIBLE
);
134 TextView fileSizeV
= (TextView
) view
.findViewById(R
.id
.file_size
);
135 TextView lastModV
= (TextView
) view
.findViewById(R
.id
.last_mod
);
136 ImageView checkBoxV
= (ImageView
) view
.findViewById(R
.id
.custom_checkbox
);
138 if (!file
.isDirectory()) {
139 fileSizeV
.setVisibility(View
.VISIBLE
);
140 fileSizeV
.setText(DisplayUtils
.bytesToHumanReadable(file
.getFileLength()));
141 lastModV
.setVisibility(View
.VISIBLE
);
142 lastModV
.setText(DisplayUtils
.unixTimeToHumanReadable(file
.getModificationTimestamp()));
143 // this if-else is needed even thoe fav icon is visible by default
144 // because android reuses views in listview
145 if (!file
.keepInSync()) {
146 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.GONE
);
148 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.VISIBLE
);
151 ListView parentList
= (ListView
)parent
;
152 if (parentList
.getChoiceMode() == ListView
.CHOICE_MODE_NONE
) {
153 checkBoxV
.setVisibility(View
.GONE
);
155 if (parentList
.isItemChecked(position
)) {
156 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_on_background
);
158 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_off_background
);
160 checkBoxV
.setVisibility(View
.VISIBLE
);
166 fileSizeV
.setVisibility(View
.VISIBLE
);
167 fileSizeV
.setText(DisplayUtils
.bytesToHumanReadable(file
.getFileLength()));
168 lastModV
.setVisibility(View
.VISIBLE
);
169 lastModV
.setText(DisplayUtils
.unixTimeToHumanReadable(file
.getModificationTimestamp()));
170 checkBoxV
.setVisibility(View
.GONE
);
171 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.GONE
);
179 public int getViewTypeCount() {
184 public boolean hasStableIds() {
189 public boolean isEmpty() {
190 return (mFiles
== null
|| mFiles
.isEmpty());
194 * Change the adapted directory for a new one
195 * @param directory New file to adapt. Can be NULL, meaning "no content to adapt".
196 * @param updatedStorageManager Optional updated storage manager; used to replace mStorageManager if is different (and not NULL)
198 public void swapDirectory(OCFile directory
, DataStorageManager updatedStorageManager
) {
200 if (updatedStorageManager
!= null
&& updatedStorageManager
!= mStorageManager
) {
201 mStorageManager
= updatedStorageManager
;
202 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
204 if (mStorageManager
!= null
) {
205 mFiles
= mStorageManager
.getDirectoryContent(mFile
);
209 notifyDataSetChanged();