package xyz.stream.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.os.StatFs;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.core.view.m3;
import androidx.core.view.r0;
import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.q1;
import java.text.DecimalFormat;
import java.util.Collections;
import java.util.Formatter;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import tv.danmaku.ijk.media.player.IjkEventListener;
import xyz.stream.view.BindingDialogBuilder;
import xyz.stream.view.autosize.AutoSize;
import xyz.stream.view.databinding.LayoutDialogNoSpaceBinding;

/* JADX INFO: loaded from: classes3.dex */
public class UiUtils {
    public static void a(SpannableStringBuilder spannableStringBuilder, String str, int i10) {
        SpannableString spannableString = new SpannableString(str);
        spannableString.setSpan(new ForegroundColorSpan(i10), 0, spannableString.length(), 33);
        spannableStringBuilder.append((CharSequence) spannableString);
    }

    public static void adaptActivityDensity(Activity activity) {
        if (activity == null) {
            return;
        }
        String name = activity.getClass().getName();
        if (!name.startsWith("com.google.android.gms.ads.") && !name.startsWith("com.bytedance.sdk.openadsdk.")) {
            ViewRuntime.restoreContentDesignSize(activity);
        }
        AutoSize.autoConvertDensityOfGlobal(activity);
    }

    public static String b(List list) {
        StringBuilder sb = new StringBuilder();
        for (int i10 = 0; i10 < list.size(); i10++) {
            sb.append(list.get(i10));
            if (i10 != list.size() - 1) {
                sb.append("  ");
            }
        }
        return sb.toString();
    }

    public static boolean checkNoSpace(Activity activity, Integer num) {
        String str;
        String str2;
        StatFs statFs = new StatFs(Environment.getDataDirectory().getPath());
        long blockSizeLong = statFs.getBlockSizeLong() * statFs.getAvailableBlocksLong();
        if (blockSizeLong >= ((long) (num == null ? 20 : num.intValue())) * 1048576) {
            return false;
        }
        if (blockSizeLong > 0) {
            double d10 = blockSizeLong;
            int iLog10 = (int) (Math.log10(d10) / Math.log10(1024.0d));
            str = new DecimalFormat("#,##0.#").format(d10 / Math.pow(1024.0d, iLog10)) + " " + new String[]{"B", "KB", "MB", "GB", "TB"}[iLog10];
        } else {
            str = "0 B";
        }
        if (num == null) {
            str2 = null;
        } else {
            str2 = num + " MB";
        }
        showSpaceDialog(activity, str2, str);
        return true;
    }

    public static void dismissProgressDialog() {
        CustomLoading.getInstance().dismiss();
    }

    public static int dp2px(float f10) {
        Activity currentContentActivity = ViewRuntime.getCurrentContentActivity();
        return (int) TypedValue.applyDimension(1, f10, (currentContentActivity != null ? currentContentActivity.getResources() : ViewRuntime.requireContext(null).getResources()).getDisplayMetrics());
    }

    public static void etRequestFocus(EditText editText) {
        if (editText == null) {
            return;
        }
        editText.post(new p000if.e(editText, 22));
    }

    public static double getScreenHeight(Context context) {
        return context.getResources().getDisplayMetrics().heightPixels;
    }

    public static double getScreenWidth(Context context) {
        return context.getResources().getDisplayMetrics().widthPixels;
    }

    public static int getStatusBarHeight(Context context) {
        return Math.round((context.getResources().getIdentifier("status_bar_height", "dimen", "android") > 0 ? context.getResources().getDimensionPixelSize(r0) : 0) / context.getResources().getDisplayMetrics().density);
    }

    public static String getStringForTime(StringBuilder sb, Formatter formatter, long j10) {
        if (j10 == -9223372036854775807L) {
            j10 = 0;
        }
        String str = j10 < 0 ? "-" : "";
        long jAbs = (Math.abs(j10) + 500) / 1000;
        long j11 = jAbs % 60;
        long j12 = (jAbs / 60) % 60;
        long j13 = jAbs / 3600;
        sb.setLength(0);
        return j13 > 0 ? formatter.format("%s%d:%02d:%02d", str, Long.valueOf(j13), Long.valueOf(j12), Long.valueOf(j11)).toString() : formatter.format("%s%02d:%02d", str, Long.valueOf(j12), Long.valueOf(j11)).toString();
    }

    public static Drawable getSvgDrawable(int i10, Integer num) {
        Context contextRequireContext = ViewRuntime.requireContext(null);
        try {
            Drawable drawable = f.a.getDrawable(contextRequireContext, i10);
            Objects.requireNonNull(drawable);
            Drawable drawableMutate = drawable.mutate();
            if (num != null) {
                int iIntValue = num.intValue();
                Object obj = g0.d.f26125a;
                drawableMutate.setTint(contextRequireContext.getColor(iIntValue));
            }
            return drawableMutate;
        } catch (Exception e10) {
            ViewLog.e("getSvgDrawable: " + e10.getMessage());
            return f.a.getDrawable(contextRequireContext, i10);
        }
    }

