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
, FLAG_AUTO_REQUERY
);
64 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
65 mTransferServiceGetter
= componentsGetter
;
68 public void setStorageManager(FileDataStorageManager storageManager
) {
69 mStorageManager
= storageManager
;
73 protected void onContentChanged() {
74 Log_OC
.d(TAG
, "onContentChanged() start");
75 super.onContentChanged();
77 notifyDataSetChanged();
78 Log_OC
.d(TAG
, "onContentChanged() end");
82 public boolean areAllItemsEnabled() {
87 public boolean isEnabled(int position) {
92 public int getCount() {
93 return mFiles != null ? mFiles.size() : 0;
97 public Object getItem(int position) {
98 if (mFiles == null || mFiles.size() <= position)
100 return mFiles.get(position);
104 public long getItemId(int position) {
105 if (mFiles == null || mFiles.size() <= position)
107 return mFiles.get(position).getFileId();
111 public int getItemViewType(int position) {
116 public int getViewTypeCount() {
121 public boolean hasStableIds() {
126 public boolean isEmpty() {
127 return (mFiles == null || mFiles.isEmpty());
132 // * Change the adapted directory for a new one
133 // * @param folder New file to adapt. Can be NULL, meaning "no content to adapt".
134 // * @param updatedStorageManager Optional updated storage manager; used to replace mStorageManager if is different (and not NULL)
136 // public void swapDirectory(OCFile folder, FileDataStorageManager updatedStorageManager) {
137 // if (updatedStorageManager != null && updatedStorageManager != mStorageManager) {
138 // mStorageManager = updatedStorageManager;
139 // mAccount = AccountUtils.getCurrentOwnCloudAccount(mContext);
141 // Cursor newCursor = null;
142 // if (mStorageManager != null) {
143 // //mFiles = mStorageManager.getFolderContent(mFile);
144 // newCursor = mStorageManager.getContent(folder.getFileId());
145 // Uri uri = Uri.withAppendedPath(
146 // ProviderTableMeta.CONTENT_URI_DIR,
147 // String.valueOf(folder.getFileId()));
148 // Log_OC.d(TAG, "swapDirectory Uri " + uri);
149 // newCursor.setNotificationUri(mContext.getContentResolver(), uri);
152 // Cursor oldCursor = swapCursor(newCursor);
153 // if (oldCursor != null){
154 // oldCursor.close();
156 // notifyDataSetChanged();
160 public void bindView(View view
, Context context
, Cursor cursor
) {
161 //Log_OC.d(TAG, "bindView start");
163 OCFile file
= mStorageManager
.createFileInstance(cursor
);
165 TextView fileName
= (TextView
) view
.findViewById(R
.id
.Filename
);
166 String name
= file
.getFileName();
168 fileName
.setText(name
);
169 ImageView fileIcon
= (ImageView
) view
.findViewById(R
.id
.imageView1
);
170 fileIcon
.setImageResource(DisplayUtils
.getResourceId(file
.getMimetype()));
171 ImageView localStateView
= (ImageView
) view
.findViewById(R
.id
.imageView2
);
172 FileDownloaderBinder downloaderBinder
= mTransferServiceGetter
.getFileDownloaderBinder();
173 FileUploaderBinder uploaderBinder
= mTransferServiceGetter
.getFileUploaderBinder();
174 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, file
)) {
175 localStateView
.setImageResource(R
.drawable
.downloading_file_indicator
);
176 localStateView
.setVisibility(View
.VISIBLE
);
177 } else if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, file
)) {
178 localStateView
.setImageResource(R
.drawable
.uploading_file_indicator
);
179 localStateView
.setVisibility(View
.VISIBLE
);
180 } else if (file
.isDown()) {
181 localStateView
.setImageResource(R
.drawable
.local_file_indicator
);
182 localStateView
.setVisibility(View
.VISIBLE
);
184 localStateView
.setVisibility(View
.INVISIBLE
);
187 TextView fileSizeV
= (TextView
) view
.findViewById(R
.id
.file_size
);
188 TextView lastModV
= (TextView
) view
.findViewById(R
.id
.last_mod
);
189 ImageView checkBoxV
= (ImageView
) view
.findViewById(R
.id
.custom_checkbox
);
191 if (!file
.isFolder()) {
192 fileSizeV
.setVisibility(View
.VISIBLE
);
193 fileSizeV
.setText(DisplayUtils
.bytesToHumanReadable(file
.getFileLength()));
194 lastModV
.setVisibility(View
.VISIBLE
);
195 lastModV
.setText(DisplayUtils
.unixTimeToHumanReadable(file
.getModificationTimestamp()));
196 // this if-else is needed even thoe fav icon is visible by default
197 // because android reuses views in listview
198 if (!file
.keepInSync()) {
199 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.GONE
);
201 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.VISIBLE
);
207 fileSizeV
.setVisibility(View
.INVISIBLE
);
208 //fileSizeV.setText(DisplayUtils.bytesToHumanReadable(file.getFileLength()));
209 lastModV
.setVisibility(View
.VISIBLE
);
210 lastModV
.setText(DisplayUtils
.unixTimeToHumanReadable(file
.getModificationTimestamp()));
211 checkBoxV
.setVisibility(View
.GONE
);
212 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.GONE
);
215 ImageView shareIconV
= (ImageView
) view
.findViewById(R
.id
.shareIcon
);
216 if (file
.isShareByLink()) {
217 shareIconV
.setVisibility(View
.VISIBLE
);
219 shareIconV
.setVisibility(View
.INVISIBLE
);
222 //Log_OC.d(TAG, "bindView end");
226 public View
newView(Context context
, Cursor cursor
, ViewGroup parent
) {
227 //Log_OC.d(TAG, "newView start");
228 LayoutInflater inflator
= (LayoutInflater
) context
.getSystemService(Context
.LAYOUT_INFLATER_SERVICE
);
229 View view
= inflator
.inflate(R
.layout
.list_item
, null
);
231 // TODO check activity to upload
232 ListView parentList
= (ListView
) parent
;
233 ImageView checkBoxV
= (ImageView
) view
.findViewById(R
.id
.custom_checkbox
);
234 if (parentList
.getChoiceMode() == ListView
.CHOICE_MODE_NONE
) {
235 checkBoxV
.setVisibility(View
.GONE
);
237 /*if (parentList.isItemChecked(position)) {
238 checkBoxV.setImageResource(android.R.drawable.checkbox_on_background);
240 checkBoxV.setImageResource(android.R.drawable.checkbox_off_background);
242 checkBoxV
.setVisibility(View
.VISIBLE
);
244 //Log_OC.d(TAG, "newView end");