PreviewMediaFragment.java 26 KB

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