    public static boolean isDpadLeft(int i10) {
        return ViewRuntime.getLanguage().equals("ar") ? i10 == 22 : i10 == 21;
    }

    public static boolean isDpadRight(int i10) {
        return ViewRuntime.getLanguage().equals("ar") ? i10 == 21 : i10 == 22;
    }

    public static int playerSubtitleSizePx(Context context, int i10) {
        int i11 = context == null ? 0 : context.getResources().getDisplayMetrics().widthPixels;
        if (i11 <= 0) {
            i11 = 1920;
        }
        return Math.max(1, Math.round((i10 * i11) / 1920.0f));
    }

    public static int px2dp(float f10) {
        Activity currentContentActivity = ViewRuntime.getCurrentContentActivity();
        return (int) ((f10 / (currentContentActivity != null ? currentContentActivity.getResources() : ViewRuntime.requireContext(null).getResources()).getDisplayMetrics().density) + 0.5f);
    }

    public static void recyclerScrollToCenter(Context context, RecyclerView recyclerView, View view) {
        if (view != null) {
            int[] iArr = new int[2];
            view.getLocationOnScreen(iArr);
            recyclerView.smoothScrollBy(0, (view.getHeight() / 2) + (iArr[1] - (((int) getScreenHeight(context)) / 2)), null, IjkEventListener.BUFFERING_START);
        }
    }

    public static int resolveThemeColor(Context context, int i10, int i11) {
        TypedValue typedValue = new TypedValue();
        if (context == null || !context.getTheme().resolveAttribute(i10, typedValue, true)) {
            return i11;
        }
        int i12 = typedValue.resourceId;
        if (i12 == 0) {
            return typedValue.data;
        }
        Object obj = g0.d.f26125a;
        return context.getColor(i12);
    }

    public static void scrollViewScrollToCenter(NestedScrollView nestedScrollView, View view) {
        if (view == null || nestedScrollView == null) {
            return;
        }
        nestedScrollView.post(new k(nestedScrollView, view, 1));
    }

    public static void setAudio(Activity activity, TextView textView, ImageView imageView, List<String> list) {
        String upperCase;
        if (list == null || list.isEmpty()) {
            textView.setVisibility(8);
            imageView.setVisibility(8);
            return;
        }
        if (list.size() > 5) {
            upperCase = b(list.subList(0, 4)).toUpperCase() + "  " + activity.getString(R.string.others);
        } else {
            upperCase = b(list).toUpperCase();
        }
        textView.setText(upperCase);
        imageView.setVisibility(0);
        textView.setVisibility(0);
    }

    public static void setCertificate(TextView textView, String str, int i10) {
        if (str == null || str.isEmpty()) {
            str = i10 == 2 ? "PG" : "TV-PG";
        }
        textView.setText(str);
    }

    public static void setReleaseDate(TextView textView, String str) {
        textView.setText((str == null || str.length() <= 4) ? "" : str.substring(0, 4));
    }

    public static void setStatusBar(Activity activity) {
        Window window = activity.getWindow();
        o6.b.B(window, false);
        window.addFlags(128);
        ViewGroup viewGroup = (ViewGroup) activity.findViewById(android.R.id.content);
        View view = new View(activity);
        view.setLayoutParams(new ViewGroup.LayoutParams(-1, getStatusBarHeight(activity)));
        Object obj = g0.d.f26125a;
        view.setBackgroundColor(activity.getColor(android.R.color.black));
        viewGroup.addView(view, 0);
    }

    public static void setSubtitle(Activity activity, TextView textView, ImageView imageView, List<String> list) {
        String upperCase;
        if (list == null || list.isEmpty()) {
            textView.setVisibility(8);
            imageView.setVisibility(8);
            return;
        }
        Collections.sort(list, new xyz.stream.utils.n(1, ViewLanguageUtils.getShortPreferredOrder()));
        imageView.setVisibility(0);
        textView.setVisibility(0);
        if (list.size() > 5) {
            upperCase = b(list.subList(0, 4)).toUpperCase() + "  " + activity.getString(R.string.others);
        } else {
            upperCase = b(list).toUpperCase();
        }
        textView.setText(upperCase);
    }

    public static void setSvgPicture(ImageView imageView, int i10) {
        setSvgPicture(imageView, i10, null);
    }

    public static void setUnFocusable(View... viewArr) {
        for (View view : viewArr) {
            view.setFocusable(false);
            view.setFocusableInTouchMode(false);
        }
    }

