5ab922c3e80db77d37906f81da3c6d12a0ef8860
[pub/Android/ownCloud.git] / src / de / mobilcom / debitel / cloud / 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 de.mobilcom.debitel.cloud.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 import de.mobilcom.debitel.cloud.android.DisplayUtils;
32 import de.mobilcom.debitel.cloud.android.R;
33 import de.mobilcom.debitel.cloud.android.authentication.AccountUtils;
34 import de.mobilcom.debitel.cloud.android.datamodel.DataStorageManager;
35 import de.mobilcom.debitel.cloud.android.datamodel.OCFile;
36 import de.mobilcom.debitel.cloud.android.files.services.FileDownloader.FileDownloaderBinder;
37 import de.mobilcom.debitel.cloud.android.files.services.FileUploader.FileUploaderBinder;
38 import de.mobilcom.debitel.cloud.android.ui.activity.TransferServiceGetter;
39
40 import java.util.Vector;
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 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(Context context, TransferServiceGetter transferServiceGetter) {
59 mContext = context;
60 mAccount = AccountUtils.getCurrentOwnCloudAccount(mContext);
61 mTransferServiceGetter = transferServiceGetter;
62 }
63
64 @Override
65 public boolean areAllItemsEnabled() {
66 return true;
67 }
68
69 @Override
70 public boolean isEnabled(int position) {
71 return true;
72 }
73
74 @Override
75 public int getCount() {
76 return mFiles != null ? mFiles.size() : 0;
77 }
78
79 @Override
80 public Object getItem(int position) {
81 if (mFiles == null || mFiles.size() <= position)
82 return null;
83 return mFiles.get(position);
84 }
85
86 @Override
87 public long getItemId(int position) {
88 if (mFiles == null || mFiles.size() <= position)
89 return 0;
90 return mFiles.get(position).getFileId();
91 }
92
93 @Override
94 public int getItemViewType(int position) {
95 return 0;
96 }
97
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 (mFiles != null && mFiles.size() > position) {
108 OCFile file = mFiles.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 fileIcon.setImageResource(DisplayUtils.getResourceId(file.getMimetype()));
115 ImageView localStateView = (ImageView) view.findViewById(R.id.imageView2);
116 FileDownloaderBinder downloaderBinder = mTransferServiceGetter.getFileDownloaderBinder();
117 FileUploaderBinder uploaderBinder = mTransferServiceGetter.getFileUploaderBinder();
118 if (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, file)) {
119 localStateView.setImageResource(R.drawable.downloading_file_indicator);
120 localStateView.setVisibility(View.VISIBLE);
121 } else if (uploaderBinder != null && uploaderBinder.isUploading(mAccount, file)) {
122 localStateView.setImageResource(R.drawable.uploading_file_indicator);
123 localStateView.setVisibility(View.VISIBLE);
124 } else if (file.isDown()) {
125 localStateView.setImageResource(R.drawable.local_file_indicator);
126 localStateView.setVisibility(View.VISIBLE);
127 } else {
128 localStateView.setVisibility(View.INVISIBLE);
129 }
130
131 TextView fileSizeV = (TextView) view.findViewById(R.id.file_size);
132 TextView lastModV = (TextView) view.findViewById(R.id.last_mod);
133 ImageView checkBoxV = (ImageView) view.findViewById(R.id.custom_checkbox);
134
135 if (!file.isDirectory()) {
136 fileSizeV.setVisibility(View.VISIBLE);
137 fileSizeV.setText(DisplayUtils.bytesToHumanReadable(file.getFileLength()));
138 lastModV.setVisibility(View.VISIBLE);
139 lastModV.setText(DisplayUtils.unixTimeToHumanReadable(file.getModificationTimestamp()));
140 // this if-else is needed even thoe fav icon is visible by default
141 // because android reuses views in listview
142 if (!file.keepInSync()) {
143 view.findViewById(R.id.imageView3).setVisibility(View.GONE);
144 } else {
145 view.findViewById(R.id.imageView3).setVisibility(View.VISIBLE);
146 }
147
148 ListView parentList = (ListView)parent;
149 if (parentList.getChoiceMode() == ListView.CHOICE_MODE_NONE) {
150 checkBoxV.setVisibility(View.GONE);
151 } else {
152 if (parentList.isItemChecked(position)) {
153 checkBoxV.setImageResource(android.R.drawable.checkbox_on_background);
154 } else {
155 checkBoxV.setImageResource(android.R.drawable.checkbox_off_background);
156 }
157 checkBoxV.setVisibility(View.VISIBLE);
158 }
159
160 }
161 else {
162
163 fileSizeV.setVisibility(View.VISIBLE);
164 fileSizeV.setText(DisplayUtils.bytesToHumanReadable(file.getFileLength()));
165 lastModV.setVisibility(View.VISIBLE);
166 lastModV.setText(DisplayUtils.unixTimeToHumanReadable(file.getModificationTimestamp()));
167 checkBoxV.setVisibility(View.GONE);
168 view.findViewById(R.id.imageView3).setVisibility(View.GONE);
169 }
170 }
171
172 return view;
173 }
174
175 @Override
176 public int getViewTypeCount() {
177 return 1;
178 }
179
180 @Override
181 public boolean hasStableIds() {
182 return true;
183 }
184
185 @Override
186 public boolean isEmpty() {
187 return (mFiles == null || mFiles.isEmpty());
188 }
189
190 /**
191 * Change the adapted directory for a new one
192 * @param directory New file to adapt. Can be NULL, meaning "no content to adapt".
193 * @param updatedStorageManager Optional updated storage manager; used to replace mStorageManager if is different (and not NULL)
194 */
195 public void swapDirectory(OCFile directory, DataStorageManager updatedStorageManager) {
196 mFile = directory;
197 if (updatedStorageManager != null && updatedStorageManager != mStorageManager) {
198 mStorageManager = updatedStorageManager;
199 mAccount = AccountUtils.getCurrentOwnCloudAccount(mContext);
200 }
201 if (mStorageManager != null) {
202 mFiles = mStorageManager.getDirectoryContent(mFile);
203 } else {
204 mFiles = null;
205 }
206 notifyDataSetChanged();
207 }
208
209 }