PreviewMediaFragment.java 25 KB

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