c144ab89d754b648e6ac2fc7552192866b1d94b8
[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 as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 */
19 package com.owncloud.android.ui.adapter;
20
21 import java.util.Vector;
22
23 import com.owncloud.android.AccountUtils;
24 import com.owncloud.android.DisplayUtils;
25 import com.owncloud.android.datamodel.DataStorageManager;
26 import com.owncloud.android.datamodel.OCFile;
27 import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
28 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
29 import com.owncloud.android.ui.activity.TransferServiceGetter;
30
31 import com.owncloud.android.R;
32
33 import android.accounts.Account;
34 import android.content.Context;
35 import android.view.LayoutInflater;
36 import android.view.View;
37 import android.view.ViewGroup;
38 import android.widget.BaseAdapter;
39 import android.widget.ImageView;
40 import android.widget.ListAdapter;
41 import android.widget.ListView;
42 import android.widget.TextView;
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 DataStorageManager mStorageManager;
56 private Account mAccount;
57 private TransferServiceGetter mTransferServiceGetter;
58
59 public FileListListAdapter(OCFile file, DataStorageManager storage_man,
60 Context context, TransferServiceGetter transferServiceGetter) {
61 mStorageManager = storage_man;
62 mContext = context;
63 mAccount = AccountUtils.getCurrentOwnCloudAccount(mContext);
64 mTransferServiceGetter = transferServiceGetter;
65 swapDirectory(file, mStorageManager);
66 /*mFile = file;
67 mFiles = mStorageManager.getDirectoryContent(mFile);*/
68 }
69
70 @Override
71 public boolean areAllItemsEnabled() {
72 return true;
73 }
74
75 @Override
76 public boolean isEnabled(int position) {
77 return true;
78 }
79
80 @Override
81 public int getCount() {
82 return mFiles != null ? mFiles.size() : 0;
83 }
84
85 @Override
86 public Object getItem(int position) {
87 if (mFiles == null || mFiles.size() <= position)
88 return null;
89 return mFiles.get(position);
90 }
91
92 @Override
93 public long getItemId(int position) {
94 if (mFiles == null || mFiles.size() <= position)
95 return 0;
96 return mFiles.get(position).getFileId();
97 }
98
99 @Override
100 public int getItemViewType(int position) {
101 return 0;
102 }
103
104 @Override
105 public View getView(int position, View convertView, ViewGroup parent) {
106 View view = convertView;
107 if (view == null) {
108 LayoutInflater inflator = (LayoutInflater) mContext
109 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
110 view = inflator.inflate(R.layout.list_item, null);
111 }
112 if (mFiles != null && mFiles.size() > position) {
113 OCFile file = mFiles.get(position);
114 TextView fileName = (TextView) view.findViewById(R.id.Filename);
115 String name = file.getFileName();
116
117 fileName.setText(name);
118 ImageView fileIcon = (ImageView) view.findViewById(R.id.imageView1);
119 fileIcon.setImageResource(DisplayUtils.getResourceId(file.getMimetype()));
120 ImageView localStateView = (ImageView) view.findViewById(R.id.imageView2);
121 FileDownloaderBinder downloaderBinder = mTransferServiceGetter.getFileDownloaderBinder();
122 FileUploaderBinder uploaderBinder = mTransferServiceGetter.getFileUploaderBinder();
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
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.isDirectory()) {
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 } else {
167 fileSizeV.setVisibility(View.GONE);
168 lastModV.setVisibility(View.GONE);
169 checkBoxV.setVisibility(View.GONE);
170 view.findViewById(R.id.imageView3).setVisibility(View.GONE);
171 }
172 }
173
174 return view;
175 }
176
177 @Override
178 public int getViewTypeCount() {
179 return 1;
180 }
181
182 @Override
183 public boolean hasStableIds() {
184 return true;
185 }
186
187 @Override
188 public boolean isEmpty() {
189 return (mFiles == null || mFiles.isEmpty());
190 }
191
192 /**
193 * Change the adapted directory for a new one
194 * @param directory New file to adapt. Can be NULL, meaning "no content to adapt".
195 * @param updatedStorageManager Optional updated storage manager; used to replace mStorageManager if is different (and not NULL)
196 */
197 public void swapDirectory(OCFile directory, DataStorageManager updatedStorageManager) {
198 mFile = directory;
199 if (updatedStorageManager != null && updatedStorageManager != mStorageManager) {
200 mStorageManager = updatedStorageManager;
201 mAccount = AccountUtils.getCurrentOwnCloudAccount(mContext);
202 }
203 if (mStorageManager != null) {
204 mFiles = mStorageManager.getDirectoryContent(mFile);
205 } else {
206 mFiles = null;
207 }
208 notifyDataSetChanged();
209 }
210
211 }