diff --git a/app/src/main/java/pl/jakubweg/SponsorBlockPreferenceFragment.java b/app/src/main/java/pl/jakubweg/SponsorBlockPreferenceFragment.java index e7da30f..5f42a49 100644 --- a/app/src/main/java/pl/jakubweg/SponsorBlockPreferenceFragment.java +++ b/app/src/main/java/pl/jakubweg/SponsorBlockPreferenceFragment.java @@ -298,6 +298,21 @@ public class SponsorBlockPreferenceFragment extends PreferenceFragment implement screen.addPreference(preference); preferencesToDisableWhenSBDisabled.add(preference); } + + { + EditTextPreference preference = new EditTextPreference(context); + Context applicationContext = context.getApplicationContext(); + + preference.setTitle(str("settings_ie")); + preference.setSummary(str("settings_ie_sum")); + preference.setText(SponsorBlockUtils.exportSettings(applicationContext)); + preference.setOnPreferenceChangeListener((preference1, newValue) -> { + SponsorBlockUtils.importSettings((String) newValue, applicationContext); + 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 87dc819..44bb5a0 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 febdddb..a05116f 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,88 @@ 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) { + Toast.makeText(context, str("settings_import_failed"), Toast.LENGTH_SHORT).show(); + ex.printStackTrace(); + } + } + + public static String exportSettings(Context context) { + 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) { + Toast.makeText(context, str("settings_export_failed"), Toast.LENGTH_SHORT).show(); + 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 6b9cce5..5dfbea7 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -157,6 +157,11 @@ 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 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