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
.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
.ComponentsGetter
;
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
;
56 private FileDataStorageManager mStorageManager
;
57 private Account mAccount
;
58 private ComponentsGetter mTransferServiceGetter
;
60 public FileListListAdapter(Context context
, ComponentsGetter transferServiceGetter
) {
62 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
63 mTransferServiceGetter
= transferServiceGetter
;
67 public boolean areAllItemsEnabled() {
72 public boolean isEnabled(int position
) {
77 public int getCount() {
78 return mFiles
!= null ? mFiles
.size() : 0;
82 public Object
getItem(int position
) {
83 if (mFiles
== null
|| mFiles
.size() <= position
)
85 return mFiles
.get(position
);
89 public long getItemId(int position
) {
90 if (mFiles
== null
|| mFiles
.size() <= position
)
92 return mFiles
.get(position
).getFileId();
96 public int getItemViewType(int position
) {
101 public View
getView(int position
, View convertView
, ViewGroup parent
) {
102 View view
= convertView
;
104 LayoutInflater inflator
= (LayoutInflater
) mContext
105 .getSystemService(Context
.LAYOUT_INFLATER_SERVICE
);
106 view
= inflator
.inflate(R
.layout
.list_item
, null
);
109 if (mFiles
!= null
&& mFiles
.size() > position
) {
110 OCFile file
= mFiles
.get(position
);
111 TextView fileName
= (TextView
) view
.findViewById(R
.id
.Filename
);
112 String name
= file
.getFileName();
114 fileName
.setText(name
);
115 ImageView fileIcon
= (ImageView
) view
.findViewById(R
.id
.imageView1
);
116 fileIcon
.setImageResource(DisplayUtils
.getResourceId(file
.getMimetype(), file
.getFileName()));
117 ImageView localStateView
= (ImageView
) view
.findViewById(R
.id
.imageView2
);
118 FileDownloaderBinder downloaderBinder
= mTransferServiceGetter
.getFileDownloaderBinder();
119 FileUploaderBinder uploaderBinder
= mTransferServiceGetter
.getFileUploaderBinder();
120 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, file
)) {
121 localStateView
.setImageResource(R
.drawable
.downloading_file_indicator
);
122 localStateView
.setVisibility(View
.VISIBLE
);
123 } else if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, file
)) {
124 localStateView
.setImageResource(R
.drawable
.uploading_file_indicator
);
125 localStateView
.setVisibility(View
.VISIBLE
);
126 } else if (file
.isDown()) {
127 localStateView
.setImageResource(R
.drawable
.local_file_indicator
);
128 localStateView
.setVisibility(View
.VISIBLE
);
130 localStateView
.setVisibility(View
.INVISIBLE
);
133 TextView fileSizeV
= (TextView
) view
.findViewById(R
.id
.file_size
);
134 TextView lastModV
= (TextView
) view
.findViewById(R
.id
.last_mod
);
135 ImageView checkBoxV
= (ImageView
) view
.findViewById(R
.id
.custom_checkbox
);
137 if (!file
.isFolder()) {
138 fileSizeV
.setVisibility(View
.VISIBLE
);
139 fileSizeV
.setText(DisplayUtils
.bytesToHumanReadable(file
.getFileLength()));
140 lastModV
.setVisibility(View
.VISIBLE
);
141 lastModV
.setText(DisplayUtils
.unixTimeToHumanReadable(file
.getModificationTimestamp()));
142 // this if-else is needed even thoe fav icon is visible by default
143 // because android reuses views in listview
144 if (!file
.keepInSync()) {
145 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.GONE
);
147 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.VISIBLE
);
150 ListView parentList
= (ListView
)parent
;
151 if (parentList
.getChoiceMode() == ListView
.CHOICE_MODE_NONE
) {
152 checkBoxV
.setVisibility(View
.GONE
);
154 if (parentList
.isItemChecked(position
)) {
155 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_on_background
);
157 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_off_background
);
159 checkBoxV
.setVisibility(View
.VISIBLE
);
165 fileSizeV
.setVisibility(View
.INVISIBLE
);
166 //fileSizeV.setText(DisplayUtils.bytesToHumanReadable(file.getFileLength()));
167 lastModV
.setVisibility(View
.VISIBLE
);
168 lastModV
.setText(DisplayUtils
.unixTimeToHumanReadable(file
.getModificationTimestamp()));
169 checkBoxV
.setVisibility(View
.GONE
);
170 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.GONE
);
173 ImageView shareIconV
= (ImageView
) view
.findViewById(R
.id
.shareIcon
);
174 if (file
.isShareByLink()) {
175 shareIconV
.setVisibility(View
.VISIBLE
);
177 shareIconV
.setVisibility(View
.INVISIBLE
);
185 public int getViewTypeCount() {
190 public boolean hasStableIds() {
195 public boolean isEmpty() {
196 return (mFiles
== null
|| mFiles
.isEmpty());
200 * Change the adapted directory for a new one
201 * @param directory New file to adapt. Can be NULL, meaning "no content to adapt".
202 * @param updatedStorageManager Optional updated storage manager; used to replace mStorageManager if is different (and not NULL)
204 public void swapDirectory(OCFile directory
, FileDataStorageManager updatedStorageManager
) {
206 if (updatedStorageManager
!= null
&& updatedStorageManager
!= mStorageManager
) {
207 mStorageManager
= updatedStorageManager
;
208 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
210 if (mStorageManager
!= null
) {
211 mFiles
= mStorageManager
.getFolderContent(mFile
);
215 notifyDataSetChanged();