add local skips tracking

This commit is contained in:
caneleex 2021-07-19 21:51:26 +02:00
parent 7a367b7726
commit 1488f03a6c
8 changed files with 85 additions and 17 deletions

View File

@ -3,6 +3,7 @@ package pl.jakubweg;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.os.Handler;
@ -24,6 +25,9 @@ import fi.vanced.libraries.youtube.player.VideoInformation;
import pl.jakubweg.objects.SponsorSegment;
import pl.jakubweg.requests.Requester;
import static pl.jakubweg.SponsorBlockSettings.skippedSegments;
import static pl.jakubweg.SponsorBlockSettings.skippedTime;
@SuppressLint({"LongLogTag"})
public class PlayerController {
public static final String TAG = "jakubweg.PlayerController";
@ -264,6 +268,15 @@ public class PlayerController {
}
private static void sendViewRequestAsync(final long millis, final SponsorSegment segment) {
if (segment.category != SponsorBlockSettings.SegmentInfo.UNSUBMITTED) {
Context context = YouTubeTikTokRoot_Application.getAppContext();
if (context != null) {
SharedPreferences preferences = SponsorBlockSettings.getPreferences(context);
long newSkippedTime = skippedTime + segment.end - segment.start;
preferences.edit().putInt(SponsorBlockSettings.PREFERENCES_KEY_SKIPPED_SEGMENTS, skippedSegments + 1).apply();
preferences.edit().putLong(SponsorBlockSettings.PREFERENCES_KEY_SKIPPED_SEGMENTS_TIME, newSkippedTime).apply();
}
}
new Thread(() -> {
if (SponsorBlockSettings.countSkips &&
segment.category != SponsorBlockSettings.SegmentInfo.UNSUBMITTED &&

View File

@ -15,10 +15,12 @@ import android.preference.PreferenceFragment;
import android.preference.PreferenceScreen;
import android.preference.SwitchPreference;
import android.text.InputType;
import android.util.Log;
import android.widget.Toast;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
import pl.jakubweg.objects.UserStats;
import pl.jakubweg.requests.Requester;
@ -37,12 +39,15 @@ import static pl.jakubweg.SponsorBlockSettings.adjustNewSegmentMillis;
import static pl.jakubweg.SponsorBlockSettings.countSkips;
import static pl.jakubweg.SponsorBlockSettings.setSeenGuidelines;
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;
@SuppressWarnings({"unused", "deprecation"}) // injected
public class SponsorBlockPreferenceFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {
private static final DecimalFormat FORMATTER = new DecimalFormat("#,###,###");
private static final String SAVED_TEMPLATE = "%dh %.1f minutes";
private final ArrayList<Preference> preferencesToDisableWhenSBDisabled = new ArrayList<>();
@Override
@ -168,16 +173,17 @@ public class SponsorBlockPreferenceFragment extends PreferenceFragment implement
private void addStatsCategory(Context context, PreferenceScreen screen) {
PreferenceCategory category = new PreferenceCategory(context);
screen.addPreference(category);
category.setTitle("stats");
category.setTitle(str("stats"));
preferencesToDisableWhenSBDisabled.add(category);
UserStats stats = Requester.getUserStats();
{
EditTextPreference preference = new EditTextPreference(context);
screen.addPreference(preference);
category.addPreference(preference);
String userName = stats.getUserName();
preference.setTitle(fromHtml("Your username: <b>" + userName + "</b>"));
preference.setSummary("Click to change your username");
preference.setTitle(fromHtml(str("stats_username", userName)));
preference.setSummary(str("stats_username_change"));
preference.setText(userName);
preference.setOnPreferenceChangeListener((preference1, newUsername) -> {
Requester.setUsername((String) newUsername);
@ -187,23 +193,42 @@ public class SponsorBlockPreferenceFragment extends PreferenceFragment implement
{
Preference preference = new Preference(context);
screen.addPreference(preference);
category.addPreference(preference);
String formatted = FORMATTER.format(stats.getSegmentCount());
preference.setTitle(fromHtml("Submissions: <b>" + formatted + "</b>"));
preference.setTitle(fromHtml(str("stats_submissions", formatted)));
}
{
Preference preference = new Preference(context);
screen.addPreference(preference);
category.addPreference(preference);
String formatted = FORMATTER.format(stats.getViewCount());
double saved = stats.getMinutesSaved();
int hoursSaved = (int) (saved / 60);
double minutesSaved = saved % 60;
String formattedSaved = String.format("%dh %.1f minutes", hoursSaved, minutesSaved);
String formattedSaved = String.format(SAVED_TEMPLATE, hoursSaved, minutesSaved);
preference.setTitle(fromHtml("You've saved people from <b>" + formatted + "</b> segments."));
preference.setSummary(fromHtml("That's <b>" + formattedSaved + "</b> of their lives."));
preference.setTitle(fromHtml(str("stats_saved", formatted)));
preference.setSummary(fromHtml(str("stats_saved_sum", formattedSaved)));
preference.setOnPreferenceClickListener(preference1 -> {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("https://sponsor.ajay.app/stats/"));
preference1.getContext().startActivity(i);
return false;
});
}
{
Preference preference = new Preference(context);
category.addPreference(preference);
String formatted = FORMATTER.format(skippedSegments);
long hoursSaved = skippedTime / 3600000;
double minutesSaved = (skippedTime / 60000d) % 60;
String formattedSaved = String.format(SAVED_TEMPLATE, hoursSaved, minutesSaved);
preference.setTitle(fromHtml(str("stats_self_saved", formatted)));
preference.setSummary(fromHtml(str("stats_self_saved_sum", formattedSaved)));
}
}
@ -219,7 +244,7 @@ public class SponsorBlockPreferenceFragment extends PreferenceFragment implement
preference.setSummary(str("about_api_sum"));
preference.setOnPreferenceClickListener(preference1 -> {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("http://sponsor.ajay.app"));
i.setData(Uri.parse("https://sponsor.ajay.app"));
preference1.getContext().startActivity(i);
return false;
});

View File

@ -25,6 +25,8 @@ public class SponsorBlockSettings {
public static final String PREFERENCES_KEY_SEEN_GUIDELINES = "sb-seen-gl";
public static final String PREFERENCES_KEY_NEW_SEGMENT_ENABLED = "sb-new-segment-enabled";
public static final String PREFERENCES_KEY_VOTING_ENABLED = "sb-voting-enabled";
public static final String PREFERENCES_KEY_SKIPPED_SEGMENTS = "sb-skipped-segments";
public static final String PREFERENCES_KEY_SKIPPED_SEGMENTS_TIME = "sb-skipped-segments-time";
public static final SegmentBehaviour DefaultBehaviour = SegmentBehaviour.SKIP_AUTOMATICALLY;
@ -37,6 +39,8 @@ public class SponsorBlockSettings {
public static int adjustNewSegmentMillis = 150;
public static String uuid = "<invalid>";
public static String sponsorBlockUrlCategories = "[]";
public static int skippedSegments;
public static long skippedTime;
@SuppressWarnings("unused")
@Deprecated
@ -113,6 +117,8 @@ public class SponsorBlockSettings {
else
sponsorBlockUrlCategories = "[%22" + TextUtils.join("%22,%22", enabledCategories) + "%22]";
skippedSegments = preferences.getInt(PREFERENCES_KEY_SKIPPED_SEGMENTS, skippedSegments);
skippedTime = preferences.getLong(PREFERENCES_KEY_SKIPPED_SEGMENTS_TIME, skippedTime);
showToastWhenSkippedAutomatically = preferences.getBoolean(PREFERENCES_KEY_SHOW_TOAST_WHEN_SKIP, showToastWhenSkippedAutomatically);
String tmp1 = preferences.getString(PREFERENCES_KEY_ADJUST_NEW_SEGMENT_STEP, null);

View File

@ -268,7 +268,7 @@ public abstract class SponsorBlockUtils {
new AlertDialog.Builder(context)
.setTitle(str("new_segment_title"))
.setMessage(String.format(str("new_segment_mark_time_as_question"),
.setMessage(str("new_segment_mark_time_as_question",
newSponsorSegmentDialogShownMillis / 60000,
newSponsorSegmentDialogShownMillis / 1000 % 60,
newSponsorSegmentDialogShownMillis % 1000))
@ -286,7 +286,7 @@ public abstract class SponsorBlockUtils {
long end = (newSponsorSegmentEndMillis) / 1000;
new AlertDialog.Builder(context)
.setTitle(str("new_segment_confirm_title"))
.setMessage(String.format(str("new_segment_confirm_content"),
.setMessage(str("new_segment_confirm_content",
start / 60, start % 60,
end / 60, end % 60,
length / 60, length % 60))

View File

@ -24,7 +24,7 @@ public class StringRef {
packageName = context.getPackageName();
}
private static HashMap<String, StringRef> strings = new HashMap<>();
private static final HashMap<String, StringRef> strings = new HashMap<>();
/**
* Gets strings reference from shared collection or creates if not exists yet,
@ -52,6 +52,18 @@ public class StringRef {
return sf(id).toString();
}
/**
* Gets string value by string id, shorthand for <code>sf(id).toString()</code> and formats the string
* with given args.
* @param id string resource name/id
* @param args the args to format the string with
* @return String value from string.xml formatted with given args
*/
@NonNull
public static String str(@NonNull String id, Object... args) {
return String.format(str(id), args);
}
/**
* Creates a StringRef object that'll not change it's value

View File

@ -131,7 +131,7 @@ public class Requester {
SponsorBlockUtils.messageToToast = str("vote_failed_rate_limit");
break;
default:
SponsorBlockUtils.messageToToast = String.format(str("vote_failed_unknown_error"), responseCode, connection.getResponseMessage());
SponsorBlockUtils.messageToToast = str("vote_failed_unknown_error", responseCode, connection.getResponseMessage());
break;
}
new Handler(Looper.getMainLooper()).post(toastRunnable);
@ -143,6 +143,10 @@ public class Requester {
}
public static UserStats getUserStats() {
UserStats defaultStats = new UserStats("N/A", -1, -1, -1);
if (!SponsorBlockSettings.isSponsorBlockEnabled)
return defaultStats;
try {
HttpURLConnection connection = getConnectionFromRoute(Route.GET_USER_STATS, SponsorBlockSettings.uuid);
JSONObject json = new JSONObject(parseJson(connection));
@ -153,7 +157,7 @@ public class Requester {
catch (Exception ex) {
ex.printStackTrace();
}
return new UserStats("N/A", -1, -1, -1);
return defaultStats;
}
public static void setUsername(String username) {

View File

@ -7,7 +7,7 @@ import static pl.jakubweg.requests.Route.Method.*;
public class Route {
public static final Route GET_SEGMENTS = new Route(GET, "skipSegments?videoID={video_id}&categories={categories}");
public static final Route VIEWED_SEGMENT = new Route(POST, "viewedVideoSponsorTime?UUID={segment_id}");
public static final Route GET_USER_STATS = new Route(GET, "userInfo?userID={user_id}");
public static final Route GET_USER_STATS = new Route(GET, "userInfo?userID={user_id}&values=[\"userName\", \"minutesSaved\", \"segmentCount\", \"viewCount\"]");
public static final Route CHANGE_USERNAME = new Route(POST, "setUsername?userID={user_id}&username={username}");
public static final Route SUBMIT_SEGMENTS = new Route(POST, "skipSegments?videoID={video_id}&userID={user_id}&startTime={start_time}&endTime={end_time}&category={category}");
public static final Route VOTE_ON_SEGMENT_QUALITY = new Route(POST, "voteOnSponsorTime?UUID={segment_id}&userID={user_id}&type={type}");

View File

@ -180,6 +180,14 @@
<string name="skip_automatically">Skip automatically</string>
<string name="skip_showbutton">Show a skip button</string>
<string name="skip_ignore">Don\'t do anything</string>
<string name="stats">Stats</string>
<string name="stats_username">Your username: &lt;b&gt;%s&lt;/b&gt;</string>
<string name="stats_username_change">Click to change your username</string>
<string name="stats_submissions">Submissions: &lt;b&gt;%s&lt;/b&gt;</string>
<string name="stats_saved">You\'ve saved people from &lt;b&gt;%s&lt;/b&gt; segments.</string>
<string name="stats_saved_sum">That\'s &lt;b&gt;%s&lt;/b&gt; of their lives. Click to see the leaderboard</string>
<string name="stats_self_saved">You\'ve skipped &lt;b&gt;%s&lt;/b&gt; segments.</string>
<string name="stats_self_saved_sum">That\'s &lt;b&gt;%s&lt;/b&gt;.</string>
<string name="about">About</string>
<string name="about_api">This app uses the API from Sponsor Block</string>
<string name="about_api_sum">Tap to learn more, and see downloads for other platforms at: sponsor.ajay.app</string>