841a5d04bd740c2b6f06dbfcf2b029e5c98a072d
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / adapter / FileListListAdapter.java
1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
4 *
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.
8 *
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.
13 *
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/>.
16 *
17 */
18 package com.owncloud.android.ui.adapter;
19
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;
30
31
32 import java.util.Vector;
33
34 import com.owncloud.android.R;
35 import com.owncloud.android.authentication.AccountUtils;
36 import com.owncloud.android.datamodel.FileDataStorageManager;
37 import com.owncloud.android.datamodel.OCFile;
38 import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
39 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
40 import com.owncloud.android.ui.activity.TransferServiceGetter;
41 import com.owncloud.android.utils.DisplayUtils;
42
43
44 /**
45 * This Adapter populates a ListView with all files and folders in an ownCloud
46 * instance.
47 *
48 * @author Bartek Przybylski
49 *
50 */
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 FileDataStorageManager mStorageManager;
56 private Account mAccount;
57 private TransferServiceGetter mTransferServiceGetter;
58
59 public FileListListAdapter(Context context, TransferServiceGetter transferServiceGetter) {
60 mContext = context;
61 mAccount = AccountUtils.getCurrentOwnCloudAccount(mContext);
62 mTransferServiceGetter = transferServiceGetter;
63 }
64
65 @Override
66 public boolean areAllItemsEnabled() {
67 return true;
68 }
69
70 @Override
71 public boolean isEnabled(int position) {
72 return true;
73 }
74
75 @Override
76 public int getCount() {
77 return mFiles != null ? mFiles.size() : 0;
78 }
79
80 @Override
81 public Object getItem(int position) {
82 if (mFiles == null || mFiles.size() <= position)
83 return null;
84 return mFiles.get(position);
85 }
86
87 @Override
88 public long getItemId(int position) {
89 if (mFiles == null || mFiles.size() <= position)
90 return 0;
91 return mFiles.get(position).getFileId();
92 }
93
94 @Override
95 public int getItemViewType(int position) {
96 return 0;
97 }
98
99 @Override
100 public View getView(int position, View convertView, ViewGroup parent) {
101 View view = convertView;
102 if (view == null) {
103 LayoutInflater inflator = (LayoutInflater) mContext
104 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
105 view = inflator.inflate(R.layout.list_item, null);
106 }
107
108 if (mFiles != null && mFiles.size() > position) {
109 OCFile file = mFiles.get(position);
110 TextView fileName = (TextView) view.findViewById(R.id.Filename);
111 String name = file.getFileName();
112
113 fileName.setText(name);
114 ImageView fileIcon = (ImageView) view.findViewById(R.id.imageView1);
115 fileIcon.setImageResource(DisplayUtils.getResourceId(file.getMimetype()));
116 ImageView localStateView = (ImageView) view.findViewById(R.id.imageView2);
117 FileDownloaderBinder downloaderBinder = mTransferServiceGetter.getFileDownloaderBinder();
118 FileUploaderBinder uploaderBinder = mTransferServiceGetter.getFileUploaderBinder();
119 if (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, file)) {
120 localStateView.setImageResource(R.drawable.downloading_file_indicator);
121 localStateView.setVisibility(View.VISIBLE);
122 } else if (uploaderBinder != null && uploaderBinder.isUploading(mAccount, file)) {
123 localStateView.setImageResource(R.drawable.uploading_file_indicator);
124 localStateView.setVisibility(View.VISIBLE);
125 } else if (file.isDown()) {
126 localStateView.setImageResource(R.drawable.local_file_indicator);
127 localStateView.setVisibility(View.VISIBLE);
128 } else {
129 localStateView.setVisibility(View.INVISIBLE);
130 }
131
132 TextView fileSizeV = (TextView) view.findViewById(R.id.file_size);
133 TextView lastModV = (TextView) view.findViewById(R.id.last_mod);
134 ImageView checkBoxV = (ImageView) view.findViewById(R.id.custom_checkbox);
135
136 if (!file.isFolder()) {
137 fileSizeV.setVisibility(View.VISIBLE);
138 fileSizeV.setText(DisplayUtils.bytesToHumanReadable(file.getFileLength()));
139 lastModV.setVisibility(View.VISIBLE);
140 lastModV.setText(DisplayUtils.unixTimeToHumanReadable(file.getModificationTimestamp()));
141 // this if-else is needed even thoe fav icon is visible by default
142 // because android reuses views in listview
143 if (!file.keepInSync()) {
144 view.findViewById(R.id.imageView3).setVisibility(View.GONE);
145 } else {
146 view.findViewById(R.id.imageView3).setVisibility(View.VISIBLE);
147 }
148
149 ListView parentList = (ListView)parent;
150 if (parentList.getChoiceMode() == ListView.CHOICE_MODE_NONE) {
151 checkBoxV.setVisibility(View.GONE);
152 } else {
153 if (parentList.isItemChecked(position)) {
154 checkBoxV.setImageResource(android.R.drawable.checkbox_on_background);
155 } else {
156 checkBoxV.setImageResource(android.R.drawable.checkbox_off_background);
157 }
158 checkBoxV.setVisibility(View.VISIBLE);
159 }
160
161 }
162 else {
163
164 fileSizeV.setVisibility(View.VISIBLE);
165 fileSizeV.setText(DisplayUtils.bytesToHumanReadable(file.getFileLength()));
166 lastModV.setVisibility(View.VISIBLE);
167 lastModV.setText(DisplayUtils.unixTimeToHumanReadable(file.getModificationTimestamp()));
168 checkBoxV.setVisibility(View.GONE);
169 view.findViewById(R.id.imageView3).setVisibility(View.GONE);
170 }
171 }
172
173 return view;
174 }
175
176 @Override
177 public int getViewTypeCount() {
178 return 1;
179 }
180
181 @Override
182 public boolean hasStableIds() {
183 return true;
184 }
185
186 @Override
187 public boolean isEmpty() {
188 return (mFiles == null || mFiles.isEmpty());
189 }
190
191 /**
192 * Change the adapted directory for a new one
193 * @param directory New file to adapt. Can be NULL, meaning "no content to adapt".
194 * @param updatedStorageManager Optional updated storage manager; used to replace mStorageManager if is different (and not NULL)
195 */
196 public void swapDirectory(OCFile directory, FileDataStorageManager updatedStorageManager) {
197 mFile = directory;
198 if (updatedStorageManager != null && updatedStorageManager != mStorageManager) {
199 mStorageManager = updatedStorageManager;
200 mAccount = AccountUtils.getCurrentOwnCloudAccount(mContext);
201 }
202 if (mStorageManager != null) {
203 mFiles = mStorageManager.getFolderContent(mFile);
204 } else {
205 mFiles = null;
206 }
207 notifyDataSetChanged();
208 }
209
210 }