123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805 |
- /* ownCloud Android client application
- * Copyright (C) 2012-2013 ownCloud Inc.
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2,
- * as published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
- */
- package com.owncloud.android.ui.preview;
- import java.io.File;
- import java.util.ArrayList;
- import java.util.List;
- import android.accounts.Account;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.content.ActivityNotFoundException;
- import android.content.ComponentName;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.content.ServiceConnection;
- import android.content.res.Configuration;
- import android.media.MediaPlayer;
- import android.media.MediaPlayer.OnCompletionListener;
- import android.media.MediaPlayer.OnErrorListener;
- import android.media.MediaPlayer.OnPreparedListener;
- import android.net.Uri;
- import android.os.Build;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.IBinder;
- import android.support.v4.app.FragmentTransaction;
- import android.view.LayoutInflater;
- import android.view.MotionEvent;
- import android.view.View;
- import android.view.View.OnTouchListener;
- import android.view.ViewGroup;
- import android.webkit.MimeTypeMap;
- import android.widget.ImageView;
- import android.widget.Toast;
- import android.widget.VideoView;
- import com.actionbarsherlock.app.SherlockFragment;
- import com.actionbarsherlock.view.Menu;
- import com.actionbarsherlock.view.MenuInflater;
- import com.actionbarsherlock.view.MenuItem;
- import com.owncloud.android.datamodel.FileDataStorageManager;
- import com.owncloud.android.datamodel.OCFile;
- import com.owncloud.android.media.MediaControlView;
- import com.owncloud.android.media.MediaService;
- import com.owncloud.android.media.MediaServiceBinder;
- import com.owncloud.android.operations.OnRemoteOperationListener;
- import com.owncloud.android.operations.RemoteOperation;
- import com.owncloud.android.operations.RemoteOperationResult;
- import com.owncloud.android.operations.RemoveFileOperation;
- import com.owncloud.android.ui.activity.FileDetailActivity;
- import com.owncloud.android.ui.activity.FileDisplayActivity;
- import com.owncloud.android.ui.fragment.ConfirmationDialogFragment;
- import com.owncloud.android.ui.fragment.FileDetailFragment;
- import com.owncloud.android.ui.fragment.FileFragment;
- import com.owncloud.android.Log_OC;
- import com.owncloud.android.R;
- import eu.alefzero.webdav.WebdavUtils;
- /**
- * This fragment shows a preview of a downloaded media file (audio or video).
- *
- * Trying to get an instance with NULL {@link OCFile} or ownCloud {@link Account} values will produce an {@link IllegalStateException}.
- *
- * By now, if the {@link OCFile} passed is not downloaded, an {@link IllegalStateException} is generated on instantiation too.
- *
- * @author David A. Velasco
- */
- public class PreviewMediaFragment extends SherlockFragment implements
- OnTouchListener , FileFragment,
- ConfirmationDialogFragment.ConfirmationDialogFragmentListener, OnRemoteOperationListener {
- public static final String EXTRA_FILE = "FILE";
- public static final String EXTRA_ACCOUNT = "ACCOUNT";
- private static final String EXTRA_PLAY_POSITION = "PLAY_POSITION";
- private static final String EXTRA_PLAYING = "PLAYING";
- private View mView;
- private OCFile mFile;
- private Account mAccount;
- private FileDataStorageManager mStorageManager;
- private ImageView mImagePreview;
- private VideoView mVideoPreview;
- private int mSavedPlaybackPosition;
-
- private Handler mHandler;
- private RemoteOperation mLastRemoteOperation;
-
- private MediaServiceBinder mMediaServiceBinder = null;
- private MediaControlView mMediaController = null;
- private MediaServiceConnection mMediaServiceConnection = null;
- private VideoHelper mVideoHelper;
- private boolean mAutoplay;
- public boolean mPrepared;
-
- private static final String TAG = PreviewMediaFragment.class.getSimpleName();
-
- /**
- * Creates a fragment to preview a file.
- *
- * When 'fileToDetail' or 'ocAccount' are null
- *
- * @param fileToDetail An {@link OCFile} to preview in the fragment
- * @param ocAccount An ownCloud account; needed to start downloads
- */
- public PreviewMediaFragment(OCFile fileToDetail, Account ocAccount, int startPlaybackPosition, boolean autoplay) {
- mFile = fileToDetail;
- mAccount = ocAccount;
- mSavedPlaybackPosition = startPlaybackPosition;
- mStorageManager = null; // we need a context to init this; the container activity is not available yet at this moment
- mAutoplay = autoplay;
- }
-
-
- /**
- * Creates an empty fragment for previews.
- *
- * MUST BE KEPT: the system uses it when tries to reinstantiate a fragment automatically (for instance, when the device is turned a aside).
- *
- * DO NOT CALL IT: an {@link OCFile} and {@link Account} must be provided for a successful construction
- */
- public PreviewMediaFragment() {
- mFile = null;
- mAccount = null;
- mSavedPlaybackPosition = 0;
- mStorageManager = null;
- mAutoplay = true;
- }
-
-
- /**
- * {@inheritDoc}
- */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- mHandler = new Handler();
- setHasOptionsMenu(true);
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- super.onCreateView(inflater, container, savedInstanceState);
- Log_OC.e(TAG, "onCreateView");
-
- mView = inflater.inflate(R.layout.file_preview, container, false);
-
- mImagePreview = (ImageView)mView.findViewById(R.id.image_preview);
- mVideoPreview = (VideoView)mView.findViewById(R.id.video_preview);
- mVideoPreview.setOnTouchListener(this);
-
- mMediaController = (MediaControlView)mView.findViewById(R.id.media_controller);
-
- return mView;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public void onAttach(Activity activity) {
- super.onAttach(activity);
- Log_OC.e(TAG, "onAttach");
-
- if (!(activity instanceof FileFragment.ContainerActivity))
- throw new ClassCastException(activity.toString() + " must implement " + FileFragment.ContainerActivity.class.getSimpleName());
- }
-
-
- /**
- * {@inheritDoc}
- */
- @Override
- public void onActivityCreated(Bundle savedInstanceState) {
- super.onActivityCreated(savedInstanceState);
- Log_OC.e(TAG, "onActivityCreated");
- mStorageManager = new FileDataStorageManager(mAccount, getActivity().getApplicationContext().getContentResolver());
- if (savedInstanceState != null) {
- mFile = savedInstanceState.getParcelable(PreviewMediaFragment.EXTRA_FILE);
- mAccount = savedInstanceState.getParcelable(PreviewMediaFragment.EXTRA_ACCOUNT);
- mSavedPlaybackPosition = savedInstanceState.getInt(PreviewMediaFragment.EXTRA_PLAY_POSITION);
- mAutoplay = savedInstanceState.getBoolean(PreviewMediaFragment.EXTRA_PLAYING);
-
- }
- if (mFile == null) {
- throw new IllegalStateException("Instanced with a NULL OCFile");
- }
- if (mAccount == null) {
- throw new IllegalStateException("Instanced with a NULL ownCloud Account");
- }
- if (!mFile.isDown()) {
- throw new IllegalStateException("There is no local file to preview");
- }
- if (mFile.isVideo()) {
- mVideoPreview.setVisibility(View.VISIBLE);
- mImagePreview.setVisibility(View.GONE);
- prepareVideo();
-
- } else {
- mVideoPreview.setVisibility(View.GONE);
- mImagePreview.setVisibility(View.VISIBLE);
- }
-
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public void onSaveInstanceState(Bundle outState) {
- super.onSaveInstanceState(outState);
- Log_OC.e(TAG, "onSaveInstanceState");
-
- outState.putParcelable(PreviewMediaFragment.EXTRA_FILE, mFile);
- outState.putParcelable(PreviewMediaFragment.EXTRA_ACCOUNT, mAccount);
-
- if (mFile.isVideo()) {
- mSavedPlaybackPosition = mVideoPreview.getCurrentPosition();
- mAutoplay = mVideoPreview.isPlaying();
- outState.putInt(PreviewMediaFragment.EXTRA_PLAY_POSITION , mSavedPlaybackPosition);
- outState.putBoolean(PreviewMediaFragment.EXTRA_PLAYING , mAutoplay);
- } else {
- outState.putInt(PreviewMediaFragment.EXTRA_PLAY_POSITION , mMediaServiceBinder.getCurrentPosition());
- outState.putBoolean(PreviewMediaFragment.EXTRA_PLAYING , mMediaServiceBinder.isPlaying());
- }
- }
-
- @Override
- public void onStart() {
- super.onStart();
- Log_OC.e(TAG, "onStart");
- if (mFile != null) {
- if (mFile.isAudio()) {
- bindMediaService();
-
- } else if (mFile.isVideo()) {
- stopAudio();
- playVideo();
- }
- }
- }
-
-
- private void stopAudio() {
- Intent i = new Intent(getSherlockActivity(), MediaService.class);
- i.setAction(MediaService.ACTION_STOP_ALL);
- getSherlockActivity().startService(i);
- }
- /**
- * {@inheritDoc}
- */
- @Override
- public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
- super.onCreateOptionsMenu(menu, inflater);
- inflater.inflate(R.menu.file_actions_menu, menu);
- List<Integer> toHide = new ArrayList<Integer>();
-
- MenuItem item = null;
- toHide.add(R.id.action_cancel_download);
- toHide.add(R.id.action_cancel_upload);
- toHide.add(R.id.action_download_file);
- toHide.add(R.id.action_sync_file);
- toHide.add(R.id.action_rename_file); // by now
- for (int i : toHide) {
- item = menu.findItem(i);
- if (item != null) {
- item.setVisible(false);
- item.setEnabled(false);
- }
- }
-
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- switch (item.getItemId()) {
- case R.id.action_open_file_with: {
- openFile();
- return true;
- }
- case R.id.action_remove_file: {
- removeFile();
- return true;
- }
- case R.id.action_see_details: {
- seeDetails();
- return true;
- }
-
- default:
- return false;
- }
- }
-
- private void seeDetails() {
- stopPreview(false);
- ((FileFragment.ContainerActivity)getActivity()).showFragmentWithDetails(mFile);
- }
- private void prepareVideo() {
- // create helper to get more control on the playback
- mVideoHelper = new VideoHelper();
- mVideoPreview.setOnPreparedListener(mVideoHelper);
- mVideoPreview.setOnCompletionListener(mVideoHelper);
- mVideoPreview.setOnErrorListener(mVideoHelper);
- }
-
- private void playVideo() {
- // create and prepare control panel for the user
- mMediaController.setMediaPlayer(mVideoPreview);
-
- // load the video file in the video player ; when done, VideoHelper#onPrepared() will be called
- mVideoPreview.setVideoPath(mFile.getStoragePath());
- }
-
- private class VideoHelper implements OnCompletionListener, OnPreparedListener, OnErrorListener {
-
- /**
- * Called when the file is ready to be played.
- *
- * Just starts the playback.
- *
- * @param mp {@link MediaPlayer} instance performing the playback.
- */
- @Override
- public void onPrepared(MediaPlayer vp) {
- Log_OC.e(TAG, "onPrepared");
- mVideoPreview.seekTo(mSavedPlaybackPosition);
- if (mAutoplay) {
- mVideoPreview.start();
- }
- mMediaController.setEnabled(true);
- mMediaController.updatePausePlay();
- mPrepared = true;
- }
-
-
- /**
- * Called when the file is finished playing.
- *
- * Finishes the activity.
- *
- * @param mp {@link MediaPlayer} instance performing the playback.
- */
- @Override
- public void onCompletion(MediaPlayer mp) {
- Log_OC.e(TAG, "completed");
- if (mp != null) {
- mVideoPreview.seekTo(0);
- // next lines are necessary to work around undesired video loops
- if (Build.VERSION.SDK_INT == Build.VERSION_CODES.GINGERBREAD) {
- mVideoPreview.pause();
-
- } else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.GINGERBREAD_MR1) {
- // mVideePreview.pause() is not enough
-
- mMediaController.setEnabled(false);
- mVideoPreview.stopPlayback();
- mAutoplay = false;
- mSavedPlaybackPosition = 0;
- mVideoPreview.setVideoPath(mFile.getStoragePath());
- }
- } // else : called from onError()
- mMediaController.updatePausePlay();
- }
-
-
- /**
- * Called when an error in playback occurs.
- *
- * @param mp {@link MediaPlayer} instance performing the playback.
- * @param what Type of error
- * @param extra Extra code specific to the error
- */
- @Override
- public boolean onError(MediaPlayer mp, int what, int extra) {
- if (mVideoPreview.getWindowToken() != null) {
- String message = MediaService.getMessageForMediaError(getActivity(), what, extra);
- new AlertDialog.Builder(getActivity())
- .setMessage(message)
- .setPositiveButton(android.R.string.VideoView_error_button,
- new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int whichButton) {
- dialog.dismiss();
- VideoHelper.this.onCompletion(null);
- }
- })
- .setCancelable(false)
- .show();
- }
- return true;
- }
-
- }
-
- @Override
- public void onPause() {
- super.onPause();
- Log_OC.e(TAG, "onPause");
- }
-
- @Override
- public void onResume() {
- super.onResume();
- Log_OC.e(TAG, "onResume");
- }
-
- @Override
- public void onDestroy() {
- super.onDestroy();
- Log_OC.e(TAG, "onDestroy");
- }
-
- @Override
- public void onStop() {
- Log_OC.e(TAG, "onStop");
- super.onStop();
- mPrepared = false;
- if (mMediaServiceConnection != null) {
- Log_OC.d(TAG, "Unbinding from MediaService ...");
- if (mMediaServiceBinder != null && mMediaController != null) {
- mMediaServiceBinder.unregisterMediaController(mMediaController);
- }
- getActivity().unbindService(mMediaServiceConnection);
- mMediaServiceConnection = null;
- mMediaServiceBinder = null;
- }
- }
-
- @Override
- public boolean onTouch(View v, MotionEvent event) {
- if (event.getAction() == MotionEvent.ACTION_DOWN && v == mVideoPreview) {
- startFullScreenVideo();
- return true;
- }
- return false;
- }
-
- private void startFullScreenVideo() {
- Intent i = new Intent(getActivity(), PreviewVideoActivity.class);
- i.putExtra(PreviewVideoActivity.EXTRA_ACCOUNT, mAccount);
- i.putExtra(PreviewVideoActivity.EXTRA_FILE, mFile);
- i.putExtra(PreviewVideoActivity.EXTRA_AUTOPLAY, mVideoPreview.isPlaying());
- mVideoPreview.pause();
- i.putExtra(PreviewVideoActivity.EXTRA_START_POSITION, mVideoPreview.getCurrentPosition());
- startActivityForResult(i, 0);
- }
- @Override
- public void onConfigurationChanged (Configuration newConfig) {
- Log_OC.e(TAG, "onConfigurationChanged " + this);
- }
-
- @Override
- public void onActivityResult (int requestCode, int resultCode, Intent data) {
- Log_OC.e(TAG, "onActivityResult " + this);
- super.onActivityResult(requestCode, resultCode, data);
- if (resultCode == Activity.RESULT_OK) {
- mSavedPlaybackPosition = data.getExtras().getInt(PreviewVideoActivity.EXTRA_START_POSITION);
- mAutoplay = data.getExtras().getBoolean(PreviewVideoActivity.EXTRA_AUTOPLAY);
- }
- }
-
- private void playAudio() {
- if (!mMediaServiceBinder.isPlaying(mFile)) {
- Log_OC.d(TAG, "starting playback of " + mFile.getStoragePath());
- mMediaServiceBinder.start(mAccount, mFile, mAutoplay, mSavedPlaybackPosition);
-
- } else {
- if (!mMediaServiceBinder.isPlaying() && mAutoplay) {
- mMediaServiceBinder.start();
- mMediaController.updatePausePlay();
- }
- }
- }
- private void bindMediaService() {
- Log_OC.d(TAG, "Binding to MediaService...");
- if (mMediaServiceConnection == null) {
- mMediaServiceConnection = new MediaServiceConnection();
- }
- getActivity().bindService( new Intent(getActivity(),
- MediaService.class),
- mMediaServiceConnection,
- Context.BIND_AUTO_CREATE);
- // follow the flow in MediaServiceConnection#onServiceConnected(...)
- }
-
- /** Defines callbacks for service binding, passed to bindService() */
- private class MediaServiceConnection implements ServiceConnection {
- @Override
- public void onServiceConnected(ComponentName component, IBinder service) {
- if (component.equals(new ComponentName(getActivity(), MediaService.class))) {
- Log_OC.d(TAG, "Media service connected");
- mMediaServiceBinder = (MediaServiceBinder) service;
- if (mMediaServiceBinder != null) {
- prepareMediaController();
- playAudio(); // do not wait for the touch of nobody to play audio
-
- Log_OC.d(TAG, "Successfully bound to MediaService, MediaController ready");
-
- } else {
- Log_OC.e(TAG, "Unexpected response from MediaService while binding");
- }
- }
- }
- private void prepareMediaController() {
- mMediaServiceBinder.registerMediaController(mMediaController);
- if (mMediaController != null) {
- mMediaController.setMediaPlayer(mMediaServiceBinder);
- mMediaController.setEnabled(true);
- mMediaController.updatePausePlay();
- }
- }
- @Override
- public void onServiceDisconnected(ComponentName component) {
- if (component.equals(new ComponentName(getActivity(), MediaService.class))) {
- Log_OC.e(TAG, "Media service suddenly disconnected");
- if (mMediaController != null) {
- mMediaController.setMediaPlayer(null);
- } else {
- Toast.makeText(getActivity(), "No media controller to release when disconnected from media service", Toast.LENGTH_SHORT).show();
- }
- mMediaServiceBinder = null;
- mMediaServiceConnection = null;
- }
- }
- }
-
- /**
- * Opens the previewed file with an external application.
- *
- * TODO - improve this; instead of prioritize the actions available for the MIME type in the server,
- * we should get a list of available apps for MIME tpye in the server and join it with the list of
- * available apps for the MIME type known from the file extension, to let the user choose
- */
- private void openFile() {
- stopPreview(true);
- String storagePath = mFile.getStoragePath();
- String encodedStoragePath = WebdavUtils.encodePath(storagePath);
- try {
- Intent i = new Intent(Intent.ACTION_VIEW);
- i.setDataAndType(Uri.parse("file://"+ encodedStoragePath), mFile.getMimetype());
- i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
- startActivity(i);
-
- } catch (Throwable t) {
- Log_OC.e(TAG, "Fail when trying to open with the mimeType provided from the ownCloud server: " + mFile.getMimetype());
- boolean toastIt = true;
- String mimeType = "";
- try {
- Intent i = new Intent(Intent.ACTION_VIEW);
- mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(storagePath.substring(storagePath.lastIndexOf('.') + 1));
- if (mimeType == null || !mimeType.equals(mFile.getMimetype())) {
- if (mimeType != null) {
- i.setDataAndType(Uri.parse("file://"+ encodedStoragePath), mimeType);
- } else {
- // desperate try
- i.setDataAndType(Uri.parse("file://"+ encodedStoragePath), "*-/*");
- }
- i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
- startActivity(i);
- toastIt = false;
- }
-
- } catch (IndexOutOfBoundsException e) {
- Log_OC.e(TAG, "Trying to find out MIME type of a file without extension: " + storagePath);
-
- } catch (ActivityNotFoundException e) {
- Log_OC.e(TAG, "No activity found to handle: " + storagePath + " with MIME type " + mimeType + " obtained from extension");
-
- } catch (Throwable th) {
- Log_OC.e(TAG, "Unexpected problem when opening: " + storagePath, th);
-
- } finally {
- if (toastIt) {
- Toast.makeText(getActivity(), "There is no application to handle file " + mFile.getFileName(), Toast.LENGTH_SHORT).show();
- }
- }
-
- }
- finish();
- }
-
- /**
- * Starts a the removal of the previewed file.
- *
- * Shows a confirmation dialog. The action continues in {@link #onConfirmation(String)} , {@link #onNeutral(String)} or {@link #onCancel(String)},
- * depending upon the user selection in the dialog.
- */
- private void removeFile() {
- ConfirmationDialogFragment confDialog = ConfirmationDialogFragment.newInstance(
- R.string.confirmation_remove_alert,
- new String[]{mFile.getFileName()},
- R.string.confirmation_remove_remote_and_local,
- R.string.confirmation_remove_local,
- R.string.common_cancel);
- confDialog.setOnConfirmationListener(this);
- confDialog.show(getFragmentManager(), ConfirmationDialogFragment.FTAG_CONFIRMATION);
- }
-
- /**
- * Performs the removal of the previewed file, both locally and in the server.
- */
- @Override
- public void onConfirmation(String callerTag) {
- if (mStorageManager.getFileById(mFile.getFileId()) != null) { // check that the file is still there;
- stopPreview(true);
- mLastRemoteOperation = new RemoveFileOperation( mFile, // TODO we need to review the interface with RemoteOperations, and use OCFile IDs instead of OCFile objects as parameters
- true,
- mStorageManager);
- mLastRemoteOperation.execute(mAccount, getSherlockActivity(), this, mHandler, getSherlockActivity());
-
- boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity;
- getActivity().showDialog((inDisplayActivity)? FileDisplayActivity.DIALOG_SHORT_WAIT : FileDetailActivity.DIALOG_SHORT_WAIT);
- }
- }
-
-
- /**
- * Removes the file from local storage
- */
- @Override
- public void onNeutral(String callerTag) {
- // TODO this code should be made in a secondary thread,
- if (mFile.isDown()) { // checks it is still there
- stopPreview(true);
- File f = new File(mFile.getStoragePath());
- f.delete();
- mFile.setStoragePath(null);
- mStorageManager.saveFile(mFile);
- finish();
- }
- }
-
- /**
- * User cancelled the removal action.
- */
- @Override
- public void onCancel(String callerTag) {
- // nothing to do here
- }
-
- /**
- * {@inheritDoc}
- */
- public OCFile getFile(){
- return mFile;
- }
-
- /*
- /**
- * Use this method to signal this Activity that it shall update its view.
- *
- * @param file : An {@link OCFile}
- *-/
- public void updateFileDetails(OCFile file, Account ocAccount) {
- mFile = file;
- if (ocAccount != null && (
- mStorageManager == null ||
- (mAccount != null && !mAccount.equals(ocAccount))
- )) {
- mStorageManager = new FileDataStorageManager(ocAccount, getActivity().getApplicationContext().getContentResolver());
- }
- mAccount = ocAccount;
- updateFileDetails(false);
- }
- */
-
- /**
- * Helper method to test if an {@link OCFile} can be passed to a {@link PreviewMediaFragment} to be previewed.
- *
- * @param file File to test if can be previewed.
- * @return 'True' if the file can be handled by the fragment.
- */
- public static boolean canBePreviewed(OCFile file) {
- return (file != null && (file.isAudio() || file.isVideo()));
- }
- /**
- * {@inheritDoc}
- */
- @Override
- public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) {
- if (operation.equals(mLastRemoteOperation)) {
- if (operation instanceof RemoveFileOperation) {
- onRemoveFileOperationFinish((RemoveFileOperation)operation, result);
- }
- }
- }
-
- private void onRemoveFileOperationFinish(RemoveFileOperation operation, RemoteOperationResult result) {
- boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity;
- getActivity().dismissDialog((inDisplayActivity)? FileDisplayActivity.DIALOG_SHORT_WAIT : FileDetailActivity.DIALOG_SHORT_WAIT);
-
- if (result.isSuccess()) {
- Toast msg = Toast.makeText(getActivity().getApplicationContext(), R.string.remove_success_msg, Toast.LENGTH_LONG);
- msg.show();
- finish();
-
- } else {
- Toast msg = Toast.makeText(getActivity(), R.string.remove_fail_msg, Toast.LENGTH_LONG);
- msg.show();
- if (result.isSslRecoverableException()) {
- // TODO show the SSL warning dialog
- }
- }
- }
- private void stopPreview(boolean stopAudio) {
- if (mFile.isAudio() && stopAudio) {
- mMediaServiceBinder.pause();
-
- } else if (mFile.isVideo()) {
- mVideoPreview.stopPlayback();
- }
- }
- /**
- * Finishes the preview
- */
- private void finish() {
- Activity container = getActivity();
- if (container instanceof FileDisplayActivity) {
- // double pane
- FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
- transaction.replace(R.id.file_details_container, new FileDetailFragment(null, null), FileDetailFragment.FTAG); // empty FileDetailFragment
- transaction.commit();
- ((FileFragment.ContainerActivity)container).onFileStateChanged();
- } else {
- container.finish();
- }
- }
- public int getPosition() {
- if (mPrepared) {
- mSavedPlaybackPosition = mVideoPreview.getCurrentPosition();
- }
- Log_OC.e(TAG, "getting position: " + mSavedPlaybackPosition);
- return mSavedPlaybackPosition;
- }
-
- public boolean isPlaying() {
- if (mPrepared) {
- mAutoplay = mVideoPreview.isPlaying();
- }
- return mAutoplay;
- }
-
- }
|