932fa513cfa515734a51319bf0a9e1fb77183cae
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / ui / fragment / FileDetailFragment.java
1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
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 eu.alefzero.owncloud.ui.fragment;
19
20 import java.util.List;
21
22 import android.accounts.Account;
23 import android.accounts.AccountManager;
24 import android.content.BroadcastReceiver;
25 import android.content.Context;
26 import android.content.Intent;
27 import android.content.IntentFilter;
28 import android.content.pm.PackageManager;
29 import android.graphics.Bitmap;
30 import android.graphics.BitmapFactory;
31 import android.net.Uri;
32 import android.os.Bundle;
33 import android.util.Log;
34 import android.view.LayoutInflater;
35 import android.view.View;
36 import android.view.View.OnClickListener;
37 import android.view.ViewGroup;
38 import android.widget.Button;
39 import android.widget.ImageView;
40 import android.widget.TextView;
41 import android.widget.Toast;
42
43 import com.actionbarsherlock.app.SherlockFragment;
44
45 import eu.alefzero.owncloud.DisplayUtils;
46 import eu.alefzero.owncloud.R;
47 import eu.alefzero.owncloud.authenticator.AccountAuthenticator;
48 import eu.alefzero.owncloud.datamodel.OCFile;
49 import eu.alefzero.owncloud.files.services.FileDownloader;
50 import eu.alefzero.owncloud.utils.OwnCloudVersion;
51
52 /**
53 * This Fragment is used to display the details about a file.
54 *
55 * @author Bartek Przybylski
56 *
57 */
58 public class FileDetailFragment extends SherlockFragment implements
59 OnClickListener {
60
61 public static final String EXTRA_FILE = "FILE";
62
63 private DownloadFinishReceiver mDownloadFinishReceiver;
64 private Intent mIntent;
65 private int mLayout;
66 private View mView;
67 private OCFile mFile;
68 private static final String TAG = "FileDetailFragment";
69
70 /**
71 * Default constructor - contains real layout
72 */
73 public FileDetailFragment(){
74 mLayout = R.layout.file_details_fragment;
75 }
76
77 /**
78 * Creates a dummy layout. For use if the user never has
79 * tapped on a file before
80 *
81 * @param useEmptyView If true, use empty layout
82 */
83 public FileDetailFragment(boolean useEmptyView){
84 if(useEmptyView){
85 mLayout = R.layout.file_details_empty;
86 } else {
87 mLayout = R.layout.file_details_fragment;
88 }
89 }
90
91 /**
92 * Use this when creating the fragment and display
93 * a file at the same time
94 *
95 * @param showDetailsIntent The Intent with the required parameters
96 * @see FileDetailFragment#updateFileDetails(Intent)
97 */
98 public FileDetailFragment(Intent showDetailsIntent) {
99 mIntent = showDetailsIntent;
100 mLayout = R.layout.file_details_fragment;
101 }
102
103 @Override
104 public void onResume() {
105 super.onResume();
106 mDownloadFinishReceiver = new DownloadFinishReceiver();
107 IntentFilter filter = new IntentFilter(
108 FileDownloader.DOWNLOAD_FINISH_MESSAGE);
109 getActivity().registerReceiver(mDownloadFinishReceiver, filter);
110 }
111
112 @Override
113 public void onPause() {
114 super.onPause();
115 getActivity().unregisterReceiver(mDownloadFinishReceiver);
116 mDownloadFinishReceiver = null;
117 }
118
119 @Override
120 public View onCreateView(LayoutInflater inflater, ViewGroup container,
121 Bundle savedInstanceState) {
122 View view = null;
123 view = inflater.inflate(mLayout, container, false);
124 mView = view;
125 if(mLayout == R.layout.file_details_fragment){
126 // Phones will launch an activity with this intent
127 if(mIntent == null){
128 mIntent = getActivity().getIntent();
129 }
130 updateFileDetails();
131 }
132
133 return view;
134 }
135
136 @Override
137 public View getView() {
138 return super.getView() == null ? mView : super.getView();
139 }
140
141 @Override
142 public void onClick(View v) {
143 Toast.makeText(getActivity(), "Downloading", Toast.LENGTH_LONG).show();
144 Intent i = new Intent(getActivity(), FileDownloader.class);
145 i.putExtra(FileDownloader.EXTRA_ACCOUNT,
146 mIntent.getParcelableExtra(FileDownloader.EXTRA_ACCOUNT));
147 i.putExtra(FileDownloader.EXTRA_FILE_PATH, mFile.getRemotePath());
148 i.putExtra(FileDownloader.EXTRA_FILE_SIZE, mFile.getFileLength());
149 getActivity().startService(i);
150 }
151
152 /**
153 * Can be used to get the file that is currently being displayed.
154 * @return The file on the screen.
155 */
156 public OCFile getDisplayedFile(){
157 return mFile;
158 }
159
160 /**
161 * Use this method to signal this Activity that it shall update its view.
162 *
163 * @param intent The {@link Intent} that contains extra information about
164 * this file The intent needs to have these extras:
165 * <p>
166 *
167 * {@link FileDetailFragment#EXTRA_FILE}: An {@link OCFile}
168 * {@link FileDownloader#EXTRA_ACCOUNT}: The Account that file
169 * belongs to (required for downloading)
170 */
171 public void updateFileDetails(Intent intent) {
172 mIntent = intent;
173 updateFileDetails();
174 }
175
176 /**
177 * Updates the view with all relevant details about that file.
178 */
179 private void updateFileDetails() {
180 mFile = mIntent.getParcelableExtra(EXTRA_FILE);
181 Button downloadButton = (Button) getView().findViewById(R.id.fdDownloadBtn);
182
183 if (mFile != null) {
184 // set file details
185 setFilename(mFile.getFileName());
186 setFiletype(DisplayUtils.convertMIMEtoPrettyPrint(mFile
187 .getMimetype()));
188 setFilesize(mFile.getFileLength());
189 if(ocVersionSupportsTimeCreated()){
190 setTimeCreated(mFile.getCreationTimestamp());
191 }
192
193 setTimeModified(mFile.getModificationTimestamp());
194
195 // Update preview
196 if (mFile.getStoragePath() != null) {
197 try {
198 if (mFile.getMimetype().startsWith("image/")) {
199 ImageView preview = (ImageView) getView().findViewById(
200 R.id.fdPreview);
201 Bitmap bmp = BitmapFactory.decodeFile(mFile.getStoragePath());
202 preview.setImageBitmap(bmp);
203 }
204 } catch (OutOfMemoryError e) {
205 Log.e(TAG, "Out of memory occured for file with size " + mFile.getFileLength());
206 }
207 downloadButton.setText(R.string.filedetails_open);
208 downloadButton.setOnClickListener(new OnClickListener() {
209 @Override
210 public void onClick(View v) {
211 Intent i = new Intent(Intent.ACTION_VIEW);
212 i.setDataAndType(Uri.parse("file://"+mFile.getStoragePath()), mFile.getMimetype());
213 List list = getActivity().getPackageManager().queryIntentActivities(i, PackageManager.MATCH_DEFAULT_ONLY);
214 if (list.size() > 0) {
215 startActivity(i);
216 } else {
217 Toast.makeText(getActivity(), "There is no application to handle file " + mFile.getFileName(), Toast.LENGTH_SHORT).show();
218 }
219 }
220 });
221 } else {
222 // Make download button effective
223 downloadButton.setOnClickListener(this);
224 }
225 }
226 }
227
228 /**
229 * Updates the filename in view
230 * @param filename to set
231 */
232 private void setFilename(String filename) {
233 TextView tv = (TextView) getView().findViewById(R.id.fdFilename);
234 if (tv != null)
235 tv.setText(filename);
236 }
237
238 /**
239 * Updates the MIME type in view
240 * @param mimetype to set
241 */
242 private void setFiletype(String mimetype) {
243 TextView tv = (TextView) getView().findViewById(R.id.fdType);
244 if (tv != null)
245 tv.setText(mimetype);
246 }
247
248 /**
249 * Updates the file size in view
250 * @param filesize in bytes to set
251 */
252 private void setFilesize(long filesize) {
253 TextView tv = (TextView) getView().findViewById(R.id.fdSize);
254 if (tv != null)
255 tv.setText(DisplayUtils.bytesToHumanReadable(filesize));
256 }
257
258 /**
259 * Updates the time that the file was created in view
260 * @param milliseconds Unix time to set
261 */
262 private void setTimeCreated(long milliseconds){
263 TextView tv = (TextView) getView().findViewById(R.id.fdCreated);
264 TextView tvLabel = (TextView) getView().findViewById(R.id.fdCreatedLabel);
265 if(tv != null){
266 tv.setText(DisplayUtils.unixTimeToHumanReadable(milliseconds));
267 tv.setVisibility(View.VISIBLE);
268 tvLabel.setVisibility(View.VISIBLE);
269 }
270 }
271
272 /**
273 * Updates the time that the file was last modified
274 * @param milliseconds Unix time to set
275 */
276 private void setTimeModified(long milliseconds){
277 TextView tv = (TextView) getView().findViewById(R.id.fdModified);
278 if(tv != null){
279 tv.setText(DisplayUtils.unixTimeToHumanReadable(milliseconds));
280 }
281 }
282
283 /**
284 * In ownCloud 3.0.3 and 4.0.0 there is a bug that SabreDAV does not return
285 * the time that the file was created. There is a chance that this will
286 * be fixed in future versions. Use this method to check if this version of
287 * ownCloud has this fix.
288 * @return True, if ownCloud the ownCloud version is > 3.0.4 and 4.0.1
289 */
290 private boolean ocVersionSupportsTimeCreated(){
291 if(mIntent != null){
292 Account ocAccount = mIntent.getParcelableExtra(FileDownloader.EXTRA_ACCOUNT);
293 if(ocAccount != null){
294 AccountManager accManager = (AccountManager) getActivity().getSystemService(Context.ACCOUNT_SERVICE);
295 OwnCloudVersion ocVersion = new OwnCloudVersion(accManager
296 .getUserData(ocAccount, AccountAuthenticator.KEY_OC_VERSION));
297 if(ocVersion.compareTo(new OwnCloudVersion(0x030004)) >= 0 || ocVersion.compareTo(new OwnCloudVersion(0x040001)) >= 0){
298 return true;
299 }
300 }
301 }
302 return false;
303 }
304
305 /**
306 * Once the file download has finished -> update view
307 * @author Bartek Przybylski
308 */
309 private class DownloadFinishReceiver extends BroadcastReceiver {
310 @Override
311 public void onReceive(Context context, Intent intent) {
312 ((OCFile)mIntent.getParcelableExtra(EXTRA_FILE)).setStoragePath(intent.getStringExtra(FileDownloader.EXTRA_FILE_PATH));
313 updateFileDetails(mIntent);
314 }
315
316 }
317
318 }