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 java
.util
.Vector
;
22 import android
.accounts
.Account
;
23 import android
.content
.Context
;
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
;
33 import com
.owncloud
.android
.R
;
34 import com
.owncloud
.android
.authentication
.AccountUtils
;
35 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
36 import com
.owncloud
.android
.datamodel
.OCFile
;
37 import com
.owncloud
.android
.files
.services
.FileDownloader
.FileDownloaderBinder
;
38 import com
.owncloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
39 import com
.owncloud
.android
.ui
.activity
.ComponentsGetter
;
40 import com
.owncloud
.android
.utils
.DisplayUtils
;
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 final static String PERMISSION_SHARED_WITH_ME
= "S";
53 private Context mContext
;
54 private OCFile mFile
= null
;
55 private Vector
<OCFile
> mFiles
= null
;
57 private FileDataStorageManager mStorageManager
;
58 private Account mAccount
;
59 private ComponentsGetter mTransferServiceGetter
;
61 public FileListListAdapter(Context context
, ComponentsGetter transferServiceGetter
) {
63 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
64 mTransferServiceGetter
= transferServiceGetter
;
68 public boolean areAllItemsEnabled() {
73 public boolean isEnabled(int position
) {
78 public int getCount() {
79 return mFiles
!= null ? mFiles
.size() : 0;
83 public Object
getItem(int position
) {
84 if (mFiles
== null
|| mFiles
.size() <= position
)
86 return mFiles
.get(position
);
90 public long getItemId(int position
) {
91 if (mFiles
== null
|| mFiles
.size() <= position
)
93 return mFiles
.get(position
).getFileId();
97 public int getItemViewType(int position
) {
102 public View
getView(int position
, View convertView
, ViewGroup parent
) {
103 View view
= convertView
;
105 LayoutInflater inflator
= (LayoutInflater
) mContext
106 .getSystemService(Context
.LAYOUT_INFLATER_SERVICE
);
107 view
= inflator
.inflate(R
.layout
.list_item
, null
);
110 if (mFiles
!= null
&& mFiles
.size() > position
) {
111 OCFile file
= mFiles
.get(position
);
112 TextView fileName
= (TextView
) view
.findViewById(R
.id
.Filename
);
113 String name
= file
.getFileName();
115 fileName
.setText(name
);
116 ImageView fileIcon
= (ImageView
) view
.findViewById(R
.id
.imageView1
);
117 ImageView sharedIconV
= (ImageView
) view
.findViewById(R
.id
.sharedIcon
);
118 ImageView sharedWithMeIconV
= (ImageView
) view
.findViewById(R
.id
.sharedWithMeIcon
);
119 sharedWithMeIconV
.setVisibility(View
.GONE
);
121 ImageView localStateView
= (ImageView
) view
.findViewById(R
.id
.imageView2
);
122 localStateView
.bringToFront();
123 FileDownloaderBinder downloaderBinder
= mTransferServiceGetter
.getFileDownloaderBinder();
124 FileUploaderBinder uploaderBinder
= mTransferServiceGetter
.getFileUploaderBinder();
125 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, file
)) {
126 localStateView
.setImageResource(R
.drawable
.downloading_file_indicator
);
127 localStateView
.setVisibility(View
.VISIBLE
);
128 } else if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, file
)) {
129 localStateView
.setImageResource(R
.drawable
.uploading_file_indicator
);
130 localStateView
.setVisibility(View
.VISIBLE
);
131 } else if (file
.isDown()) {
132 localStateView
.setImageResource(R
.drawable
.local_file_indicator
);
133 localStateView
.setVisibility(View
.VISIBLE
);
135 localStateView
.setVisibility(View
.INVISIBLE
);
138 TextView fileSizeV
= (TextView
) view
.findViewById(R
.id
.file_size
);
139 TextView lastModV
= (TextView
) view
.findViewById(R
.id
.last_mod
);
140 ImageView checkBoxV
= (ImageView
) view
.findViewById(R
.id
.custom_checkbox
);
142 if (!file
.isFolder()) {
143 fileSizeV
.setVisibility(View
.VISIBLE
);
144 fileSizeV
.setText(DisplayUtils
.bytesToHumanReadable(file
.getFileLength()));
145 lastModV
.setVisibility(View
.VISIBLE
);
146 lastModV
.setText(DisplayUtils
.unixTimeToHumanReadable(file
.getModificationTimestamp()));
147 // this if-else is needed even thoe fav icon is visible by default
148 // because android reuses views in listview
149 if (!file
.keepInSync()) {
150 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.GONE
);
152 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.VISIBLE
);
155 ListView parentList
= (ListView
)parent
;
156 if (parentList
.getChoiceMode() == ListView
.CHOICE_MODE_NONE
) {
157 checkBoxV
.setVisibility(View
.GONE
);
159 if (parentList
.isItemChecked(position
)) {
160 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_on_background
);
162 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_off_background
);
164 checkBoxV
.setVisibility(View
.VISIBLE
);
167 fileIcon
.setImageResource(DisplayUtils
.getResourceId(file
.getMimetype(), file
.getFileName()));
169 if (checkIfFileIsSharedWithMe(file
)) {
170 sharedWithMeIconV
.setVisibility(View
.VISIBLE
);
175 fileSizeV
.setVisibility(View
.INVISIBLE
);
176 //fileSizeV.setText(DisplayUtils.bytesToHumanReadable(file.getFileLength()));
177 lastModV
.setVisibility(View
.VISIBLE
);
178 lastModV
.setText(DisplayUtils
.unixTimeToHumanReadable(file
.getModificationTimestamp()));
179 checkBoxV
.setVisibility(View
.GONE
);
180 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.GONE
);
182 if (checkIfFileIsSharedWithMe(file
)) {
183 fileIcon
.setImageResource(R
.drawable
.shared_with_me_folder
);
184 sharedWithMeIconV
.setVisibility(View
.VISIBLE
);
186 fileIcon
.setImageResource(DisplayUtils
.getResourceId(file
.getMimetype(), file
.getFileName()));
189 // If folder is sharedByLink, icon folder must be changed to
191 if (file
.isShareByLink()) {
192 fileIcon
.setImageResource(R
.drawable
.folder_public
);
196 if (file
.isShareByLink()) {
197 sharedIconV
.setVisibility(View
.VISIBLE
);
199 sharedIconV
.setVisibility(View
.GONE
);
207 public int getViewTypeCount() {
212 public boolean hasStableIds() {
217 public boolean isEmpty() {
218 return (mFiles
== null
|| mFiles
.isEmpty());
222 * Change the adapted directory for a new one
223 * @param directory New file to adapt. Can be NULL, meaning "no content to adapt".
224 * @param updatedStorageManager Optional updated storage manager; used to replace mStorageManager if is different (and not NULL)
226 public void swapDirectory(OCFile directory
, FileDataStorageManager updatedStorageManager
) {
228 if (updatedStorageManager
!= null
&& updatedStorageManager
!= mStorageManager
) {
229 mStorageManager
= updatedStorageManager
;
230 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
232 if (mStorageManager
!= null
) {
233 mFiles
= mStorageManager
.getFolderContent(mFile
);
237 notifyDataSetChanged();
241 * Check if parent folder does not include 'S' permission and if file/folder
244 * @param file: OCFile
245 * @return boolean: True if it is shared with me and false if it is not
247 private boolean checkIfFileIsSharedWithMe(OCFile file
) {
248 return (mFile
.getPermissions() != null
&& !mFile
.getPermissions().contains(PERMISSION_SHARED_WITH_ME
)
249 && file
.getPermissions() != null
&& file
.getPermissions().contains(PERMISSION_SHARED_WITH_ME
));