X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/bea36f23b0a22070891f534fa9007d0548d04e37..e901b609baa4dd5f681e2a5257c9e504997e3377:/src/com/owncloud/android/ui/preview/PreviewMediaFragment.java diff --git a/src/com/owncloud/android/ui/preview/PreviewMediaFragment.java b/src/com/owncloud/android/ui/preview/PreviewMediaFragment.java index 8df42c98..7d6489b2 100644 --- a/src/com/owncloud/android/ui/preview/PreviewMediaFragment.java +++ b/src/com/owncloud/android/ui/preview/PreviewMediaFragment.java @@ -16,9 +16,6 @@ */ package com.owncloud.android.ui.preview; -import java.util.ArrayList; -import java.util.List; - import android.accounts.Account; import android.app.Activity; import android.app.AlertDialog; @@ -32,6 +29,7 @@ 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.IBinder; @@ -48,15 +46,16 @@ import com.actionbarsherlock.view.Menu; import com.actionbarsherlock.view.MenuInflater; import com.actionbarsherlock.view.MenuItem; import com.owncloud.android.R; -import com.owncloud.android.datamodel.FileDataStorageManager; import com.owncloud.android.datamodel.OCFile; +import com.owncloud.android.files.FileMenuFilter; +import com.owncloud.android.lib.common.utils.Log_OC; import com.owncloud.android.media.MediaControlView; import com.owncloud.android.media.MediaService; import com.owncloud.android.media.MediaServiceBinder; import com.owncloud.android.ui.activity.FileActivity; import com.owncloud.android.ui.dialog.ConfirmationDialogFragment; +import com.owncloud.android.ui.dialog.RemoveFileDialogFragment; import com.owncloud.android.ui.fragment.FileFragment; -import com.owncloud.android.utils.Log_OC; /** @@ -69,8 +68,7 @@ import com.owncloud.android.utils.Log_OC; * @author David A. Velasco */ public class PreviewMediaFragment extends FileFragment implements - OnTouchListener, - ConfirmationDialogFragment.ConfirmationDialogFragmentListener { + OnTouchListener { public static final String EXTRA_FILE = "FILE"; public static final String EXTRA_ACCOUNT = "ACCOUNT"; @@ -90,8 +88,6 @@ public class PreviewMediaFragment extends FileFragment implements private boolean mAutoplay; public boolean mPrepared; - private FileFragment.ContainerActivity mContainerActivity; - private static final String TAG = PreviewMediaFragment.class.getSimpleName(); @@ -103,7 +99,12 @@ public class PreviewMediaFragment extends FileFragment implements * @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) { + public PreviewMediaFragment( + OCFile fileToDetail, + Account ocAccount, + int startPlaybackPosition, + boolean autoplay) { + super(fileToDetail); mAccount = ocAccount; mSavedPlaybackPosition = startPlaybackPosition; @@ -114,9 +115,11 @@ public class PreviewMediaFragment extends FileFragment implements /** * 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). + * 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 + * DO NOT CALL IT: an {@link OCFile} and {@link Account} must be provided for a successful + * construction */ public PreviewMediaFragment() { super(); @@ -166,31 +169,37 @@ public class PreviewMediaFragment extends FileFragment implements super.onActivityCreated(savedInstanceState); Log_OC.e(TAG, "onActivityCreated"); - if (savedInstanceState != null) { - setFile((OCFile)savedInstanceState.getParcelable(PreviewMediaFragment.EXTRA_FILE)); + OCFile file = getFile(); + if (savedInstanceState == null) { + if (file == null) { + throw new IllegalStateException("Instanced with a NULL OCFile"); + } + if (mAccount == null) { + throw new IllegalStateException("Instanced with a NULL ownCloud Account"); + } + if (!file.isDown()) { + throw new IllegalStateException("There is no local file to preview"); + } + + } else { + file = (OCFile)savedInstanceState.getParcelable(PreviewMediaFragment.EXTRA_FILE); + setFile(file); mAccount = savedInstanceState.getParcelable(PreviewMediaFragment.EXTRA_ACCOUNT); - mSavedPlaybackPosition = savedInstanceState.getInt(PreviewMediaFragment.EXTRA_PLAY_POSITION); + mSavedPlaybackPosition = + savedInstanceState.getInt(PreviewMediaFragment.EXTRA_PLAY_POSITION); mAutoplay = savedInstanceState.getBoolean(PreviewMediaFragment.EXTRA_PLAYING); } - OCFile file = getFile(); - if (file == null) { - throw new IllegalStateException("Instanced with a NULL OCFile"); - } - if (mAccount == null) { - throw new IllegalStateException("Instanced with a NULL ownCloud Account"); - } - if (!file.isDown()) { - throw new IllegalStateException("There is no local file to preview"); - } - if (file.isVideo()) { - mVideoPreview.setVisibility(View.VISIBLE); - mImagePreview.setVisibility(View.GONE); - prepareVideo(); + if (file != null && file.isDown()) { + if (file.isVideo()) { + mVideoPreview.setVisibility(View.VISIBLE); + mImagePreview.setVisibility(View.GONE); + prepareVideo(); - } else { - mVideoPreview.setVisibility(View.GONE); - mImagePreview.setVisibility(View.VISIBLE); + } else { + mVideoPreview.setVisibility(View.GONE); + mImagePreview.setVisibility(View.VISIBLE); + } } } @@ -213,8 +222,11 @@ public class PreviewMediaFragment extends FileFragment implements 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()); + outState.putInt( + PreviewMediaFragment.EXTRA_PLAY_POSITION , + mMediaServiceBinder.getCurrentPosition()); + outState.putBoolean( + PreviewMediaFragment.EXTRA_PLAYING , mMediaServiceBinder.isPlaying()); } } @@ -225,7 +237,7 @@ public class PreviewMediaFragment extends FileFragment implements Log_OC.e(TAG, "onStart"); OCFile file = getFile(); - if (file != null) { + if (file != null && file.isDown()) { if (file.isAudio()) { bindMediaService(); @@ -250,36 +262,7 @@ public class PreviewMediaFragment extends FileFragment implements @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { super.onCreateOptionsMenu(menu, inflater); - inflater.inflate(R.menu.file_actions_menu, menu); - List toHide = new ArrayList(); - - 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 - - // Options shareLink - if (!getFile().isShareByLink()) { - toHide.add(R.id.action_unshare_file); - } - - // Send file - boolean sendEnabled = getString(R.string.send_files_to_other_apps).equalsIgnoreCase("on"); - if (!sendEnabled) { - toHide.add(R.id.action_send_file); - } - - for (int i : toHide) { - item = menu.findItem(i); - if (item != null) { - item.setVisible(false); - item.setEnabled(false); - } - } - } @@ -290,14 +273,29 @@ public class PreviewMediaFragment extends FileFragment implements public void onPrepareOptionsMenu(Menu menu) { super.onPrepareOptionsMenu(menu); - MenuItem item = menu.findItem(R.id.action_unshare_file); - // Options shareLink - if (!getFile().isShareByLink()) { + if (mContainerActivity.getStorageManager() != null) { + FileMenuFilter mf = new FileMenuFilter( + getFile(), + mContainerActivity.getStorageManager().getAccount(), + mContainerActivity, + getSherlockActivity() + ); + mf.filter(menu); + } + + // additional restriction for this fragment + // TODO allow renaming in PreviewImageFragment + MenuItem item = menu.findItem(R.id.action_rename_file); + if (item != null) { + item.setVisible(false); + item.setEnabled(false); + } + + // additional restriction for this fragment + item = menu.findItem(R.id.action_move); + if (item != null) { item.setVisible(false); item.setEnabled(false); - } else { - item.setVisible(true); - item.setEnabled(true); } } @@ -309,11 +307,13 @@ public class PreviewMediaFragment extends FileFragment implements public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.action_share_file: { - shareFileWithLink(); + stopPreview(false); + mContainerActivity.getFileOperationsHelper().shareFileWithLink(getFile()); return true; } case R.id.action_unshare_file: { - unshareFileWithLink(); + stopPreview(false); + mContainerActivity.getFileOperationsHelper().unshareFileWithLink(getFile()); return true; } case R.id.action_open_file_with: { @@ -321,7 +321,8 @@ public class PreviewMediaFragment extends FileFragment implements return true; } case R.id.action_remove_file: { - removeFile(); + RemoveFileDialogFragment dialog = RemoveFileDialogFragment.newInstance(getFile()); + dialog.show(getFragmentManager(), ConfirmationDialogFragment.FTAG_CONFIRMATION); return true; } case R.id.action_see_details: { @@ -330,8 +331,13 @@ public class PreviewMediaFragment extends FileFragment implements } case R.id.action_send_file: { sendFile(); + return true; } - + case R.id.action_sync_file: { + mContainerActivity.getFileOperationsHelper().syncFile(getFile()); + return true; + } + default: return false; } @@ -347,17 +353,6 @@ public class PreviewMediaFragment extends FileFragment implements setFile(file); } - private void unshareFileWithLink() { - stopPreview(false); - mContainerActivity.getFileOperationsHelper().unshareFileWithLink(getFile()); - } - - private void shareFileWithLink() { - stopPreview(false); - mContainerActivity.getFileOperationsHelper().shareFileWithLink(getFile()); - - } - private void sendFile() { stopPreview(false); mContainerActivity.getFileOperationsHelper().sendDownloadedFile(getFile()); @@ -378,12 +373,15 @@ public class PreviewMediaFragment extends FileFragment implements mVideoPreview.setOnErrorListener(mVideoHelper); } + @SuppressWarnings("static-access") 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(getFile().getStoragePath()); + // load the video file in the video player ; + // when done, VideoHelper#onPrepared() will be called + Uri uri = Uri.parse(getFile().getStoragePath()); + mVideoPreview.setVideoPath(uri.encode(getFile().getStoragePath())); } @@ -449,7 +447,8 @@ public class PreviewMediaFragment extends FileFragment implements @Override public boolean onError(MediaPlayer mp, int what, int extra) { if (mVideoPreview.getWindowToken() != null) { - String message = MediaService.getMessageForMediaError(getSherlockActivity(), what, extra); + String message = MediaService.getMessageForMediaError( + getSherlockActivity(), what, extra); new AlertDialog.Builder(getSherlockActivity()) .setMessage(message) .setPositiveButton(android.R.string.VideoView_error_button, @@ -470,8 +469,8 @@ public class PreviewMediaFragment extends FileFragment implements @Override public void onPause() { - super.onPause(); Log_OC.e(TAG, "onPause"); + super.onPause(); } @Override @@ -482,14 +481,13 @@ public class PreviewMediaFragment extends FileFragment implements @Override public void onDestroy() { - super.onDestroy(); Log_OC.e(TAG, "onDestroy"); + super.onDestroy(); } @Override public void onStop() { Log_OC.e(TAG, "onStop"); - super.onStop(); mPrepared = false; if (mMediaServiceConnection != null) { @@ -501,6 +499,8 @@ public class PreviewMediaFragment extends FileFragment implements mMediaServiceConnection = null; mMediaServiceBinder = null; } + + super.onStop(); } @Override @@ -533,7 +533,8 @@ public class PreviewMediaFragment extends FileFragment implements Log_OC.e(TAG, "onActivityResult " + this); super.onActivityResult(requestCode, resultCode, data); if (resultCode == Activity.RESULT_OK) { - mSavedPlaybackPosition = data.getExtras().getInt(PreviewVideoActivity.EXTRA_START_POSITION); + mSavedPlaybackPosition = data.getExtras().getInt( + PreviewVideoActivity.EXTRA_START_POSITION); mAutoplay = data.getExtras().getBoolean(PreviewVideoActivity.EXTRA_AUTOPLAY); } } @@ -572,7 +573,8 @@ public class PreviewMediaFragment extends FileFragment implements @Override public void onServiceConnected(ComponentName component, IBinder service) { if (getSherlockActivity() != null) { - if (component.equals(new ComponentName(getSherlockActivity(), MediaService.class))) { + if (component.equals( + new ComponentName(getSherlockActivity(), MediaService.class))) { Log_OC.d(TAG, "Media service connected"); mMediaServiceBinder = (MediaServiceBinder) service; if (mMediaServiceBinder != null) { @@ -604,7 +606,10 @@ public class PreviewMediaFragment extends FileFragment implements if (mMediaController != null) { mMediaController.setMediaPlayer(null); } else { - Toast.makeText(getSherlockActivity(), "No media controller to release when disconnected from media service", Toast.LENGTH_SHORT).show(); + Toast.makeText( + getSherlockActivity(), + "No media controller to release when disconnected from media service", + Toast.LENGTH_SHORT).show(); } mMediaServiceBinder = null; mMediaServiceConnection = null; @@ -624,59 +629,8 @@ public class PreviewMediaFragment extends FileFragment implements } /** - * 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[]{getFile().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) { - OCFile file = getFile(); - FileDataStorageManager storageManager = mContainerActivity.getStorageManager(); - if (storageManager.getFileById(file.getFileId()) != null) { // check that the file is still there; - stopPreview(true); - mContainerActivity.getFileOperationsHelper().removeFile(file, true); - } - } - - - /** - * Removes the file from local storage - */ - @Override - public void onNeutral(String callerTag) { - OCFile file = getFile(); - stopPreview(true); - mContainerActivity.getStorageManager().removeFile(file, false, true); // TODO perform in background task / new thread - finish(); - } - - /** - * User cancelled the removal action. - */ - @Override - public void onCancel(String callerTag) { - // nothing to do here - } - - - /** - * Helper method to test if an {@link OCFile} can be passed to a {@link PreviewMediaFragment} to be previewed. + * 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. @@ -686,7 +640,7 @@ public class PreviewMediaFragment extends FileFragment implements } - private void stopPreview(boolean stopAudio) { + public void stopPreview(boolean stopAudio) { OCFile file = getFile(); if (file.isAudio() && stopAudio) { mMediaServiceBinder.pause();