    public static void setUpActivity(Activity activity, String str) {
        Window window = activity.getWindow();
        if (ViewRuntime.isTv()) {
            window.addFlags(128);
        } else {
            window.clearFlags(128);
        }
        o6.b.B(window, false);
        m3 m3Var = new m3(window, window.getDecorView());
        m3Var.a(7);
        m3Var.b();
        m3Var.f1786a.k(true);
        if (str == null) {
            str = activity.getClass().getSimpleName();
        }
        ViewRuntime.reportScreen(str);
    }

    public static void setViewRtl(View... viewArr) {
        for (View view : viewArr) {
            if (view != null) {
                view.post(new r0(view, 9));
            }
        }
    }

    public static void showErrorTipDialog(Activity activity, String str) {
        if (activity == null || activity.isFinishing() || activity.isDestroyed()) {
            return;
        }
        new Handler(Looper.getMainLooper()).post(new ve.b(activity, str, 29));
    }

    public static void showProgressDialog(Context context, String str) {
        CustomLoading.getInstance().show(context, str);
    }

    public static void showSpaceDialog(final Activity activity, final String str, final String str2) {
        if (activity == null || activity.isFinishing() || activity.isDestroyed()) {
            return;
        }
        BindingDialogBuilder.with(activity, new xyz.netfly.viewmodel.a(19)).theme(R.style.CustomDialog).setup(new BindingDialogBuilder.DialogSetup() { // from class: xyz.stream.view.j
            @Override // xyz.stream.view.BindingDialogBuilder.DialogSetup
            public final void setup(v3.a aVar, e.m mVar) {
                LayoutDialogNoSpaceBinding layoutDialogNoSpaceBinding = (LayoutDialogNoSpaceBinding) aVar;
                String str3 = str;
                Activity activity2 = activity;
                if (str3 != null) {
                    int i10 = R.color.f36332d9;
                    Object obj = g0.d.f26125a;
                    int color = activity2.getColor(i10);
                    int iResolveThemeColor = UiUtils.resolveThemeColor(activity2, R.attr.streamMainColor, color);
                    SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder();
                    UiUtils.a(spannableStringBuilder, activity2.getString(R.string.required_free_space), color);
                    UiUtils.a(spannableStringBuilder, "  " + str3 + "\n", iResolveThemeColor);
                    UiUtils.a(spannableStringBuilder, activity2.getString(R.string.currnet_free_space), color);
                    UiUtils.a(spannableStringBuilder, "  " + str2 + "\n", iResolveThemeColor);
                    UiUtils.a(spannableStringBuilder, activity2.getString(R.string.do_want_to_cleaner), color);
                    layoutDialogNoSpaceBinding.tvSpaceTip.setText(spannableStringBuilder);
                } else {
                    layoutDialogNoSpaceBinding.tvSpaceTip.setText(activity2.getString(R.string.storage_left));
                }
                layoutDialogNoSpaceBinding.tvCancel.setOnClickListener(new xyz.netfly.mobile.view.o(mVar, 9));
                layoutDialogNoSpaceBinding.tvConfirm.requestFocus();
                layoutDialogNoSpaceBinding.tvConfirm.setOnClickListener(new lf.i(activity2, mVar, 2));
            }
        }).width(680).maskOwner(activity).show();
    }

    public static void showTip(Context context, String str) {
        new Handler(Looper.getMainLooper()).post(new k(context, str, 0));
    }

    public static String stringToTime(long j10) {
        StringBuilder sb = new StringBuilder();
        return getStringForTime(sb, new Formatter(sb, Locale.US), j10);
    }

    public static void setSvgPicture(ImageView imageView, int i10, Integer num) {
        Context context;
        if (imageView == null) {
            context = null;
        } else {
            try {
                context = imageView.getContext();
            } catch (Exception e10) {
                ViewLog.e("setSvgPicture: " + e10.getMessage());
                return;
            }
        }
        Context contextRequireContext = ViewRuntime.requireContext(context);
        Drawable drawable = f.a.getDrawable(contextRequireContext, i10);
        Objects.requireNonNull(drawable);
        Drawable drawableMutate = drawable.mutate();
        if (num != null) {
            int iIntValue = num.intValue();
            Object obj = g0.d.f26125a;
            drawableMutate.setTint(contextRequireContext.getColor(iIntValue));
        }
        imageView.setImageDrawable(drawableMutate);
    }

    public static void showProgressDialog(Context context) {
        int identifier;
        String string = "";
        if (context != null && (identifier = context.getResources().getIdentifier("loading", "string", context.getPackageName())) != 0) {
            try {
                string = context.getString(identifier);
            } catch (Throwable unused) {
            }
        }
        showProgressDialog(context, string);
    }

    public static void recyclerScrollToCenter(RecyclerView recyclerView, int i10) {
        q1 layoutManager = recyclerView.getLayoutManager();
        if (layoutManager instanceof LinearLayoutManager) {
            ((LinearLayoutManager) layoutManager).scrollToPositionWithOffset(i10, dp2px(300.0f));
        }
    }
}
