afr option refactor

This commit is contained in:
Yuriy Liskov
2020-09-21 02:47:38 +03:00
parent b7d66dccf4
commit 1f7a3776c3
6 changed files with 83 additions and 29 deletions
@@ -13,6 +13,7 @@ public class AutoFrameRateManager extends PlayerEventListenerHelper {
private boolean mEnabled;
private boolean mCorrectionEnabled;
private boolean mInitDone;
private FormatItem mSelectedVideoTrack;
public AutoFrameRateManager(PlayerUiManager uiManager) {
mUiManager = uiManager;
@@ -22,28 +23,42 @@ public class AutoFrameRateManager extends PlayerEventListenerHelper {
public void setController(PlayerController controller) {
super.setController(controller);
mAutoFrameRateHelper = new AutoFrameRateHelper(mActivity);
if (!mInitDone) {
mAutoFrameRateHelper = new AutoFrameRateHelper(mActivity);
String title = mActivity.getString(R.string.auto_frame_rate_enable);
String fpsCorrection = mActivity.getString(R.string.auto_frame_rate_correction, "(30 => 29.97)");
mUiManager.addHQSwitch(title, UiOptionItem.from(title,
(optionItem) -> mEnabled = optionItem.isSelected(), mEnabled));
(optionItem) -> {
mEnabled = optionItem.isSelected();
if (mEnabled) {
mAutoFrameRateHelper.apply(mSelectedVideoTrack);
} else {
mAutoFrameRateHelper.restoreOriginalState();
}
}, mEnabled));
mUiManager.addHQSwitch(title, UiOptionItem.from(fpsCorrection,
(optionItem) -> mCorrectionEnabled = optionItem.isSelected(), mCorrectionEnabled));
(optionItem) -> {
mCorrectionEnabled = optionItem.isSelected();
mAutoFrameRateHelper.setFpsCorrectionEnabled(mCorrectionEnabled);
}, mCorrectionEnabled));
mInitDone = true;
} else {
mAutoFrameRateHelper.updateActivity(mActivity);
}
}
@Override
public void onTrackChanged(FormatItem track) {
if (!mEnabled) {
return;
}
if (track.getType() == FormatItem.TYPE_VIDEO) {
mAutoFrameRateHelper.apply(track);
mSelectedVideoTrack = track;
if (mEnabled) {
mAutoFrameRateHelper.apply(track);
}
}
}
}
@@ -10,7 +10,7 @@ import java.util.HashMap;
public class AutoFrameRateHelper {
private static final String TAG = AutoFrameRateHelper.class.getSimpleName();
private final Activity mActivity;
private Activity mActivity;
private final DisplaySyncHelper mSyncHelper;
private static final long THROTTLE_INTERVAL_MS = 5_000;
private long mPrevCall;
@@ -32,8 +32,13 @@ public class AutoFrameRateHelper {
}
public void apply(FormatItem format) {
if (mActivity == null) {
Log.e(TAG, "Activity in null. exiting...");
return;
}
if (!isSupported()) {
Log.d(TAG, "Autoframerate not supported... exiting...");
Log.d(TAG, "Autoframerate not supported. Exiting...");
return;
}
@@ -69,6 +74,11 @@ public class AutoFrameRateHelper {
}
private void saveOriginalState() {
if (mActivity == null) {
Log.e(TAG, "Activity in null. exiting...");
return;
}
if (!isSupported()) {
return;
}
@@ -121,4 +131,9 @@ public class AutoFrameRateHelper {
mSyncHelper.applyModeChangeFix(mActivity.getWindow());
}
public void updateActivity(Activity activity) {
mActivity = activity;
mSyncHelper.setContext(activity);
}
}
@@ -21,7 +21,7 @@ public class DisplaySyncHelper implements UhdHelperListener {
private static final String TAG = DisplaySyncHelper.class.getSimpleName();
private static final int STATE_ORIGINAL = 1;
private static final int STATE_CURRENT = 2;
protected final Context mContext;
protected Context mContext;
private boolean mDisplaySyncInProgress = false;
private UhdHelper mUhdHelper;
protected Mode mOriginalMode;
@@ -454,4 +454,8 @@ public class DisplaySyncHelper implements UhdHelperListener {
public boolean isResolutionSwitchEnabled() {
return mSwitchToUHD || mSwitchToFHD;
}
public void setContext(Context context) {
mContext = context;
}
}
@@ -154,11 +154,14 @@ public class ExoPlayerController implements EventListener, PlayerController {
@Override
public void onTracksChanged(TrackGroupArray trackGroups, TrackSelectionArray trackSelections) {
Log.d(TAG, "onTracksChanged: start");
Log.d(TAG, "onTracksChanged: start: groups length: " + trackGroups.length);
if (trackGroups.length == 0) {
Log.i(TAG, "onTracksChanged: Hmm. Strange. Received empty groups, no selections. Why is this happens only on next/prev videos?");
}
for (TrackSelection selection : trackSelections.getAll()) {
if (selection != null) {
Log.d(TAG, "onTracksChanged: format: " + selection.getSelectedFormat());
mEventListener.onTrackChanged(ExoFormatItem.from(selection.getSelectedFormat()));
}
}
@@ -17,7 +17,9 @@ import com.liskovsoft.smartyoutubetv2.common.app.presenters.VideoSettingsPresent
import com.liskovsoft.smartyoutubetv2.common.app.presenters.VideoSettingsPresenter.SettingsCategory;
import com.liskovsoft.smartyoutubetv2.common.app.views.VideoSettingsView;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class VideoSettingsFragment extends LeanbackSettingsFragment
implements DialogPreference.TargetFragment, VideoSettingsView {
@@ -159,7 +161,7 @@ public class VideoSettingsFragment extends LeanbackSettingsFragment
pref.setEntryValues(prefData.values);
pref.setValue(prefData.defaultValue);
// don't close menu on select
// TODO: don't close menu on select
pref.setOnPreferenceChangeListener((preference, newValue) -> {
for (OptionItem optionItem : category.items) {
@@ -184,14 +186,22 @@ public class VideoSettingsFragment extends LeanbackSettingsFragment
pref.setEntries(prefData.entries);
pref.setEntryValues(prefData.values);
pref.setValues(prefData.defaultValues);
// don't close menu on select
// TODO: don't close menu on select
pref.setOnPreferenceChangeListener((preference, newValue) -> {
for (OptionItem optionItem : category.items) {
if (newValue.equals(optionItem.toString())) {
optionItem.onSelect(true);
break;
if (newValue instanceof Set) {
Set values = ((Set) newValue);
for (OptionItem item : category.items) {
boolean found = false;
for (Object value : values) {
found = value.equals(item.toString());
if (found) {
break;
}
}
item.onSelect(found);
}
}
@@ -205,30 +215,37 @@ public class VideoSettingsFragment extends LeanbackSettingsFragment
CharSequence[] titles = new CharSequence[items.size()];
CharSequence[] hashes = new CharSequence[items.size()];
String defaultValue = null;
Set<String> defaultValues = new HashSet<>(); // used in multi set lists
for (int i = 0; i < items.size(); i++) {
OptionItem optionItem = items.get(i);
titles[i] = optionItem.getTitle();
hashes[i] = optionItem.toString();
CharSequence title = optionItem.getTitle();
String value = optionItem.toString();
titles[i] = title;
hashes[i] = value;
if (optionItem.isSelected()) {
defaultValue = optionItem.toString();
defaultValue = value;
defaultValues.add(value);
}
}
return new ListPrefData(titles, hashes, defaultValue);
return new ListPrefData(titles, hashes, defaultValue, defaultValues);
}
private static class ListPrefData {
public final CharSequence[] entries;
public final CharSequence[] values;
public final String defaultValue;
public final Set<String> defaultValues;
public ListPrefData(CharSequence[] entries, CharSequence[] values, String defaultValue) {
this.entries = entries;
this.values = values;
this.defaultValue = defaultValue;
public ListPrefData(CharSequence[] entries, CharSequence[] values, String defaultValue, Set<String> defaultValues) {
this.entries = entries;
this.values = values;
this.defaultValue = defaultValue;
this.defaultValues = defaultValues;
}
}
}
@@ -110,7 +110,7 @@
<!-- NEW ITEMS -->
<string name="signin_view_title">User Code is loading…</string>
<string name="signin_view_description">To sign-in enter this code on %s</string>
<string name="signin_view_description">To sign-in enter this code on page %s</string>
<string name="signin_view_action_text">Done</string>
<string name="action_subscribe_off">Subscribe Off</string>
<string name="action_subscribe_on">Subscribe On</string>