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