Skip to content
This repository has been archived by the owner on Mar 25, 2022. It is now read-only.

add whitelist menus #74

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.util.Collections;
import java.util.EnumMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import fi.vanced.libraries.youtube.player.ChannelModel;
Expand Down Expand Up @@ -117,8 +116,7 @@ private static boolean isWhitelisted(WhitelistType whitelistType) {
}
return false;
}
List<ChannelModel> whitelistedChannels = whitelistMap.get(whitelistType);
for (ChannelModel channel : whitelistedChannels) {
for (ChannelModel channel : getWhitelistedChannels(whitelistType)) {
if (channel.getAuthor().equals(channelName)) {
if (debug) {
Log.d(TAG, String.format("Whitelist for channel %s for type %s", channelName, whitelistType));
Expand All @@ -130,7 +128,7 @@ private static boolean isWhitelisted(WhitelistType whitelistType) {
}

public static boolean addToWhitelist(WhitelistType whitelistType, Context context, ChannelModel channel) {
ArrayList<ChannelModel> whitelisted = whitelistMap.get(whitelistType);
ArrayList<ChannelModel> whitelisted = getWhitelistedChannels(whitelistType);
for (ChannelModel whitelistedChannel : whitelisted) {
String channelId = channel.getChannelId();
if (whitelistedChannel.getChannelId().equals(channelId)) {
Expand All @@ -146,7 +144,7 @@ public static boolean addToWhitelist(WhitelistType whitelistType, Context contex
}

public static void removeFromWhitelist(WhitelistType whitelistType, Context context, String channelName) {
ArrayList<ChannelModel> channels = whitelistMap.get(whitelistType);
ArrayList<ChannelModel> channels = getWhitelistedChannels(whitelistType);
Iterator<ChannelModel> iterator = channels.iterator();
while (iterator.hasNext()) {
ChannelModel channel = iterator.next();
Expand All @@ -165,7 +163,7 @@ public static void removeFromWhitelist(WhitelistType whitelistType, Context cont
}
}

private static boolean updateWhitelist(WhitelistType whitelistType, ArrayList<ChannelModel> channels, Context context) {
public static boolean updateWhitelist(WhitelistType whitelistType, ArrayList<ChannelModel> channels, Context context) {
if (context == null) {
return false;
}
Expand All @@ -185,4 +183,8 @@ private static boolean updateWhitelist(WhitelistType whitelistType, ArrayList<Ch
public static void setEnabled(WhitelistType whitelistType, boolean enabled) {
enabledMap.put(whitelistType, enabled);
}

public static ArrayList<ChannelModel> getWhitelistedChannels(WhitelistType whitelistType) {
return whitelistMap.get(whitelistType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import fi.vanced.libraries.youtube.whitelisting.WhitelistType;
import fi.vanced.utils.SharedPrefUtils;
import pl.jakubweg.objects.EditTextListPreference;
import pl.jakubweg.objects.WhitelistedChannelsPreference;
import pl.jakubweg.requests.SBRequester;

@SuppressWarnings({"unused", "deprecation"}) // injected
Expand Down Expand Up @@ -381,6 +382,14 @@ private void addGeneralCategory(final Context context, PreferenceScreen screen)
screen.addPreference(preference);
preferencesToDisableWhenSBDisabled.add(preference);
}

{
WhitelistedChannelsPreference preference = new WhitelistedChannelsPreference(context);
preference.setTitle("Manage your whitelisted channels");
preference.setWhitelistType(WhitelistType.SPONSORBLOCK);
screen.addPreference(preference);
preferencesToDisableWhenSBDisabled.add(preference);
}
}

private static class APIURLChangeListener implements DialogInterface.OnClickListener {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package pl.jakubweg.objects;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;

import fi.vanced.libraries.youtube.player.ChannelModel;
import fi.vanced.libraries.youtube.whitelisting.Whitelist;
import fi.vanced.libraries.youtube.whitelisting.WhitelistType;
import fi.vanced.utils.VancedUtils;

public class WhitelistedChannelsPreference extends DialogPreference {

private ArrayList<ChannelModel> mEntries;
private WhitelistType whitelistType;

public WhitelistedChannelsPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}

public WhitelistedChannelsPreference(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}

public WhitelistedChannelsPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}

public WhitelistedChannelsPreference(Context context) {
super(context);
}

@Override
protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
mEntries = Whitelist.getWhitelistedChannels(whitelistType);

Context context = getContext();
if (mEntries.isEmpty()) {
TextView emptyView = new TextView(context);
emptyView.setText("There are no whitelisted channels.");
emptyView.setTextSize(18);
builder.setView(emptyView);
}
else {
builder.setView(getEntriesListView(context));
}
}

private View getEntriesListView(Context context) {
LinearLayout entriesContainer = new LinearLayout(context);
entriesContainer.setOrientation(LinearLayout.VERTICAL);
for (final ChannelModel entry : mEntries) {
String author = entry.getAuthor();
View entryView = getEntryView(context, author, v -> {
entriesContainer.removeView(entriesContainer.findViewWithTag(author));
mEntries.remove(entry);
});
entryView.setTag(author);
entriesContainer.addView(entryView);
}
return entriesContainer;
}

@Override
protected void onDialogClosed(boolean positiveResult) {
if (positiveResult && mEntries != null) {
Whitelist.updateWhitelist(whitelistType, mEntries, getContext());
}
}

private View getEntryView(Context context, CharSequence entry,
View.OnClickListener onDeleteClickListener) {
int horizontalPadding = 80;

Drawable divider = context.getResources().getDrawable(android.R.drawable.divider_horizontal_dark);
Drawable deleteIcon = context.getResources().getDrawable(VancedUtils.getIdentifier("ic_baseline_delete_24", "drawable"));

LinearLayout.LayoutParams entryContainerParams = new LinearLayout.LayoutParams(
new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
entryContainerParams.setMargins(horizontalPadding, 0, horizontalPadding, 0);

LinearLayout.LayoutParams entryLabelLayoutParams = new LinearLayout.LayoutParams(
0, LinearLayout.LayoutParams.WRAP_CONTENT, 1);
entryLabelLayoutParams.gravity = Gravity.CENTER;

LinearLayout entryContainer = new LinearLayout(context);
entryContainer.setOrientation(LinearLayout.HORIZONTAL);
entryContainer.setDividerDrawable(divider);
entryContainer.setLayoutParams(entryContainerParams);

TextView entryLabel = new TextView(context);
entryLabel.setText(entry);
entryLabel.setLayoutParams(entryLabelLayoutParams);
entryLabel.setTextSize(18);

ImageButton deleteButton = new ImageButton(context);
deleteButton.setImageDrawable(deleteIcon);
deleteButton.setOnClickListener(onDeleteClickListener);
deleteButton.setBackground(null);

entryContainer.addView(entryLabel);
entryContainer.addView(deleteButton);
return entryContainer;
}

public void setWhitelistType(WhitelistType whitelistType) {
this.whitelistType = whitelistType;
}
}
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/ic_baseline_delete_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#FFFFFF"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M6,19c0,1.1 0.9,2 2,2h8c1.1,0 2,-0.9 2,-2V7H6v12zM19,4h-3.5l-1,-1h-5l-1,1H5v2h14V4z"/>
</vector>