|
| 1 | +package app.revanced.integrations.videoplayer; |
| 2 | + |
| 3 | +import android.app.AlertDialog; |
| 4 | +import android.content.Context; |
| 5 | +import android.content.Intent; |
| 6 | +import android.content.pm.PackageManager; |
| 7 | +import android.support.constraint.ConstraintLayout; |
| 8 | +import android.view.View; |
| 9 | +import android.view.animation.Animation; |
| 10 | +import android.view.animation.AnimationUtils; |
| 11 | +import android.widget.ImageView; |
| 12 | +import android.widget.Toast; |
| 13 | + |
| 14 | + |
| 15 | +import java.lang.ref.WeakReference; |
| 16 | + |
| 17 | +import app.revanced.integrations.patches.downloads.DownloadsPatch; |
| 18 | +import app.revanced.integrations.settings.SettingsEnum; |
| 19 | +import app.revanced.integrations.sponsorblock.StringRef; |
| 20 | +import app.revanced.integrations.utils.LogHelper; |
| 21 | +import app.revanced.integrations.utils.ReVancedUtils; |
| 22 | +import app.revanced.integrations.utils.SharedPrefHelper; |
| 23 | + |
| 24 | +/* loaded from: classes6.dex */ |
| 25 | +//ToDo: Refactor |
| 26 | +public class DownloadButton { |
| 27 | + static WeakReference<ImageView> _button = new WeakReference<>(null); |
| 28 | + static ConstraintLayout _constraintLayout; |
| 29 | + static int fadeDurationFast; |
| 30 | + static int fadeDurationScheduled; |
| 31 | + static Animation fadeIn; |
| 32 | + static Animation fadeOut; |
| 33 | + public static boolean isDownloadButtonEnabled; |
| 34 | + static boolean isShowing; |
| 35 | + |
| 36 | + public static void initializeDownloadButton(Object obj) { |
| 37 | + try { |
| 38 | + LogHelper.debug(DownloadButton.class, "initializing"); |
| 39 | + _constraintLayout = (ConstraintLayout) obj; |
| 40 | + isDownloadButtonEnabled = shouldBeShown(); |
| 41 | + ImageView imageView = _constraintLayout.findViewById(getIdentifier("download_button", "id")); |
| 42 | + if (imageView == null) { |
| 43 | + LogHelper.debug(DownloadButton.class, "Couldn't find imageView with id \"download_button\""); |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + imageView.setOnClickListener(view -> { |
| 48 | + LogHelper.debug(DownloadButton.class, "Download button clicked"); |
| 49 | + |
| 50 | + final var context = view.getContext(); |
| 51 | + final var powerTubePackageName = "ussr.razar.youtube_dl"; |
| 52 | + |
| 53 | + boolean packageEnabled = false; |
| 54 | + try { |
| 55 | + assert context != null; |
| 56 | + packageEnabled = context.getPackageManager().getApplicationInfo(powerTubePackageName, 0).enabled; |
| 57 | + } catch (PackageManager.NameNotFoundException error) { |
| 58 | + LogHelper.debug(DownloadButton.class, "PowerTube could not be found: " + error); |
| 59 | + } |
| 60 | + |
| 61 | + // If the package is not installed, show the toast |
| 62 | + if (!packageEnabled) { |
| 63 | + Toast.makeText(context, StringRef.str("powertube_not_installed_warning"), Toast.LENGTH_SHORT).show(); |
| 64 | + Toast.makeText(context, StringRef.str("powertube_not_installed_notice"), Toast.LENGTH_LONG).show(); |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + // Launch PowerTube intent |
| 69 | + try { |
| 70 | + String content = String.format("https://youtu.be/%s", DownloadsPatch.getCurrentVideoId()); |
| 71 | + |
| 72 | + Intent intent = new Intent("android.intent.action.SEND"); |
| 73 | + intent.setType("text/plain"); |
| 74 | + intent.setPackage(powerTubePackageName); |
| 75 | + intent.putExtra("android.intent.extra.TEXT", content); |
| 76 | + context.startActivity(intent); |
| 77 | + |
| 78 | + LogHelper.debug(DownloadButton.class, "Launched the intent with the content: " + content); |
| 79 | + } catch (Exception error) { |
| 80 | + LogHelper.debug(DownloadButton.class, "Failed to launch the intent: " + error); |
| 81 | + } |
| 82 | + |
| 83 | + //var options = Arrays.asList("Video", "Audio").toArray(new CharSequence[0]); |
| 84 | + // |
| 85 | + //new AlertDialog.Builder(view.getContext()) |
| 86 | + // .setItems(options, (dialog, which) -> { |
| 87 | + // LogHelper.debug(DownloadButton.class, String.valueOf(options[which])); |
| 88 | + // }) |
| 89 | + // .show(); |
| 90 | + // TODO: show popup and download via newpipe |
| 91 | + }); |
| 92 | + _button = new WeakReference<>(imageView); |
| 93 | + fadeDurationFast = getInteger("fade_duration_fast"); |
| 94 | + fadeDurationScheduled = getInteger("fade_duration_scheduled"); |
| 95 | + Animation animation = getAnimation("fade_in"); |
| 96 | + fadeIn = animation; |
| 97 | + animation.setDuration(fadeDurationFast); |
| 98 | + Animation animation2 = getAnimation("fade_out"); |
| 99 | + fadeOut = animation2; |
| 100 | + animation2.setDuration(fadeDurationScheduled); |
| 101 | + isShowing = true; |
| 102 | + changeVisibility(false); |
| 103 | + |
| 104 | + } catch (Exception e) { |
| 105 | + LogHelper.printException(DownloadButton.class, "Unable to set FrameLayout", e); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + public static void changeVisibility(boolean z) { |
| 110 | + if (isShowing == z) return; |
| 111 | + |
| 112 | + isShowing = z; |
| 113 | + ImageView imageView = _button.get(); |
| 114 | + if (_constraintLayout != null && imageView != null) { |
| 115 | + if (z && isDownloadButtonEnabled) { |
| 116 | + LogHelper.debug(DownloadButton.class, "Fading in"); |
| 117 | + imageView.setVisibility(View.VISIBLE); |
| 118 | + imageView.startAnimation(fadeIn); |
| 119 | + } else if (imageView.getVisibility() == View.VISIBLE) { |
| 120 | + LogHelper.debug(DownloadButton.class, "Fading out"); |
| 121 | + imageView.startAnimation(fadeOut); |
| 122 | + imageView.setVisibility(View.GONE); |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + public static void refreshShouldBeShown() { |
| 128 | + isDownloadButtonEnabled = shouldBeShown(); |
| 129 | + } |
| 130 | + |
| 131 | + private static boolean shouldBeShown() { |
| 132 | + if (!SettingsEnum.DOWNLOAD_BUTTON_SHOWN.getBoolean()) { |
| 133 | + return false; |
| 134 | + } |
| 135 | + |
| 136 | + Context appContext = ReVancedUtils.getContext(); |
| 137 | + if (appContext == null) { |
| 138 | + LogHelper.printException(DownloadButton.class, "shouldBeShown - context is null!"); |
| 139 | + return false; |
| 140 | + } |
| 141 | + String string = SharedPrefHelper.getString(appContext, SharedPrefHelper.SharedPrefNames.YOUTUBE, "pref_download_button_list", "PLAYER" /* TODO: set the default to null, as this will be set by the settings page later */); |
| 142 | + if (string == null || string.isEmpty()) { |
| 143 | + return false; |
| 144 | + } |
| 145 | + return string.equalsIgnoreCase("PLAYER"); |
| 146 | + } |
| 147 | + |
| 148 | + private static int getIdentifier(String str, String str2) { |
| 149 | + Context appContext = ReVancedUtils.getContext(); |
| 150 | + return appContext.getResources().getIdentifier(str, str2, appContext.getPackageName()); |
| 151 | + } |
| 152 | + |
| 153 | + private static int getInteger(String str) { |
| 154 | + return ReVancedUtils.getContext().getResources().getInteger(getIdentifier(str, "integer")); |
| 155 | + } |
| 156 | + private static Animation getAnimation(String str) { |
| 157 | + return AnimationUtils.loadAnimation(ReVancedUtils.getContext(), getIdentifier(str, "anim")); |
| 158 | + } |
| 159 | +} |
| 160 | + |
0 commit comments