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
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 Cursor mCursor
= null
;
64 private static String
[] cursorFrom
= { ProviderMeta
.ProviderTableMeta
.FILE_NAME
,
65 ProviderMeta
.ProviderTableMeta
.FILE_MODIFIED
,
66 ProviderMeta
.ProviderTableMeta
.FILE_CONTENT_LENGTH
68 private static int[] cursorTo
= { R
.id
.Filename
,
73 public FileListListAdapter(Context context
, int layout
, Cursor c
, String
[] from
, int[] to
, int flags
) {
74 super(context
, layout
, c
, from
, to
, flags
);
75 // TODO Auto-generated constructor stub
78 public FileListListAdapter(Context context
, TransferServiceGetter transferServiceGetter
, OCFile file
) {
86 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
87 mTransferServiceGetter
= transferServiceGetter
;
89 mCursor
= file
== null ? null
: mStorageManager
.getContent(file
.getParentId());
93 public boolean areAllItemsEnabled() {
98 public boolean isEnabled(int position
) {
103 public int getCount() {
104 return mFiles
!= null ? mFiles
.size() : 0;
108 public Object
getItem(int position
) {
109 if (mFiles
== null
|| mFiles
.size() <= position
)
111 return mFiles
.get(position
);
115 public long getItemId(int position
) {
116 if (mFiles
== null
|| mFiles
.size() <= position
)
118 return mFiles
.get(position
).getFileId();
122 public int getItemViewType(int position
) {
127 public View
getView(int position
, View convertView
, ViewGroup parent
) {
128 View view
= convertView
;
130 LayoutInflater inflator
= (LayoutInflater
) mContext
131 .getSystemService(Context
.LAYOUT_INFLATER_SERVICE
);
132 view
= inflator
.inflate(R
.layout
.list_item
, null
);
135 if (mFiles
!= null
&& mFiles
.size() > position
) {
136 OCFile file
= mFiles
.get(position
);
137 TextView fileName
= (TextView
) view
.findViewById(R
.id
.Filename
);
138 String name
= file
.getFileName();
140 fileName
.setText(name
);
141 ImageView fileIcon
= (ImageView
) view
.findViewById(R
.id
.imageView1
);
142 fileIcon
.setImageResource(DisplayUtils
.getResourceId(file
.getMimetype()));
143 ImageView localStateView
= (ImageView
) view
.findViewById(R
.id
.imageView2
);
144 FileDownloaderBinder downloaderBinder
= mTransferServiceGetter
.getFileDownloaderBinder();
145 FileUploaderBinder uploaderBinder
= mTransferServiceGetter
.getFileUploaderBinder();
146 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, file
)) {
147 localStateView
.setImageResource(R
.drawable
.downloading_file_indicator
);
148 localStateView
.setVisibility(View
.VISIBLE
);
149 } else if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, file
)) {
150 localStateView
.setImageResource(R
.drawable
.uploading_file_indicator
);
151 localStateView
.setVisibility(View
.VISIBLE
);
152 } else if (file
.isDown()) {
153 localStateView
.setImageResource(R
.drawable
.local_file_indicator
);
154 localStateView
.setVisibility(View
.VISIBLE
);
156 localStateView
.setVisibility(View
.INVISIBLE
);
159 TextView fileSizeV
= (TextView
) view
.findViewById(R
.id
.file_size
);
160 TextView lastModV
= (TextView
) view
.findViewById(R
.id
.last_mod
);
161 ImageView checkBoxV
= (ImageView
) view
.findViewById(R
.id
.custom_checkbox
);
163 if (!file
.isFolder()) {
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 // this if-else is needed even thoe fav icon is visible by default
169 // because android reuses views in listview
170 if (!file
.keepInSync()) {
171 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.GONE
);
173 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.VISIBLE
);
176 ListView parentList
= (ListView
)parent
;
177 if (parentList
.getChoiceMode() == ListView
.CHOICE_MODE_NONE
) {
178 checkBoxV
.setVisibility(View
.GONE
);
180 if (parentList
.isItemChecked(position
)) {
181 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_on_background
);
183 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_off_background
);
185 checkBoxV
.setVisibility(View
.VISIBLE
);
191 fileSizeV
.setVisibility(View
.INVISIBLE
);
192 //fileSizeV.setText(DisplayUtils.bytesToHumanReadable(file.getFileLength()));
193 lastModV
.setVisibility(View
.VISIBLE
);
194 lastModV
.setText(DisplayUtils
.unixTimeToHumanReadable(file
.getModificationTimestamp()));
195 checkBoxV
.setVisibility(View
.GONE
);
196 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.GONE
);
199 ImageView shareIconV
= (ImageView
) view
.findViewById(R
.id
.shareIcon
);
200 if (file
.isShareByLink()) {
201 shareIconV
.setVisibility(View
.VISIBLE
);
203 shareIconV
.setVisibility(View
.INVISIBLE
);
211 public int getViewTypeCount() {
216 public boolean hasStableIds() {
221 public boolean isEmpty() {
222 return (mFiles
== null
|| mFiles
.isEmpty());
226 * Change the adapted directory for a new one
227 * @param directory New file to adapt. Can be NULL, meaning "no content to adapt".
228 * @param updatedStorageManager Optional updated storage manager; used to replace mStorageManager if is different (and not NULL)
230 public void swapDirectory(OCFile directory
, FileDataStorageManager updatedStorageManager
) {
232 if (updatedStorageManager
!= null
&& updatedStorageManager
!= mStorageManager
) {
233 mStorageManager
= updatedStorageManager
;
234 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
236 if (mStorageManager
!= null
) {
237 mFiles
= mStorageManager
.getFolderContent(mFile
);
238 mCursor
= mStorageManager
.getContent(mFile
.getParentId());
243 notifyDataSetChanged();