X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/ec19a11a385ff21d3e85a94e618d48d8be9ef20d..9826f99c41c0685ee15540b4544997c8da8c0eb4:/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 7d6489b2..21be61d0 100644 --- a/src/com/owncloud/android/ui/preview/PreviewMediaFragment.java +++ b/src/com/owncloud/android/ui/preview/PreviewMediaFragment.java @@ -1,5 +1,8 @@ -/* ownCloud Android client application - * Copyright (C) 2012-2013 ownCloud Inc. +/** + * ownCloud Android client application + * + * @author David A. Velasco + * Copyright (C) 2015 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, @@ -17,14 +20,22 @@ package com.owncloud.android.ui.preview; import android.accounts.Account; +import android.accounts.AuthenticatorException; +import android.accounts.OperationCanceledException; import android.app.Activity; -import android.app.AlertDialog; +import android.content.ActivityNotFoundException; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.media.MediaMetadataRetriever; +import android.os.AsyncTask; +import android.support.v7.app.AlertDialog; 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.content.res.Resources; import android.media.MediaPlayer; import android.media.MediaPlayer.OnCompletionListener; import android.media.MediaPlayer.OnErrorListener; @@ -34,6 +45,9 @@ import android.os.Build; import android.os.Bundle; import android.os.IBinder; import android.view.LayoutInflater; +import android.view.Menu; +import android.view.MenuInflater; +import android.view.MenuItem; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; @@ -42,12 +56,16 @@ import android.widget.ImageView; import android.widget.Toast; import android.widget.VideoView; -import com.actionbarsherlock.view.Menu; -import com.actionbarsherlock.view.MenuInflater; -import com.actionbarsherlock.view.MenuItem; +import com.owncloud.android.MainApp; import com.owncloud.android.R; import com.owncloud.android.datamodel.OCFile; +import com.owncloud.android.datamodel.ThumbnailsCacheManager; import com.owncloud.android.files.FileMenuFilter; +import com.owncloud.android.lib.common.OwnCloudAccount; +import com.owncloud.android.lib.common.OwnCloudClient; +import com.owncloud.android.lib.common.OwnCloudClientManagerFactory; +import com.owncloud.android.lib.common.OwnCloudCredentials; +import com.owncloud.android.lib.common.accounts.AccountUtils; import com.owncloud.android.lib.common.utils.Log_OC; import com.owncloud.android.media.MediaControlView; import com.owncloud.android.media.MediaService; @@ -57,15 +75,18 @@ import com.owncloud.android.ui.dialog.ConfirmationDialogFragment; import com.owncloud.android.ui.dialog.RemoveFileDialogFragment; import com.owncloud.android.ui.fragment.FileFragment; +import java.io.IOException; +import java.util.concurrent.ExecutionException; + /** * 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}. * - * 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 + * By now, if the {@link OCFile} passed is not downloaded, an {@link IllegalStateException} is + * generated on instantiation too. */ public class PreviewMediaFragment extends FileFragment implements OnTouchListener { @@ -80,46 +101,47 @@ public class PreviewMediaFragment extends FileFragment implements private ImageView mImagePreview; private VideoView mVideoPreview; private int mSavedPlaybackPosition; - + private String mUri; + 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 + * + * @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, + OCFile fileToDetail, + Account ocAccount, + int startPlaybackPosition, boolean autoplay) { - + super(fileToDetail); mAccount = ocAccount; mSavedPlaybackPosition = startPlaybackPosition; 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 + * 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() { super(); @@ -127,8 +149,8 @@ public class PreviewMediaFragment extends FileFragment implements mSavedPlaybackPosition = 0; mAutoplay = true; } - - + + /** * {@inheritDoc} */ @@ -137,29 +159,29 @@ public class PreviewMediaFragment extends FileFragment implements super.onCreate(savedInstanceState); setHasOptionsMenu(true); } - + /** * {@inheritDoc} */ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, - Bundle savedInstanceState) { + 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); + + 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); - + + mMediaController = (MediaControlView) mView.findViewById(R.id.media_controller); + return mView; } - + /** * {@inheritDoc} @@ -177,33 +199,59 @@ public class PreviewMediaFragment extends FileFragment implements 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); +// 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 = + mSavedPlaybackPosition = savedInstanceState.getInt(PreviewMediaFragment.EXTRA_PLAY_POSITION); mAutoplay = savedInstanceState.getBoolean(PreviewMediaFragment.EXTRA_PLAYING); - + } - if (file != null && file.isDown()) { + if (file != null) { if (file.isVideo()) { mVideoPreview.setVisibility(View.VISIBLE); mImagePreview.setVisibility(View.GONE); prepareVideo(); - - } else { + + } + else { mVideoPreview.setVisibility(View.GONE); mImagePreview.setVisibility(View.VISIBLE); + extractAndSetCoverArt(file); } } - + } - + + /** + * tries to read the cover art from the audio file and sets it as cover art. + * + * @param file audio file with potential cover art + */ + private void extractAndSetCoverArt(OCFile file) { + if (file.isAudio()) { + try { + MediaMetadataRetriever mmr = new MediaMetadataRetriever(); + mmr.setDataSource(file.getStoragePath()); + byte[] data = mmr.getEmbeddedPicture(); + if (data != null) { + Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length); + mImagePreview.setImageBitmap(bitmap); //associated cover art in bitmap + } else { + mImagePreview.setImageResource(R.drawable.logo); + } + } catch (Throwable t) { + mImagePreview.setImageResource(R.drawable.logo); + } + } + } + /** * {@inheritDoc} @@ -212,24 +260,25 @@ public class PreviewMediaFragment extends FileFragment implements public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); Log_OC.e(TAG, "onSaveInstanceState"); - + outState.putParcelable(PreviewMediaFragment.EXTRA_FILE, getFile()); outState.putParcelable(PreviewMediaFragment.EXTRA_ACCOUNT, mAccount); - + if (getFile().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, mSavedPlaybackPosition); + outState.putBoolean(PreviewMediaFragment.EXTRA_PLAYING, mAutoplay); + } + else { outState.putInt( - PreviewMediaFragment.EXTRA_PLAY_POSITION , + PreviewMediaFragment.EXTRA_PLAY_POSITION, mMediaServiceBinder.getCurrentPosition()); outState.putBoolean( - PreviewMediaFragment.EXTRA_PLAYING , mMediaServiceBinder.isPlaying()); + PreviewMediaFragment.EXTRA_PLAYING, mMediaServiceBinder.isPlaying()); } } - + @Override public void onStart() { @@ -237,22 +286,25 @@ public class PreviewMediaFragment extends FileFragment implements Log_OC.e(TAG, "onStart"); OCFile file = getFile(); - if (file != null && file.isDown()) { - if (file.isAudio()) { - bindMediaService(); - - } else if (file.isVideo()) { - stopAudio(); - playVideo(); - } + if (file != null) { + if (file.isAudio()) { + bindMediaService(); + + } + else { + if (file.isVideo()) { + stopAudio(); + playVideo(); + } + } } } - - + + private void stopAudio() { - Intent i = new Intent(getSherlockActivity(), MediaService.class); + Intent i = new Intent(getActivity(), MediaService.class); i.setAction(MediaService.ACTION_STOP_ALL); - getSherlockActivity().startService(i); + getActivity().startService(i); } @@ -272,13 +324,13 @@ public class PreviewMediaFragment extends FileFragment implements @Override public void onPrepareOptionsMenu(Menu menu) { super.onPrepareOptionsMenu(menu); - + if (mContainerActivity.getStorageManager() != null) { FileMenuFilter mf = new FileMenuFilter( getFile(), mContainerActivity.getStorageManager().getAccount(), mContainerActivity, - getSherlockActivity() + getActivity() ); mf.filter(menu); } @@ -297,9 +349,16 @@ public class PreviewMediaFragment extends FileFragment implements item.setVisible(false); item.setEnabled(false); } + + // additional restriction for this fragment + item = menu.findItem(R.id.action_copy); + if (item != null) { + item.setVisible(false); + item.setEnabled(false); + } } - - + + /** * {@inheritDoc} */ @@ -337,31 +396,38 @@ public class PreviewMediaFragment extends FileFragment implements mContainerActivity.getFileOperationsHelper().syncFile(getFile()); return true; } - + case R.id.action_favorite_file:{ + mContainerActivity.getFileOperationsHelper().toggleFavorite(getFile(), true); + return true; + } + case R.id.action_unfavorite_file:{ + mContainerActivity.getFileOperationsHelper().toggleFavorite(getFile(), false); + return true; + } default: return false; } } - /** * Update the file of the fragment with file value + * * @param file */ - public void updateFile(OCFile file){ + public void updateFile(OCFile file) { setFile(file); } - + private void sendFile() { stopPreview(false); mContainerActivity.getFileOperationsHelper().sendDownloadedFile(getFile()); - + } private void seeDetails() { stopPreview(false); - mContainerActivity.showDetails(getFile()); + mContainerActivity.showDetails(getFile()); } @@ -372,119 +438,191 @@ public class PreviewMediaFragment extends FileFragment implements mVideoPreview.setOnCompletionListener(mVideoHelper); 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 - Uri uri = Uri.parse(getFile().getStoragePath()); - mVideoPreview.setVideoPath(uri.encode(getFile().getStoragePath())); + if (getFile().isDown()) { + mUri = getFile().getStoragePath(); + } else { + Context context = MainApp.getAppContext(); + Account account = mContainerActivity.getStorageManager().getAccount(); + + mUri = generateUrlWithCredentials(account, context, getFile()); + } + + mVideoPreview.setVideoPath(mUri); + } + + public static String generateUrlWithCredentials(Account account, Context context, OCFile file){ + OwnCloudAccount ocAccount = null; + try { + ocAccount = new OwnCloudAccount(account, context); + + final ClientGenerationTask task = new ClientGenerationTask(); + task.execute(ocAccount); + + OwnCloudClient mClient = task.get(); + String url = AccountUtils.constructFullURLForAccount(context, account) + Uri.encode(file.getRemotePath(), "/"); + OwnCloudCredentials credentials = mClient.getCredentials(); + + return url.replace("//", "//" + credentials.getUsername() + ":" + credentials.getAuthToken() + "@"); + + } catch (AccountUtils.AccountNotFoundException e) { + e.printStackTrace(); + + } catch (InterruptedException e) { + e.printStackTrace(); + } catch (ExecutionException e) { + e.printStackTrace(); + } + return ""; } - + + public static class ClientGenerationTask extends AsyncTask