From 35acef9f8de9614f9cd108aa6a443ce0ba2abae5 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Sun, 5 Jan 2020 10:57:32 +0800 Subject: [PATCH] Mute videos by default is now available. --- .../Activity/ViewVideoActivity.java | 71 +++++++++++++++++++ .../infinityforreddit/AppComponent.java | 3 + .../Utils/SharedPreferencesUtils.java | 1 + .../ic_mute_preferences_24dp.xml | 9 +++ app/src/main/res/drawable/ic_mute_24dp.xml | 9 +++ .../res/drawable/ic_mute_preferences_24dp.xml | 9 +++ app/src/main/res/drawable/ic_unmute_24dp.xml | 9 +++ .../res/layout/exo_playback_control_view.xml | 11 ++- app/src/main/res/values/strings.xml | 1 + app/src/main/res/xml/main_preferences.xml | 6 ++ 10 files changed, 126 insertions(+), 3 deletions(-) create mode 100644 app/src/main/res/drawable-night/ic_mute_preferences_24dp.xml create mode 100644 app/src/main/res/drawable/ic_mute_24dp.xml create mode 100644 app/src/main/res/drawable/ic_mute_preferences_24dp.xml create mode 100644 app/src/main/res/drawable/ic_unmute_24dp.xml diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewVideoActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewVideoActivity.java index 27d937ab..6a2a4fdc 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewVideoActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewVideoActivity.java @@ -7,6 +7,7 @@ import android.animation.ValueAnimator; import android.app.DownloadManager; import android.content.Context; import android.content.Intent; +import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.content.res.Configuration; import android.graphics.PorterDuff; @@ -22,6 +23,7 @@ import android.view.MenuItem; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; +import android.widget.ImageButton; import android.widget.LinearLayout; import android.widget.RelativeLayout; import android.widget.Toast; @@ -37,10 +39,12 @@ import com.github.pwittchen.swipe.library.rx2.Swipe; import com.google.android.exoplayer2.ExoPlayerFactory; import com.google.android.exoplayer2.Player; import com.google.android.exoplayer2.SimpleExoPlayer; +import com.google.android.exoplayer2.source.TrackGroupArray; import com.google.android.exoplayer2.source.hls.HlsMediaSource; import com.google.android.exoplayer2.trackselection.AdaptiveTrackSelection; import com.google.android.exoplayer2.trackselection.DefaultTrackSelector; import com.google.android.exoplayer2.trackselection.TrackSelection; +import com.google.android.exoplayer2.trackselection.TrackSelectionArray; import com.google.android.exoplayer2.trackselection.TrackSelector; import com.google.android.exoplayer2.ui.PlayerView; import com.google.android.exoplayer2.upstream.DataSource; @@ -49,9 +53,13 @@ import com.google.android.exoplayer2.util.Util; import java.io.File; +import javax.inject.Inject; + import butterknife.BindView; import butterknife.ButterKnife; +import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; public class ViewVideoActivity extends AppCompatActivity { @@ -59,10 +67,13 @@ public class ViewVideoActivity extends AppCompatActivity { public static final String EXTRA_SUBREDDIT = "ES"; public static final String EXTRA_ID = "EI"; private static final int PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE = 0; + private static final String IS_MUTE_STATE = "IMS"; @BindView(R.id.relative_layout_view_video_activity) RelativeLayout relativeLayout; @BindView(R.id.player_view_view_video_activity) PlayerView videoPlayerView; + @BindView(R.id.mute_exo_playback_control_view) + ImageButton muteButton; private Uri mVideoUri; private SimpleExoPlayer player; @@ -75,15 +86,22 @@ public class ViewVideoActivity extends AppCompatActivity { private String videoFileName; private boolean wasPlaying; private boolean isDownloading = false; + private boolean isMute = false; private float totalLengthY = 0.0f; private float touchY = -1.0f; + @Inject + SharedPreferences mSharedPreferences; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getTheme().applyStyle(R.style.Theme_Default, true); setContentView(R.layout.activity_view_video); + + ((Infinity) getApplication()).getAppComponent().inject(this); + ButterKnife.bind(this); setVolumeControlStream(AudioManager.STREAM_MUSIC); @@ -293,6 +311,53 @@ public class ViewVideoActivity extends AppCompatActivity { player.setRepeatMode(Player.REPEAT_MODE_ALL); player.setPlayWhenReady(true); wasPlaying = true; + + boolean muteVideo = mSharedPreferences.getBoolean(SharedPreferencesUtils.MUTE_VIDEO, false); + + if (savedInstanceState != null) { + isMute = savedInstanceState.getBoolean(IS_MUTE_STATE); + if (isMute) { + player.setVolume(0f); + muteButton.setImageResource(R.drawable.ic_unmute_24dp); + } else { + player.setVolume(1f); + muteButton.setImageResource(R.drawable.ic_mute_24dp); + } + } else if (muteVideo) { + isMute = true; + player.setVolume(0f); + muteButton.setImageResource(R.drawable.ic_unmute_24dp); + } else { + muteButton.setImageResource(R.drawable.ic_mute_24dp); + } + + player.addListener(new Player.EventListener() { + @Override + public void onTracksChanged(TrackGroupArray trackGroups, TrackSelectionArray trackSelections) { + if (!trackGroups.isEmpty()) { + for (int i = 0; i < trackGroups.length; i++) { + String mimeType = trackGroups.get(i).getFormat(0).sampleMimeType; + if (mimeType != null && mimeType.contains("audio")) { + muteButton.setVisibility(View.VISIBLE); + muteButton.setOnClickListener(view -> { + if (isMute) { + isMute = false; + player.setVolume(1f); + muteButton.setImageResource(R.drawable.ic_mute_24dp); + } else { + isMute = true; + player.setVolume(0f); + muteButton.setImageResource(R.drawable.ic_unmute_24dp); + } + }); + break; + } + } + } else { + muteButton.setVisibility(View.GONE); + } + } + }); } @Override @@ -423,4 +488,10 @@ public class ViewVideoActivity extends AppCompatActivity { super.onPause(); overridePendingTransition(0, 0); } + + @Override + protected void onSaveInstanceState(@NonNull Bundle outState) { + super.onSaveInstanceState(outState); + outState.putBoolean(IS_MUTE_STATE, isMute); + } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java index df7141bb..b7fe4a79 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java @@ -32,6 +32,7 @@ import ml.docilealligator.infinityforreddit.Activity.ViewPostDetailActivity; import ml.docilealligator.infinityforreddit.Activity.ViewSidebarActivity; import ml.docilealligator.infinityforreddit.Activity.ViewSubredditDetailActivity; import ml.docilealligator.infinityforreddit.Activity.ViewUserDetailActivity; +import ml.docilealligator.infinityforreddit.Activity.ViewVideoActivity; import ml.docilealligator.infinityforreddit.Fragment.CommentsListingFragment; import ml.docilealligator.infinityforreddit.Fragment.FlairBottomSheetFragment; import ml.docilealligator.infinityforreddit.Fragment.FollowedUsersListingFragment; @@ -125,4 +126,6 @@ public interface AppComponent { void inject(ViewMultiRedditDetailActivity viewMultiRedditDetailActivity); void inject(ViewSidebarActivity viewSidebarActivity); + + void inject(ViewVideoActivity viewVideoActivity); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java index ef6354aa..b0434d82 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java @@ -65,4 +65,5 @@ public class SharedPreferencesUtils { public static final String SWIPE_RIGHT_TO_GO_BACK_FROM_POST_DETAIL = "swipe_to_go_back_from_post_detail"; public static final String VOLUME_KEYS_NAVIGATE_COMMENTS = "volume_keys_navigate_comments"; public static final String VOLUME_KEYS_NAVIGATE_POSTS = "volume_keys_navigate_posts"; + public static final String MUTE_VIDEO = "mute_video"; } diff --git a/app/src/main/res/drawable-night/ic_mute_preferences_24dp.xml b/app/src/main/res/drawable-night/ic_mute_preferences_24dp.xml new file mode 100644 index 00000000..ff764c78 --- /dev/null +++ b/app/src/main/res/drawable-night/ic_mute_preferences_24dp.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/drawable/ic_mute_24dp.xml b/app/src/main/res/drawable/ic_mute_24dp.xml new file mode 100644 index 00000000..ff764c78 --- /dev/null +++ b/app/src/main/res/drawable/ic_mute_24dp.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/drawable/ic_mute_preferences_24dp.xml b/app/src/main/res/drawable/ic_mute_preferences_24dp.xml new file mode 100644 index 00000000..c206c848 --- /dev/null +++ b/app/src/main/res/drawable/ic_mute_preferences_24dp.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/drawable/ic_unmute_24dp.xml b/app/src/main/res/drawable/ic_unmute_24dp.xml new file mode 100644 index 00000000..d2c9bb24 --- /dev/null +++ b/app/src/main/res/drawable/ic_unmute_24dp.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/layout/exo_playback_control_view.xml b/app/src/main/res/layout/exo_playback_control_view.xml index ed305e41..d76c46fc 100644 --- a/app/src/main/res/layout/exo_playback_control_view.xml +++ b/app/src/main/res/layout/exo_playback_control_view.xml @@ -18,16 +18,21 @@ android:orientation="horizontal"> + style="@style/ExoMediaButton.Rewind" /> + style="@style/ExoMediaButton.Pause" /> + style="@style/ExoMediaButton.FastForward" /> + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 64cbedf9..dc6bcf92 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -298,6 +298,7 @@ Vote Buttons on the Right Use Volume Keys to Navigate Comments in Posts Use Volume Keys to Navigate Posts + Mute Video Show Elapsed Time in Posts and Comments Swipe Right to Go Back From Comments Lazy Mode Interval diff --git a/app/src/main/res/xml/main_preferences.xml b/app/src/main/res/xml/main_preferences.xml index 02c0f750..561222cd 100644 --- a/app/src/main/res/xml/main_preferences.xml +++ b/app/src/main/res/xml/main_preferences.xml @@ -56,6 +56,12 @@ app:key="volume_keys_navigate_posts" app:title="@string/settings_volume_keys_navigate_posts_title" /> + +