Copyright note fixes
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / preview / PreviewMediaFragment.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012-2013 ownCloud Inc.
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 version 2,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 */
17 package com.owncloud.android.ui.preview;
18
19 import java.io.File;
20 import java.util.ArrayList;
21 import java.util.List;
22
23 import android.accounts.Account;
24 import android.app.Activity;
25 import android.app.AlertDialog;
26 import android.content.ActivityNotFoundException;
27 import android.content.ComponentName;
28 import android.content.Context;
29 import android.content.DialogInterface;
30 import android.content.Intent;
31 import android.content.ServiceConnection;
32 import android.media.MediaPlayer;
33 import android.media.MediaPlayer.OnCompletionListener;
34 import android.media.MediaPlayer.OnErrorListener;
35 import android.media.MediaPlayer.OnPreparedListener;
36 import android.net.Uri;
37 import android.os.Build;
38 import android.os.Bundle;
39 import android.os.Handler;
40 import android.os.IBinder;
41 import android.support.v4.app.FragmentTransaction;
42 import android.util.Log;
43 import android.view.LayoutInflater;
44 import android.view.MotionEvent;
45 import android.view.View;
46 import android.view.View.OnTouchListener;
47 import android.view.ViewGroup;
48 import android.webkit.MimeTypeMap;
49 import android.widget.ImageView;
50 import android.widget.Toast;
51 import android.widget.VideoView;
52
53 import com.actionbarsherlock.app.SherlockFragment;
54 import com.actionbarsherlock.view.Menu;
55 import com.actionbarsherlock.view.MenuInflater;
56 import com.actionbarsherlock.view.MenuItem;
57 import com.owncloud.android.datamodel.FileDataStorageManager;
58 import com.owncloud.android.datamodel.OCFile;
59 import com.owncloud.android.media.MediaControlView;
60 import com.owncloud.android.media.MediaService;
61 import com.owncloud.android.media.MediaServiceBinder;
62 import com.owncloud.android.network.OwnCloudClientUtils;
63 import com.owncloud.android.operations.OnRemoteOperationListener;
64 import com.owncloud.android.operations.RemoteOperation;
65 import com.owncloud.android.operations.RemoteOperationResult;
66 import com.owncloud.android.operations.RemoveFileOperation;
67 import com.owncloud.android.ui.activity.FileDetailActivity;
68 import com.owncloud.android.ui.activity.FileDisplayActivity;
69 import com.owncloud.android.ui.fragment.ConfirmationDialogFragment;
70 import com.owncloud.android.ui.fragment.FileDetailFragment;
71 import com.owncloud.android.ui.fragment.FileFragment;
72
73 import com.owncloud.android.R;
74 import eu.alefzero.webdav.WebdavClient;
75 import eu.alefzero.webdav.WebdavUtils;
76
77 /**
78 * This fragment shows a preview of a downloaded media file (audio or video).
79 *
80 * Trying to get an instance with NULL {@link OCFile} or ownCloud {@link Account} values will produce an {@link IllegalStateException}.
81 *
82 * By now, if the {@link OCFile} passed is not downloaded, an {@link IllegalStateException} is generated on instantiation too.
83 *
84 * @author David A. Velasco
85 */
86 public class PreviewMediaFragment extends SherlockFragment implements
87 OnTouchListener , FileFragment,
88 ConfirmationDialogFragment.ConfirmationDialogFragmentListener, OnRemoteOperationListener {
89
90 public static final String EXTRA_FILE = "FILE";
91 public static final String EXTRA_ACCOUNT = "ACCOUNT";
92 private static final String EXTRA_PLAY_POSITION = "PLAY_POSITION";
93 private static final String EXTRA_PLAYING = "PLAYING";
94
95 private View mView;
96 private OCFile mFile;
97 private Account mAccount;
98 private FileDataStorageManager mStorageManager;
99 private ImageView mImagePreview;
100 private VideoView mVideoPreview;
101 private int mSavedPlaybackPosition;
102
103 private Handler mHandler;
104 private RemoteOperation mLastRemoteOperation;
105
106 private MediaServiceBinder mMediaServiceBinder = null;
107 private MediaControlView mMediaController = null;
108 private MediaServiceConnection mMediaServiceConnection = null;
109 private VideoHelper mVideoHelper;
110 private boolean mAutoplay;
111
112 private static final String TAG = PreviewMediaFragment.class.getSimpleName();
113
114
115 /**
116 * Creates a fragment to preview a file.
117 *
118 * When 'fileToDetail' or 'ocAccount' are null
119 *
120 * @param fileToDetail An {@link OCFile} to preview in the fragment
121 * @param ocAccount An ownCloud account; needed to start downloads
122 */
123 public PreviewMediaFragment(OCFile fileToDetail, Account ocAccount) {
124 mFile = fileToDetail;
125 mAccount = ocAccount;
126 mSavedPlaybackPosition = 0;
127 mStorageManager = null; // we need a context to init this; the container activity is not available yet at this moment
128 mAutoplay = true;
129 }
130
131
132 /**
133 * Creates an empty fragment for previews.
134 *
135 * MUST BE KEPT: the system uses it when tries to reinstantiate a fragment automatically (for instance, when the device is turned a aside).
136 *
137 * DO NOT CALL IT: an {@link OCFile} and {@link Account} must be provided for a successful construction
138 */
139 public PreviewMediaFragment() {
140 mFile = null;
141 mAccount = null;
142 mSavedPlaybackPosition = 0;
143 mStorageManager = null;
144 mAutoplay = true;
145 }
146
147
148 /**
149 * {@inheritDoc}
150 */
151 @Override
152 public void onCreate(Bundle savedInstanceState) {
153 super.onCreate(savedInstanceState);
154 mHandler = new Handler();
155 setHasOptionsMenu(true);
156 }
157
158
159 /**
160 * {@inheritDoc}
161 */
162 @Override
163 public View onCreateView(LayoutInflater inflater, ViewGroup container,
164 Bundle savedInstanceState) {
165 super.onCreateView(inflater, container, savedInstanceState);
166
167 mView = inflater.inflate(R.layout.file_preview, container, false);
168
169 mImagePreview = (ImageView)mView.findViewById(R.id.image_preview);
170 mVideoPreview = (VideoView)mView.findViewById(R.id.video_preview);
171 mVideoPreview.setOnTouchListener(this);
172
173 mMediaController = (MediaControlView)mView.findViewById(R.id.media_controller);
174
175 return mView;
176 }
177
178
179 /**
180 * {@inheritDoc}
181 */
182 @Override
183 public void onAttach(Activity activity) {
184 super.onAttach(activity);
185 if (!(activity instanceof FileFragment.ContainerActivity))
186 throw new ClassCastException(activity.toString() + " must implement " + FileFragment.ContainerActivity.class.getSimpleName());
187 }
188
189
190 /**
191 * {@inheritDoc}
192 */
193 @Override
194 public void onActivityCreated(Bundle savedInstanceState) {
195 super.onActivityCreated(savedInstanceState);
196
197 mStorageManager = new FileDataStorageManager(mAccount, getActivity().getApplicationContext().getContentResolver());
198 if (savedInstanceState != null) {
199 mFile = savedInstanceState.getParcelable(PreviewMediaFragment.EXTRA_FILE);
200 mAccount = savedInstanceState.getParcelable(PreviewMediaFragment.EXTRA_ACCOUNT);
201 mSavedPlaybackPosition = savedInstanceState.getInt(PreviewMediaFragment.EXTRA_PLAY_POSITION);
202 mAutoplay = savedInstanceState.getBoolean(PreviewMediaFragment.EXTRA_PLAYING);
203
204 }
205 if (mFile == null) {
206 throw new IllegalStateException("Instanced with a NULL OCFile");
207 }
208 if (mAccount == null) {
209 throw new IllegalStateException("Instanced with a NULL ownCloud Account");
210 }
211 if (!mFile.isDown()) {
212 throw new IllegalStateException("There is no local file to preview");
213 }
214 if (mFile.isVideo()) {
215 mVideoPreview.setVisibility(View.VISIBLE);
216 mImagePreview.setVisibility(View.GONE);
217 prepareVideo();
218
219 } else {
220 mVideoPreview.setVisibility(View.GONE);
221 mImagePreview.setVisibility(View.VISIBLE);
222 }
223
224 }
225
226
227 /**
228 * {@inheritDoc}
229 */
230 @Override
231 public void onSaveInstanceState(Bundle outState) {
232 super.onSaveInstanceState(outState);
233 outState.putParcelable(PreviewMediaFragment.EXTRA_FILE, mFile);
234 outState.putParcelable(PreviewMediaFragment.EXTRA_ACCOUNT, mAccount);
235
236 if (mFile.isVideo()) {
237 mSavedPlaybackPosition = mVideoPreview.getCurrentPosition();
238 mAutoplay = mVideoPreview.isPlaying();
239 outState.putInt(PreviewMediaFragment.EXTRA_PLAY_POSITION , mSavedPlaybackPosition);
240 outState.putBoolean(PreviewMediaFragment.EXTRA_PLAYING , mAutoplay);
241 } else {
242 outState.putInt(PreviewMediaFragment.EXTRA_PLAY_POSITION , mMediaServiceBinder.getCurrentPosition());
243 outState.putBoolean(PreviewMediaFragment.EXTRA_PLAYING , mMediaServiceBinder.isPlaying());
244 }
245 }
246
247
248 @Override
249 public void onStart() {
250 super.onStart();
251
252 if (mFile != null) {
253 if (mFile.isAudio()) {
254 bindMediaService();
255
256 } else if (mFile.isVideo()) {
257 stopAudio();
258 playVideo();
259 }
260 }
261 }
262
263
264 private void stopAudio() {
265 Intent i = new Intent(getSherlockActivity(), MediaService.class);
266 i.setAction(MediaService.ACTION_STOP_ALL);
267 getSherlockActivity().startService(i);
268 }
269
270
271 /**
272 * {@inheritDoc}
273 */
274 @Override
275 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
276 super.onCreateOptionsMenu(menu, inflater);
277
278 inflater.inflate(R.menu.file_actions_menu, menu);
279 List<Integer> toHide = new ArrayList<Integer>();
280
281 MenuItem item = null;
282 toHide.add(R.id.action_cancel_download);
283 toHide.add(R.id.action_cancel_upload);
284 toHide.add(R.id.action_download_file);
285 toHide.add(R.id.action_rename_file); // by now
286
287 for (int i : toHide) {
288 item = menu.findItem(i);
289 if (item != null) {
290 item.setVisible(false);
291 item.setEnabled(false);
292 }
293 }
294
295 }
296
297
298 /**
299 * {@inheritDoc}
300 */
301 @Override
302 public boolean onOptionsItemSelected(MenuItem item) {
303 switch (item.getItemId()) {
304 case R.id.action_open_file_with: {
305 openFile();
306 return true;
307 }
308 case R.id.action_remove_file: {
309 removeFile();
310 return true;
311 }
312 case R.id.action_see_details: {
313 seeDetails();
314 return true;
315 }
316
317 default:
318 return false;
319 }
320 }
321
322
323 private void seeDetails() {
324 stopPreview(false);
325 ((FileFragment.ContainerActivity)getActivity()).showFragmentWithDetails(mFile);
326 }
327
328
329 private void prepareVideo() {
330 // create helper to get more control on the playback
331 mVideoHelper = new VideoHelper();
332 mVideoPreview.setOnPreparedListener(mVideoHelper);
333 mVideoPreview.setOnCompletionListener(mVideoHelper);
334 mVideoPreview.setOnErrorListener(mVideoHelper);
335 }
336
337 private void playVideo() {
338 // create and prepare control panel for the user
339 mMediaController.setMediaPlayer(mVideoPreview);
340
341 // load the video file in the video player ; when done, VideoHelper#onPrepared() will be called
342 mVideoPreview.setVideoPath(mFile.getStoragePath());
343 }
344
345
346 private class VideoHelper implements OnCompletionListener, OnPreparedListener, OnErrorListener {
347
348 /**
349 * Called when the file is ready to be played.
350 *
351 * Just starts the playback.
352 *
353 * @param mp {@link MediaPlayer} instance performing the playback.
354 */
355 @Override
356 public void onPrepared(MediaPlayer vp) {
357 Log.e(TAG, "onPrepared");
358 mVideoPreview.seekTo(mSavedPlaybackPosition);
359 if (mAutoplay) {
360 mVideoPreview.start();
361 }
362 mMediaController.setEnabled(true);
363 mMediaController.updatePausePlay();
364 }
365
366
367 /**
368 * Called when the file is finished playing.
369 *
370 * Finishes the activity.
371 *
372 * @param mp {@link MediaPlayer} instance performing the playback.
373 */
374 @Override
375 public void onCompletion(MediaPlayer mp) {
376 Log.e(TAG, "completed");
377 if (mp != null) {
378 mVideoPreview.seekTo(0);
379 // next lines are necessary to work around undesired video loops
380 if (Build.VERSION.SDK_INT == Build.VERSION_CODES.GINGERBREAD) {
381 mVideoPreview.pause();
382
383 } else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.GINGERBREAD_MR1) {
384 // mVideePreview.pause() is not enough
385
386 mMediaController.setEnabled(false);
387 mVideoPreview.stopPlayback();
388 mAutoplay = false;
389 mSavedPlaybackPosition = 0;
390 mVideoPreview.setVideoPath(mFile.getStoragePath());
391 }
392 } // else : called from onError()
393 mMediaController.updatePausePlay();
394 }
395
396
397 /**
398 * Called when an error in playback occurs.
399 *
400 * @param mp {@link MediaPlayer} instance performing the playback.
401 * @param what Type of error
402 * @param extra Extra code specific to the error
403 */
404 @Override
405 public boolean onError(MediaPlayer mp, int what, int extra) {
406 if (mVideoPreview.getWindowToken() != null) {
407 String message = MediaService.getMessageForMediaError(getActivity(), what, extra);
408 new AlertDialog.Builder(getActivity())
409 .setMessage(message)
410 .setPositiveButton(android.R.string.VideoView_error_button,
411 new DialogInterface.OnClickListener() {
412 public void onClick(DialogInterface dialog, int whichButton) {
413 dialog.dismiss();
414 VideoHelper.this.onCompletion(null);
415 }
416 })
417 .setCancelable(false)
418 .show();
419 }
420 return true;
421 }
422
423 }
424
425
426 @Override
427 public void onStop() {
428 super.onStop();
429
430 if (mMediaServiceConnection != null) {
431 Log.d(TAG, "Unbinding from MediaService ...");
432 if (mMediaServiceBinder != null && mMediaController != null) {
433 mMediaServiceBinder.unregisterMediaController(mMediaController);
434 }
435 getActivity().unbindService(mMediaServiceConnection);
436 mMediaServiceConnection = null;
437 mMediaServiceBinder = null;
438 }
439 }
440
441 @Override
442 public boolean onTouch(View v, MotionEvent event) {
443 if (event.getAction() == MotionEvent.ACTION_DOWN && v == mVideoPreview) {
444 startFullScreenVideo();
445 return true;
446 }
447 return false;
448 }
449
450
451 private void startFullScreenVideo() {
452 Intent i = new Intent(getActivity(), PreviewVideoActivity.class);
453 i.putExtra(PreviewVideoActivity.EXTRA_ACCOUNT, mAccount);
454 i.putExtra(PreviewVideoActivity.EXTRA_FILE, mFile);
455 i.putExtra(PreviewVideoActivity.EXTRA_AUTOPLAY, mVideoPreview.isPlaying());
456 mVideoPreview.pause();
457 i.putExtra(PreviewVideoActivity.EXTRA_START_POSITION, mVideoPreview.getCurrentPosition());
458 startActivityForResult(i, 0);
459 }
460
461
462 @Override
463 public void onActivityResult (int requestCode, int resultCode, Intent data) {
464 super.onActivityResult(requestCode, resultCode, data);
465 if (resultCode == Activity.RESULT_OK) {
466 mSavedPlaybackPosition = data.getExtras().getInt(PreviewVideoActivity.EXTRA_START_POSITION);
467 mAutoplay = data.getExtras().getBoolean(PreviewVideoActivity.EXTRA_AUTOPLAY);
468 }
469 }
470
471
472 private void playAudio() {
473 if (!mMediaServiceBinder.isPlaying(mFile)) {
474 Log.d(TAG, "starting playback of " + mFile.getStoragePath());
475 mMediaServiceBinder.start(mAccount, mFile, mAutoplay, mSavedPlaybackPosition);
476
477 } else {
478 if (!mMediaServiceBinder.isPlaying() && mAutoplay) {
479 mMediaServiceBinder.start();
480 mMediaController.updatePausePlay();
481 }
482 }
483 }
484
485
486 private void bindMediaService() {
487 Log.d(TAG, "Binding to MediaService...");
488 if (mMediaServiceConnection == null) {
489 mMediaServiceConnection = new MediaServiceConnection();
490 }
491 getActivity().bindService( new Intent(getActivity(),
492 MediaService.class),
493 mMediaServiceConnection,
494 Context.BIND_AUTO_CREATE);
495 // follow the flow in MediaServiceConnection#onServiceConnected(...)
496 }
497
498 /** Defines callbacks for service binding, passed to bindService() */
499 private class MediaServiceConnection implements ServiceConnection {
500
501 @Override
502 public void onServiceConnected(ComponentName component, IBinder service) {
503 if (component.equals(new ComponentName(getActivity(), MediaService.class))) {
504 Log.d(TAG, "Media service connected");
505 mMediaServiceBinder = (MediaServiceBinder) service;
506 if (mMediaServiceBinder != null) {
507 prepareMediaController();
508 playAudio(); // do not wait for the touch of nobody to play audio
509
510 Log.d(TAG, "Successfully bound to MediaService, MediaController ready");
511
512 } else {
513 Log.e(TAG, "Unexpected response from MediaService while binding");
514 }
515 }
516 }
517
518 private void prepareMediaController() {
519 mMediaServiceBinder.registerMediaController(mMediaController);
520 if (mMediaController != null) {
521 mMediaController.setMediaPlayer(mMediaServiceBinder);
522 mMediaController.setEnabled(true);
523 mMediaController.updatePausePlay();
524 }
525 }
526
527 @Override
528 public void onServiceDisconnected(ComponentName component) {
529 if (component.equals(new ComponentName(getActivity(), MediaService.class))) {
530 Log.e(TAG, "Media service suddenly disconnected");
531 if (mMediaController != null) {
532 mMediaController.setMediaPlayer(null);
533 } else {
534 Toast.makeText(getActivity(), "No media controller to release when disconnected from media service", Toast.LENGTH_SHORT).show();
535 }
536 mMediaServiceBinder = null;
537 mMediaServiceConnection = null;
538 }
539 }
540 }
541
542
543
544 /**
545 * Opens the previewed file with an external application.
546 *
547 * TODO - improve this; instead of prioritize the actions available for the MIME type in the server,
548 * we should get a list of available apps for MIME tpye in the server and join it with the list of
549 * available apps for the MIME type known from the file extension, to let the user choose
550 */
551 private void openFile() {
552 stopPreview(true);
553 String storagePath = mFile.getStoragePath();
554 String encodedStoragePath = WebdavUtils.encodePath(storagePath);
555 try {
556 Intent i = new Intent(Intent.ACTION_VIEW);
557 i.setDataAndType(Uri.parse("file://"+ encodedStoragePath), mFile.getMimetype());
558 i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
559 startActivity(i);
560
561 } catch (Throwable t) {
562 Log.e(TAG, "Fail when trying to open with the mimeType provided from the ownCloud server: " + mFile.getMimetype());
563 boolean toastIt = true;
564 String mimeType = "";
565 try {
566 Intent i = new Intent(Intent.ACTION_VIEW);
567 mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(storagePath.substring(storagePath.lastIndexOf('.') + 1));
568 if (mimeType == null || !mimeType.equals(mFile.getMimetype())) {
569 if (mimeType != null) {
570 i.setDataAndType(Uri.parse("file://"+ encodedStoragePath), mimeType);
571 } else {
572 // desperate try
573 i.setDataAndType(Uri.parse("file://"+ encodedStoragePath), "*-/*");
574 }
575 i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
576 startActivity(i);
577 toastIt = false;
578 }
579
580 } catch (IndexOutOfBoundsException e) {
581 Log.e(TAG, "Trying to find out MIME type of a file without extension: " + storagePath);
582
583 } catch (ActivityNotFoundException e) {
584 Log.e(TAG, "No activity found to handle: " + storagePath + " with MIME type " + mimeType + " obtained from extension");
585
586 } catch (Throwable th) {
587 Log.e(TAG, "Unexpected problem when opening: " + storagePath, th);
588
589 } finally {
590 if (toastIt) {
591 Toast.makeText(getActivity(), "There is no application to handle file " + mFile.getFileName(), Toast.LENGTH_SHORT).show();
592 }
593 }
594
595 }
596 finish();
597 }
598
599 /**
600 * Starts a the removal of the previewed file.
601 *
602 * Shows a confirmation dialog. The action continues in {@link #onConfirmation(String)} , {@link #onNeutral(String)} or {@link #onCancel(String)},
603 * depending upon the user selection in the dialog.
604 */
605 private void removeFile() {
606 ConfirmationDialogFragment confDialog = ConfirmationDialogFragment.newInstance(
607 R.string.confirmation_remove_alert,
608 new String[]{mFile.getFileName()},
609 R.string.confirmation_remove_remote_and_local,
610 R.string.confirmation_remove_local,
611 R.string.common_cancel);
612 confDialog.setOnConfirmationListener(this);
613 confDialog.show(getFragmentManager(), ConfirmationDialogFragment.FTAG_CONFIRMATION);
614 }
615
616
617 /**
618 * Performs the removal of the previewed file, both locally and in the server.
619 */
620 @Override
621 public void onConfirmation(String callerTag) {
622 if (mStorageManager.getFileById(mFile.getFileId()) != null) { // check that the file is still there;
623 stopPreview(true);
624 mLastRemoteOperation = new RemoveFileOperation( mFile, // TODO we need to review the interface with RemoteOperations, and use OCFile IDs instead of OCFile objects as parameters
625 true,
626 mStorageManager);
627 WebdavClient wc = OwnCloudClientUtils.createOwnCloudClient(mAccount, getSherlockActivity().getApplicationContext());
628 mLastRemoteOperation.execute(wc, this, mHandler);
629
630 boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity;
631 getActivity().showDialog((inDisplayActivity)? FileDisplayActivity.DIALOG_SHORT_WAIT : FileDetailActivity.DIALOG_SHORT_WAIT);
632 }
633 }
634
635
636 /**
637 * Removes the file from local storage
638 */
639 @Override
640 public void onNeutral(String callerTag) {
641 // TODO this code should be made in a secondary thread,
642 if (mFile.isDown()) { // checks it is still there
643 stopPreview(true);
644 File f = new File(mFile.getStoragePath());
645 f.delete();
646 mFile.setStoragePath(null);
647 mStorageManager.saveFile(mFile);
648 finish();
649 }
650 }
651
652 /**
653 * User cancelled the removal action.
654 */
655 @Override
656 public void onCancel(String callerTag) {
657 // nothing to do here
658 }
659
660
661 /**
662 * {@inheritDoc}
663 */
664 public OCFile getFile(){
665 return mFile;
666 }
667
668 /*
669 /**
670 * Use this method to signal this Activity that it shall update its view.
671 *
672 * @param file : An {@link OCFile}
673 *-/
674 public void updateFileDetails(OCFile file, Account ocAccount) {
675 mFile = file;
676 if (ocAccount != null && (
677 mStorageManager == null ||
678 (mAccount != null && !mAccount.equals(ocAccount))
679 )) {
680 mStorageManager = new FileDataStorageManager(ocAccount, getActivity().getApplicationContext().getContentResolver());
681 }
682 mAccount = ocAccount;
683 updateFileDetails(false);
684 }
685 */
686
687
688 /**
689 * Helper method to test if an {@link OCFile} can be passed to a {@link PreviewMediaFragment} to be previewed.
690 *
691 * @param file File to test if can be previewed.
692 * @return 'True' if the file can be handled by the fragment.
693 */
694 public static boolean canBePreviewed(OCFile file) {
695 return (file != null && (file.isAudio() || file.isVideo()));
696 }
697
698 /**
699 * {@inheritDoc}
700 */
701 @Override
702 public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) {
703 if (operation.equals(mLastRemoteOperation)) {
704 if (operation instanceof RemoveFileOperation) {
705 onRemoveFileOperationFinish((RemoveFileOperation)operation, result);
706 }
707 }
708 }
709
710 private void onRemoveFileOperationFinish(RemoveFileOperation operation, RemoteOperationResult result) {
711 boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity;
712 getActivity().dismissDialog((inDisplayActivity)? FileDisplayActivity.DIALOG_SHORT_WAIT : FileDetailActivity.DIALOG_SHORT_WAIT);
713
714 if (result.isSuccess()) {
715 Toast msg = Toast.makeText(getActivity().getApplicationContext(), R.string.remove_success_msg, Toast.LENGTH_LONG);
716 msg.show();
717 finish();
718
719 } else {
720 Toast msg = Toast.makeText(getActivity(), R.string.remove_fail_msg, Toast.LENGTH_LONG);
721 msg.show();
722 if (result.isSslRecoverableException()) {
723 // TODO show the SSL warning dialog
724 }
725 }
726 }
727
728 private void stopPreview(boolean stopAudio) {
729 if (mFile.isAudio() && stopAudio) {
730 mMediaServiceBinder.pause();
731
732 } else if (mFile.isVideo()) {
733 mVideoPreview.stopPlayback();
734 }
735 }
736
737
738
739 /**
740 * Finishes the preview
741 */
742 private void finish() {
743 Activity container = getActivity();
744 if (container instanceof FileDisplayActivity) {
745 // double pane
746 FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
747 transaction.replace(R.id.file_details_container, new FileDetailFragment(null, null), FileDetailFragment.FTAG); // empty FileDetailFragment
748 transaction.commit();
749 ((FileFragment.ContainerActivity)container).onFileStateChanged();
750 } else {
751 container.finish();
752 }
753 }
754
755 }