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
.database
.Cursor
;
23 import android
.support
.v4
.widget
.CursorAdapter
;
24 import android
.view
.LayoutInflater
;
25 import android
.view
.View
;
26 import android
.view
.ViewGroup
;
27 import android
.widget
.ImageView
;
28 import android
.widget
.ListAdapter
;
29 import android
.widget
.ListView
;
30 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
;
41 import com
.owncloud
.android
.utils
.Log_OC
;
45 * This Adapter populates a ListView with all files and folders in an ownCloud
48 * @author Bartek Przybylski
51 public class FileListListAdapter
extends CursorAdapter
implements ListAdapter
{
53 private static final String TAG
= FileListListAdapter
.class.getSimpleName();
55 private Context mContext
;
56 private FileDataStorageManager mStorageManager
;
57 private Account mAccount
;
58 private ComponentsGetter mTransferServiceGetter
;
61 public FileListListAdapter(Context context
, ComponentsGetter componentsGetter
) {
62 super(context
, null
, 0);
64 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
65 mTransferServiceGetter
= componentsGetter
;
70 public boolean areAllItemsEnabled() {
75 public boolean isEnabled(int position) {
80 public int getCount() {
81 return mFiles != null ? mFiles.size() : 0;
85 public Object getItem(int position) {
86 if (mFiles == null || mFiles.size() <= position)
88 return mFiles.get(position);
92 public long getItemId(int position) {
93 if (mFiles == null || mFiles.size() <= position)
95 return mFiles.get(position).getFileId();
99 public int getItemViewType(int position) {
104 public int getViewTypeCount() {
109 public boolean hasStableIds() {
114 public boolean isEmpty() {
115 return (mFiles == null || mFiles.isEmpty());
120 * Change the adapted directory for a new one
121 * @param folder New file to adapt. Can be NULL, meaning "no content to adapt".
122 * @param updatedStorageManager Optional updated storage manager; used to replace mStorageManager if is different (and not NULL)
124 public void swapDirectory(OCFile folder
, FileDataStorageManager updatedStorageManager
) {
125 if (updatedStorageManager
!= null
&& updatedStorageManager
!= mStorageManager
) {
126 mStorageManager
= updatedStorageManager
;
127 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
129 Cursor newCursor
= null
;
130 if (mStorageManager
!= null
) {
131 //mFiles = mStorageManager.getFolderContent(mFile);
132 newCursor
= mStorageManager
.getContent(folder
.getFileId());
134 Cursor oldCursor
= swapCursor(newCursor
);
135 if (oldCursor
!= null
){
138 notifyDataSetChanged();
142 public void bindView(View view
, Context context
, Cursor cursor
) {
143 Log_OC
.d(TAG
, "bindView start");
145 OCFile file
= mStorageManager
.createFileInstance(cursor
);
147 TextView fileName
= (TextView
) view
.findViewById(R
.id
.Filename
);
148 String name
= file
.getFileName();
150 fileName
.setText(name
);
151 ImageView fileIcon
= (ImageView
) view
.findViewById(R
.id
.imageView1
);
152 fileIcon
.setImageResource(DisplayUtils
.getResourceId(file
.getMimetype()));
153 ImageView localStateView
= (ImageView
) view
.findViewById(R
.id
.imageView2
);
154 FileDownloaderBinder downloaderBinder
= mTransferServiceGetter
.getFileDownloaderBinder();
155 FileUploaderBinder uploaderBinder
= mTransferServiceGetter
.getFileUploaderBinder();
156 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, file
)) {
157 localStateView
.setImageResource(R
.drawable
.downloading_file_indicator
);
158 localStateView
.setVisibility(View
.VISIBLE
);
159 } else if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, file
)) {
160 localStateView
.setImageResource(R
.drawable
.uploading_file_indicator
);
161 localStateView
.setVisibility(View
.VISIBLE
);
162 } else if (file
.isDown()) {
163 localStateView
.setImageResource(R
.drawable
.local_file_indicator
);
164 localStateView
.setVisibility(View
.VISIBLE
);
166 localStateView
.setVisibility(View
.INVISIBLE
);
169 TextView fileSizeV
= (TextView
) view
.findViewById(R
.id
.file_size
);
170 TextView lastModV
= (TextView
) view
.findViewById(R
.id
.last_mod
);
171 ImageView checkBoxV
= (ImageView
) view
.findViewById(R
.id
.custom_checkbox
);
173 if (!file
.isFolder()) {
174 fileSizeV
.setVisibility(View
.VISIBLE
);
175 fileSizeV
.setText(DisplayUtils
.bytesToHumanReadable(file
.getFileLength()));
176 lastModV
.setVisibility(View
.VISIBLE
);
177 lastModV
.setText(DisplayUtils
.unixTimeToHumanReadable(file
.getModificationTimestamp()));
178 // this if-else is needed even thoe fav icon is visible by default
179 // because android reuses views in listview
180 if (!file
.keepInSync()) {
181 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.GONE
);
183 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.VISIBLE
);
189 fileSizeV
.setVisibility(View
.INVISIBLE
);
190 //fileSizeV.setText(DisplayUtils.bytesToHumanReadable(file.getFileLength()));
191 lastModV
.setVisibility(View
.VISIBLE
);
192 lastModV
.setText(DisplayUtils
.unixTimeToHumanReadable(file
.getModificationTimestamp()));
193 checkBoxV
.setVisibility(View
.GONE
);
194 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.GONE
);
197 ImageView shareIconV
= (ImageView
) view
.findViewById(R
.id
.shareIcon
);
198 if (file
.isShareByLink()) {
199 shareIconV
.setVisibility(View
.VISIBLE
);
201 shareIconV
.setVisibility(View
.INVISIBLE
);
204 Log_OC
.d(TAG
, "bindView end");
208 public View
newView(Context context
, Cursor cursor
, ViewGroup parent
) {
209 Log_OC
.d(TAG
, "newView start");
210 LayoutInflater inflator
= (LayoutInflater
) context
.getSystemService(Context
.LAYOUT_INFLATER_SERVICE
);
211 View view
= inflator
.inflate(R
.layout
.list_item
, null
);
213 // TODO check activity to upload
214 ListView parentList
= (ListView
) parent
;
215 ImageView checkBoxV
= (ImageView
) view
.findViewById(R
.id
.custom_checkbox
);
216 if (parentList
.getChoiceMode() == ListView
.CHOICE_MODE_NONE
) {
217 checkBoxV
.setVisibility(View
.GONE
);
219 /*if (parentList.isItemChecked(position)) {
220 checkBoxV.setImageResource(android.R.drawable.checkbox_on_background);
222 checkBoxV.setImageResource(android.R.drawable.checkbox_off_background);
224 checkBoxV
.setVisibility(View
.VISIBLE
);
226 Log_OC
.d(TAG
, "newView end");