408154acc63e7bd67d26a9f659abd1bb80fb8d3d
[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 java.util.Vector;
21
22 import android.accounts.Account;
23 import android.content.Context;
24 import android.view.LayoutInflater;
25 import android.view.View;
26 import android.view.ViewGroup;
27 import android.widget.BaseAdapter;
28 import android.widget.ImageView;
29 import android.widget.ListAdapter;
30 import android.widget.ListView;
31 import android.widget.TextView;
32
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
42
43 /**
44 * This Adapter populates a ListView with all files and folders in an ownCloud
45 * instance.
46 *
47 * @author Bartek Przybylski
48 *
49 */
50 public class FileListListAdapter extends BaseAdapter implements ListAdapter {
51 private final static String PERMISSION_SHARED_WITH_ME = "S";
52
53 private Context mContext;
54 private OCFile mFile = null;
55 private Vector<OCFile> mFiles = null;
56
57 private FileDataStorageManager mStorageManager;
58 private Account mAccount;
59 private ComponentsGetter mTransferServiceGetter;
60
61 public FileListListAdapter(Context context, ComponentsGetter transferServiceGetter) {
62 mContext = context;
63 mAccount = AccountUtils.getCurrentOwnCloudAccount(mContext);
64 mTransferServiceGetter = transferServiceGetter;
65 }
66
67 @Override
68 public boolean areAllItemsEnabled() {
69 return true;
70 }
71
72 @Override
73 public boolean isEnabled(int position) {
74 return true;
75 }
76
77 @Override
78 public int getCount() {
79 return mFiles != null ? mFiles.size() : 0;
80 }
81
82 @Override
83 public Object getItem(int position) {
84 if (mFiles == null || mFiles.size() <= position)
85 return null;
86 return mFiles.get(position);
87 }
88
89 @Override
90 public long getItemId(int position) {
91 if (mFiles == null || mFiles.size() <= position)
92 return 0;
93 return mFiles.get(position).getFileId();
94 }
95
96 @Override
97 public int getItemViewType(int position) {
98 return 0;
99 }
100
101 @Override
102 public View getView(int position, View convertView, ViewGroup parent) {
103 View view = convertView;
104 if (view == null) {
105 LayoutInflater inflator = (LayoutInflater) mContext
106 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
107 view = inflator.inflate(R.layout.list_item, null);
108 }
109
110 if (mFiles != null && mFiles.size() > position) {
111 OCFile file = mFiles.get(position);
112 TextView fileName = (TextView) view.findViewById(R.id.Filename);
113 String name = file.getFileName();
114
115 fileName.setText(name);
116 ImageView fileIcon = (ImageView) view.findViewById(R.id.imageView1);
117 ImageView sharedIconV = (ImageView) view.findViewById(R.id.sharedIcon);
118 ImageView sharedWithMeIconV = (ImageView) view.findViewById(R.id.sharedWithMeIcon);
119 sharedWithMeIconV.setVisibility(View.GONE);
120
121 ImageView localStateView = (ImageView) view.findViewById(R.id.imageView2);
122 FileDownloaderBinder downloaderBinder = mTransferServiceGetter.getFileDownloaderBinder();
123 FileUploaderBinder uploaderBinder = mTransferServiceGetter.getFileUploaderBinder();
124 if (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, file)) {
125 localStateView.setImageResource(R.drawable.downloading_file_indicator);
126 localStateView.setVisibility(View.VISIBLE);
127 } else if (uploaderBinder != null && uploaderBinder.isUploading(mAccount, file)) {
128 localStateView.setImageResource(R.drawable.uploading_file_indicator);
129 localStateView.setVisibility(View.VISIBLE);
130 } else if (file.isDown()) {
131 localStateView.setImageResource(R.drawable.local_file_indicator);
132 localStateView.setVisibility(View.VISIBLE);
133 } else {
134 localStateView.setVisibility(View.INVISIBLE);
135 }
136
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);
140
141 if (!file.isFolder()) {
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);
150 } else {
151 view.findViewById(R.id.imageView3).setVisibility(View.VISIBLE);
152 }
153
154 ListView parentList = (ListView)parent;
155 if (parentList.getChoiceMode() == ListView.CHOICE_MODE_NONE) {
156 checkBoxV.setVisibility(View.GONE);
157 } else {
158 if (parentList.isItemChecked(position)) {
159 checkBoxV.setImageResource(android.R.drawable.checkbox_on_background);
160 } else {
161 checkBoxV.setImageResource(android.R.drawable.checkbox_off_background);
162 }
163 checkBoxV.setVisibility(View.VISIBLE);
164 }
165
166 fileIcon.setImageResource(DisplayUtils.getResourceId(file.getMimetype(), file.getFileName()));
167
168 if (checkIfFileIsSharedWithMe(file)) {
169 sharedWithMeIconV.setVisibility(View.VISIBLE);
170 }
171 }
172 else {
173
174 fileSizeV.setVisibility(View.INVISIBLE);
175 //fileSizeV.setText(DisplayUtils.bytesToHumanReadable(file.getFileLength()));
176 lastModV.setVisibility(View.VISIBLE);
177 lastModV.setText(DisplayUtils.unixTimeToHumanReadable(file.getModificationTimestamp()));
178 checkBoxV.setVisibility(View.GONE);
179 view.findViewById(R.id.imageView3).setVisibility(View.GONE);
180
181 if (checkIfFileIsSharedWithMe(file)) {
182 fileIcon.setImageResource(R.drawable.shared_with_me_folder);
183 sharedWithMeIconV.setVisibility(View.VISIBLE);
184 } else {
185 fileIcon.setImageResource(DisplayUtils.getResourceId(file.getMimetype(), file.getFileName()));
186 }
187 }
188
189 if (file.isShareByLink()) {
190 sharedIconV.setVisibility(View.VISIBLE);
191 } else {
192 sharedIconV.setVisibility(View.GONE);
193 }
194 }
195
196 return view;
197 }
198
199 @Override
200 public int getViewTypeCount() {
201 return 1;
202 }
203
204 @Override
205 public boolean hasStableIds() {
206 return true;
207 }
208
209 @Override
210 public boolean isEmpty() {
211 return (mFiles == null || mFiles.isEmpty());
212 }
213
214 /**
215 * Change the adapted directory for a new one
216 * @param directory New file to adapt. Can be NULL, meaning "no content to adapt".
217 * @param updatedStorageManager Optional updated storage manager; used to replace mStorageManager if is different (and not NULL)
218 */
219 public void swapDirectory(OCFile directory, FileDataStorageManager updatedStorageManager) {
220 mFile = directory;
221 if (updatedStorageManager != null && updatedStorageManager != mStorageManager) {
222 mStorageManager = updatedStorageManager;
223 mAccount = AccountUtils.getCurrentOwnCloudAccount(mContext);
224 }
225 if (mStorageManager != null) {
226 mFiles = mStorageManager.getFolderContent(mFile);
227 } else {
228 mFiles = null;
229 }
230 notifyDataSetChanged();
231 }
232
233 /**
234 * Check if parent folder does not include 'S' permission and if file/folder
235 * is shared with me
236 *
237 * @param file: OCFile
238 * @return boolean: True if it is shared with me and false if it is not
239 */
240 private boolean checkIfFileIsSharedWithMe(OCFile file) {
241 return (mFile.getPermissions() != null && !mFile.getPermissions().contains(PERMISSION_SHARED_WITH_ME)
242 && file.getPermissions() != null && file.getPermissions().contains(PERMISSION_SHARED_WITH_ME));
243 }
244 }