80f535809f9a1941f0dcdf343c26d7451a1cb0a6
[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 java.util.Vector;
21
22 import com.owncloud.android.AccountUtils;
23 import com.owncloud.android.DisplayUtils;
24 import com.owncloud.android.datamodel.DataStorageManager;
25 import com.owncloud.android.datamodel.OCFile;
26 import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
27 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
28 import com.owncloud.android.ui.activity.TransferServiceGetter;
29
30 import com.owncloud.android.R;
31
32 import android.accounts.Account;
33 import android.content.Context;
34 import android.view.LayoutInflater;
35 import android.view.View;
36 import android.view.ViewGroup;
37 import android.widget.BaseAdapter;
38 import android.widget.ImageView;
39 import android.widget.ListAdapter;
40 import android.widget.ListView;
41 import android.widget.TextView;
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 Context mContext;
52 private OCFile mFile = null;
53 private Vector<OCFile> mFiles = null;
54 private DataStorageManager mStorageManager;
55 private Account mAccount;
56 private TransferServiceGetter mTransferServiceGetter;
57
58 public FileListListAdapter(OCFile file, DataStorageManager storage_man,
59 Context context, TransferServiceGetter transferServiceGetter) {
60 mStorageManager = storage_man;
61 mContext = context;
62 mAccount = AccountUtils.getCurrentOwnCloudAccount(mContext);
63 mTransferServiceGetter = transferServiceGetter;
64 swapDirectory(file, mStorageManager);
65 /*mFile = file;
66 mFiles = mStorageManager.getDirectoryContent(mFile);*/
67 }
68
69 @Override
70 public boolean areAllItemsEnabled() {
71 return true;
72 }
73
74 @Override
75 public boolean isEnabled(int position) {
76 return true;
77 }
78
79 @Override
80 public int getCount() {
81 return mFiles != null ? mFiles.size() : 0;
82 }
83
84 @Override
85 public Object getItem(int position) {
86 if (mFiles == null || mFiles.size() <= position)
87 return null;
88 return mFiles.get(position);
89 }
90
91 @Override
92 public long getItemId(int position) {
93 if (mFiles == null || mFiles.size() <= position)
94 return 0;
95 return mFiles.get(position).getFileId();
96 }
97
98 @Override
99 public int getItemViewType(int position) {
100 return 0;
101 }
102
103 @Override
104 public View getView(int position, View convertView, ViewGroup parent) {
105 View view = convertView;
106 if (view == null) {
107 LayoutInflater inflator = (LayoutInflater) mContext
108 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
109 view = inflator.inflate(R.layout.list_item, null);
110 }
111 if (mFiles != null && mFiles.size() > position) {
112 OCFile file = mFiles.get(position);
113 TextView fileName = (TextView) view.findViewById(R.id.Filename);
114 String name = file.getFileName();
115
116 fileName.setText(name);
117 ImageView fileIcon = (ImageView) view.findViewById(R.id.imageView1);
118 fileIcon.setImageResource(DisplayUtils.getResourceId(file.getMimetype()));
119 ImageView localStateView = (ImageView) view.findViewById(R.id.imageView2);
120 FileDownloaderBinder downloaderBinder = mTransferServiceGetter.getFileDownloaderBinder();
121 FileUploaderBinder uploaderBinder = mTransferServiceGetter.getFileUploaderBinder();
122 if (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, file)) {
123 localStateView.setImageResource(R.drawable.downloading_file_indicator);
124 localStateView.setVisibility(View.VISIBLE);
125 } else if (uploaderBinder != null && uploaderBinder.isUploading(mAccount, file)) {
126 localStateView.setImageResource(R.drawable.uploading_file_indicator);
127 localStateView.setVisibility(View.VISIBLE);
128 } else if (file.isDown()) {
129 localStateView.setImageResource(R.drawable.local_file_indicator);
130 localStateView.setVisibility(View.VISIBLE);
131 } else {
132 localStateView.setVisibility(View.INVISIBLE);
133 }
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 if (!file.isDirectory()) {
141 fileSizeV.setVisibility(View.VISIBLE);
142 fileSizeV.setText(DisplayUtils.bytesToHumanReadable(file.getFileLength()));
143 lastModV.setVisibility(View.VISIBLE);
144 lastModV.setText(DisplayUtils.unixTimeToHumanReadable(file.getModificationTimestamp()));
145 // this if-else is needed even thoe fav icon is visible by default
146 // because android reuses views in listview
147 if (!file.keepInSync()) {
148 view.findViewById(R.id.imageView3).setVisibility(View.GONE);
149 } else {
150 view.findViewById(R.id.imageView3).setVisibility(View.VISIBLE);
151 }
152
153 ListView parentList = (ListView)parent;
154 if (parentList.getChoiceMode() == ListView.CHOICE_MODE_NONE) {
155 checkBoxV.setVisibility(View.GONE);
156 } else {
157 if (parentList.isItemChecked(position)) {
158 checkBoxV.setImageResource(android.R.drawable.checkbox_on_background);
159 } else {
160 checkBoxV.setImageResource(android.R.drawable.checkbox_off_background);
161 }
162 checkBoxV.setVisibility(View.VISIBLE);
163 }
164
165 } else {
166 fileSizeV.setVisibility(View.GONE);
167 lastModV.setVisibility(View.GONE);
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, DataStorageManager 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.getDirectoryContent(mFile);
204 } else {
205 mFiles = null;
206 }
207 notifyDataSetChanged();
208 }
209
210 }