From 27d53e80817184ad432e311d111a4feafaa9974b Mon Sep 17 00:00:00 2001 From: Felix Date: Thu, 23 Nov 2017 23:54:40 +0100 Subject: [PATCH] replace last usage of butterknife with databinding and add Playback-Item to decouple player from Entities --- touch/build.gradle | 2 - .../touch/playback/ExoPlayerFragment.java | 96 ++++------ .../chaosflix/touch/playback/PlaybackItem.kt | 36 ++++ .../touch/playback/PlayerActivity.kt | 3 +- .../touch/playback/PlayerViewModel.kt | 25 ++- .../layout/exo_playback_controlsoverlay.xml | 181 ++++++++++-------- .../main/res/layout/fragment_exo_player.xml | 50 +++-- 7 files changed, 219 insertions(+), 174 deletions(-) create mode 100644 touch/src/main/java/de/nicidienase/chaosflix/touch/playback/PlaybackItem.kt diff --git a/touch/build.gradle b/touch/build.gradle index a2249914..8d92396d 100644 --- a/touch/build.gradle +++ b/touch/build.gradle @@ -81,8 +81,6 @@ dependencies { implementation 'com.github.medyo:android-about-page:1.2.2' implementation 'com.google.android.exoplayer:exoplayer:r2.5.2' implementation 'com.squareup.picasso:picasso:2.5.2' - implementation 'com.jakewharton:butterknife:8.5.1' - kapt 'com.jakewharton:butterknife-compiler:8.5.1' debugImplementation 'com.facebook.stetho:stetho:1.4.2' debugImplementation 'com.facebook.stetho:stetho-okhttp3:1.4.2' diff --git a/touch/src/main/java/de/nicidienase/chaosflix/touch/playback/ExoPlayerFragment.java b/touch/src/main/java/de/nicidienase/chaosflix/touch/playback/ExoPlayerFragment.java index 32f3cdfc..487a35ca 100644 --- a/touch/src/main/java/de/nicidienase/chaosflix/touch/playback/ExoPlayerFragment.java +++ b/touch/src/main/java/de/nicidienase/chaosflix/touch/playback/ExoPlayerFragment.java @@ -2,6 +2,7 @@ package de.nicidienase.chaosflix.touch.playback; import android.arch.lifecycle.ViewModelProviders; import android.content.Context; +import android.databinding.DataBindingUtil; import android.net.Uri; import android.os.Bundle; import android.os.Handler; @@ -9,15 +10,12 @@ import android.support.annotation.Nullable; import android.support.design.widget.Snackbar; import android.support.v4.app.Fragment; import android.support.v7.app.AppCompatActivity; -import android.support.v7.widget.Toolbar; import android.text.TextUtils; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.WindowManager; -import android.widget.ProgressBar; -import android.widget.TextView; import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.DefaultLoadControl; @@ -35,7 +33,6 @@ import com.google.android.exoplayer2.source.smoothstreaming.DefaultSsChunkSource import com.google.android.exoplayer2.source.smoothstreaming.SsMediaSource; import com.google.android.exoplayer2.trackselection.AdaptiveTrackSelection; import com.google.android.exoplayer2.trackselection.DefaultTrackSelector; -import com.google.android.exoplayer2.ui.SimpleExoPlayerView; import com.google.android.exoplayer2.upstream.DataSource; import com.google.android.exoplayer2.upstream.DefaultBandwidthMeter; import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory; @@ -44,50 +41,38 @@ import com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory; import com.google.android.exoplayer2.upstream.HttpDataSource; import com.google.android.exoplayer2.util.Util; -import butterknife.BindView; -import butterknife.ButterKnife; import de.nicidienase.chaosflix.R; import de.nicidienase.chaosflix.common.entities.recording.persistence.PersistentEvent; import de.nicidienase.chaosflix.common.entities.recording.persistence.PersistentRecording; +import de.nicidienase.chaosflix.databinding.ExoPlaybackControlsoverlayBinding; +import de.nicidienase.chaosflix.databinding.FragmentExoPlayerBinding; import de.nicidienase.chaosflix.touch.ViewModelFactory; public class ExoPlayerFragment extends Fragment implements PlayerEventListener.PlayerStateChangeListener { - private static final String TAG = ExoPlayerFragment.class.getSimpleName(); - public static final String PLAYBACK_STATE = "playback_state"; - private static final String ARG_EVENT = "event"; - private static final String ARG_RECORDING = "recording"; + private static final String TAG = ExoPlayerFragment.class.getSimpleName(); + private static final String PLAYBACK_STATE = "playback_state"; + private static final String ARG_item = "item"; private OnMediaPlayerInteractionListener listener; private final DefaultBandwidthMeter BANDWIDTH_METER = new DefaultBandwidthMeter(); - @BindView(R.id.video_view) - SimpleExoPlayerView videoView; - @BindView(R.id.progressBar) - ProgressBar progressBar; - - @Nullable - @BindView(R.id.toolbar) - Toolbar toolbar; - @Nullable - @BindView(R.id.subtitle_text) - TextView subtitleText; - private String userAgent; - private Handler mainHandler = new Handler(); + private Handler mainHandler = new Handler(); private boolean playbackState = true; - private PersistentEvent event; - private PersistentRecording recording; - private SimpleExoPlayer exoPlayer; - private PlayerViewModel viewModel; + private SimpleExoPlayer exoPlayer; + private PlayerViewModel viewModel; + private PlaybackItem item; + + FragmentExoPlayerBinding binding; + ExoPlaybackControlsoverlayBinding overlayBinding; public ExoPlayerFragment() { } - public static ExoPlayerFragment newInstance(PersistentEvent event, PersistentRecording recording) { + public static ExoPlayerFragment newInstance(PlaybackItem item) { ExoPlayerFragment fragment = new ExoPlayerFragment(); Bundle args = new Bundle(); - args.putParcelable(ARG_EVENT, event); - args.putParcelable(ARG_RECORDING, recording); + args.putParcelable(ARG_item, item); fragment.setArguments(args); return fragment; } @@ -96,8 +81,7 @@ public class ExoPlayerFragment extends Fragment implements PlayerEventListener.P public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() != null) { - event = getArguments().getParcelable(ARG_EVENT); - recording = getArguments().getParcelable(ARG_RECORDING); + item = getArguments().getParcelable(ARG_item); } if (savedInstanceState != null) { playbackState = savedInstanceState.getBoolean(PLAYBACK_STATE, true); @@ -107,17 +91,20 @@ public class ExoPlayerFragment extends Fragment implements PlayerEventListener.P @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - return inflater.inflate(R.layout.fragment_exo_player, container, false); + binding = DataBindingUtil.inflate(inflater, R.layout.fragment_exo_player, container, false); +// overlayBinding = DataBindingUtil.findBinding(binding.videoView.getOverlayFrameLayout()); + overlayBinding = DataBindingUtil.inflate(inflater, R.layout.exo_playback_controlsoverlay, null, false); + overlayBinding.setItem(item); +// binding.videoView.getOverlayFrameLayout().removeAllViews(); +// binding.videoView.getOverlayFrameLayout().addView(overlayBinding.getRoot()); + return binding.getRoot(); } @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); - ButterKnife.bind(this, view); - if (toolbar != null) { - toolbar.setTitle(event.getTitle()); - toolbar.setSubtitle(event.getSubtitle()); - ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar); + if (overlayBinding != null) { + ((AppCompatActivity) getActivity()).setSupportActionBar(overlayBinding.toolbar); ((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true); } if (exoPlayer == null) { @@ -130,12 +117,12 @@ public class ExoPlayerFragment extends Fragment implements PlayerEventListener.P super.onResume(); if (exoPlayer != null) { exoPlayer.setPlayWhenReady(playbackState); - viewModel.getPlaybackProgress(event.getEventId()).observe(this, playbackProgress -> { + viewModel.getPlaybackProgress(item.getEventId()).observe(this, playbackProgress -> { if (playbackProgress != null) { exoPlayer.seekTo(playbackProgress.getProgress()); } }); - videoView.setPlayer(exoPlayer); + binding.videoView.setPlayer(exoPlayer); } } @@ -150,7 +137,7 @@ public class ExoPlayerFragment extends Fragment implements PlayerEventListener.P public void onPause() { super.onPause(); if (exoPlayer != null) { - viewModel.setPlaybackProgress(event, exoPlayer.getCurrentPosition()); + viewModel.setPlaybackProgress(item.getEventId(), exoPlayer.getCurrentPosition()); exoPlayer.setPlayWhenReady(false); } } @@ -172,7 +159,7 @@ public class ExoPlayerFragment extends Fragment implements PlayerEventListener.P private SimpleExoPlayer setupPlayer() { Log.d(TAG, "Setting up Player."); - videoView.setKeepScreenOn(true); + binding.videoView.setKeepScreenOn(true); userAgent = Util.getUserAgent(getContext(), getResources().getString(R.string.app_name)); @@ -189,7 +176,7 @@ public class ExoPlayerFragment extends Fragment implements PlayerEventListener.P exoPlayer.setPlayWhenReady(playbackState); - exoPlayer.prepare(buildMediaSource(Uri.parse(recording.getRecordingUrl()), "")); + exoPlayer.prepare(buildMediaSource(Uri.parse(item.getUrl()), "")); return exoPlayer; } @@ -211,29 +198,30 @@ public class ExoPlayerFragment extends Fragment implements PlayerEventListener.P @Override public void notifyLoadingStart() { - if (progressBar != null) { - progressBar.setVisibility(View.VISIBLE); + if (binding.progressBar != null) { + binding.progressBar.setVisibility(View.VISIBLE); } } @Override public void notifyLoadingFinished() { - if (progressBar != null) { - progressBar.setVisibility(View.INVISIBLE); + if (binding.progressBar != null) { + binding.progressBar.setVisibility(View.INVISIBLE); } } @Override public void notifyError(String errorMessage) { - Snackbar.make(videoView, errorMessage, Snackbar.LENGTH_LONG).show(); + Snackbar.make(binding.videoView, errorMessage, Snackbar.LENGTH_LONG).show(); } @Override public void notifyEnd() { - viewModel.deletePlaybackProgress(event); + viewModel.deletePlaybackProgress(item.getEventId()); } - public interface OnMediaPlayerInteractionListener {} + public interface OnMediaPlayerInteractionListener { + } private MediaSource buildMediaSource(Uri uri, String overrideExtension) { DataSource.Factory mediaDataSourceFactory = buildDataSourceFactory(true); @@ -263,9 +251,9 @@ public class ExoPlayerFragment extends Fragment implements PlayerEventListener.P private HttpDataSource.Factory buildHttpDataSourceFactory(DefaultBandwidthMeter bandwidthMeter) { return new DefaultHttpDataSourceFactory(userAgent, - bandwidthMeter, - DefaultHttpDataSource.DEFAULT_CONNECT_TIMEOUT_MILLIS, - DefaultHttpDataSource.DEFAULT_READ_TIMEOUT_MILLIS, - true); + bandwidthMeter, + DefaultHttpDataSource.DEFAULT_CONNECT_TIMEOUT_MILLIS, + DefaultHttpDataSource.DEFAULT_READ_TIMEOUT_MILLIS, + true); } } diff --git a/touch/src/main/java/de/nicidienase/chaosflix/touch/playback/PlaybackItem.kt b/touch/src/main/java/de/nicidienase/chaosflix/touch/playback/PlaybackItem.kt new file mode 100644 index 00000000..1764eefb --- /dev/null +++ b/touch/src/main/java/de/nicidienase/chaosflix/touch/playback/PlaybackItem.kt @@ -0,0 +1,36 @@ +package de.nicidienase.chaosflix.touch.playback + +import android.os.Parcel +import android.os.Parcelable + +data class PlaybackItem (val title: String, val subtitle: String, val eventId: Long, val url: String) : Parcelable { + + constructor(parcel: Parcel) : this( + parcel.readString(), + parcel.readString(), + parcel.readLong(), + parcel.readString()) { + } + + override fun writeToParcel(parcel: Parcel, flags: Int) { + parcel.writeString(title) + parcel.writeString(subtitle) + parcel.writeLong(eventId) + parcel.writeString(url) + } + + override fun describeContents(): Int { + return 0 + } + + companion object CREATOR : Parcelable.Creator { + override fun createFromParcel(parcel: Parcel): PlaybackItem { + return PlaybackItem(parcel) + } + + override fun newArray(size: Int): Array { + return arrayOfNulls(size) + } + } + +} \ No newline at end of file diff --git a/touch/src/main/java/de/nicidienase/chaosflix/touch/playback/PlayerActivity.kt b/touch/src/main/java/de/nicidienase/chaosflix/touch/playback/PlayerActivity.kt index cf1f64fc..9dbf245e 100644 --- a/touch/src/main/java/de/nicidienase/chaosflix/touch/playback/PlayerActivity.kt +++ b/touch/src/main/java/de/nicidienase/chaosflix/touch/playback/PlayerActivity.kt @@ -25,7 +25,8 @@ class PlayerActivity : AppCompatActivity(), ExoPlayerFragment.OnMediaPlayerInter if (savedInstanceState == null) { val ft = supportFragmentManager.beginTransaction() - val playerFragment = ExoPlayerFragment.newInstance(event, recording) + val playbackItem = PlaybackItem(event.title, event.subtitle ?: "", event.eventId, recording.recordingUrl) + val playerFragment = ExoPlayerFragment.newInstance(playbackItem) ft.replace(R.id.fragment_container, playerFragment) ft.commit() } diff --git a/touch/src/main/java/de/nicidienase/chaosflix/touch/playback/PlayerViewModel.kt b/touch/src/main/java/de/nicidienase/chaosflix/touch/playback/PlayerViewModel.kt index add5f007..3fd1d7f8 100644 --- a/touch/src/main/java/de/nicidienase/chaosflix/touch/playback/PlayerViewModel.kt +++ b/touch/src/main/java/de/nicidienase/chaosflix/touch/playback/PlayerViewModel.kt @@ -3,7 +3,6 @@ package de.nicidienase.chaosflix.touch.playback import android.arch.lifecycle.LiveData import android.arch.lifecycle.ViewModel import de.nicidienase.chaosflix.common.entities.ChaosflixDatabase -import de.nicidienase.chaosflix.common.entities.recording.persistence.PersistentEvent import de.nicidienase.chaosflix.common.entities.userdata.PlaybackProgress import de.nicidienase.chaosflix.common.network.RecordingService import de.nicidienase.chaosflix.common.network.StreamingService @@ -13,18 +12,18 @@ import io.reactivex.schedulers.Schedulers internal class PlayerViewModel(val database: ChaosflixDatabase, val recordingApi: RecordingService, val streamingApi: StreamingService) : ViewModel() { - fun getPlaybackProgress(apiID: Long): LiveData - = database.playbackProgressDao().getProgressForEvent(apiID) + fun getPlaybackProgress(apiID: Long): LiveData + = database.playbackProgressDao().getProgressForEvent(apiID) - fun setPlaybackProgress(event: PersistentEvent, progress: Long) { - Single.fromCallable { - database.playbackProgressDao().saveProgress(PlaybackProgress(event.eventId, progress)) - }.subscribeOn(Schedulers.io()).subscribe() - } + fun setPlaybackProgress(eventId: Long, progress: Long) { + Single.fromCallable { + database.playbackProgressDao().saveProgress(PlaybackProgress(eventId, progress)) + }.subscribeOn(Schedulers.io()).subscribe() + } - fun deletePlaybackProgress(event: PersistentEvent) { - Single.fromCallable { - database.playbackProgressDao().deleteItem(event.eventId) - } - } + fun deletePlaybackProgress(eventId: Long) { + Single.fromCallable { + database.playbackProgressDao().deleteItem(eventId) + } + } } diff --git a/touch/src/main/res/layout/exo_playback_controlsoverlay.xml b/touch/src/main/res/layout/exo_playback_controlsoverlay.xml index fed07fde..7b109048 100644 --- a/touch/src/main/res/layout/exo_playback_controlsoverlay.xml +++ b/touch/src/main/res/layout/exo_playback_controlsoverlay.xml @@ -1,101 +1,112 @@ - + - + + + - + - + - + - + - + - + - + - + - + - + - + - + + + + + - - \ No newline at end of file + + + + \ No newline at end of file diff --git a/touch/src/main/res/layout/fragment_exo_player.xml b/touch/src/main/res/layout/fragment_exo_player.xml index 61811344..3e2779cf 100644 --- a/touch/src/main/res/layout/fragment_exo_player.xml +++ b/touch/src/main/res/layout/fragment_exo_player.xml @@ -1,22 +1,34 @@ - + - + - + - + + + + + + + + + \ No newline at end of file