package androidx.core.text;

import android.annotation.SuppressLint;
import android.icu.util.ULocale;
import android.os.Build;
import android.util.Log;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Locale;

/* JADX INFO: compiled from: ICUCompat.java */
/* JADX INFO: loaded from: classes.dex */
public final class b {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    private static Method f1807a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private static Method f1808b;

    /* JADX INFO: compiled from: ICUCompat.java */
    static class a {
        static String a(Locale locale) {
            return locale.getScript();
        }
    }

    /* JADX INFO: renamed from: androidx.core.text.b$b, reason: collision with other inner class name */
    /* JADX INFO: compiled from: ICUCompat.java */
    static class C0017b {
        static ULocale a(Object obj) {
            return ULocale.addLikelySubtags((ULocale) obj);
        }

        static ULocale b(Locale locale) {
            return ULocale.forLocale(locale);
        }

        static String c(Object obj) {
            return ((ULocale) obj).getScript();
        }
    }

    static {
        int i3 = Build.VERSION.SDK_INT;
        if (i3 >= 21) {
            if (i3 < 24) {
                try {
                    f1808b = Class.forName("libcore.icu.ICU").getMethod("addLikelySubtags", Locale.class);
                    return;
                } catch (Exception e3) {
                    throw new IllegalStateException(e3);
                }
            }
            return;
        }
        try {
            Class<?> cls = Class.forName("libcore.icu.ICU");
            f1807a = cls.getMethod("getScript", String.class);
            f1808b = cls.getMethod("addLikelySubtags", String.class);
        } catch (Exception e4) {
            f1807a = null;
            f1808b = null;
            Log.w("ICUCompat", e4);
        }
    }

    @SuppressLint({"BanUncheckedReflection"})
    private static String a(Locale locale) {
        String string = locale.toString();
        try {
            Method method = f1808b;
            if (method != null) {
                return (String) method.invoke(null, string);
            }
        } catch (IllegalAccessException e3) {
            Log.w("ICUCompat", e3);
        } catch (InvocationTargetException e4) {
            Log.w("ICUCompat", e4);
        }
        return string;
    }

    @SuppressLint({"BanUncheckedReflection"})
    private static String b(String str) {
        try {
            Method method = f1807a;
            if (method != null) {
                return (String) method.invoke(null, str);
            }
        } catch (IllegalAccessException e3) {
            Log.w("ICUCompat", e3);
        } catch (InvocationTargetException e4) {
            Log.w("ICUCompat", e4);
        }
        return null;
    }

    public static String c(Locale locale) {
        int i3 = Build.VERSION.SDK_INT;
        if (i3 >= 24) {
            return C0017b.c(C0017b.a(C0017b.b(locale)));
        }
        if (i3 < 21) {
            String strA = a(locale);
            if (strA != null) {
                return b(strA);
            }
            return null;
        }
        try {
            return a.a((Locale) f1808b.invoke(null, locale));
        } catch (IllegalAccessException e3) {
            Log.w("ICUCompat", e3);
            return a.a(locale);
        } catch (InvocationTargetException e4) {
            Log.w("ICUCompat", e4);
            return a.a(locale);
        }
    }
}
