656e0042934331078b574b10a9d9a871bb6641b2
[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-2014 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.ComponentsGetter;
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
56 private FileDataStorageManager mStorageManager;
57 private Account mAccount;
58 private ComponentsGetter mTransferServiceGetter;
59
60 public FileListListAdapter(Context context, ComponentsGetter transferServiceGetter) {
61 mContext = context;
62 mAccount = AccountUtils.getCurrentOwnCloudAccount(mContext);
63 mTransferServiceGetter = transferServiceGetter;
64 }
65
66 @Override
67 public boolean areAllItemsEnabled() {
68 return true;
69 }
70
71 @Override
72 public boolean isEnabled(int position) {
73 return true;
74 }
75
76 @Override
77 public int getCount() {
78 return mFiles != null ? mFiles.size() : 0;
79 }
80
81 @Override
82 public Object getItem(int position) {
83 if (mFiles == null || mFiles.size() <= position)
84 return null;
85 return mFiles.get(position);
86 }
87
88 @Override
89 public long getItemId(int position) {
90 if (mFiles == null || mFiles.size() <= position)
91 return 0;
92 return mFiles.get(position).getFileId();
93 }
94
95 @Override
96 public int getItemViewType(int position) {
97 return 0;
98 }
99
100 @Override
101 public View getView(int position, View convertView, ViewGroup parent) {
102 View view = convertView;
103 if (view == null) {
104 LayoutInflater inflator = (LayoutInflater) mContext
105 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
106 view = inflator.inflate(R.layout.list_item, null);
107 }
108
109 if (mFiles != null && mFiles.size() > position) {
110 OCFile file = mFiles.get(position);
111 TextView fileName = (TextView) view.findViewById(R.id.Filename);
112 String name = file.getFileName();
113
114 fileName.setText(name);
115 ImageView fileIcon = (ImageView) view.findViewById(R.id.imageView1);
116 fileIcon.setImageResource(DisplayUtils.getResourceId(file.getMimetype(), file.getFileName()));
117 ImageView localStateView = (ImageView) view.findViewById(R.id.imageView2);
118 FileDownloaderBinder downloaderBinder = mTransferServiceGetter.getFileDownloaderBinder();
119 FileUploaderBinder uploaderBinder = mTransferServiceGetter.getFileUploaderBinder();
120 if (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, file)) {
121 localStateView.setImageResource(R.drawable.downloading_file_indicator);
122 localStateView.setVisibility(View.VISIBLE);
123 } else if (uploaderBinder != null && uploaderBinder.isUploading(mAccount, file)) {
124 localStateView.setImageResource(R.drawable.uploading_file_indicator);
125 localStateView.setVisibility(View.VISIBLE);
126 } else if (file.isDown()) {
127 localStateView.setImageResource(R.drawable.local_file_indicator);
128 localStateView.setVisibility(View.VISIBLE);
129 } else {
130 localStateView.setVisibility(View.INVISIBLE);
131 }
132
133 TextView fileSizeV = (TextView) view.findViewById(R.id.file_size);
134 TextView lastModV = (TextView) view.findViewById(R.id.last_mod);
135 ImageView checkBoxV = (ImageView) view.findViewById(R.id.custom_checkbox);
136
137 if (!file.isFolder()) {
138 fileSizeV.setVisibility(View.VISIBLE);
139 fileSizeV.setText(DisplayUtils.bytesToHumanReadable(file.getFileLength()));
140 lastModV.setVisibility(View.VISIBLE);
141 lastModV.setText(DisplayUtils.unixTimeToHumanReadable(file.getModificationTimestamp()));
142 // this if-else is needed even thoe fav icon is visible by default
143 // because android reuses views in listview
144 if (!file.keepInSync()) {
145 view.findViewById(R.id.imageView3).setVisibility(View.GONE);
146 } else {
147 view.findViewById(R.id.imageView3).setVisibility(View.VISIBLE);
148 }
149
150 ListView parentList = (ListView)parent;
151 if (parentList.getChoiceMode() == ListView.CHOICE_MODE_NONE) {
152 checkBoxV.setVisibility(View.GONE);
153 } else {
154 if (parentList.isItemChecked(position)) {
155 checkBoxV.setImageResource(android.R.drawable.checkbox_on_background);
156 } else {
157 checkBoxV.setImageResource(android.R.drawable.checkbox_off_background);
158 }
159 checkBoxV.setVisibility(View.VISIBLE);
160 }
161
162 }
163 else {
164
165 fileSizeV.setVisibility(View.INVISIBLE);
166 //fileSizeV.setText(DisplayUtils.bytesToHumanReadable(file.getFileLength()));
167 lastModV.setVisibility(View.VISIBLE);
168 lastModV.setText(DisplayUtils.unixTimeToHumanReadable(file.getModificationTimestamp()));
169 checkBoxV.setVisibility(View.GONE);
170 view.findViewById(R.id.imageView3).setVisibility(View.GONE);
171 }
172
173 ImageView shareIconV = (ImageView) view.findViewById(R.id.shareIcon);
174 if (file.isShareByLink()) {
175 shareIconV.setVisibility(View.VISIBLE);
176 } else {
177 shareIconV.setVisibility(View.INVISIBLE);
178 }
179 }
180
181 return view;
182 }
183
184 @Override
185 public int getViewTypeCount() {
186 return 1;
187 }
188
189 @Override
190 public boolean hasStableIds() {
191 return true;
192 }
193
194 @Override
195 public boolean isEmpty() {
196 return (mFiles == null || mFiles.isEmpty());
197 }
198
199 /**
200 * Change the adapted directory for a new one
201 * @param directory New file to adapt. Can be NULL, meaning "no content to adapt".
202 * @param updatedStorageManager Optional updated storage manager; used to replace mStorageManager if is different (and not NULL)
203 */
204 public void swapDirectory(OCFile directory, FileDataStorageManager updatedStorageManager) {
205 mFile = directory;
206 if (updatedStorageManager != null && updatedStorageManager != mStorageManager) {
207 mStorageManager = updatedStorageManager;
208 mAccount = AccountUtils.getCurrentOwnCloudAccount(mContext);
209 }
210 if (mStorageManager != null) {
211 mFiles = mStorageManager.getFolderContent(mFile);
212 } else {
213 mFiles = null;
214 }
215 notifyDataSetChanged();
216 }
217
218 }