Merge branch 'develop' into accounts_view
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / fragment / FileDetailFragment.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.fragment;
19
20 import java.lang.ref.WeakReference;
21
22 import android.accounts.Account;
23 import android.content.Intent;
24 import android.os.Bundle;
25 import android.view.LayoutInflater;
26 import android.view.View;
27 import android.view.View.OnClickListener;
28 import android.view.ViewGroup;
29 import android.widget.CheckBox;
30 import android.widget.ImageView;
31 import android.widget.ProgressBar;
32 import android.widget.TextView;
33
34 import com.actionbarsherlock.view.Menu;
35 import com.actionbarsherlock.view.MenuInflater;
36 import com.actionbarsherlock.view.MenuItem;
37 import com.owncloud.android.R;
38 import com.owncloud.android.datamodel.FileDataStorageManager;
39 import com.owncloud.android.datamodel.OCFile;
40 import com.owncloud.android.files.FileMenuFilter;
41 import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
42 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
43 import com.owncloud.android.lib.common.network.OnDatatransferProgressListener;
44 import com.owncloud.android.services.observer.FileObserverService;
45 import com.owncloud.android.ui.activity.FileActivity;
46 import com.owncloud.android.ui.activity.FileDisplayActivity;
47 import com.owncloud.android.ui.dialog.RemoveFileDialogFragment;
48 import com.owncloud.android.ui.dialog.RenameFileDialogFragment;
49 import com.owncloud.android.utils.DisplayUtils;
50 import com.owncloud.android.utils.Log_OC;
51
52
53 /**
54 * This Fragment is used to display the details about a file.
55 *
56 * @author Bartek Przybylski
57 * @author David A. Velasco
58 */
59 public class FileDetailFragment extends FileFragment implements OnClickListener {
60
61 private int mLayout;
62 private View mView;
63 private Account mAccount;
64
65 public ProgressListener mProgressListener;
66
67 private static final String TAG = FileDetailFragment.class.getSimpleName();
68 public static final String FTAG_CONFIRMATION = "REMOVE_CONFIRMATION_FRAGMENT";
69 public static final String FTAG_RENAME_FILE = "RENAME_FILE_FRAGMENT";
70
71
72 /**
73 * Creates an empty details fragment.
74 *
75 * It's necessary to keep a public constructor without parameters; the system uses it when tries to reinstantiate a fragment automatically.
76 */
77 public FileDetailFragment() {
78 super();
79 mAccount = null;
80 mLayout = R.layout.file_details_empty;
81 mProgressListener = null;
82 }
83
84 /**
85 * Creates a details fragment.
86 *
87 * When 'fileToDetail' or 'ocAccount' are null, creates a dummy layout (to use when a file wasn't tapped before).
88 *
89 * @param fileToDetail An {@link OCFile} to show in the fragment
90 * @param ocAccount An ownCloud account; needed to start downloads
91 */
92 public FileDetailFragment(OCFile fileToDetail, Account ocAccount) {
93 super(fileToDetail);
94 mAccount = ocAccount;
95 mLayout = R.layout.file_details_empty;
96 mProgressListener = null;
97 }
98
99
100 @Override
101 public void onActivityCreated(Bundle savedInstanceState) {
102 super.onCreate(savedInstanceState);
103 setHasOptionsMenu(true);
104 }
105
106
107 @Override
108 public View onCreateView(LayoutInflater inflater, ViewGroup container,
109 Bundle savedInstanceState) {
110
111 if (savedInstanceState != null) {
112 setFile((OCFile)savedInstanceState.getParcelable(FileActivity.EXTRA_FILE));
113 mAccount = savedInstanceState.getParcelable(FileActivity.EXTRA_ACCOUNT);
114 }
115
116 if(getFile() != null && mAccount != null) {
117 mLayout = R.layout.file_details_fragment;
118 }
119
120 View view = null;
121 view = inflater.inflate(mLayout, null);
122 mView = view;
123
124 if (mLayout == R.layout.file_details_fragment) {
125 mView.findViewById(R.id.fdKeepInSync).setOnClickListener(this);
126 ProgressBar progressBar = (ProgressBar)mView.findViewById(R.id.fdProgressBar);
127 mProgressListener = new ProgressListener(progressBar);
128 mView.findViewById(R.id.fdCancelBtn).setOnClickListener(this);
129 }
130
131 updateFileDetails(false, false);
132 return view;
133 }
134
135 @Override
136 public void onSaveInstanceState(Bundle outState) {
137 super.onSaveInstanceState(outState);
138 outState.putParcelable(FileActivity.EXTRA_FILE, getFile());
139 outState.putParcelable(FileActivity.EXTRA_ACCOUNT, mAccount);
140 }
141
142 @Override
143 public void onStart() {
144 super.onStart();
145 listenForTransferProgress();
146 }
147
148 @Override
149 public void onStop() {
150 leaveTransferProgress();
151 super.onStop();
152 }
153
154
155 @Override
156 public View getView() {
157 return super.getView() == null ? mView : super.getView();
158 }
159
160
161 /**
162 * {@inheritDoc}
163 */
164 @Override
165 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
166 super.onCreateOptionsMenu(menu, inflater);
167 inflater.inflate(R.menu.file_actions_menu, menu);
168 }
169
170
171 /**
172 * {@inheritDoc}
173 */
174 @Override
175 public void onPrepareOptionsMenu (Menu menu) {
176 super.onPrepareOptionsMenu(menu);
177
178 if (mContainerActivity.getStorageManager() != null) {
179 FileMenuFilter mf = new FileMenuFilter(
180 getFile(),
181 mContainerActivity.getStorageManager().getAccount(),
182 mContainerActivity,
183 getSherlockActivity()
184 );
185 mf.filter(menu);
186 }
187
188 // additional restriction for this fragment
189 MenuItem item = menu.findItem(R.id.action_see_details);
190 if (item != null) {
191 item.setVisible(false);
192 item.setEnabled(false);
193 }
194 }
195
196
197 /**
198 * {@inheritDoc}
199 */
200 @Override
201 public boolean onOptionsItemSelected(MenuItem item) {
202 switch (item.getItemId()) {
203 case R.id.action_share_file: {
204 mContainerActivity.getFileOperationsHelper().shareFileWithLink(getFile());
205 return true;
206 }
207 case R.id.action_unshare_file: {
208 mContainerActivity.getFileOperationsHelper().unshareFileWithLink(getFile());
209 return true;
210 }
211 case R.id.action_open_file_with: {
212 mContainerActivity.getFileOperationsHelper().openFile(getFile());
213 return true;
214 }
215 case R.id.action_remove_file: {
216 RemoveFileDialogFragment dialog = RemoveFileDialogFragment.newInstance(getFile());
217 dialog.show(getFragmentManager(), FTAG_CONFIRMATION);
218 return true;
219 }
220 case R.id.action_rename_file: {
221 RenameFileDialogFragment dialog = RenameFileDialogFragment.newInstance(getFile());
222 dialog.show(getFragmentManager(), FTAG_RENAME_FILE);
223 return true;
224 }
225 case R.id.action_cancel_download:
226 case R.id.action_cancel_upload: {
227 ((FileDisplayActivity)mContainerActivity).cancelTransference(getFile());
228 return true;
229 }
230 case R.id.action_download_file:
231 case R.id.action_sync_file: {
232 mContainerActivity.getFileOperationsHelper().syncFile(getFile());
233 return true;
234 }
235 case R.id.action_send_file: {
236 // Obtain the file
237 if (!getFile().isDown()) { // Download the file
238 Log_OC.d(TAG, getFile().getRemotePath() + " : File must be downloaded");
239 ((FileDisplayActivity)mContainerActivity).startDownloadForSending(getFile());
240
241 } else {
242 mContainerActivity.getFileOperationsHelper().sendDownloadedFile(getFile());
243 }
244 return true;
245 }
246 default:
247 return false;
248 }
249 }
250
251 @Override
252 public void onClick(View v) {
253 switch (v.getId()) {
254 case R.id.fdKeepInSync: {
255 toggleKeepInSync();
256 break;
257 }
258 case R.id.fdCancelBtn: {
259 ((FileDisplayActivity)mContainerActivity).cancelTransference(getFile());
260 break;
261 }
262 default:
263 Log_OC.e(TAG, "Incorrect view clicked!");
264 }
265 }
266
267
268 private void toggleKeepInSync() {
269 CheckBox cb = (CheckBox) getView().findViewById(R.id.fdKeepInSync);
270 OCFile file = getFile();
271 file.setKeepInSync(cb.isChecked());
272 mContainerActivity.getStorageManager().saveFile(file);
273
274 /// register the OCFile instance in the observer service to monitor local updates
275 Intent observedFileIntent = FileObserverService.makeObservedFileIntent(
276 getActivity(),
277 file,
278 mAccount,
279 cb.isChecked());
280 getActivity().startService(observedFileIntent);
281
282 /// immediate content synchronization
283 if (file.keepInSync()) {
284 mContainerActivity.getFileOperationsHelper().syncFile(getFile());
285 }
286 }
287
288 /**
289 * Check if the fragment was created with an empty layout. An empty fragment can't show file details, must be replaced.
290 *
291 * @return True when the fragment was created with the empty layout.
292 */
293 public boolean isEmpty() {
294 return (mLayout == R.layout.file_details_empty || getFile() == null || mAccount == null);
295 }
296
297
298 /**
299 * Use this method to signal this Activity that it shall update its view.
300 *
301 * @param file : An {@link OCFile}
302 */
303 public void updateFileDetails(OCFile file, Account ocAccount) {
304 setFile(file);
305 mAccount = ocAccount;
306 updateFileDetails(false, false);
307 }
308
309 /**
310 * Updates the view with all relevant details about that file.
311 *
312 * TODO Remove parameter when the transferring state of files is kept in database.
313 *
314 * @param transferring Flag signaling if the file should be considered as downloading or uploading,
315 * although {@link FileDownloaderBinder#isDownloading(Account, OCFile)} and
316 * {@link FileUploaderBinder#isUploading(Account, OCFile)} return false.
317 *
318 * @param refresh If 'true', try to refresh the whole file from the database
319 */
320 public void updateFileDetails(boolean transferring, boolean refresh) {
321 if (readyToShow()) {
322 FileDataStorageManager storageManager = mContainerActivity.getStorageManager();
323 if (refresh && storageManager != null) {
324 setFile(storageManager.getFileByPath(getFile().getRemotePath()));
325 }
326 OCFile file = getFile();
327
328 // set file details
329 setFilename(file.getFileName());
330 setFiletype(file.getMimetype(), file.getFileName());
331 setFilesize(file.getFileLength());
332 if(ocVersionSupportsTimeCreated()){
333 setTimeCreated(file.getCreationTimestamp());
334 }
335
336 setTimeModified(file.getModificationTimestamp());
337
338 CheckBox cb = (CheckBox)getView().findViewById(R.id.fdKeepInSync);
339 cb.setChecked(file.keepInSync());
340
341 // configure UI for depending upon local state of the file
342 FileDownloaderBinder downloaderBinder = mContainerActivity.getFileDownloaderBinder();
343 FileUploaderBinder uploaderBinder = mContainerActivity.getFileUploaderBinder();
344 if (transferring || (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, file)) || (uploaderBinder != null && uploaderBinder.isUploading(mAccount, file))) {
345 setButtonsForTransferring();
346
347 } else if (file.isDown()) {
348
349 setButtonsForDown();
350
351 } else {
352 // TODO load default preview image; when the local file is removed, the preview remains there
353 setButtonsForRemote();
354 }
355 }
356 getView().invalidate();
357 }
358
359 /**
360 * Checks if the fragment is ready to show details of a OCFile
361 *
362 * @return 'True' when the fragment is ready to show details of a file
363 */
364 private boolean readyToShow() {
365 return (getFile() != null && mAccount != null && mLayout == R.layout.file_details_fragment);
366 }
367
368
369 /**
370 * Updates the filename in view
371 * @param filename to set
372 */
373 private void setFilename(String filename) {
374 TextView tv = (TextView) getView().findViewById(R.id.fdFilename);
375 if (tv != null)
376 tv.setText(filename);
377 }
378
379 /**
380 * Updates the MIME type in view
381 * @param mimetype to set
382 * @param filename
383 */
384 private void setFiletype(String mimetype, String filename) {
385 TextView tv = (TextView) getView().findViewById(R.id.fdType);
386 if (tv != null) {
387 String printableMimetype = DisplayUtils.convertMIMEtoPrettyPrint(mimetype);;
388 tv.setText(printableMimetype);
389 }
390 ImageView iv = (ImageView) getView().findViewById(R.id.fdIcon);
391 if (iv != null) {
392 iv.setImageResource(DisplayUtils.getResourceId(mimetype, filename));
393 }
394 }
395
396 /**
397 * Updates the file size in view
398 * @param filesize in bytes to set
399 */
400 private void setFilesize(long filesize) {
401 TextView tv = (TextView) getView().findViewById(R.id.fdSize);
402 if (tv != null)
403 tv.setText(DisplayUtils.bytesToHumanReadable(filesize));
404 }
405
406 /**
407 * Updates the time that the file was created in view
408 * @param milliseconds Unix time to set
409 */
410 private void setTimeCreated(long milliseconds){
411 TextView tv = (TextView) getView().findViewById(R.id.fdCreated);
412 TextView tvLabel = (TextView) getView().findViewById(R.id.fdCreatedLabel);
413 if(tv != null){
414 tv.setText(DisplayUtils.unixTimeToHumanReadable(milliseconds));
415 tv.setVisibility(View.VISIBLE);
416 tvLabel.setVisibility(View.VISIBLE);
417 }
418 }
419
420 /**
421 * Updates the time that the file was last modified
422 * @param milliseconds Unix time to set
423 */
424 private void setTimeModified(long milliseconds){
425 TextView tv = (TextView) getView().findViewById(R.id.fdModified);
426 if(tv != null){
427 tv.setText(DisplayUtils.unixTimeToHumanReadable(milliseconds));
428 }
429 }
430
431 /**
432 * Enables or disables buttons for a file being downloaded
433 */
434 private void setButtonsForTransferring() {
435 if (!isEmpty()) {
436 // let's protect the user from himself ;)
437 getView().findViewById(R.id.fdKeepInSync).setEnabled(false);
438
439 // show the progress bar for the transfer
440 getView().findViewById(R.id.fdProgressBlock).setVisibility(View.VISIBLE);
441 TextView progressText = (TextView)getView().findViewById(R.id.fdProgressText);
442 progressText.setVisibility(View.VISIBLE);
443 FileDownloaderBinder downloaderBinder = mContainerActivity.getFileDownloaderBinder();
444 FileUploaderBinder uploaderBinder = mContainerActivity.getFileUploaderBinder();
445 if (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, getFile())) {
446 progressText.setText(R.string.downloader_download_in_progress_ticker);
447 } else if (uploaderBinder != null && uploaderBinder.isUploading(mAccount, getFile())) {
448 progressText.setText(R.string.uploader_upload_in_progress_ticker);
449 }
450 }
451 }
452
453 /**
454 * Enables or disables buttons for a file locally available
455 */
456 private void setButtonsForDown() {
457 if (!isEmpty()) {
458 getView().findViewById(R.id.fdKeepInSync).setEnabled(true);
459
460 // hides the progress bar
461 getView().findViewById(R.id.fdProgressBlock).setVisibility(View.GONE);
462 TextView progressText = (TextView)getView().findViewById(R.id.fdProgressText);
463 progressText.setVisibility(View.GONE);
464 }
465 }
466
467 /**
468 * Enables or disables buttons for a file not locally available
469 */
470 private void setButtonsForRemote() {
471 if (!isEmpty()) {
472 getView().findViewById(R.id.fdKeepInSync).setEnabled(true);
473
474 // hides the progress bar
475 getView().findViewById(R.id.fdProgressBlock).setVisibility(View.GONE);
476 TextView progressText = (TextView)getView().findViewById(R.id.fdProgressText);
477 progressText.setVisibility(View.GONE);
478 }
479 }
480
481
482 /**
483 * In ownCloud 3.X.X and 4.X.X there is a bug that SabreDAV does not return
484 * the time that the file was created. There is a chance that this will
485 * be fixed in future versions. Use this method to check if this version of
486 * ownCloud has this fix.
487 * @return True, if ownCloud the ownCloud version is supporting creation time
488 */
489 private boolean ocVersionSupportsTimeCreated(){
490 /*if(mAccount != null){
491 AccountManager accManager = (AccountManager) getActivity().getSystemService(Context.ACCOUNT_SERVICE);
492 OwnCloudVersion ocVersion = new OwnCloudVersion(accManager
493 .getUserData(mAccount, AccountAuthenticator.KEY_OC_VERSION));
494 if(ocVersion.compareTo(new OwnCloudVersion(0x030000)) < 0) {
495 return true;
496 }
497 }*/
498 return false;
499 }
500
501
502 public void listenForTransferProgress() {
503 if (mProgressListener != null) {
504 if (mContainerActivity.getFileDownloaderBinder() != null) {
505 mContainerActivity.getFileDownloaderBinder().addDatatransferProgressListener(mProgressListener, mAccount, getFile());
506 }
507 if (mContainerActivity.getFileUploaderBinder() != null) {
508 mContainerActivity.getFileUploaderBinder().addDatatransferProgressListener(mProgressListener, mAccount, getFile());
509 }
510 }
511 }
512
513
514 public void leaveTransferProgress() {
515 if (mProgressListener != null) {
516 if (mContainerActivity.getFileDownloaderBinder() != null) {
517 mContainerActivity.getFileDownloaderBinder().removeDatatransferProgressListener(mProgressListener, mAccount, getFile());
518 }
519 if (mContainerActivity.getFileUploaderBinder() != null) {
520 mContainerActivity.getFileUploaderBinder().removeDatatransferProgressListener(mProgressListener, mAccount, getFile());
521 }
522 }
523 }
524
525
526
527 /**
528 * Helper class responsible for updating the progress bar shown for file uploading or downloading
529 *
530 * @author David A. Velasco
531 */
532 private class ProgressListener implements OnDatatransferProgressListener {
533 int mLastPercent = 0;
534 WeakReference<ProgressBar> mProgressBar = null;
535
536 ProgressListener(ProgressBar progressBar) {
537 mProgressBar = new WeakReference<ProgressBar>(progressBar);
538 }
539
540 @Override
541 public void onTransferProgress(long progressRate, long totalTransferredSoFar, long totalToTransfer, String filename) {
542 int percent = (int)(100.0*((double)totalTransferredSoFar)/((double)totalToTransfer));
543 if (percent != mLastPercent) {
544 ProgressBar pb = mProgressBar.get();
545 if (pb != null) {
546 pb.setProgress(percent);
547 pb.postInvalidate();
548 }
549 }
550 mLastPercent = percent;
551 }
552
553 };
554
555 }