From f31b4744e510bbcbb9de14d6fd6b3eb3b59360b1 Mon Sep 17 00:00:00 2001 From: caneleex Date: Tue, 3 Aug 2021 16:23:44 +0200 Subject: [PATCH 1/4] add support for importing/exporting applicable SB settings --- .../SponsorBlockPreferenceFragment.java | 13 +++ .../pl/jakubweg/SponsorBlockSettings.java | 24 +++-- .../java/pl/jakubweg/SponsorBlockUtils.java | 92 +++++++++++++++++++ app/src/main/res/values/strings.xml | 4 + 4 files changed, 127 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/pl/jakubweg/SponsorBlockPreferenceFragment.java b/app/src/main/java/pl/jakubweg/SponsorBlockPreferenceFragment.java index e7da30fe..0949d2b4 100644 --- a/app/src/main/java/pl/jakubweg/SponsorBlockPreferenceFragment.java +++ b/app/src/main/java/pl/jakubweg/SponsorBlockPreferenceFragment.java @@ -298,6 +298,19 @@ public class SponsorBlockPreferenceFragment extends PreferenceFragment implement screen.addPreference(preference); preferencesToDisableWhenSBDisabled.add(preference); } + + { + EditTextPreference preference = new EditTextPreference(context); + preference.setTitle(str("settings_ie")); + preference.setSummary(str("settings_ie_sum")); + preference.setText(SponsorBlockUtils.exportSettings()); + preference.setOnPreferenceChangeListener((preference1, newValue) -> { + SponsorBlockUtils.importSettings((String) newValue, context.getApplicationContext()); + return false; + }); + screen.addPreference(preference); + preferencesToDisableWhenSBDisabled.add(preference); + } } @Override diff --git a/app/src/main/java/pl/jakubweg/SponsorBlockSettings.java b/app/src/main/java/pl/jakubweg/SponsorBlockSettings.java index 87dc819c..44bb5a00 100644 --- a/app/src/main/java/pl/jakubweg/SponsorBlockSettings.java +++ b/app/src/main/java/pl/jakubweg/SponsorBlockSettings.java @@ -1,5 +1,7 @@ package pl.jakubweg; +import static pl.jakubweg.StringRef.sf; + import android.content.Context; import android.content.SharedPreferences; import android.graphics.Color; @@ -13,8 +15,6 @@ import java.util.HashMap; import java.util.Map; import java.util.UUID; -import static pl.jakubweg.StringRef.sf; - public class SponsorBlockSettings { public static final String PREFERENCES_NAME = "sponsor-block"; @@ -119,7 +119,7 @@ public class SponsorBlockSettings { } //"[%22sponsor%22,%22outro%22,%22music_offtopic%22,%22intro%22,%22selfpromo%22,%22interaction%22,%22preview%22]"; - if (enabledCategories.size() == 0) + if (enabledCategories.isEmpty()) sponsorBlockUrlCategories = "[]"; else sponsorBlockUrlCategories = "[%22" + TextUtils.join("%22,%22", enabledCategories) + "%22]"; @@ -146,24 +146,36 @@ public class SponsorBlockSettings { } public enum SegmentBehaviour { - SKIP_AUTOMATICALLY("skip", sf("skip_automatically"), true, true), - MANUAL_SKIP("manual-skip", sf("skip_showbutton"), false, true), - IGNORE("ignore", sf("skip_ignore"), false, false); + SKIP_AUTOMATICALLY("skip", 2, sf("skip_automatically"), true, true), + MANUAL_SKIP("manual-skip", 1, sf("skip_showbutton"), false, true), + IGNORE("ignore", -1, sf("skip_ignore"), false, false); public final String key; + public final int desktopKey; public final StringRef name; public final boolean skip; public final boolean showOnTimeBar; SegmentBehaviour(String key, + int desktopKey, StringRef name, boolean skip, boolean showOnTimeBar) { this.key = key; + this.desktopKey = desktopKey; this.name = name; this.skip = skip; this.showOnTimeBar = showOnTimeBar; } + + public static SegmentBehaviour byDesktopKey(int desktopKey) { + for (SegmentBehaviour behaviour : values()) { + if (behaviour.desktopKey == desktopKey) { + return behaviour; + } + } + return null; + } } public enum SegmentInfo { diff --git a/app/src/main/java/pl/jakubweg/SponsorBlockUtils.java b/app/src/main/java/pl/jakubweg/SponsorBlockUtils.java index febdddbe..74ef177c 100644 --- a/app/src/main/java/pl/jakubweg/SponsorBlockUtils.java +++ b/app/src/main/java/pl/jakubweg/SponsorBlockUtils.java @@ -11,10 +11,18 @@ import static pl.jakubweg.PlayerController.sponsorSegmentsOfCurrentVideo; import static pl.jakubweg.SponsorBlockPreferenceFragment.FORMATTER; import static pl.jakubweg.SponsorBlockPreferenceFragment.SAVED_TEMPLATE; import static pl.jakubweg.SponsorBlockSettings.PREFERENCES_KEY_CATEGORY_COLOR_SUFFIX; +import static pl.jakubweg.SponsorBlockSettings.PREFERENCES_KEY_COUNT_SKIPS; +import static pl.jakubweg.SponsorBlockSettings.PREFERENCES_KEY_SHOW_TIME_WITHOUT_SEGMENTS; +import static pl.jakubweg.SponsorBlockSettings.PREFERENCES_KEY_SHOW_TOAST_WHEN_SKIP; +import static pl.jakubweg.SponsorBlockSettings.PREFERENCES_KEY_UUID; +import static pl.jakubweg.SponsorBlockSettings.countSkips; +import static pl.jakubweg.SponsorBlockSettings.getPreferences; import static pl.jakubweg.SponsorBlockSettings.isSponsorBlockEnabled; import static pl.jakubweg.SponsorBlockSettings.showTimeWithoutSegments; +import static pl.jakubweg.SponsorBlockSettings.showToastWhenSkippedAutomatically; import static pl.jakubweg.SponsorBlockSettings.skippedSegments; import static pl.jakubweg.SponsorBlockSettings.skippedTime; +import static pl.jakubweg.SponsorBlockSettings.uuid; import static pl.jakubweg.StringRef.str; import static pl.jakubweg.requests.Requester.voteForSegment; @@ -38,6 +46,9 @@ import android.widget.EditText; import android.widget.ImageView; import android.widget.Toast; +import org.json.JSONArray; +import org.json.JSONObject; + import java.lang.ref.WeakReference; import java.text.ParseException; import java.text.SimpleDateFormat; @@ -549,6 +560,87 @@ public abstract class SponsorBlockUtils { } } + public static void importSettings(String json, Context context) { + try { + JSONObject settingsJson = new JSONObject(json); + + JSONObject barTypesObject = settingsJson.getJSONObject("barTypes"); + JSONArray categorySelectionsArray = settingsJson.getJSONArray("categorySelections"); + + SharedPreferences.Editor editor = getPreferences(context).edit(); + + SponsorBlockSettings.SegmentInfo[] categories = SponsorBlockSettings.SegmentInfo.valuesWithoutUnsubmitted(); + for (SponsorBlockSettings.SegmentInfo category : categories) { + String categoryKey = category.key; + JSONObject categoryObject = barTypesObject.getJSONObject(categoryKey); + String color = categoryObject.getString("color"); + + editor.putString(categoryKey + PREFERENCES_KEY_CATEGORY_COLOR_SUFFIX, color); + editor.putString(categoryKey, SponsorBlockSettings.SegmentBehaviour.IGNORE.key); + } + + for (int i = 0; i < categorySelectionsArray.length(); i++) { + JSONObject categorySelectionObject = categorySelectionsArray.getJSONObject(i); + + String categoryKey = categorySelectionObject.getString("name"); + SponsorBlockSettings.SegmentInfo category = SponsorBlockSettings.SegmentInfo.byCategoryKey(categoryKey); + + int desktopKey = categorySelectionObject.getInt("option"); + SponsorBlockSettings.SegmentBehaviour behaviour = SponsorBlockSettings.SegmentBehaviour.byDesktopKey(desktopKey); + editor.putString(category.key, behaviour.key); + } + + editor.putBoolean(PREFERENCES_KEY_SHOW_TOAST_WHEN_SKIP, !settingsJson.getBoolean("dontShowNotice")); + editor.putBoolean(PREFERENCES_KEY_SHOW_TIME_WITHOUT_SEGMENTS, settingsJson.getBoolean("showTimeWithSkips")); + editor.putBoolean(PREFERENCES_KEY_COUNT_SKIPS, settingsJson.getBoolean("trackViewCount")); + editor.putString(PREFERENCES_KEY_UUID, settingsJson.getString("userID")); + editor.apply(); + + Toast.makeText(context, str("settings_import_successful"), Toast.LENGTH_SHORT).show(); + } + catch (Exception ex) { + ex.printStackTrace(); + Toast.makeText(context, str("settings_import_failed"), Toast.LENGTH_SHORT).show(); + } + } + + public static String exportSettings() { + try { + JSONObject json = new JSONObject(); + + JSONObject barTypesObject = new JSONObject(); // categories' colors + JSONArray categorySelectionsArray = new JSONArray(); // categories' behavior + + SponsorBlockSettings.SegmentInfo[] categories = SponsorBlockSettings.SegmentInfo.valuesWithoutUnsubmitted(); + for (SponsorBlockSettings.SegmentInfo category : categories) { + JSONObject categoryObject = new JSONObject(); + String categoryKey = category.key; + categoryObject.put("color", formatColorString(category.color)); + barTypesObject.put(categoryKey, categoryObject); + + int desktopKey = category.behaviour.desktopKey; + if (desktopKey != -1) { + JSONObject behaviorObject = new JSONObject(); + behaviorObject.put("name", categoryKey); + behaviorObject.put("option", desktopKey); + categorySelectionsArray.put(behaviorObject); + } + } + json.put("dontShowNotice", !showToastWhenSkippedAutomatically); + json.put("barTypes", barTypesObject); + json.put("showTimeWithSkips", showTimeWithoutSegments); + json.put("trackViewCount", countSkips); + json.put("categorySelections", categorySelectionsArray); + json.put("userID", uuid); + + return json.toString(); + } + catch (Exception ex) { + ex.printStackTrace(); + return ""; + } + } + public static boolean isSettingEnabled(boolean setting) { return isSponsorBlockEnabled && setting; } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 6b9cce5b..5bdbb713 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -157,6 +157,10 @@ This is the number of milliseconds you can move when you use the time adjustment buttons while adding new segment Your unique user id This should be kept private. This is like a password and should not be shared with anyone. If someone has this, they can impersonate you + Import/Export settings + This is your entire configuration in JSON. This includes your userID, so be sure to share this wisely. + Settings were successfully imported + Failed to import settings Sponsor Paid promotion, paid referrals and direct advertisements Intermission/Intro Animation From eba12ba2e63e54ee65995177a40add97d38c986b Mon Sep 17 00:00:00 2001 From: caneleex Date: Tue, 3 Aug 2021 16:33:18 +0200 Subject: [PATCH 2/4] clarify the summary --- app/src/main/res/values/strings.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 5bdbb713..ecc5a254 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -158,7 +158,7 @@ Your unique user id This should be kept private. This is like a password and should not be shared with anyone. If someone has this, they can impersonate you Import/Export settings - This is your entire configuration in JSON. This includes your userID, so be sure to share this wisely. + This is your entire configuration that is applicable in the desktop extension in JSON. This includes your userID, so be sure to share this wisely. Settings were successfully imported Failed to import settings Sponsor From c0c9ea451edd6c8e4a0c0b360ca877e3197ce290 Mon Sep 17 00:00:00 2001 From: caneleex Date: Tue, 3 Aug 2021 16:38:38 +0200 Subject: [PATCH 3/4] add toast for failed exporting --- .../java/pl/jakubweg/SponsorBlockPreferenceFragment.java | 6 ++++-- app/src/main/java/pl/jakubweg/SponsorBlockUtils.java | 3 ++- app/src/main/res/values/strings.xml | 1 + 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/pl/jakubweg/SponsorBlockPreferenceFragment.java b/app/src/main/java/pl/jakubweg/SponsorBlockPreferenceFragment.java index 0949d2b4..5f42a490 100644 --- a/app/src/main/java/pl/jakubweg/SponsorBlockPreferenceFragment.java +++ b/app/src/main/java/pl/jakubweg/SponsorBlockPreferenceFragment.java @@ -301,11 +301,13 @@ public class SponsorBlockPreferenceFragment extends PreferenceFragment implement { EditTextPreference preference = new EditTextPreference(context); + Context applicationContext = context.getApplicationContext(); + preference.setTitle(str("settings_ie")); preference.setSummary(str("settings_ie_sum")); - preference.setText(SponsorBlockUtils.exportSettings()); + preference.setText(SponsorBlockUtils.exportSettings(applicationContext)); preference.setOnPreferenceChangeListener((preference1, newValue) -> { - SponsorBlockUtils.importSettings((String) newValue, context.getApplicationContext()); + SponsorBlockUtils.importSettings((String) newValue, applicationContext); return false; }); screen.addPreference(preference); diff --git a/app/src/main/java/pl/jakubweg/SponsorBlockUtils.java b/app/src/main/java/pl/jakubweg/SponsorBlockUtils.java index 74ef177c..1c881f92 100644 --- a/app/src/main/java/pl/jakubweg/SponsorBlockUtils.java +++ b/app/src/main/java/pl/jakubweg/SponsorBlockUtils.java @@ -604,7 +604,7 @@ public abstract class SponsorBlockUtils { } } - public static String exportSettings() { + public static String exportSettings(Context context) { try { JSONObject json = new JSONObject(); @@ -636,6 +636,7 @@ public abstract class SponsorBlockUtils { return json.toString(); } catch (Exception ex) { + Toast.makeText(context, str("settings_export_failed"), Toast.LENGTH_SHORT).show(); ex.printStackTrace(); return ""; } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index ecc5a254..5dfbea77 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -161,6 +161,7 @@ This is your entire configuration that is applicable in the desktop extension in JSON. This includes your userID, so be sure to share this wisely. Settings were successfully imported Failed to import settings + Failed to export settings Sponsor Paid promotion, paid referrals and direct advertisements Intermission/Intro Animation From 77832cf83af4a724953db6c4a41582181fc6e4cb Mon Sep 17 00:00:00 2001 From: caneleex Date: Tue, 3 Aug 2021 19:22:12 +0200 Subject: [PATCH 4/4] ocd --- app/src/main/java/pl/jakubweg/SponsorBlockUtils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/pl/jakubweg/SponsorBlockUtils.java b/app/src/main/java/pl/jakubweg/SponsorBlockUtils.java index 1c881f92..a05116f4 100644 --- a/app/src/main/java/pl/jakubweg/SponsorBlockUtils.java +++ b/app/src/main/java/pl/jakubweg/SponsorBlockUtils.java @@ -599,8 +599,8 @@ public abstract class SponsorBlockUtils { Toast.makeText(context, str("settings_import_successful"), Toast.LENGTH_SHORT).show(); } catch (Exception ex) { - ex.printStackTrace(); Toast.makeText(context, str("settings_import_failed"), Toast.LENGTH_SHORT).show(); + ex.printStackTrace(); } }