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 com
.owncloud
.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
;
32 import java
.util
.Vector
;
34 import com
.owncloud
.android
.R
;
35 import com
.owncloud
.android
.authentication
.AccountUtils
;
36 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
37 import com
.owncloud
.android
.datamodel
.OCFile
;
38 import com
.owncloud
.android
.files
.services
.FileDownloader
.FileDownloaderBinder
;
39 import com
.owncloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
40 import com
.owncloud
.android
.ui
.activity
.TransferServiceGetter
;
41 import com
.owncloud
.android
.utils
.DisplayUtils
;
45 * This Adapter populates a ListView with all files and folders in an ownCloud
48 * @author Bartek Przybylski
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 FileDataStorageManager mStorageManager
;
56 private Account mAccount
;
57 private TransferServiceGetter mTransferServiceGetter
;
59 public FileListListAdapter(Context context
, TransferServiceGetter transferServiceGetter
) {
61 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
62 mTransferServiceGetter
= transferServiceGetter
;
66 public boolean areAllItemsEnabled() {
71 public boolean isEnabled(int position
) {
76 public int getCount() {
77 return mFiles
!= null ? mFiles
.size() : 0;
81 public Object
getItem(int position
) {
82 if (mFiles
== null
|| mFiles
.size() <= position
)
84 return mFiles
.get(position
);
88 public long getItemId(int position
) {
89 if (mFiles
== null
|| mFiles
.size() <= position
)
91 return mFiles
.get(position
).getFileId();
95 public int getItemViewType(int position
) {
100 public View
getView(int position
, View convertView
, ViewGroup parent
) {
101 View view
= convertView
;
103 LayoutInflater inflator
= (LayoutInflater
) mContext
104 .getSystemService(Context
.LAYOUT_INFLATER_SERVICE
);
105 view
= inflator
.inflate(R
.layout
.list_item
, null
);
108 if (mFiles
!= null
&& mFiles
.size() > position
) {
109 OCFile file
= mFiles
.get(position
);
110 TextView fileName
= (TextView
) view
.findViewById(R
.id
.Filename
);
111 String name
= file
.getFileName();
113 fileName
.setText(name
);
114 ImageView fileIcon
= (ImageView
) view
.findViewById(R
.id
.imageView1
);
115 fileIcon
.setImageResource(DisplayUtils
.getResourceId(file
.getMimetype()));
116 ImageView localStateView
= (ImageView
) view
.findViewById(R
.id
.imageView2
);
117 FileDownloaderBinder downloaderBinder
= mTransferServiceGetter
.getFileDownloaderBinder();
118 FileUploaderBinder uploaderBinder
= mTransferServiceGetter
.getFileUploaderBinder();
119 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, file
)) {
120 localStateView
.setImageResource(R
.drawable
.downloading_file_indicator
);
121 localStateView
.setVisibility(View
.VISIBLE
);
122 } else if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, file
)) {
123 localStateView
.setImageResource(R
.drawable
.uploading_file_indicator
);
124 localStateView
.setVisibility(View
.VISIBLE
);
125 } else if (file
.isDown()) {
126 localStateView
.setImageResource(R
.drawable
.local_file_indicator
);
127 localStateView
.setVisibility(View
.VISIBLE
);
129 localStateView
.setVisibility(View
.INVISIBLE
);
132 TextView fileSizeV
= (TextView
) view
.findViewById(R
.id
.file_size
);
133 TextView lastModV
= (TextView
) view
.findViewById(R
.id
.last_mod
);
134 ImageView checkBoxV
= (ImageView
) view
.findViewById(R
.id
.custom_checkbox
);
136 if (!file
.isFolder()) {
137 fileSizeV
.setVisibility(View
.VISIBLE
);
138 fileSizeV
.setText(DisplayUtils
.bytesToHumanReadable(file
.getFileLength()));
139 lastModV
.setVisibility(View
.VISIBLE
);
140 lastModV
.setText(DisplayUtils
.unixTimeToHumanReadable(file
.getModificationTimestamp()));
141 // this if-else is needed even thoe fav icon is visible by default
142 // because android reuses views in listview
143 if (!file
.keepInSync()) {
144 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.GONE
);
146 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.VISIBLE
);
149 ListView parentList
= (ListView
)parent
;
150 if (parentList
.getChoiceMode() == ListView
.CHOICE_MODE_NONE
) {
151 checkBoxV
.setVisibility(View
.GONE
);
153 if (parentList
.isItemChecked(position
)) {
154 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_on_background
);
156 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_off_background
);
158 checkBoxV
.setVisibility(View
.VISIBLE
);
164 fileSizeV
.setVisibility(View
.VISIBLE
);
165 fileSizeV
.setText(DisplayUtils
.bytesToHumanReadable(file
.getFileLength()));
166 lastModV
.setVisibility(View
.VISIBLE
);
167 lastModV
.setText(DisplayUtils
.unixTimeToHumanReadable(file
.getModificationTimestamp()));
168 checkBoxV
.setVisibility(View
.GONE
);
169 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.GONE
);
177 public int getViewTypeCount() {
182 public boolean hasStableIds() {
187 public boolean isEmpty() {
188 return (mFiles
== null
|| mFiles
.isEmpty());
192 * Change the adapted directory for a new one
193 * @param directory New file to adapt. Can be NULL, meaning "no content to adapt".
194 * @param updatedStorageManager Optional updated storage manager; used to replace mStorageManager if is different (and not NULL)
196 public void swapDirectory(OCFile directory
, FileDataStorageManager updatedStorageManager
) {
198 if (updatedStorageManager
!= null
&& updatedStorageManager
!= mStorageManager
) {
199 mStorageManager
= updatedStorageManager
;
200 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
202 if (mStorageManager
!= null
) {
203 mFiles
= mStorageManager
.getFolderContent(mFile
);
207 notifyDataSetChanged();