mirror of
https://github.com/YTVanced/Integrations
synced 2024-11-30 15:13:03 +00:00
New shield button POC
This commit is contained in:
parent
2cc648897f
commit
8091722bfd
4 changed files with 153 additions and 6 deletions
|
@ -0,0 +1,15 @@
|
|||
package com.google.android.apps.youtube.app;
|
||||
|
||||
import android.app.Application;
|
||||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
|
||||
public class YouTubeApplication extends Application {
|
||||
protected void onCreate(final Bundle bundle) {
|
||||
super.onCreate();
|
||||
}
|
||||
|
||||
public static Context getAppContext() {
|
||||
return null;
|
||||
}
|
||||
}
|
132
app/src/main/java/pl/jakubweg/ShieldButton.java
Normal file
132
app/src/main/java/pl/jakubweg/ShieldButton.java
Normal file
|
@ -0,0 +1,132 @@
|
|||
package pl.jakubweg;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.view.animation.Animation;
|
||||
import android.view.animation.AnimationUtils;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.RelativeLayout;
|
||||
|
||||
import com.google.android.apps.youtube.app.YouTubeApplication;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
|
||||
public class ShieldButton {
|
||||
static boolean debug = true;
|
||||
static String TAG = "SHIELD";
|
||||
static RelativeLayout _youtubeControlsLayout;
|
||||
static WeakReference<ImageView> _shieldBtn = new WeakReference<>(null);
|
||||
static int fadeDurationFast;
|
||||
static int fadeDurationScheduled;
|
||||
static Animation fadeIn;
|
||||
static Animation fadeOut;
|
||||
static boolean isShowing;
|
||||
|
||||
public static void initialize(Object viewStub) {
|
||||
try {
|
||||
if(debug){
|
||||
Log.d(TAG, "initializing shield button");
|
||||
}
|
||||
|
||||
_youtubeControlsLayout = (RelativeLayout) viewStub;
|
||||
initButtonVisibilitySettings();
|
||||
|
||||
ImageView imageView = (ImageView)_youtubeControlsLayout
|
||||
.findViewById(getIdentifier("sponsorblock_button", "id"));
|
||||
|
||||
if (debug && imageView == null){
|
||||
Log.d(TAG, "Couldn't find imageView with tag \"sponsorblock_button\"");
|
||||
}
|
||||
if (imageView == null) return;
|
||||
imageView.setOnClickListener(SponsorBlockUtils.sponsorBlockBtnListener);
|
||||
_shieldBtn = new WeakReference<>(imageView);
|
||||
|
||||
// Animations
|
||||
fadeDurationFast = getInteger("fade_duration_fast");
|
||||
fadeDurationScheduled = getInteger("fade_duration_scheduled");
|
||||
fadeIn = getAnimation("fade_in");
|
||||
fadeIn.setDuration(fadeDurationFast);
|
||||
fadeOut = getAnimation("fade_out");
|
||||
fadeOut.setDuration(fadeDurationScheduled);
|
||||
isShowing = true;
|
||||
changeVisibilityImmediate(false);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
Log.e("XError", "Unable to set RelativeLayout", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public static void changeVisibilityImmediate(boolean visible) {
|
||||
changeVisibility(visible, true);
|
||||
}
|
||||
|
||||
public static void changeVisibilityNegatedImmediate(boolean visible) {
|
||||
changeVisibility(!visible, true);
|
||||
}
|
||||
|
||||
public static void changeVisibility(boolean visible) {
|
||||
changeVisibility(visible, false);
|
||||
}
|
||||
|
||||
public static void changeVisibility(boolean visible, boolean immediate) {
|
||||
if (isShowing == visible) return;
|
||||
isShowing = visible;
|
||||
|
||||
ImageView iView = _shieldBtn.get();
|
||||
if (_youtubeControlsLayout == null || iView == null) return;
|
||||
|
||||
if (visible && shouldBeShown()) {
|
||||
if (debug){
|
||||
Log.d(TAG, "Fading in");
|
||||
}
|
||||
iView.setVisibility(View.VISIBLE);
|
||||
if (!immediate)
|
||||
iView.startAnimation(fadeIn);
|
||||
return;
|
||||
}
|
||||
|
||||
if (iView.getVisibility() == View.VISIBLE) {
|
||||
if (debug){
|
||||
Log.d(TAG, "Fading out");
|
||||
}
|
||||
if (!immediate)
|
||||
iView.startAnimation(fadeOut);
|
||||
iView.setVisibility(shouldBeShown() ? View.INVISIBLE : View.GONE);
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean shouldBeShown() {
|
||||
return SponsorBlockSettings.isSponsorBlockEnabled && SponsorBlockSettings.isAddNewSegmentEnabled;
|
||||
}
|
||||
|
||||
private static void initButtonVisibilitySettings() {
|
||||
Context context = YouTubeApplication.getAppContext();
|
||||
if(context == null){
|
||||
Log.e(TAG, "context is null");
|
||||
SponsorBlockSettings.isSponsorBlockEnabled = false;
|
||||
SponsorBlockSettings.isAddNewSegmentEnabled = false;
|
||||
return;
|
||||
}
|
||||
|
||||
SharedPreferences sharedPreferences = context.getSharedPreferences(SponsorBlockSettings.PREFERENCES_NAME, Context.MODE_PRIVATE);
|
||||
SponsorBlockSettings.isSponsorBlockEnabled = sharedPreferences.getBoolean(SponsorBlockSettings.PREFERENCES_KEY_SPONSOR_BLOCK_ENABLED, false);
|
||||
SponsorBlockSettings.isAddNewSegmentEnabled = sharedPreferences.getBoolean(SponsorBlockSettings.PREFERENCES_KEY_NEW_SEGMENT_ENABLED, false);
|
||||
}
|
||||
|
||||
//region Helpers
|
||||
private static int getIdentifier(String name, String defType) {
|
||||
Context context = YouTubeApplication.getAppContext();
|
||||
return context.getResources().getIdentifier(name, defType, context.getPackageName());
|
||||
}
|
||||
|
||||
private static int getInteger(String name) {
|
||||
return YouTubeApplication.getAppContext().getResources().getInteger(getIdentifier(name, "integer"));
|
||||
}
|
||||
|
||||
private static Animation getAnimation(String name) {
|
||||
return AnimationUtils.loadAnimation(YouTubeApplication.getAppContext(), getIdentifier(name, "anim"));
|
||||
}
|
||||
//endregion
|
||||
}
|
|
@ -75,12 +75,12 @@ public class SponsorBlockSettings {
|
|||
NewSegmentHelperLayout.hide();
|
||||
SponsorBlockUtils.hideButton();
|
||||
PlayerController.sponsorSegmentsOfCurrentVideo = null;
|
||||
} else if (isAddNewSegmentEnabled) {
|
||||
} else if (/*isAddNewSegmentEnabled*/false) {
|
||||
SponsorBlockUtils.showButton();
|
||||
}
|
||||
|
||||
isAddNewSegmentEnabled = preferences.getBoolean(PREFERENCES_KEY_NEW_SEGMENT_ENABLED, isAddNewSegmentEnabled);
|
||||
if (!isAddNewSegmentEnabled) {
|
||||
if (!/*isAddNewSegmentEnabled*/false) {
|
||||
NewSegmentHelperLayout.hide();
|
||||
SponsorBlockUtils.hideButton();
|
||||
} else {
|
||||
|
|
|
@ -61,7 +61,7 @@ public abstract class SponsorBlockUtils {
|
|||
@SuppressLint("SimpleDateFormat")
|
||||
public static final SimpleDateFormat dateFormatter = new SimpleDateFormat(DATE_FORMAT);
|
||||
private static final int sponsorBtnId = 1234;
|
||||
private static final View.OnClickListener sponsorBlockBtnListener = new View.OnClickListener() {
|
||||
public static final View.OnClickListener sponsorBlockBtnListener = new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
NewSegmentHelperLayout.toggle();
|
||||
|
@ -292,7 +292,7 @@ public abstract class SponsorBlockUtils {
|
|||
if (existingSponsorBtn != null) {
|
||||
if (VERBOSE)
|
||||
Log.d(TAG, "addImageButton: sponsorBtn exists");
|
||||
if (SponsorBlockSettings.isAddNewSegmentEnabled)
|
||||
if (/*isAddNewSegmentEnabled*/false)
|
||||
showButton();
|
||||
return;
|
||||
}
|
||||
|
@ -365,7 +365,7 @@ public abstract class SponsorBlockUtils {
|
|||
instance.setOnClickListener(sponsorBlockBtnListener);
|
||||
sponsorBlockBtn = new WeakReference<>(instance);
|
||||
isShown = true;
|
||||
if (!SponsorBlockSettings.isAddNewSegmentEnabled)
|
||||
if (!/*isAddNewSegmentEnabled*/false)
|
||||
hideButton();
|
||||
if (VERBOSE)
|
||||
Log.i(TAG, "Image Button added");
|
||||
|
@ -444,7 +444,7 @@ public abstract class SponsorBlockUtils {
|
|||
}
|
||||
|
||||
public static void notifyShareBtnVisibilityChanged(View v) {
|
||||
if (v.getId() != shareBtnId || !SponsorBlockSettings.isAddNewSegmentEnabled) return;
|
||||
if (v.getId() != shareBtnId || !/*SponsorBlockSettings.isAddNewSegmentEnabled*/false) return;
|
||||
// if (VERBOSE)
|
||||
// Log.d(TAG, "VISIBILITY CHANGED of view " + v);
|
||||
ImageView sponsorBtn = sponsorBlockBtn.get();
|
||||
|
|
Loading…
Reference in a new issue