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 java
.util
.Vector
;
22 import com
.owncloud
.android
.AccountUtils
;
23 import com
.owncloud
.android
.DisplayUtils
;
24 import com
.owncloud
.android
.datamodel
.DataStorageManager
;
25 import com
.owncloud
.android
.datamodel
.OCFile
;
26 import com
.owncloud
.android
.files
.services
.FileDownloader
.FileDownloaderBinder
;
27 import com
.owncloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
28 import com
.owncloud
.android
.ui
.activity
.TransferServiceGetter
;
30 import com
.owncloud
.android
.R
;
32 import android
.accounts
.Account
;
33 import android
.content
.Context
;
34 import android
.view
.LayoutInflater
;
35 import android
.view
.View
;
36 import android
.view
.ViewGroup
;
37 import android
.widget
.BaseAdapter
;
38 import android
.widget
.ImageView
;
39 import android
.widget
.ListAdapter
;
40 import android
.widget
.ListView
;
41 import android
.widget
.TextView
;
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
;
58 public FileListListAdapter(OCFile file
, DataStorageManager storage_man
,
59 Context context
, TransferServiceGetter transferServiceGetter
) {
60 mStorageManager
= storage_man
;
62 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
63 mTransferServiceGetter
= transferServiceGetter
;
64 swapDirectory(file
, mStorageManager
);
66 mFiles = mStorageManager.getDirectoryContent(mFile);*/
70 public boolean areAllItemsEnabled() {
75 public boolean isEnabled(int position
) {
80 public int getCount() {
81 return mFiles
!= null ? mFiles
.size() : 0;
85 public Object
getItem(int position
) {
86 if (mFiles
== null
|| mFiles
.size() <= position
)
88 return mFiles
.get(position
);
92 public long getItemId(int position
) {
93 if (mFiles
== null
|| mFiles
.size() <= position
)
95 return mFiles
.get(position
).getFileId();
99 public int getItemViewType(int position
) {
104 public View
getView(int position
, View convertView
, ViewGroup parent
) {
105 View view
= convertView
;
107 LayoutInflater inflator
= (LayoutInflater
) mContext
108 .getSystemService(Context
.LAYOUT_INFLATER_SERVICE
);
109 view
= inflator
.inflate(R
.layout
.list_item
, null
);
111 if (mFiles
!= null
&& mFiles
.size() > position
) {
112 OCFile file
= mFiles
.get(position
);
113 TextView fileName
= (TextView
) view
.findViewById(R
.id
.Filename
);
114 String name
= file
.getFileName();
116 fileName
.setText(name
);
117 ImageView fileIcon
= (ImageView
) view
.findViewById(R
.id
.imageView1
);
118 fileIcon
.setImageResource(DisplayUtils
.getResourceId(file
.getMimetype()));
119 ImageView localStateView
= (ImageView
) view
.findViewById(R
.id
.imageView2
);
120 FileDownloaderBinder downloaderBinder
= mTransferServiceGetter
.getFileDownloaderBinder();
121 FileUploaderBinder uploaderBinder
= mTransferServiceGetter
.getFileUploaderBinder();
122 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, file
)) {
123 localStateView
.setImageResource(R
.drawable
.downloading_file_indicator
);
124 localStateView
.setVisibility(View
.VISIBLE
);
125 } else if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, file
)) {
126 localStateView
.setImageResource(R
.drawable
.uploading_file_indicator
);
127 localStateView
.setVisibility(View
.VISIBLE
);
128 } else if (file
.isDown()) {
129 localStateView
.setImageResource(R
.drawable
.local_file_indicator
);
130 localStateView
.setVisibility(View
.VISIBLE
);
132 localStateView
.setVisibility(View
.INVISIBLE
);
136 TextView fileSizeV
= (TextView
) view
.findViewById(R
.id
.file_size
);
137 TextView lastModV
= (TextView
) view
.findViewById(R
.id
.last_mod
);
138 ImageView checkBoxV
= (ImageView
) view
.findViewById(R
.id
.custom_checkbox
);
140 if (!file
.isDirectory()) {
141 fileSizeV
.setVisibility(View
.VISIBLE
);
142 fileSizeV
.setText(DisplayUtils
.bytesToHumanReadable(file
.getFileLength()));
143 lastModV
.setVisibility(View
.VISIBLE
);
144 lastModV
.setText(DisplayUtils
.unixTimeToHumanReadable(file
.getModificationTimestamp()));
145 // this if-else is needed even thoe fav icon is visible by default
146 // because android reuses views in listview
147 if (!file
.keepInSync()) {
148 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.GONE
);
150 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.VISIBLE
);
153 ListView parentList
= (ListView
)parent
;
154 if (parentList
.getChoiceMode() == ListView
.CHOICE_MODE_NONE
) {
155 checkBoxV
.setVisibility(View
.GONE
);
157 if (parentList
.isItemChecked(position
)) {
158 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_on_background
);
160 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_off_background
);
162 checkBoxV
.setVisibility(View
.VISIBLE
);
166 fileSizeV
.setVisibility(View
.GONE
);
167 lastModV
.setVisibility(View
.GONE
);
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
, DataStorageManager updatedStorageManager
) {
198 if (updatedStorageManager
!= null
&& updatedStorageManager
!= mStorageManager
) {
199 mStorageManager
= updatedStorageManager
;
200 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
202 if (mStorageManager
!= null
) {
203 mFiles
= mStorageManager
.getDirectoryContent(mFile
);
207 notifyDataSetChanged();