final touches

This commit is contained in:
caneleex
2022-01-23 22:36:01 +01:00
parent d04c8f99ab
commit 16073a7f74
12 changed files with 83 additions and 34 deletions

View File

@ -13,7 +13,6 @@ import android.os.Build;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.apps.youtube.app.YouTubeTikTokRoot_Application;
@ -240,8 +239,6 @@ public class ReturnYouTubeDislikes {
Log.d(TAG, "Like button " + likeActive + " | Dislike button " + dislikeActive);
}
Toast.makeText(YouTubeTikTokRoot_Application.getAppContext(), "Voting value: " + votingValue, Toast.LENGTH_SHORT).show();
sendVote(votingValue);
}
catch (Exception ex) {

View File

@ -22,7 +22,7 @@ public class AdButton extends SlimButton {
public AdButton(Context context, ViewGroup container) {
super(context, container, SlimButton.SLIM_METADATA_BUTTON_ID,
SharedPrefUtils.getBoolean(context, "youtube", WhitelistType.ADS.getPreferenceEnabledName(), false));
SharedPrefUtils.getBoolean(context, WhitelistType.ADS.getSharedPreferencesName(), WhitelistType.ADS.getPreferenceEnabledName(), false));
initialize();
}
@ -55,7 +55,7 @@ public class AdButton extends SlimButton {
private void removeFromWhitelist() {
try {
Whitelist.removeFromWhitelist(WhitelistType.ADS, this.context, VideoInformation.channelName);
this.button_icon.setEnabled(false);
changeEnabled(false);
}
catch (Exception ex) {
Log.e(TAG, "Failed to remove from whitelist", ex);

View File

@ -2,6 +2,7 @@ package fi.vanced.libraries.youtube.ui;
import static fi.razerman.youtube.XGlobals.debug;
import static fi.vanced.libraries.youtube.player.VideoInformation.currentVideoId;
import static pl.jakubweg.StringRef.str;
import android.content.Context;
import android.util.Log;
@ -15,35 +16,34 @@ import fi.vanced.libraries.youtube.whitelisting.WhitelistType;
import fi.vanced.libraries.youtube.whitelisting.requests.WhitelistRequester;
import fi.vanced.utils.SharedPrefUtils;
import fi.vanced.utils.VancedUtils;
import pl.jakubweg.SponsorBlockSettings;
public class SBWhitelistButton extends SlimButton {
public static final String TAG = "VI - SBWhitelistButton";
public SBWhitelistButton(Context context, ViewGroup container) {
super(context, container, SlimButton.SLIM_METADATA_BUTTON_ID,
SharedPrefUtils.getBoolean(context, SponsorBlockSettings.PREFERENCES_NAME, WhitelistType.SPONSORBLOCK.getPreferenceEnabledName(), false));
SharedPrefUtils.getBoolean(context, WhitelistType.SPONSORBLOCK.getSharedPreferencesName(), WhitelistType.SPONSORBLOCK.getPreferenceEnabledName(), false));
initialize();
}
private void initialize() {
this.button_icon.setImageResource(VancedUtils.getIdentifier("vanced_sb_logo", "drawable"));
this.button_text.setText("SB");
changeEnabled(Whitelist.shouldShowSegments());
this.button_icon.setImageResource(VancedUtils.getIdentifier("vanced_yt_sb_button", "drawable"));
this.button_text.setText(str("action_segments"));
changeEnabled(Whitelist.isChannelSBWhitelisted());
}
public void changeEnabled(boolean enabled) {
if (debug) {
Log.d(TAG, "changeEnabled " + enabled);
}
this.button_icon.setEnabled(enabled);
this.button_icon.setEnabled(!enabled); // enabled == true -> strikethrough (no segments), enabled == false -> clear (segments)
}
@Override
public void onClick(View view) {
this.view.setEnabled(false);
if (this.button_icon.isEnabled()) {
if (Whitelist.isChannelSBWhitelisted()) {
removeFromWhitelist();
return;
}
@ -55,7 +55,7 @@ public class SBWhitelistButton extends SlimButton {
private void removeFromWhitelist() {
try {
Whitelist.removeFromWhitelist(WhitelistType.SPONSORBLOCK, this.context, VideoInformation.channelName);
this.button_icon.setEnabled(false);
changeEnabled(false);
}
catch (Exception ex) {
Log.e(TAG, "Failed to remove from whitelist", ex);

View File

@ -78,7 +78,7 @@ public class SlimButtonContainer extends SlimMetadataScrollableButtonContainerLa
WhitelistType whitelistAds = WhitelistType.ADS;
String adsEnabledPreferenceName = whitelistAds.getPreferenceEnabledName();
if (adsEnabledPreferenceName.equals(key) && adBlockButton != null) {
boolean enabled = SharedPrefUtils.getBoolean(context, "youtube", adsEnabledPreferenceName, false);
boolean enabled = SharedPrefUtils.getBoolean(context, whitelistAds.getSharedPreferencesName(), adsEnabledPreferenceName, false);
Whitelist.setEnabled(whitelistAds, enabled);
adBlockButton.setVisible(enabled);
return;
@ -86,7 +86,7 @@ public class SlimButtonContainer extends SlimMetadataScrollableButtonContainerLa
WhitelistType whitelistSB = WhitelistType.SPONSORBLOCK;
String sbEnabledPreferenceName = whitelistSB.getPreferenceEnabledName();
if (sbEnabledPreferenceName.equals(key) && sbWhitelistButton != null) {
boolean enabled = SharedPrefUtils.getBoolean(context, "youtube", sbEnabledPreferenceName, false);
boolean enabled = SharedPrefUtils.getBoolean(context, whitelistSB.getSharedPreferencesName(), sbEnabledPreferenceName, false);
Whitelist.setEnabled(whitelistSB, enabled);
sbWhitelistButton.setVisible(enabled);
return;
@ -97,7 +97,9 @@ public class SlimButtonContainer extends SlimMetadataScrollableButtonContainerLa
}
};
context.getSharedPreferences("youtube", Context.MODE_PRIVATE)
context.getSharedPreferences(WhitelistType.ADS.getSharedPreferencesName(), Context.MODE_PRIVATE)
.registerOnSharedPreferenceChangeListener(listener);
context.getSharedPreferences(WhitelistType.SPONSORBLOCK.getSharedPreferencesName(), Context.MODE_PRIVATE)
.registerOnSharedPreferenceChangeListener(listener);
}
}

View File

@ -51,14 +51,14 @@ public class Whitelist {
adBlockButton.changeEnabled(shouldShowAds());
}
if (enabledMap.get(WhitelistType.SPONSORBLOCK) && sbWhitelistButton != null) {
sbWhitelistButton.changeEnabled(shouldShowSegments());
sbWhitelistButton.changeEnabled(isChannelSBWhitelisted());
}
}
// the rest
public static boolean shouldShowSegments() {
return !isWhitelisted(WhitelistType.SPONSORBLOCK);
public static boolean isChannelSBWhitelisted() {
return isWhitelisted(WhitelistType.SPONSORBLOCK);
}
private static Map<WhitelistType, ArrayList<ChannelModel>> parseWhitelist(Context context) {
@ -98,7 +98,7 @@ public class Whitelist {
private static Map<WhitelistType, Boolean> parseEnabledMap(Context context) {
Map<WhitelistType, Boolean> enabledMap = new EnumMap<>(WhitelistType.class);
for (WhitelistType whitelistType : WhitelistType.values()) {
enabledMap.put(whitelistType, SharedPrefUtils.getBoolean(context, "youtube", whitelistType.getPreferenceEnabledName()));
enabledMap.put(whitelistType, SharedPrefUtils.getBoolean(context, whitelistType.getSharedPreferencesName(), whitelistType.getPreferenceEnabledName()));
}
return enabledMap;
}

View File

@ -1,15 +1,19 @@
package fi.vanced.libraries.youtube.whitelisting;
import pl.jakubweg.SponsorBlockSettings;
public enum WhitelistType {
ADS("Ads", "vanced_whitelist_ads_enabled"),
SPONSORBLOCK("SponsorBlock", "vanced_whitelist_sb_enabled");
ADS("Ads", "youtube", "vanced_whitelist_ads_enabled"),
SPONSORBLOCK("SponsorBlock", SponsorBlockSettings.PREFERENCES_NAME, "vanced_whitelist_sb_enabled");
private final String friendlyName;
private final String preferencesName;
private final String sharedPreferencesName;
private final String preferenceEnabledName;
WhitelistType(String friendlyName, String preferenceEnabledName) {
WhitelistType(String friendlyName, String sharedPreferencesName, String preferenceEnabledName) {
this.friendlyName = friendlyName;
this.sharedPreferencesName = sharedPreferencesName;
this.preferencesName = "whitelist_" + name();
this.preferenceEnabledName = preferenceEnabledName;
}
@ -18,6 +22,10 @@ public enum WhitelistType {
return friendlyName;
}
public String getSharedPreferencesName() {
return sharedPreferencesName;
}
public String getPreferencesName() {
return preferencesName;
}

View File

@ -47,7 +47,8 @@ public class WhitelistRequester {
byte[] input = jsonInputString.getBytes(StandardCharsets.UTF_8);
os.write(input, 0, input.length);
}
if (connection.getResponseCode() == 200) {
int responseCode = connection.getResponseCode();
if (responseCode == 200) {
JSONObject json = getJSONObject(connection);
JSONObject videoInfo = json.getJSONObject("videoDetails");
ChannelModel channelModel = new ChannelModel(videoInfo.getString("author"), videoInfo.getString("channelId"));
@ -58,36 +59,41 @@ public class WhitelistRequester {
boolean success = Whitelist.addToWhitelist(whitelistType, context, channelModel);
String whitelistTypeName = whitelistType.getFriendlyName();
new Handler(Looper.getMainLooper()).post(() -> {
runOnMainThread(() -> {
if (success) {
buttonIcon.setEnabled(true);
buttonIcon.setEnabled(whitelistType != WhitelistType.SPONSORBLOCK);
Toast.makeText(context, str("vanced_whitelisting_added", author, whitelistTypeName), Toast.LENGTH_SHORT).show();
}
else {
buttonIcon.setEnabled(false);
buttonIcon.setEnabled(whitelistType == WhitelistType.SPONSORBLOCK);
Toast.makeText(context, str("vanced_whitelisting_add_failed", author, whitelistTypeName), Toast.LENGTH_SHORT).show();
}
view.setEnabled(true);
});
}
else {
if (debug) {
Log.d(TAG, "player fetch response was " + connection.getResponseCode());
Log.d(TAG, "player fetch response was " + responseCode);
}
buttonIcon.setEnabled(false);
view.setEnabled(true);
runOnMainThread(() -> {
Toast.makeText(context, str("vanced_whitelisting_fetch_failed", responseCode), Toast.LENGTH_SHORT).show();
buttonIcon.setEnabled(true);
view.setEnabled(true);
});
}
}
catch (Exception ex) {
Log.e(TAG, "Failed to fetch channelId", ex);
view.setEnabled(true);
runOnMainThread(() -> view.setEnabled(true));
}
}
// helpers
private static void runOnMainThread(Runnable runnable) {
new Handler(Looper.getMainLooper()).post(runnable);
}
private static HttpURLConnection getConnectionFromRoute(Route route, String... params) throws IOException {
return Requester.getConnectionFromRoute(YT_API_URL, route, params);
}

View File

@ -128,7 +128,7 @@ public class PlayerController {
public static void executeDownloadSegments(String videoId) {
videoHasSegments = false;
timeWithoutSegments = "";
if (!Whitelist.shouldShowSegments())
if (Whitelist.isChannelSBWhitelisted())
return;
SponsorSegment[] segments = SBRequester.getSegments(videoId);
Arrays.sort(segments);