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 as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 package com
.owncloud
.android
.ui
.adapter
;
21 import java
.util
.Vector
;
23 import com
.owncloud
.android
.AccountUtils
;
24 import com
.owncloud
.android
.DisplayUtils
;
25 import com
.owncloud
.android
.datamodel
.DataStorageManager
;
26 import com
.owncloud
.android
.datamodel
.OCFile
;
27 import com
.owncloud
.android
.files
.services
.FileDownloader
.FileDownloaderBinder
;
28 import com
.owncloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
29 import com
.owncloud
.android
.ui
.activity
.TransferServiceGetter
;
31 import com
.owncloud
.android
.R
;
33 import android
.accounts
.Account
;
34 import android
.content
.Context
;
35 import android
.view
.LayoutInflater
;
36 import android
.view
.View
;
37 import android
.view
.ViewGroup
;
38 import android
.widget
.BaseAdapter
;
39 import android
.widget
.ImageView
;
40 import android
.widget
.ListAdapter
;
41 import android
.widget
.ListView
;
42 import android
.widget
.TextView
;
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
;
55 private DataStorageManager mStorageManager
;
56 private Account mAccount
;
57 private TransferServiceGetter mTransferServiceGetter
;
59 public FileListListAdapter(OCFile file
, DataStorageManager storage_man
,
60 Context context
, TransferServiceGetter transferServiceGetter
) {
61 mStorageManager
= storage_man
;
63 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
64 mTransferServiceGetter
= transferServiceGetter
;
65 swapDirectory(file
, mStorageManager
);
67 mFiles = mStorageManager.getDirectoryContent(mFile);*/
71 public boolean areAllItemsEnabled() {
76 public boolean isEnabled(int position
) {
81 public int getCount() {
82 return mFiles
!= null ? mFiles
.size() : 0;
86 public Object
getItem(int position
) {
87 if (mFiles
== null
|| mFiles
.size() <= position
)
89 return mFiles
.get(position
);
93 public long getItemId(int position
) {
94 if (mFiles
== null
|| mFiles
.size() <= position
)
96 return mFiles
.get(position
).getFileId();
100 public int getItemViewType(int position
) {
105 public View
getView(int position
, View convertView
, ViewGroup parent
) {
106 View view
= convertView
;
108 LayoutInflater inflator
= (LayoutInflater
) mContext
109 .getSystemService(Context
.LAYOUT_INFLATER_SERVICE
);
110 view
= inflator
.inflate(R
.layout
.list_item
, null
);
112 if (mFiles
!= null
&& mFiles
.size() > position
) {
113 OCFile file
= mFiles
.get(position
);
114 TextView fileName
= (TextView
) view
.findViewById(R
.id
.Filename
);
115 String name
= file
.getFileName();
117 fileName
.setText(name
);
118 ImageView fileIcon
= (ImageView
) view
.findViewById(R
.id
.imageView1
);
119 fileIcon
.setImageResource(DisplayUtils
.getResourceId(file
.getMimetype()));
120 ImageView localStateView
= (ImageView
) view
.findViewById(R
.id
.imageView2
);
121 FileDownloaderBinder downloaderBinder
= mTransferServiceGetter
.getFileDownloaderBinder();
122 FileUploaderBinder uploaderBinder
= mTransferServiceGetter
.getFileUploaderBinder();
123 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, file
)) {
124 localStateView
.setImageResource(R
.drawable
.downloading_file_indicator
);
125 localStateView
.setVisibility(View
.VISIBLE
);
126 } else if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, file
)) {
127 localStateView
.setImageResource(R
.drawable
.uploading_file_indicator
);
128 localStateView
.setVisibility(View
.VISIBLE
);
129 } else if (file
.isDown()) {
130 localStateView
.setImageResource(R
.drawable
.local_file_indicator
);
131 localStateView
.setVisibility(View
.VISIBLE
);
133 localStateView
.setVisibility(View
.INVISIBLE
);
137 TextView fileSizeV
= (TextView
) view
.findViewById(R
.id
.file_size
);
138 TextView lastModV
= (TextView
) view
.findViewById(R
.id
.last_mod
);
139 ImageView checkBoxV
= (ImageView
) view
.findViewById(R
.id
.custom_checkbox
);
141 if (!file
.isDirectory()) {
142 fileSizeV
.setVisibility(View
.VISIBLE
);
143 fileSizeV
.setText(DisplayUtils
.bytesToHumanReadable(file
.getFileLength()));
144 lastModV
.setVisibility(View
.VISIBLE
);
145 lastModV
.setText(DisplayUtils
.unixTimeToHumanReadable(file
.getModificationTimestamp()));
146 // this if-else is needed even thoe fav icon is visible by default
147 // because android reuses views in listview
148 if (!file
.keepInSync()) {
149 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.GONE
);
151 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.VISIBLE
);
154 ListView parentList
= (ListView
)parent
;
155 if (parentList
.getChoiceMode() == ListView
.CHOICE_MODE_NONE
) {
156 checkBoxV
.setVisibility(View
.GONE
);
158 if (parentList
.isItemChecked(position
)) {
159 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_on_background
);
161 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_off_background
);
163 checkBoxV
.setVisibility(View
.VISIBLE
);
167 fileSizeV
.setVisibility(View
.GONE
);
168 lastModV
.setVisibility(View
.GONE
);
169 checkBoxV
.setVisibility(View
.GONE
);
170 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.GONE
);
178 public int getViewTypeCount() {
183 public boolean hasStableIds() {
188 public boolean isEmpty() {
189 return (mFiles
== null
|| mFiles
.isEmpty());
193 * Change the adapted directory for a new one
194 * @param directory New file to adapt. Can be NULL, meaning "no content to adapt".
195 * @param updatedStorageManager Optional updated storage manager; used to replace mStorageManager if is different (and not NULL)
197 public void swapDirectory(OCFile directory
, DataStorageManager updatedStorageManager
) {
199 if (updatedStorageManager
!= null
&& updatedStorageManager
!= mStorageManager
) {
200 mStorageManager
= updatedStorageManager
;
201 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
203 if (mStorageManager
!= null
) {
204 mFiles
= mStorageManager
.getDirectoryContent(mFile
);
208 notifyDataSetChanged();