PreviewMediaFragment.java 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  1. /*
  2. * ownCloud Android client application
  3. *
  4. * @author David A. Velasco
  5. * Copyright (C) 2016 ownCloud Inc.
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2,
  9. * as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. package com.owncloud.android.ui.preview;
  21. import android.accounts.Account;
  22. import android.app.Activity;
  23. import android.content.ComponentName;
  24. import android.content.Context;
  25. import android.content.Intent;
  26. import android.content.ServiceConnection;
  27. import android.content.res.Configuration;
  28. import android.content.res.Resources;
  29. import android.graphics.Bitmap;
  30. import android.graphics.BitmapFactory;
  31. import android.media.MediaMetadataRetriever;
  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.os.Bundle;
  37. import android.os.IBinder;
  38. import android.support.annotation.DrawableRes;
  39. import android.support.annotation.StringRes;
  40. import android.view.LayoutInflater;
  41. import android.view.Menu;
  42. import android.view.MenuInflater;
  43. import android.view.MenuItem;
  44. import android.view.MotionEvent;
  45. import android.view.View;
  46. import android.view.View.OnTouchListener;
  47. import android.view.ViewGroup;
  48. import android.widget.ImageView;
  49. import android.widget.LinearLayout;
  50. import android.widget.ProgressBar;
  51. import android.widget.RelativeLayout;
  52. import android.widget.TextView;
  53. import android.widget.Toast;
  54. import android.widget.VideoView;
  55. import com.owncloud.android.R;
  56. import com.owncloud.android.datamodel.OCFile;
  57. import com.owncloud.android.files.FileMenuFilter;
  58. import com.owncloud.android.lib.common.utils.Log_OC;
  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.ui.activity.FileActivity;
  63. import com.owncloud.android.ui.dialog.ConfirmationDialogFragment;
  64. import com.owncloud.android.ui.dialog.RemoveFilesDialogFragment;
  65. import com.owncloud.android.ui.fragment.FileFragment;
  66. import com.owncloud.android.utils.AnalyticsUtils;
  67. import com.owncloud.android.utils.MimeTypeUtil;
  68. /**
  69. * This fragment shows a preview of a downloaded media file (audio or video).
  70. *
  71. * Trying to get an instance with NULL {@link OCFile} or ownCloud {@link Account} values will
  72. * produce an {@link IllegalStateException}.
  73. *
  74. * By now, if the {@link OCFile} passed is not downloaded, an {@link IllegalStateException} is
  75. * generated on instantiation too.
  76. */
  77. public class PreviewMediaFragment extends FileFragment implements
  78. OnTouchListener {
  79. public static final String EXTRA_FILE = "FILE";
  80. public static final String EXTRA_ACCOUNT = "ACCOUNT";
  81. private static final String EXTRA_PLAY_POSITION = "PLAY_POSITION";
  82. private static final String EXTRA_PLAYING = "PLAYING";
  83. private View mView;
  84. private Account mAccount;
  85. private ImageView mImagePreview;
  86. private VideoView mVideoPreview;
  87. private int mSavedPlaybackPosition;
  88. private RelativeLayout mMultiView;
  89. private RelativeLayout mPreviewContainer;
  90. protected LinearLayout mMultiListContainer;
  91. protected TextView mMultiListMessage;
  92. protected TextView mMultiListHeadline;
  93. protected ImageView mMultiListIcon;
  94. protected ProgressBar mMultiListProgress;
  95. private MediaServiceBinder mMediaServiceBinder = null;
  96. private MediaControlView mMediaController = null;
  97. private MediaServiceConnection mMediaServiceConnection = null;
  98. private VideoHelper mVideoHelper;
  99. private boolean mAutoplay;
  100. private static boolean mOnResume = false;
  101. public boolean mPrepared;
  102. private static final String TAG = PreviewMediaFragment.class.getSimpleName();
  103. private static final String SCREEN_NAME = "Audio/Video Preview";
  104. private static final String FILE = "FILE";
  105. private static final String ACCOUNT = "ACCOUNT";
  106. private static final String PLAYBACK_POSITION = "PLAYBACK_POSITION";
  107. private static final String AUTOPLAY = "AUTOPLAY";
  108. /**
  109. * Creates a fragment to preview a file.
  110. * <p/>
  111. * When 'fileToDetail' or 'ocAccount' are null
  112. *
  113. * @param fileToDetail An {@link OCFile} to preview in the fragment
  114. * @param ocAccount An ownCloud account; needed to start downloads
  115. */
  116. public static PreviewMediaFragment newInstance(OCFile fileToDetail, Account ocAccount, int startPlaybackPosition,
  117. boolean autoplay) {
  118. PreviewMediaFragment previewMediaFragment = new PreviewMediaFragment();
  119. Bundle bundle = new Bundle();
  120. bundle.putParcelable(FILE, fileToDetail);
  121. bundle.putParcelable(ACCOUNT, ocAccount);
  122. bundle.putInt(PLAYBACK_POSITION, startPlaybackPosition);
  123. bundle.putBoolean(AUTOPLAY, autoplay);
  124. previewMediaFragment.setArguments(bundle);
  125. return previewMediaFragment;
  126. }
  127. /**
  128. * Creates an empty fragment for previews.
  129. * <p/>
  130. * MUST BE KEPT: the system uses it when tries to reinstantiate a fragment automatically
  131. * (for instance, when the device is turned a aside).
  132. * <p/>
  133. * DO NOT CALL IT: an {@link OCFile} and {@link Account} must be provided for a successful
  134. * construction
  135. */
  136. public PreviewMediaFragment() {
  137. super();
  138. mAccount = null;
  139. mSavedPlaybackPosition = 0;
  140. mAutoplay = true;
  141. }
  142. /**
  143. * {@inheritDoc}
  144. */
  145. @Override
  146. public void onCreate(Bundle savedInstanceState) {
  147. super.onCreate(savedInstanceState);
  148. setHasOptionsMenu(true);
  149. Bundle bundle = getArguments();
  150. setFile((OCFile) bundle.getParcelable(FILE));
  151. mAccount = bundle.getParcelable(ACCOUNT);
  152. mSavedPlaybackPosition = bundle.getInt(PLAYBACK_POSITION);
  153. mAutoplay = bundle.getBoolean(AUTOPLAY);
  154. }
  155. /**
  156. * {@inheritDoc}
  157. */
  158. @Override
  159. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  160. Bundle savedInstanceState) {
  161. super.onCreateView(inflater, container, savedInstanceState);
  162. Log_OC.v(TAG, "onCreateView");
  163. mView = inflater.inflate(R.layout.file_preview, container, false);
  164. mPreviewContainer = (RelativeLayout) mView.findViewById(R.id.file_preview_container);
  165. mImagePreview = (ImageView) mView.findViewById(R.id.image_preview);
  166. mVideoPreview = (VideoView) mView.findViewById(R.id.video_preview);
  167. mVideoPreview.setOnTouchListener(this);
  168. mMediaController = (MediaControlView) mView.findViewById(R.id.media_controller);
  169. mMultiView = (RelativeLayout) mView.findViewById(R.id.multi_view);
  170. setupMultiView(mView);
  171. setMultiListLoadingMessage();
  172. return mView;
  173. }
  174. protected void setupMultiView(View view) {
  175. mMultiListContainer = (LinearLayout) view.findViewById(R.id.empty_list_view);
  176. mMultiListMessage = (TextView) view.findViewById(R.id.empty_list_view_text);
  177. mMultiListHeadline = (TextView) view.findViewById(R.id.empty_list_view_headline);
  178. mMultiListIcon = (ImageView) view.findViewById(R.id.empty_list_icon);
  179. mMultiListProgress = (ProgressBar) view.findViewById(R.id.empty_list_progress);
  180. }
  181. private void setMultiListLoadingMessage() {
  182. if (mMultiView != null) {
  183. mMultiListHeadline.setText(R.string.file_list_loading);
  184. mMultiListMessage.setText("");
  185. mMultiListIcon.setVisibility(View.GONE);
  186. mMultiListProgress.setVisibility(View.VISIBLE);
  187. }
  188. }
  189. public void setMessageForMultiList(String headline, @StringRes int message, @DrawableRes int icon) {
  190. if (mMultiListContainer != null && mMultiListMessage != null) {
  191. mMultiListHeadline.setText(headline);
  192. mMultiListMessage.setText(message);
  193. mMultiListIcon.setImageResource(icon);
  194. mMultiListIcon.setVisibility(View.VISIBLE);
  195. mMultiListProgress.setVisibility(View.GONE);
  196. }
  197. }
  198. /**
  199. * {@inheritDoc}
  200. */
  201. @Override
  202. public void onActivityCreated(Bundle savedInstanceState) {
  203. mOnResume = true;
  204. super.onActivityCreated(savedInstanceState);
  205. Log_OC.v(TAG, "onActivityCreated");
  206. OCFile file = getFile();
  207. if (savedInstanceState == null) {
  208. if (file == null) {
  209. throw new IllegalStateException("Instanced with a NULL OCFile");
  210. }
  211. if (mAccount == null) {
  212. throw new IllegalStateException("Instanced with a NULL ownCloud Account");
  213. }
  214. if (!file.isDown()) {
  215. throw new IllegalStateException("There is no local file to preview");
  216. }
  217. } else {
  218. file = savedInstanceState.getParcelable(PreviewMediaFragment.EXTRA_FILE);
  219. setFile(file);
  220. mAccount = savedInstanceState.getParcelable(PreviewMediaFragment.EXTRA_ACCOUNT);
  221. mSavedPlaybackPosition = savedInstanceState.getInt(PreviewMediaFragment.EXTRA_PLAY_POSITION);
  222. mAutoplay = savedInstanceState.getBoolean(PreviewMediaFragment.EXTRA_PLAYING);
  223. }
  224. if (file != null && file.isDown()) {
  225. if (MimeTypeUtil.isVideo(file)) {
  226. mVideoPreview.setVisibility(View.VISIBLE);
  227. mImagePreview.setVisibility(View.GONE);
  228. prepareVideo();
  229. } else {
  230. mVideoPreview.setVisibility(View.GONE);
  231. mImagePreview.setVisibility(View.VISIBLE);
  232. extractAndSetCoverArt(file);
  233. }
  234. }
  235. }
  236. /**
  237. * tries to read the cover art from the audio file and sets it as cover art.
  238. *
  239. * @param file audio file with potential cover art
  240. */
  241. private void extractAndSetCoverArt(OCFile file) {
  242. if (MimeTypeUtil.isAudio(file)) {
  243. try {
  244. MediaMetadataRetriever mmr = new MediaMetadataRetriever();
  245. mmr.setDataSource(file.getStoragePath());
  246. byte[] data = mmr.getEmbeddedPicture();
  247. if (data != null) {
  248. Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
  249. mImagePreview.setImageBitmap(bitmap); //associated cover art in bitmap
  250. } else {
  251. mImagePreview.setImageResource(R.drawable.logo);
  252. }
  253. } catch (Throwable t) {
  254. mImagePreview.setImageResource(R.drawable.logo);
  255. }
  256. }
  257. }
  258. /**
  259. * {@inheritDoc}
  260. */
  261. @Override
  262. public void onSaveInstanceState(Bundle outState) {
  263. super.onSaveInstanceState(outState);
  264. Log_OC.v(TAG, "onSaveInstanceState");
  265. outState.putParcelable(PreviewMediaFragment.EXTRA_FILE, getFile());
  266. outState.putParcelable(PreviewMediaFragment.EXTRA_ACCOUNT, mAccount);
  267. if (MimeTypeUtil.isVideo(getFile())) {
  268. if (mVideoPreview != null) {
  269. mSavedPlaybackPosition = mVideoPreview.getCurrentPosition();
  270. mAutoplay = mVideoPreview.isPlaying();
  271. outState.putInt(PreviewMediaFragment.EXTRA_PLAY_POSITION, mSavedPlaybackPosition);
  272. outState.putBoolean(PreviewMediaFragment.EXTRA_PLAYING, mAutoplay);
  273. }
  274. }
  275. else {
  276. if (mMediaServiceBinder != null) {
  277. outState.putInt(PreviewMediaFragment.EXTRA_PLAY_POSITION, mMediaServiceBinder.getCurrentPosition());
  278. outState.putBoolean(PreviewMediaFragment.EXTRA_PLAYING, mMediaServiceBinder.isPlaying());
  279. }
  280. }
  281. }
  282. @Override
  283. public void onStart() {
  284. super.onStart();
  285. Log_OC.v(TAG, "onStart");
  286. OCFile file = getFile();
  287. if (file != null && file.isDown()) {
  288. if (MimeTypeUtil.isAudio(file)) {
  289. bindMediaService();
  290. }
  291. else {
  292. if (MimeTypeUtil.isVideo(file)) {
  293. stopAudio();
  294. playVideo();
  295. }
  296. }
  297. }
  298. }
  299. private void stopAudio() {
  300. Intent i = new Intent(getActivity(), MediaService.class);
  301. i.setAction(MediaService.ACTION_STOP_ALL);
  302. getActivity().startService(i);
  303. }
  304. /**
  305. * {@inheritDoc}
  306. */
  307. @Override
  308. public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
  309. super.onCreateOptionsMenu(menu, inflater);
  310. inflater.inflate(R.menu.file_actions_menu, menu);
  311. }
  312. /**
  313. * {@inheritDoc}
  314. */
  315. @Override
  316. public void onPrepareOptionsMenu(Menu menu) {
  317. super.onPrepareOptionsMenu(menu);
  318. if (mContainerActivity.getStorageManager() != null) {
  319. FileMenuFilter mf = new FileMenuFilter(
  320. getFile(),
  321. mContainerActivity.getStorageManager().getAccount(),
  322. mContainerActivity,
  323. getActivity()
  324. );
  325. mf.filter(menu);
  326. }
  327. // additional restriction for this fragment
  328. // TODO allow renaming in PreviewImageFragment
  329. MenuItem item = menu.findItem(R.id.action_rename_file);
  330. if (item != null) {
  331. item.setVisible(false);
  332. item.setEnabled(false);
  333. }
  334. // additional restriction for this fragment
  335. item = menu.findItem(R.id.action_move);
  336. if (item != null) {
  337. item.setVisible(false);
  338. item.setEnabled(false);
  339. }
  340. // additional restriction for this fragment
  341. item = menu.findItem(R.id.action_copy);
  342. if (item != null) {
  343. item.setVisible(false);
  344. item.setEnabled(false);
  345. }
  346. // additional restriction for this fragment
  347. item = menu.findItem(R.id.action_favorite);
  348. if (item != null) {
  349. item.setVisible(false);
  350. item.setEnabled(false);
  351. }
  352. // additional restriction for this fragment
  353. item = menu.findItem(R.id.action_unset_favorite);
  354. if (item != null) {
  355. item.setVisible(false);
  356. item.setEnabled(false);
  357. }
  358. if(getFile().isSharedWithMe() && !getFile().canReshare()){
  359. // additional restriction for this fragment
  360. item = menu.findItem(R.id.action_share_file);
  361. if(item != null){
  362. item.setVisible(false);
  363. item.setEnabled(false);
  364. }
  365. }
  366. }
  367. /**
  368. * {@inheritDoc}
  369. */
  370. @Override
  371. public boolean onOptionsItemSelected(MenuItem item) {
  372. switch (item.getItemId()) {
  373. case R.id.action_share_file: {
  374. seeShareFile();
  375. return true;
  376. }
  377. case R.id.action_open_file_with: {
  378. openFile();
  379. return true;
  380. }
  381. case R.id.action_remove_file: {
  382. RemoveFilesDialogFragment dialog = RemoveFilesDialogFragment.newInstance(getFile());
  383. dialog.show(getFragmentManager(), ConfirmationDialogFragment.FTAG_CONFIRMATION);
  384. return true;
  385. }
  386. case R.id.action_see_details: {
  387. seeDetails();
  388. return true;
  389. }
  390. case R.id.action_send_file: {
  391. sendFile();
  392. return true;
  393. }
  394. case R.id.action_sync_file: {
  395. mContainerActivity.getFileOperationsHelper().syncFile(getFile());
  396. return true;
  397. }
  398. default:
  399. return super.onOptionsItemSelected(item);
  400. }
  401. }
  402. /**
  403. * Update the file of the fragment with file value
  404. *
  405. * @param file Replaces the held file with a new one
  406. */
  407. public void updateFile(OCFile file) {
  408. setFile(file);
  409. }
  410. private void sendFile() {
  411. stopPreview(false);
  412. mContainerActivity.getFileOperationsHelper().sendDownloadedFile(getFile());
  413. }
  414. private void seeDetails() {
  415. stopPreview(false);
  416. mContainerActivity.showDetails(getFile());
  417. }
  418. private void seeShareFile() {
  419. stopPreview(false);
  420. mContainerActivity.getFileOperationsHelper().showShareFile(getFile());
  421. }
  422. private void prepareVideo() {
  423. // create helper to get more control on the playback
  424. mVideoHelper = new VideoHelper();
  425. mVideoPreview.setOnPreparedListener(mVideoHelper);
  426. mVideoPreview.setOnCompletionListener(mVideoHelper);
  427. mVideoPreview.setOnErrorListener(mVideoHelper);
  428. }
  429. @SuppressWarnings("static-access")
  430. private void playVideo() {
  431. // create and prepare control panel for the user
  432. mMediaController.setMediaPlayer(mVideoPreview);
  433. // load the video file in the video player ;
  434. // when done, VideoHelper#onPrepared() will be called
  435. mVideoPreview.setVideoURI(getFile().getStorageUri());
  436. }
  437. private class VideoHelper implements OnCompletionListener, OnPreparedListener, OnErrorListener {
  438. /**
  439. * Called when the file is ready to be played.
  440. * <p/>
  441. * Just starts the playback.
  442. *
  443. * @param vp {@link MediaPlayer} instance performing the playback.
  444. */
  445. @Override
  446. public void onPrepared(MediaPlayer vp) {
  447. Log_OC.v(TAG, "onPrepared");
  448. mMultiView.setVisibility(View.GONE);
  449. mPreviewContainer.setVisibility(View.VISIBLE);
  450. mVideoPreview.seekTo(mSavedPlaybackPosition);
  451. if (mAutoplay) {
  452. mVideoPreview.start();
  453. }
  454. mMediaController.setEnabled(true);
  455. mMediaController.updatePausePlay();
  456. mPrepared = true;
  457. }
  458. /**
  459. * Called when the file is finished playing.
  460. * <p/>
  461. * Finishes the activity.
  462. *
  463. * @param mp {@link MediaPlayer} instance performing the playback.
  464. */
  465. @Override
  466. public void onCompletion(MediaPlayer mp) {
  467. Log_OC.v(TAG, "completed");
  468. if (mp != null) {
  469. mVideoPreview.seekTo(0);
  470. } // else : called from onError()
  471. mMediaController.updatePausePlay();
  472. }
  473. /**
  474. * Called when an error in playback occurs.
  475. *
  476. * @param mp {@link MediaPlayer} instance performing the playback.
  477. * @param what Type of error
  478. * @param extra Extra code specific to the error
  479. */
  480. @Override
  481. public boolean onError(MediaPlayer mp, int what, int extra) {
  482. Log_OC.e(TAG, "Error in video playback, what = " + what + ", extra = " + extra);
  483. mPreviewContainer.setVisibility(View.GONE);
  484. if (mVideoPreview.getWindowToken() != null) {
  485. String message = MediaService.getMessageForMediaError(
  486. getActivity(), what, extra);
  487. mMultiView.setVisibility(View.VISIBLE);
  488. setMessageForMultiList(message, R.string.preview_sorry, R.drawable.file_movie);
  489. }
  490. return true;
  491. }
  492. }
  493. @Override
  494. public void onPause() {
  495. Log_OC.v(TAG, "onPause");
  496. super.onPause();
  497. }
  498. @Override
  499. public void onResume() {
  500. super.onResume();
  501. mOnResume = !mOnResume;
  502. if (getActivity() != null) {
  503. AnalyticsUtils.setCurrentScreenName(getActivity(), SCREEN_NAME, TAG);
  504. }
  505. Log_OC.v(TAG, "onResume");
  506. }
  507. @Override
  508. public void onDestroy() {
  509. Log_OC.v(TAG, "onDestroy");
  510. super.onDestroy();
  511. }
  512. @Override
  513. public void onStop() {
  514. Log_OC.v(TAG, "onStop");
  515. mPrepared = false;
  516. if (mMediaServiceConnection != null) {
  517. Log_OC.d(TAG, "Unbinding from MediaService ...");
  518. if (mMediaServiceBinder != null && mMediaController != null) {
  519. mMediaController.stopMediaPlayerMessages();
  520. mMediaServiceBinder.unregisterMediaController(mMediaController);
  521. }
  522. getActivity().unbindService(mMediaServiceConnection);
  523. mMediaServiceConnection = null;
  524. mMediaServiceBinder = null;
  525. }
  526. super.onStop();
  527. }
  528. @Override
  529. public boolean onTouch(View v, MotionEvent event) {
  530. if (event.getAction() == MotionEvent.ACTION_DOWN && v.equals(mVideoPreview)) {
  531. // added a margin on the left to avoid interfering with gesture to open navigation drawer
  532. if (event.getX() / Resources.getSystem().getDisplayMetrics().density > 24.0) {
  533. startFullScreenVideo();
  534. }
  535. return true;
  536. }
  537. return false;
  538. }
  539. private void startFullScreenVideo() {
  540. Intent i = new Intent(getActivity(), PreviewVideoActivity.class);
  541. i.putExtra(FileActivity.EXTRA_ACCOUNT, mAccount);
  542. i.putExtra(FileActivity.EXTRA_FILE, getFile());
  543. i.putExtra(PreviewVideoActivity.EXTRA_AUTOPLAY, mVideoPreview.isPlaying());
  544. mVideoPreview.pause();
  545. i.putExtra(PreviewVideoActivity.EXTRA_START_POSITION, mVideoPreview.getCurrentPosition());
  546. startActivityForResult(i, FileActivity.REQUEST_CODE__LAST_SHARED + 1);
  547. }
  548. @Override
  549. public void onConfigurationChanged(Configuration newConfig) {
  550. super.onConfigurationChanged(newConfig);
  551. Log_OC.v(TAG, "onConfigurationChanged " + this);
  552. }
  553. @Override
  554. public void onActivityResult(int requestCode, int resultCode, Intent data) {
  555. Log_OC.v(TAG, "onActivityResult " + this);
  556. super.onActivityResult(requestCode, resultCode, data);
  557. if (resultCode == Activity.RESULT_OK) {
  558. mSavedPlaybackPosition = data.getExtras().getInt(
  559. PreviewVideoActivity.EXTRA_START_POSITION);
  560. mAutoplay = data.getExtras().getBoolean(PreviewVideoActivity.EXTRA_AUTOPLAY);
  561. }
  562. }
  563. private void playAudio() {
  564. OCFile file = getFile();
  565. if (!mMediaServiceBinder.isPlaying(file) && !mOnResume) {
  566. Log_OC.d(TAG, "starting playback of " + file.getStoragePath());
  567. mMediaServiceBinder.start(mAccount, file, mAutoplay, mSavedPlaybackPosition);
  568. }
  569. else {
  570. if (!mMediaServiceBinder.isPlaying() && mAutoplay) {
  571. mMediaServiceBinder.start();
  572. mMediaController.updatePausePlay();
  573. }
  574. }
  575. mOnResume = false;
  576. }
  577. private void bindMediaService() {
  578. Log_OC.d(TAG, "Binding to MediaService...");
  579. if (mMediaServiceConnection == null) {
  580. mMediaServiceConnection = new MediaServiceConnection();
  581. }
  582. getActivity().bindService( new Intent(getActivity(),
  583. MediaService.class),
  584. mMediaServiceConnection,
  585. Context.BIND_AUTO_CREATE);
  586. // follow the flow in MediaServiceConnection#onServiceConnected(...)
  587. }
  588. /** Defines callbacks for service binding, passed to bindService() */
  589. private class MediaServiceConnection implements ServiceConnection {
  590. @Override
  591. public void onServiceConnected(ComponentName component, IBinder service) {
  592. if (getActivity() != null
  593. && component.equals(new ComponentName(getActivity(), MediaService.class))) {
  594. Log_OC.d(TAG, "Media service connected");
  595. mMediaServiceBinder = (MediaServiceBinder) service;
  596. if (mMediaServiceBinder != null) {
  597. prepareMediaController();
  598. playAudio(); // do not wait for the touch of nobody to play audio
  599. Log_OC.d(TAG, "Successfully bound to MediaService, MediaController ready");
  600. } else {
  601. Log_OC.e(TAG, "Unexpected response from MediaService while binding");
  602. }
  603. }
  604. }
  605. private void prepareMediaController() {
  606. mMultiView.setVisibility(View.GONE);
  607. mPreviewContainer.setVisibility(View.VISIBLE);
  608. mMediaServiceBinder.registerMediaController(mMediaController);
  609. if (mMediaController != null) {
  610. mMediaController.setMediaPlayer(mMediaServiceBinder);
  611. mMediaController.setEnabled(true);
  612. mMediaController.updatePausePlay();
  613. }
  614. }
  615. @Override
  616. public void onServiceDisconnected(ComponentName component) {
  617. if (component.equals(new ComponentName(getActivity(), MediaService.class))) {
  618. Log_OC.w(TAG, "Media service suddenly disconnected");
  619. if (mMediaController != null) {
  620. mMediaController.setMediaPlayer(null);
  621. }
  622. else {
  623. Toast.makeText(
  624. getActivity(),
  625. "No media controller to release when disconnected from media service",
  626. Toast.LENGTH_SHORT).show();
  627. }
  628. mMediaServiceBinder = null;
  629. mMediaServiceConnection = null;
  630. }
  631. }
  632. }
  633. /**
  634. * Opens the previewed file with an external application.
  635. */
  636. private void openFile() {
  637. stopPreview(true);
  638. mContainerActivity.getFileOperationsHelper().openFile(getFile());
  639. finish();
  640. }
  641. /**
  642. * Helper method to test if an {@link OCFile} can be passed to a {@link PreviewMediaFragment}
  643. * to be previewed.
  644. *
  645. * @param file File to test if can be previewed.
  646. * @return 'True' if the file can be handled by the fragment.
  647. */
  648. public static boolean canBePreviewed(OCFile file) {
  649. return (file != null && (MimeTypeUtil.isAudio(file) || MimeTypeUtil.isVideo(file)));
  650. }
  651. public void stopPreview(boolean stopAudio) {
  652. OCFile file = getFile();
  653. if (MimeTypeUtil.isAudio(file) && stopAudio) {
  654. mMediaServiceBinder.pause();
  655. }
  656. else {
  657. if (MimeTypeUtil.isVideo(file)) {
  658. mVideoPreview.stopPlayback();
  659. }
  660. }
  661. }
  662. /**
  663. * Finishes the preview
  664. */
  665. private void finish() {
  666. getActivity().onBackPressed();
  667. }
  668. public int getPosition() {
  669. if (mPrepared) {
  670. mSavedPlaybackPosition = mVideoPreview.getCurrentPosition();
  671. }
  672. Log_OC.v(TAG, "getting position: " + mSavedPlaybackPosition);
  673. return mSavedPlaybackPosition;
  674. }
  675. public boolean isPlaying() {
  676. if (mPrepared) {
  677. mAutoplay = mVideoPreview.isPlaying();
  678. }
  679. return mAutoplay;
  680. }
  681. }