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
.io
.ObjectInputStream
.GetField
;
21 import java
.util
.Vector
;
23 import android
.accounts
.Account
;
24 import android
.content
.Context
;
25 import android
.graphics
.Bitmap
;
26 import android
.graphics
.BitmapFactory
;
27 import android
.media
.ThumbnailUtils
;
28 import android
.view
.LayoutInflater
;
29 import android
.view
.View
;
30 import android
.view
.ViewGroup
;
31 import android
.widget
.BaseAdapter
;
32 import android
.widget
.ImageView
;
33 import android
.widget
.ListAdapter
;
34 import android
.widget
.ListView
;
35 import android
.widget
.TextView
;
37 import com
.owncloud
.android
.R
;
38 import com
.owncloud
.android
.authentication
.AccountUtils
;
39 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
40 import com
.owncloud
.android
.datamodel
.OCFile
;
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
.ComponentsGetter
;
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 BaseAdapter
implements ListAdapter
{
55 private final static String PERMISSION_SHARED_WITH_ME
= "S";
57 private Context mContext
;
58 private OCFile mFile
= null
;
59 private Vector
<OCFile
> mFiles
= null
;
61 private FileDataStorageManager mStorageManager
;
62 private Account mAccount
;
63 private ComponentsGetter mTransferServiceGetter
;
65 public FileListListAdapter(Context context
, ComponentsGetter transferServiceGetter
) {
67 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
68 mTransferServiceGetter
= transferServiceGetter
;
72 public boolean areAllItemsEnabled() {
77 public boolean isEnabled(int position
) {
82 public int getCount() {
83 return mFiles
!= null ? mFiles
.size() : 0;
87 public Object
getItem(int position
) {
88 if (mFiles
== null
|| mFiles
.size() <= position
)
90 return mFiles
.get(position
);
94 public long getItemId(int position
) {
95 if (mFiles
== null
|| mFiles
.size() <= position
)
97 return mFiles
.get(position
).getFileId();
101 public int getItemViewType(int position
) {
106 public View
getView(int position
, View convertView
, ViewGroup parent
) {
107 View view
= convertView
;
109 LayoutInflater inflator
= (LayoutInflater
) mContext
110 .getSystemService(Context
.LAYOUT_INFLATER_SERVICE
);
111 view
= inflator
.inflate(R
.layout
.list_item
, null
);
114 if (mFiles
!= null
&& mFiles
.size() > position
) {
115 OCFile file
= mFiles
.get(position
);
116 TextView fileName
= (TextView
) view
.findViewById(R
.id
.Filename
);
117 String name
= file
.getFileName();
119 fileName
.setText(name
);
120 ImageView fileIcon
= (ImageView
) view
.findViewById(R
.id
.thumbnail
);
121 ImageView sharedIconV
= (ImageView
) view
.findViewById(R
.id
.sharedIcon
);
122 ImageView sharedWithMeIconV
= (ImageView
) view
.findViewById(R
.id
.sharedWithMeIcon
);
123 sharedWithMeIconV
.setVisibility(View
.GONE
);
125 ImageView localStateView
= (ImageView
) view
.findViewById(R
.id
.imageView2
);
126 localStateView
.bringToFront();
127 FileDownloaderBinder downloaderBinder
= mTransferServiceGetter
.getFileDownloaderBinder();
128 FileUploaderBinder uploaderBinder
= mTransferServiceGetter
.getFileUploaderBinder();
129 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, file
)) {
130 localStateView
.setImageResource(R
.drawable
.downloading_file_indicator
);
131 localStateView
.setVisibility(View
.VISIBLE
);
132 } else if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, file
)) {
133 localStateView
.setImageResource(R
.drawable
.uploading_file_indicator
);
134 localStateView
.setVisibility(View
.VISIBLE
);
135 } else if (file
.isDown()) {
136 localStateView
.setImageResource(R
.drawable
.local_file_indicator
);
137 localStateView
.setVisibility(View
.VISIBLE
);
139 localStateView
.setVisibility(View
.INVISIBLE
);
142 TextView fileSizeV
= (TextView
) view
.findViewById(R
.id
.file_size
);
143 TextView lastModV
= (TextView
) view
.findViewById(R
.id
.last_mod
);
144 ImageView checkBoxV
= (ImageView
) view
.findViewById(R
.id
.custom_checkbox
);
146 if (!file
.isFolder()) {
147 fileSizeV
.setVisibility(View
.VISIBLE
);
148 fileSizeV
.setText(DisplayUtils
.bytesToHumanReadable(file
.getFileLength()));
149 lastModV
.setVisibility(View
.VISIBLE
);
150 lastModV
.setText(DisplayUtils
.unixTimeToHumanReadable(file
.getModificationTimestamp()));
151 // this if-else is needed even thoe fav icon is visible by default
152 // because android reuses views in listview
153 if (!file
.keepInSync()) {
154 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.GONE
);
156 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.VISIBLE
);
159 ListView parentList
= (ListView
)parent
;
160 if (parentList
.getChoiceMode() == ListView
.CHOICE_MODE_NONE
) {
161 checkBoxV
.setVisibility(View
.GONE
);
163 if (parentList
.isItemChecked(position
)) {
164 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_on_background
);
166 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_off_background
);
168 checkBoxV
.setVisibility(View
.VISIBLE
);
171 // generate Thumbnail if file is available local and image
172 if (file
.isDown() && file
.isImage()){
173 Bitmap bitmap
= BitmapFactory
.decodeFile(file
.getStoragePath());
174 fileIcon
.setImageBitmap(ThumbnailUtils
.extractThumbnail(bitmap
, 50, 50));
176 fileIcon
.setImageResource(DisplayUtils
.getResourceId(file
.getMimetype(), file
.getFileName()));
179 if (checkIfFileIsSharedWithMe(file
)) {
180 sharedWithMeIconV
.setVisibility(View
.VISIBLE
);
185 fileSizeV
.setVisibility(View
.INVISIBLE
);
186 //fileSizeV.setText(DisplayUtils.bytesToHumanReadable(file.getFileLength()));
187 lastModV
.setVisibility(View
.VISIBLE
);
188 lastModV
.setText(DisplayUtils
.unixTimeToHumanReadable(file
.getModificationTimestamp()));
189 checkBoxV
.setVisibility(View
.GONE
);
190 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.GONE
);
192 if (checkIfFileIsSharedWithMe(file
)) {
193 fileIcon
.setImageResource(R
.drawable
.shared_with_me_folder
);
194 sharedWithMeIconV
.setVisibility(View
.VISIBLE
);
196 fileIcon
.setImageResource(DisplayUtils
.getResourceId(file
.getMimetype(), file
.getFileName()));
199 // If folder is sharedByLink, icon folder must be changed to
201 if (file
.isShareByLink()) {
202 fileIcon
.setImageResource(R
.drawable
.folder_public
);
206 if (file
.isShareByLink()) {
207 sharedIconV
.setVisibility(View
.VISIBLE
);
209 sharedIconV
.setVisibility(View
.GONE
);
217 public int getViewTypeCount() {
222 public boolean hasStableIds() {
227 public boolean isEmpty() {
228 return (mFiles
== null
|| mFiles
.isEmpty());
232 * Change the adapted directory for a new one
233 * @param directory New file to adapt. Can be NULL, meaning "no content to adapt".
234 * @param updatedStorageManager Optional updated storage manager; used to replace mStorageManager if is different (and not NULL)
236 public void swapDirectory(OCFile directory
, FileDataStorageManager updatedStorageManager
) {
238 if (updatedStorageManager
!= null
&& updatedStorageManager
!= mStorageManager
) {
239 mStorageManager
= updatedStorageManager
;
240 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
242 if (mStorageManager
!= null
) {
243 mFiles
= mStorageManager
.getFolderContent(mFile
);
247 notifyDataSetChanged();
251 * Check if parent folder does not include 'S' permission and if file/folder
254 * @param file: OCFile
255 * @return boolean: True if it is shared with me and false if it is not
257 private boolean checkIfFileIsSharedWithMe(OCFile file
) {
258 return (mFile
.getPermissions() != null
&& !mFile
.getPermissions().contains(PERMISSION_SHARED_WITH_ME
)
259 && file
.getPermissions() != null
&& file
.getPermissions().contains(PERMISSION_SHARED_WITH_ME
));