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 de
.mobilcom
.debitel
.cloud
.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
;
31 import de
.mobilcom
.debitel
.cloud
.android
.DisplayUtils
;
32 import de
.mobilcom
.debitel
.cloud
.android
.R
;
33 import de
.mobilcom
.debitel
.cloud
.android
.authentication
.AccountUtils
;
34 import de
.mobilcom
.debitel
.cloud
.android
.datamodel
.DataStorageManager
;
35 import de
.mobilcom
.debitel
.cloud
.android
.datamodel
.OCFile
;
36 import de
.mobilcom
.debitel
.cloud
.android
.files
.services
.FileDownloader
.FileDownloaderBinder
;
37 import de
.mobilcom
.debitel
.cloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
38 import de
.mobilcom
.debitel
.cloud
.android
.ui
.activity
.TransferServiceGetter
;
40 import java
.util
.Vector
;
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(Context context
, TransferServiceGetter transferServiceGetter
) {
60 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
61 mTransferServiceGetter
= transferServiceGetter
;
65 public boolean areAllItemsEnabled() {
70 public boolean isEnabled(int position
) {
75 public int getCount() {
76 return mFiles
!= null ? mFiles
.size() : 0;
80 public Object
getItem(int position
) {
81 if (mFiles
== null
|| mFiles
.size() <= position
)
83 return mFiles
.get(position
);
87 public long getItemId(int position
) {
88 if (mFiles
== null
|| mFiles
.size() <= position
)
90 return mFiles
.get(position
).getFileId();
94 public int getItemViewType(int position
) {
99 public View
getView(int position
, View convertView
, ViewGroup parent
) {
100 View view
= convertView
;
102 LayoutInflater inflator
= (LayoutInflater
) mContext
103 .getSystemService(Context
.LAYOUT_INFLATER_SERVICE
);
104 view
= inflator
.inflate(R
.layout
.list_item
, null
);
107 if (mFiles
!= null
&& mFiles
.size() > position
) {
108 OCFile file
= mFiles
.get(position
);
109 TextView fileName
= (TextView
) view
.findViewById(R
.id
.Filename
);
110 String name
= file
.getFileName();
112 fileName
.setText(name
);
113 ImageView fileIcon
= (ImageView
) view
.findViewById(R
.id
.imageView1
);
114 fileIcon
.setImageResource(DisplayUtils
.getResourceId(file
.getMimetype()));
115 ImageView localStateView
= (ImageView
) view
.findViewById(R
.id
.imageView2
);
116 FileDownloaderBinder downloaderBinder
= mTransferServiceGetter
.getFileDownloaderBinder();
117 FileUploaderBinder uploaderBinder
= mTransferServiceGetter
.getFileUploaderBinder();
118 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, file
)) {
119 localStateView
.setImageResource(R
.drawable
.downloading_file_indicator
);
120 localStateView
.setVisibility(View
.VISIBLE
);
121 } else if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, file
)) {
122 localStateView
.setImageResource(R
.drawable
.uploading_file_indicator
);
123 localStateView
.setVisibility(View
.VISIBLE
);
124 } else if (file
.isDown()) {
125 localStateView
.setImageResource(R
.drawable
.local_file_indicator
);
126 localStateView
.setVisibility(View
.VISIBLE
);
128 localStateView
.setVisibility(View
.INVISIBLE
);
131 TextView fileSizeV
= (TextView
) view
.findViewById(R
.id
.file_size
);
132 TextView lastModV
= (TextView
) view
.findViewById(R
.id
.last_mod
);
133 ImageView checkBoxV
= (ImageView
) view
.findViewById(R
.id
.custom_checkbox
);
135 if (!file
.isDirectory()) {
136 fileSizeV
.setVisibility(View
.VISIBLE
);
137 fileSizeV
.setText(DisplayUtils
.bytesToHumanReadable(file
.getFileLength()));
138 lastModV
.setVisibility(View
.VISIBLE
);
139 lastModV
.setText(DisplayUtils
.unixTimeToHumanReadable(file
.getModificationTimestamp()));
140 // this if-else is needed even thoe fav icon is visible by default
141 // because android reuses views in listview
142 if (!file
.keepInSync()) {
143 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.GONE
);
145 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.VISIBLE
);
148 ListView parentList
= (ListView
)parent
;
149 if (parentList
.getChoiceMode() == ListView
.CHOICE_MODE_NONE
) {
150 checkBoxV
.setVisibility(View
.GONE
);
152 if (parentList
.isItemChecked(position
)) {
153 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_on_background
);
155 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_off_background
);
157 checkBoxV
.setVisibility(View
.VISIBLE
);
163 fileSizeV
.setVisibility(View
.VISIBLE
);
164 fileSizeV
.setText(DisplayUtils
.bytesToHumanReadable(file
.getFileLength()));
165 lastModV
.setVisibility(View
.VISIBLE
);
166 lastModV
.setText(DisplayUtils
.unixTimeToHumanReadable(file
.getModificationTimestamp()));
167 checkBoxV
.setVisibility(View
.GONE
);
168 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.GONE
);
176 public int getViewTypeCount() {
181 public boolean hasStableIds() {
186 public boolean isEmpty() {
187 return (mFiles
== null
|| mFiles
.isEmpty());
191 * Change the adapted directory for a new one
192 * @param directory New file to adapt. Can be NULL, meaning "no content to adapt".
193 * @param updatedStorageManager Optional updated storage manager; used to replace mStorageManager if is different (and not NULL)
195 public void swapDirectory(OCFile directory
, DataStorageManager updatedStorageManager
) {
197 if (updatedStorageManager
!= null
&& updatedStorageManager
!= mStorageManager
) {
198 mStorageManager
= updatedStorageManager
;
199 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
201 if (mStorageManager
!= null
) {
202 mFiles
= mStorageManager
.getDirectoryContent(mFile
);
206 notifyDataSetChanged();