From ef180f082ef4063eb0bd52186d303bbf94a81677 Mon Sep 17 00:00:00 2001 From: "Andrei.Rosca" Date: Fri, 1 Jun 2018 22:36:09 +0200 Subject: [PATCH] Remember brightness for the session --- .../newpipe/player/MainVideoPlayer.java | 11 ++++++- .../newpipe/player/helper/PlayerHelper.java | 32 ++++++++++++++++++- app/src/main/res/values/settings_keys.xml | 2 ++ 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/player/MainVideoPlayer.java b/app/src/main/java/org/schabi/newpipe/player/MainVideoPlayer.java index fbf0000bb..0dea47e56 100644 --- a/app/src/main/java/org/schabi/newpipe/player/MainVideoPlayer.java +++ b/app/src/main/java/org/schabi/newpipe/player/MainVideoPlayer.java @@ -119,6 +119,10 @@ public final class MainVideoPlayer extends AppCompatActivity if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) getWindow().setStatusBarColor(Color.BLACK); setVolumeControlStream(AudioManager.STREAM_MUSIC); + WindowManager.LayoutParams lp = getWindow().getAttributes(); + lp.screenBrightness = PlayerHelper.getScreenBrightness(getApplicationContext()); + getWindow().setAttributes(lp); + hideSystemUi(); setContentView(R.layout.activity_main_player); playerImpl = new VideoPlayerImpl(this); @@ -205,6 +209,9 @@ public final class MainVideoPlayer extends AppCompatActivity if (DEBUG) Log.d(TAG, "onStop() called"); super.onStop(); playerImpl.destroy(); + + PlayerHelper.setScreenBrightness(getApplicationContext(), + getWindow().getAttributes().screenBrightness); } /*////////////////////////////////////////////////////////////////////////// @@ -884,7 +891,9 @@ public final class MainVideoPlayer extends AppCompatActivity private final boolean isPlayerGestureEnabled = PlayerHelper.isPlayerGestureEnabled(getApplicationContext()); private final float stepsBrightness = 15, stepBrightness = (1f / stepsBrightness), minBrightness = .01f; - private float currentBrightness = .5f; + private float currentBrightness = getWindow().getAttributes().screenBrightness > 0 + ? getWindow().getAttributes().screenBrightness + : 0.5f; private int currentVolume, maxVolume = playerImpl.getAudioReactor().getMaxVolume(); private final float stepsVolume = 15, stepVolume = (float) Math.ceil(maxVolume / stepsVolume), minVolume = 0; diff --git a/app/src/main/java/org/schabi/newpipe/player/helper/PlayerHelper.java b/app/src/main/java/org/schabi/newpipe/player/helper/PlayerHelper.java index 4ae8eec2a..dbe0e9f46 100644 --- a/app/src/main/java/org/schabi/newpipe/player/helper/PlayerHelper.java +++ b/app/src/main/java/org/schabi/newpipe/player/helper/PlayerHelper.java @@ -9,9 +9,9 @@ import android.support.annotation.Nullable; import android.view.accessibility.CaptioningManager; import com.google.android.exoplayer2.SeekParameters; +import com.google.android.exoplayer2.text.CaptionStyleCompat; import com.google.android.exoplayer2.trackselection.AdaptiveTrackSelection; import com.google.android.exoplayer2.trackselection.TrackSelection; -import com.google.android.exoplayer2.text.CaptionStyleCompat; import com.google.android.exoplayer2.ui.AspectRatioFrameLayout; import com.google.android.exoplayer2.upstream.BandwidthMeter; import com.google.android.exoplayer2.util.MimeTypes; @@ -37,6 +37,7 @@ import java.util.HashSet; import java.util.List; import java.util.Locale; import java.util.Set; +import java.util.concurrent.TimeUnit; import static com.google.android.exoplayer2.ui.AspectRatioFrameLayout.RESIZE_MODE_FILL; import static com.google.android.exoplayer2.ui.AspectRatioFrameLayout.RESIZE_MODE_FIT; @@ -260,6 +261,16 @@ public class PlayerHelper { return captioningManager.getFontScale(); } + + public static float getScreenBrightness(@NonNull final Context context) { + //a value of less than 0, the default, means to use the preferred screen brightness + return getScreenBrightness(context, -1); + } + + public static void setScreenBrightness(@NonNull final Context context, final float setScreenBrightness) { + setScreenBrightness(context, setScreenBrightness, System.currentTimeMillis()); + } + //////////////////////////////////////////////////////////////////////////// // Private helpers //////////////////////////////////////////////////////////////////////////// @@ -292,4 +303,23 @@ public class PlayerHelper { private static boolean isAutoQueueEnabled(@NonNull final Context context, final boolean b) { return getPreferences(context).getBoolean(context.getString(R.string.auto_queue_key), b); } + + private static void setScreenBrightness(@NonNull final Context context, final float screenBrightness, final long timestamp) { + SharedPreferences.Editor editor = getPreferences(context).edit(); + editor.putFloat(context.getString(R.string.screen_brightness_key), screenBrightness); + editor.putLong(context.getString(R.string.screen_brightness_timestamp_key), timestamp); + editor.apply(); + } + + private static float getScreenBrightness(@NonNull final Context context, final float screenBrightness) { + SharedPreferences sp = getPreferences(context); + long timestamp = sp.getLong(context.getString(R.string.screen_brightness_timestamp_key), 0); + // hypothesis: 4h covers a viewing block, eg evening. External lightning conditions will change in the next + // viewing block so we fall back to the default brightness + if ((System.currentTimeMillis() - timestamp) > TimeUnit.HOURS.toMillis(4)) { + return screenBrightness; + } else { + return sp.getFloat(context.getString(R.string.screen_brightness_key), screenBrightness); + } + } } diff --git a/app/src/main/res/values/settings_keys.xml b/app/src/main/res/values/settings_keys.xml index 438cbc0c2..fc7cfd1b4 100644 --- a/app/src/main/res/values/settings_keys.xml +++ b/app/src/main/res/values/settings_keys.xml @@ -22,6 +22,8 @@ popup_remember_size_pos_key use_inexact_seek_key auto_queue_key + screen_brightness_key + screen_brightness_timestamp_key default_resolution 360p