SponsorBlock: Improved category settings screen UX

Added color previews and Reset Colors option in the category settings screen. Changed segment skipping toast message to reflect the type of segment skipped. Also some more setting string refactoring.
This commit is contained in:
polymorphicshade 2020-08-05 17:26:39 -06:00
parent 99810fb02a
commit 8a1e474b6f
9 changed files with 233 additions and 40 deletions

View file

@ -717,18 +717,47 @@ public abstract class BasePlayer implements
); );
if (mPrefs.getBoolean(context.getString(R.string.sponsorblock_enable_key), false)) { if (mPrefs.getBoolean(context.getString(R.string.sponsorblock_enable_key), false)) {
int skipTo = getSponsorEndTimeFromProgress(currentProgress); VideoSegment segment = getSkippableSegment(currentProgress);
if (segment == null) {
if (skipTo == 0) {
return; return;
} }
int skipTo = (int) Math.ceil((segment.endTime));
seekTo(skipTo); seekTo(skipTo);
if (mPrefs.getBoolean( if (mPrefs.getBoolean(
context.getString(R.string.sponsorblock_notifications_key), false)) { context.getString(R.string.sponsorblock_notifications_key), false)) {
String toastText = context.getString(R.string.sponsorblock_skipped_segment); String toastText = "";
Toast.makeText(context, toastText, Toast.LENGTH_SHORT).show();
switch (segment.category) {
case "sponsor":
toastText = context
.getString(R.string.sponsorblock_skip_sponsor_message);
break;
case "intro":
toastText = context
.getString(R.string.sponsorblock_skip_intro_message);
break;
case "outro":
toastText = context
.getString(R.string.sponsorblock_skip_outro_message);
break;
case "interaction":
toastText = context
.getString(R.string.sponsorblock_skip_interaction_message);
break;
case "selfpromo":
toastText = context
.getString(R.string.sponsorblock_skip_self_promo_message);
break;
case "music_offtopic":
toastText = context
.getString(R.string.sponsorblock_skip_non_music_message);
break;
}
Toast.makeText(context, toastText, Toast.LENGTH_LONG).show();
} }
if (DEBUG) { if (DEBUG) {
@ -738,9 +767,9 @@ public abstract class BasePlayer implements
} }
} }
private int getSponsorEndTimeFromProgress(final int progress) { public VideoSegment getSkippableSegment(final int progress) {
if (videoSegments == null) { if (videoSegments == null) {
return 0; return null;
} }
for (VideoSegment segment : videoSegments) { for (VideoSegment segment : videoSegments) {
@ -752,10 +781,10 @@ public abstract class BasePlayer implements
continue; continue;
} }
return (int) Math.ceil((segment.endTime)); return segment;
} }
return 0; return null;
} }
private Disposable getProgressReactor() { private Disposable getProgressReactor() {
@ -1160,7 +1189,7 @@ public abstract class BasePlayer implements
boolean includeMusicCategory = boolean includeMusicCategory =
mPrefs.getBoolean( mPrefs.getBoolean(
context.getString( context.getString(
R.string.sponsorblock_category_music_key), R.string.sponsorblock_category_non_music_key),
false); false);
videoSegments = new SponsorBlockApiTask(apiUrl) videoSegments = new SponsorBlockApiTask(apiUrl)

View file

@ -620,10 +620,10 @@ public abstract class VideoPlayer extends BasePlayer
super.onPrepared(playWhenReady); super.onPrepared(playWhenReady);
markSponsorTimes(); markSegments();
} }
private void markSponsorTimes() { private void markSegments() {
VideoSegment[] segments = getVideoSegments(); VideoSegment[] segments = getVideoSegments();
if (segments == null || segments.length == 0) { if (segments == null || segments.length == 0) {
@ -710,12 +710,12 @@ public abstract class VideoPlayer extends BasePlayer
} }
break; break;
case "music_offtopic": case "music_offtopic":
key = context.getString(R.string.sponsorblock_category_music_key); key = context.getString(R.string.sponsorblock_category_non_music_key);
if (mPrefs.getBoolean(key, false)) { if (mPrefs.getBoolean(key, false)) {
key = context.getString(R.string.sponsorblock_category_music_color_key); key = context.getString(R.string.sponsorblock_category_non_music_color_key);
colorStr = mPrefs.getString(key, null); colorStr = mPrefs.getString(key, null);
return colorStr == null return colorStr == null
? context.getResources().getColor(R.color.music_offtopic_segment) ? context.getResources().getColor(R.color.non_music_segment)
: Color.parseColor(colorStr); : Color.parseColor(colorStr);
} }
break; break;

View file

@ -1,12 +1,77 @@
package org.schabi.newpipe.settings; package org.schabi.newpipe.settings;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle; import android.os.Bundle;
import androidx.annotation.ColorRes;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.preference.Preference;
import org.schabi.newpipe.R; import org.schabi.newpipe.R;
import org.schabi.newpipe.settings.custom.EditColorPreference;
public class SponsorBlockCategoriesSettingsFragment extends BasePreferenceFragment { public class SponsorBlockCategoriesSettingsFragment extends BasePreferenceFragment {
@Override
public void onCreate(@Nullable final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override @Override
public void onCreatePreferences(final Bundle savedInstanceState, final String rootKey) { public void onCreatePreferences(final Bundle savedInstanceState, final String rootKey) {
addPreferencesFromResource(R.xml.sponsor_block_category_settings); addPreferencesFromResource(R.xml.sponsor_block_category_settings);
Preference resetPreferenceView =
findPreference(getString(R.string.sponsorblock_category_reset_key));
if (resetPreferenceView != null) {
resetPreferenceView.setOnPreferenceClickListener(preference -> {
Context context = getContext();
if (context != null) {
SharedPreferences.Editor editor =
getPreferenceManager()
.getSharedPreferences()
.edit();
setColorPreference(editor,
R.string.sponsorblock_category_sponsor_color_key,
R.color.sponsor_segment);
setColorPreference(editor,
R.string.sponsorblock_category_intro_color_key,
R.color.intro_segment);
setColorPreference(editor,
R.string.sponsorblock_category_outro_color_key,
R.color.outro_segment);
setColorPreference(editor,
R.string.sponsorblock_category_interaction_color_key,
R.color.interaction_segment);
setColorPreference(editor,
R.string.sponsorblock_category_self_promo_color_key,
R.color.self_promo_segment);
setColorPreference(editor,
R.string.sponsorblock_category_non_music_color_key,
R.color.non_music_segment);
editor.apply();
}
return true;
});
}
}
private void setColorPreference(final SharedPreferences.Editor editor,
@StringRes final int resId,
@ColorRes final int colorId) {
String colorStr = "#" + Integer.toHexString(getResources().getColor(colorId));
editor.putString(getString(resId), colorStr);
EditColorPreference colorPreference = findPreference(getString(resId));
colorPreference.setText(colorStr);
} }
} }

View file

@ -0,0 +1,81 @@
package org.schabi.newpipe.settings.custom;
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Toast;
import androidx.preference.EditTextPreference;
import androidx.preference.Preference;
import androidx.preference.PreferenceViewHolder;
import org.schabi.newpipe.R;
public class EditColorPreference extends EditTextPreference
implements Preference.OnPreferenceChangeListener {
private PreferenceViewHolder viewHolder;
public EditColorPreference(final Context context, final AttributeSet attrs,
final int defStyleAttr, final int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init();
}
public EditColorPreference(final Context context, final AttributeSet attrs,
final int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
public EditColorPreference(final Context context, final AttributeSet attrs) {
super(context, attrs);
init();
}
public EditColorPreference(final Context context) {
super(context);
init();
}
private void init() {
setWidgetLayoutResource(R.layout.preference_edit_color);
setOnPreferenceChangeListener(this);
}
@Override
public void onBindViewHolder(final PreferenceViewHolder holder) {
super.onBindViewHolder(holder);
viewHolder = holder;
String colorStr =
getPreferenceManager()
.getSharedPreferences()
.getString(getKey(), null);
if (colorStr == null) {
return;
}
int color = Color.parseColor(colorStr);
View view = viewHolder.findViewById(R.id.segment_color_view);
view.setBackgroundColor(color);
}
@Override
public boolean onPreferenceChange(final Preference preference, final Object newValue) {
try {
int color = Color.parseColor((String) newValue);
View view = viewHolder.findViewById(R.id.segment_color_view);
view.setBackgroundColor(color);
return true;
} catch (Exception e) {
Toast.makeText(getContext(), "Invalid color", Toast.LENGTH_SHORT).show();
return false;
}
}
}

View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<View xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/segment_color_view"
android:layout_width="98dp"
android:layout_height="6dp"
android:background="@color/black"/>

View file

@ -89,5 +89,5 @@
<color name="outro_segment">#0202ed</color> <color name="outro_segment">#0202ed</color>
<color name="interaction_segment">#cc00ff</color> <color name="interaction_segment">#cc00ff</color>
<color name="self_promo_segment">#ffff00</color> <color name="self_promo_segment">#ffff00</color>
<color name="music_offtopic_segment">#ff9900</color> <color name="non_music_segment">#ff9900</color>
</resources> </resources>

View file

@ -245,6 +245,7 @@
<string name="sponsorblock_notifications_key" translatable="false">sponsorblock_notifications</string> <string name="sponsorblock_notifications_key" translatable="false">sponsorblock_notifications</string>
<string name="sponsorblock_privacy_key" translatable="false">sponsorblock_privacy</string> <string name="sponsorblock_privacy_key" translatable="false">sponsorblock_privacy</string>
<string name="sponsorblock_categories_key" translatable="false">sponsorblock_categories</string> <string name="sponsorblock_categories_key" translatable="false">sponsorblock_categories</string>
<string name="sponsorblock_category_reset_key" translatable="false">sponsorblock_category_reset</string>
<string name="sponsorblock_category_sponsor_key" translatable="false">sponsorblock_category_sponsor</string> <string name="sponsorblock_category_sponsor_key" translatable="false">sponsorblock_category_sponsor</string>
<string name="sponsorblock_category_sponsor_color_key" translatable="false">sponsorblock_category_sponsor_color</string> <string name="sponsorblock_category_sponsor_color_key" translatable="false">sponsorblock_category_sponsor_color</string>
<string name="sponsorblock_category_intro_key" translatable="false">sponsorblock_category_intro</string> <string name="sponsorblock_category_intro_key" translatable="false">sponsorblock_category_intro</string>
@ -255,8 +256,8 @@
<string name="sponsorblock_category_interaction_color_key" translatable="false">sponsorblock_category_interaction_color</string> <string name="sponsorblock_category_interaction_color_key" translatable="false">sponsorblock_category_interaction_color</string>
<string name="sponsorblock_category_self_promo_key" translatable="false">sponsorblock_category_self_promo</string> <string name="sponsorblock_category_self_promo_key" translatable="false">sponsorblock_category_self_promo</string>
<string name="sponsorblock_category_self_promo_color_key" translatable="false">sponsorblock_category_self_promo_color</string> <string name="sponsorblock_category_self_promo_color_key" translatable="false">sponsorblock_category_self_promo_color</string>
<string name="sponsorblock_category_music_key" translatable="false">sponsorblock_category_music</string> <string name="sponsorblock_category_non_music_key" translatable="false">sponsorblock_category_music</string>
<string name="sponsorblock_category_music_color_key" translatable="false">sponsorblock_category_music_color</string> <string name="sponsorblock_category_non_music_color_key" translatable="false">sponsorblock_category_music_color</string>
<!-- FileName Downloads --> <!-- FileName Downloads -->
<string name="settings_file_charset_key" translatable="false">file_rename_charset</string> <string name="settings_file_charset_key" translatable="false">file_rename_charset</string>

View file

@ -135,7 +135,8 @@
<string name="settings_category_sponsorblock_title">SponsorBlock (beta, third party service)</string> <string name="settings_category_sponsorblock_title">SponsorBlock (beta, third party service)</string>
<string name="settings_category_sponsorblock_categories_title">SponsorBlock Categories</string> <string name="settings_category_sponsorblock_categories_title">SponsorBlock Categories</string>
<string name="settings_category_sponsorblock_categories_summary">Customize which video segments to skip, along with their color markings on the seek bar.</string> <string name="settings_category_sponsorblock_categories_summary">Customize which video segments to skip, along with their color markings on the seek bar.</string>
<string name="settings_category_sponsorblock_category_enable">Enable</string> <string name="settings_category_sponsorblock_categories_reset_colors_title">Reset Colors</string>
<string name="settings_category_sponsorblock_category_enable_title">Enable</string>
<string name="settings_category_sponsorblock_category_color">Seek Bar Color</string> <string name="settings_category_sponsorblock_category_color">Seek Bar Color</string>
<string name="settings_category_sponsorblock_category_sponsor_title">Sponsor</string> <string name="settings_category_sponsorblock_category_sponsor_title">Sponsor</string>
<string name="settings_category_sponsorblock_category_sponsor_summary">Paid promotion, paid referrals and direct advertisements. Not for self-promotion or free shoutouts to causes/creators/websites/products they like.</string> <string name="settings_category_sponsorblock_category_sponsor_summary">Paid promotion, paid referrals and direct advertisements. Not for self-promotion or free shoutouts to causes/creators/websites/products they like.</string>
@ -147,8 +148,8 @@
<string name="settings_category_sponsorblock_category_interaction_summary">When there is a short reminder to like, subscribe or follow them in the middle of content. If it is long or about something specific, it should be under self promotion instead.</string> <string name="settings_category_sponsorblock_category_interaction_summary">When there is a short reminder to like, subscribe or follow them in the middle of content. If it is long or about something specific, it should be under self promotion instead.</string>
<string name="settings_category_sponsorblock_category_self_promo_title">Unpaid/Self Promotion</string> <string name="settings_category_sponsorblock_category_self_promo_title">Unpaid/Self Promotion</string>
<string name="settings_category_sponsorblock_category_self_promo_summary">Similar to "sponsor" except for unpaid or self promotion. This includes sections about merchandise, donations, or information about who they collaborated with.</string> <string name="settings_category_sponsorblock_category_self_promo_summary">Similar to "sponsor" except for unpaid or self promotion. This includes sections about merchandise, donations, or information about who they collaborated with.</string>
<string name="settings_category_sponsorblock_category_music_title">Music: Non-Music Section</string> <string name="settings_category_sponsorblock_category_non_music_title">Music: Non-Music Section</string>
<string name="settings_category_sponsorblock_category_music_summary">Only for use in music videos. This includes introductions or outros in music videos.</string> <string name="settings_category_sponsorblock_category_non_music_summary">Only for use in music videos. This includes introductions or outros in music videos.</string>
<string name="background_player_playing_toast">Playing in background</string> <string name="background_player_playing_toast">Playing in background</string>
<string name="popup_playing_toast">Playing in popup mode</string> <string name="popup_playing_toast">Playing in popup mode</string>
<string name="background_player_append">Queued on background player</string> <string name="background_player_append">Queued on background player</string>
@ -633,7 +634,6 @@
<string name="remove_watched_popup_warning">Videos that have been watched before and after being added to the playlist will be removed.\nAre you sure? This cannot be undone!</string> <string name="remove_watched_popup_warning">Videos that have been watched before and after being added to the playlist will be removed.\nAre you sure? This cannot be undone!</string>
<string name="remove_watched_popup_yes_and_partially_watched_videos">Yes, and partially watched videos</string> <string name="remove_watched_popup_yes_and_partially_watched_videos">Yes, and partially watched videos</string>
<string name="new_seek_duration_toast">Due to ExoPlayer constraints the seek duration was set to %d seconds</string> <string name="new_seek_duration_toast">Due to ExoPlayer constraints the seek duration was set to %d seconds</string>
<string name="sponsorblock_skipped_segment">Segment skipped</string>
<!-- Time duration plurals --> <!-- Time duration plurals -->
<plurals name="seconds"> <plurals name="seconds">
<item quantity="one">%d second</item> <item quantity="one">%d second</item>
@ -699,4 +699,10 @@
<string name="sponsorblock_homepage_url">https://sponsor.ajay.app/</string> <string name="sponsorblock_homepage_url">https://sponsor.ajay.app/</string>
<string name="sponsorblock_default_api_url">https://sponsor.ajay.app/api/</string> <string name="sponsorblock_default_api_url">https://sponsor.ajay.app/api/</string>
<string name="sponsorblock_privacy_policy_url">https://gist.github.com/ajayyy/aa9f8ded2b573d4f73a3ffa0ef74f796</string> <string name="sponsorblock_privacy_policy_url">https://gist.github.com/ajayyy/aa9f8ded2b573d4f73a3ffa0ef74f796</string>
<string name="sponsorblock_skip_sponsor_message">Skipped sponsor</string>
<string name="sponsorblock_skip_intro_message">Skipped intermission/intro segment</string>
<string name="sponsorblock_skip_outro_message">Skipped endcards/credits segment</string>
<string name="sponsorblock_skip_interaction_message">Skipped interaction reminder segment</string>
<string name="sponsorblock_skip_self_promo_message">Skipped unpaid/self promo segment</string>
<string name="sponsorblock_skip_non_music_message">Skipped non-music segment</string>
</resources> </resources>

View file

@ -4,6 +4,11 @@
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
android:title="@string/settings_category_sponsorblock_categories_title"> android:title="@string/settings_category_sponsorblock_categories_title">
<Preference
app:iconSpaceReserved="false"
android:key="@string/sponsorblock_category_reset_key"
android:title="@string/settings_category_sponsorblock_categories_reset_colors_title"/>
<PreferenceCategory <PreferenceCategory
android:layout="@layout/settings_category_header_layout" android:layout="@layout/settings_category_header_layout"
android:title="@string/settings_category_sponsorblock_category_sponsor_title"> android:title="@string/settings_category_sponsorblock_category_sponsor_title">
@ -17,9 +22,9 @@
app:iconSpaceReserved="false" app:iconSpaceReserved="false"
android:defaultValue="true" android:defaultValue="true"
android:key="@string/sponsorblock_category_sponsor_key" android:key="@string/sponsorblock_category_sponsor_key"
android:title="@string/settings_category_sponsorblock_category_enable"/> android:title="@string/settings_category_sponsorblock_category_enable_title"/>
<EditTextPreference <org.schabi.newpipe.settings.custom.EditColorPreference
app:iconSpaceReserved="false" app:iconSpaceReserved="false"
android:dependency="@string/sponsorblock_category_sponsor_key" android:dependency="@string/sponsorblock_category_sponsor_key"
android:defaultValue="@color/sponsor_segment" android:defaultValue="@color/sponsor_segment"
@ -41,9 +46,9 @@
app:iconSpaceReserved="false" app:iconSpaceReserved="false"
android:defaultValue="false" android:defaultValue="false"
android:key="@string/sponsorblock_category_intro_key" android:key="@string/sponsorblock_category_intro_key"
android:title="@string/settings_category_sponsorblock_category_enable"/> android:title="@string/settings_category_sponsorblock_category_enable_title"/>
<EditTextPreference <org.schabi.newpipe.settings.custom.EditColorPreference
app:iconSpaceReserved="false" app:iconSpaceReserved="false"
android:dependency="@string/sponsorblock_category_intro_key" android:dependency="@string/sponsorblock_category_intro_key"
android:defaultValue="@color/intro_segment" android:defaultValue="@color/intro_segment"
@ -65,9 +70,9 @@
app:iconSpaceReserved="false" app:iconSpaceReserved="false"
android:defaultValue="false" android:defaultValue="false"
android:key="@string/sponsorblock_category_outro_key" android:key="@string/sponsorblock_category_outro_key"
android:title="@string/settings_category_sponsorblock_category_enable"/> android:title="@string/settings_category_sponsorblock_category_enable_title"/>
<EditTextPreference <org.schabi.newpipe.settings.custom.EditColorPreference
app:iconSpaceReserved="false" app:iconSpaceReserved="false"
android:dependency="@string/sponsorblock_category_outro_key" android:dependency="@string/sponsorblock_category_outro_key"
android:defaultValue="@color/outro_segment" android:defaultValue="@color/outro_segment"
@ -89,9 +94,9 @@
app:iconSpaceReserved="false" app:iconSpaceReserved="false"
android:defaultValue="false" android:defaultValue="false"
android:key="@string/sponsorblock_category_interaction_key" android:key="@string/sponsorblock_category_interaction_key"
android:title="@string/settings_category_sponsorblock_category_enable"/> android:title="@string/settings_category_sponsorblock_category_enable_title"/>
<EditTextPreference <org.schabi.newpipe.settings.custom.EditColorPreference
app:iconSpaceReserved="false" app:iconSpaceReserved="false"
android:dependency="@string/sponsorblock_category_interaction_key" android:dependency="@string/sponsorblock_category_interaction_key"
android:defaultValue="@color/interaction_segment" android:defaultValue="@color/interaction_segment"
@ -113,9 +118,9 @@
app:iconSpaceReserved="false" app:iconSpaceReserved="false"
android:defaultValue="false" android:defaultValue="false"
android:key="@string/sponsorblock_category_self_promo_key" android:key="@string/sponsorblock_category_self_promo_key"
android:title="@string/settings_category_sponsorblock_category_enable"/> android:title="@string/settings_category_sponsorblock_category_enable_title"/>
<EditTextPreference <org.schabi.newpipe.settings.custom.EditColorPreference
app:iconSpaceReserved="false" app:iconSpaceReserved="false"
android:dependency="@string/sponsorblock_category_self_promo_key" android:dependency="@string/sponsorblock_category_self_promo_key"
android:defaultValue="@color/self_promo_segment" android:defaultValue="@color/self_promo_segment"
@ -126,24 +131,24 @@
<PreferenceCategory <PreferenceCategory
android:layout="@layout/settings_category_header_layout" android:layout="@layout/settings_category_header_layout"
android:title="@string/settings_category_sponsorblock_category_music_title"> android:title="@string/settings_category_sponsorblock_category_non_music_title">
<Preference <Preference
app:iconSpaceReserved="false" app:iconSpaceReserved="false"
android:summary="@string/settings_category_sponsorblock_category_music_summary" android:summary="@string/settings_category_sponsorblock_category_non_music_summary"
android:selectable="false"/> android:selectable="false"/>
<SwitchPreference <SwitchPreference
app:iconSpaceReserved="false" app:iconSpaceReserved="false"
android:defaultValue="false" android:defaultValue="false"
android:key="@string/sponsorblock_category_music_key" android:key="@string/sponsorblock_category_non_music_key"
android:title="@string/settings_category_sponsorblock_category_enable"/> android:title="@string/settings_category_sponsorblock_category_enable_title"/>
<EditTextPreference <org.schabi.newpipe.settings.custom.EditColorPreference
app:iconSpaceReserved="false" app:iconSpaceReserved="false"
android:dependency="@string/sponsorblock_category_music_key" android:dependency="@string/sponsorblock_category_non_music_key"
android:defaultValue="@color/music_offtopic_segment" android:defaultValue="@color/non_music_segment"
android:key="@string/sponsorblock_category_music_color_key" android:key="@string/sponsorblock_category_non_music_color_key"
android:title="@string/settings_category_sponsorblock_category_color"/> android:title="@string/settings_category_sponsorblock_category_color"/>
</PreferenceCategory> </PreferenceCategory>