add support for importing/exporting applicable SB settings

This commit is contained in:
caneleex 2021-08-03 16:23:44 +02:00
parent 52c6032f1a
commit b30f81e4d4
4 changed files with 127 additions and 6 deletions

View File

@ -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

View File

@ -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 {

View File

@ -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;
}

View File

@ -157,6 +157,10 @@
<string name="general_adjusting_sum">This is the number of milliseconds you can move when you use the time adjustment buttons while adding new segment</string>
<string name="general_uuid">Your unique user id</string>
<string name="general_uuid_sum">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</string>
<string name="settings_ie">Import/Export settings</string>
<string name="settings_ie_sum">This is your entire configuration in JSON. This includes your userID, so be sure to share this wisely.</string>
<string name="settings_import_successful">Settings were successfully imported</string>
<string name="settings_import_failed">Failed to import settings</string>
<string name="segments_sponsor">Sponsor</string>
<string name="segments_sponsor_sum">Paid promotion, paid referrals and direct advertisements</string>
<string name="segments_intermission">Intermission/Intro Animation</string>