1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 * Copyright (C) 2012-2014 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
.database
.Cursor
;
23 import android
.support
.v4
.widget
.SimpleCursorAdapter
;
24 import android
.view
.LayoutInflater
;
25 import android
.view
.View
;
26 import android
.view
.ViewGroup
;
27 //import android.widget.BaseAdapter;
28 import android
.widget
.ImageView
;
29 import android
.widget
.ListAdapter
;
30 import android
.widget
.ListView
;
31 import android
.widget
.TextView
;
34 import java
.util
.Vector
;
36 import com
.owncloud
.android
.R
;
37 import com
.owncloud
.android
.authentication
.AccountUtils
;
38 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
39 import com
.owncloud
.android
.datamodel
.OCFile
;
40 import com
.owncloud
.android
.db
.ProviderMeta
;
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
;
44 import com
.owncloud
.android
.utils
.DisplayUtils
;
48 * This Adapter populates a ListView with all files and folders in an ownCloud
51 * @author Bartek Przybylski
54 public class FileListListAdapter
extends SimpleCursorAdapter
/*BaseAdapter*/ implements ListAdapter
{
56 private Context mContext
;
57 private static OCFile mFile
= null
;
58 private Vector
<OCFile
> mFiles
= null
;
59 private static FileDataStorageManager mStorageManager
;
60 private Account mAccount
;
61 private TransferServiceGetter mTransferServiceGetter
;
62 private static String
[] cursorFrom
= { ProviderMeta
.ProviderTableMeta
.FILE_NAME
,
63 ProviderMeta
.ProviderTableMeta
.FILE_MODIFIED
,
64 ProviderMeta
.ProviderTableMeta
.FILE_CONTENT_LENGTH
66 private static int[] cursorTo
= { R
.id
.Filename
,
71 public FileListListAdapter(Context context
, int layout
, Cursor c
, String
[] from
, int[] to
, int flags
) {
72 super(context
, layout
, c
, from
, to
, flags
);
73 // TODO Auto-generated constructor stub
76 public FileListListAdapter(Context context
, TransferServiceGetter transferServiceGetter
, OCFile file
) {
79 file
== null ? null
: mStorageManager
.getContent(file
.getParentId()),
84 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
85 mTransferServiceGetter
= transferServiceGetter
;
90 public boolean areAllItemsEnabled() {
95 public boolean isEnabled(int position
) {
100 public int getCount() {
101 return mFiles
!= null ? mFiles
.size() : 0;
105 public Object
getItem(int position
) {
106 if (mFiles
== null
|| mFiles
.size() <= position
)
108 return mFiles
.get(position
);
112 public long getItemId(int position
) {
113 if (mFiles
== null
|| mFiles
.size() <= position
)
115 return mFiles
.get(position
).getFileId();
119 public int getItemViewType(int position
) {
124 public View
getView(int position
, View convertView
, ViewGroup parent
) {
125 View view
= convertView
;
127 LayoutInflater inflator
= (LayoutInflater
) mContext
128 .getSystemService(Context
.LAYOUT_INFLATER_SERVICE
);
129 view
= inflator
.inflate(R
.layout
.list_item
, null
);
132 if (mFiles
!= null
&& mFiles
.size() > position
) {
133 OCFile file
= mFiles
.get(position
);
134 TextView fileName
= (TextView
) view
.findViewById(R
.id
.Filename
);
135 String name
= file
.getFileName();
137 fileName
.setText(name
);
138 ImageView fileIcon
= (ImageView
) view
.findViewById(R
.id
.imageView1
);
139 fileIcon
.setImageResource(DisplayUtils
.getResourceId(file
.getMimetype()));
140 ImageView localStateView
= (ImageView
) view
.findViewById(R
.id
.imageView2
);
141 FileDownloaderBinder downloaderBinder
= mTransferServiceGetter
.getFileDownloaderBinder();
142 FileUploaderBinder uploaderBinder
= mTransferServiceGetter
.getFileUploaderBinder();
143 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, file
)) {
144 localStateView
.setImageResource(R
.drawable
.downloading_file_indicator
);
145 localStateView
.setVisibility(View
.VISIBLE
);
146 } else if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, file
)) {
147 localStateView
.setImageResource(R
.drawable
.uploading_file_indicator
);
148 localStateView
.setVisibility(View
.VISIBLE
);
149 } else if (file
.isDown()) {
150 localStateView
.setImageResource(R
.drawable
.local_file_indicator
);
151 localStateView
.setVisibility(View
.VISIBLE
);
153 localStateView
.setVisibility(View
.INVISIBLE
);
156 TextView fileSizeV
= (TextView
) view
.findViewById(R
.id
.file_size
);
157 TextView lastModV
= (TextView
) view
.findViewById(R
.id
.last_mod
);
158 ImageView checkBoxV
= (ImageView
) view
.findViewById(R
.id
.custom_checkbox
);
160 if (!file
.isFolder()) {
161 fileSizeV
.setVisibility(View
.VISIBLE
);
162 fileSizeV
.setText(DisplayUtils
.bytesToHumanReadable(file
.getFileLength()));
163 lastModV
.setVisibility(View
.VISIBLE
);
164 lastModV
.setText(DisplayUtils
.unixTimeToHumanReadable(file
.getModificationTimestamp()));
165 // this if-else is needed even thoe fav icon is visible by default
166 // because android reuses views in listview
167 if (!file
.keepInSync()) {
168 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.GONE
);
170 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.VISIBLE
);
173 ListView parentList
= (ListView
)parent
;
174 if (parentList
.getChoiceMode() == ListView
.CHOICE_MODE_NONE
) {
175 checkBoxV
.setVisibility(View
.GONE
);
177 if (parentList
.isItemChecked(position
)) {
178 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_on_background
);
180 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_off_background
);
182 checkBoxV
.setVisibility(View
.VISIBLE
);
188 fileSizeV
.setVisibility(View
.INVISIBLE
);
189 //fileSizeV.setText(DisplayUtils.bytesToHumanReadable(file.getFileLength()));
190 lastModV
.setVisibility(View
.VISIBLE
);
191 lastModV
.setText(DisplayUtils
.unixTimeToHumanReadable(file
.getModificationTimestamp()));
192 checkBoxV
.setVisibility(View
.GONE
);
193 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.GONE
);
196 ImageView shareIconV
= (ImageView
) view
.findViewById(R
.id
.shareIcon
);
197 if (file
.isShareByLink()) {
198 shareIconV
.setVisibility(View
.VISIBLE
);
200 shareIconV
.setVisibility(View
.INVISIBLE
);
208 public int getViewTypeCount() {
213 public boolean hasStableIds() {
218 public boolean isEmpty() {
219 return (mFiles
== null
|| mFiles
.isEmpty());
223 * Change the adapted directory for a new one
224 * @param directory New file to adapt. Can be NULL, meaning "no content to adapt".
225 * @param updatedStorageManager Optional updated storage manager; used to replace mStorageManager if is different (and not NULL)
227 public void swapDirectory(OCFile directory
, FileDataStorageManager updatedStorageManager
) {
229 if (updatedStorageManager
!= null
&& updatedStorageManager
!= mStorageManager
) {
230 mStorageManager
= updatedStorageManager
;
231 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
233 if (mStorageManager
!= null
) {
234 mFiles
= mStorageManager
.getFolderContent(mFile
);
238 notifyDataSetChanged();