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