package io.grpc; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Iterator; import java.util.List; import java.util.ServiceConfigurationError; import java.util.ServiceLoader; /* loaded from: classes6.dex */ final class ServiceProviders { /* loaded from: classes6.dex */ public interface PriorityAccessor { int getPriority(T t); boolean isAvailable(T t); } private ServiceProviders() { } public static T load(Class cls, Iterable> iterable, ClassLoader classLoader, PriorityAccessor priorityAccessor) { List loadAll = loadAll(cls, iterable, classLoader, priorityAccessor); if (loadAll.isEmpty()) { return null; } return (T) loadAll.get(0); } public static List loadAll(Class cls, Iterable> iterable, ClassLoader classLoader, PriorityAccessor priorityAccessor) { Iterable candidatesViaServiceLoader; if (isAndroid(classLoader)) { candidatesViaServiceLoader = getCandidatesViaHardCoded(cls, iterable); } else { candidatesViaServiceLoader = getCandidatesViaServiceLoader(cls, classLoader); } ArrayList arrayList = new ArrayList(); for (T t : candidatesViaServiceLoader) { if (priorityAccessor.isAvailable(t)) { arrayList.add(t); } } Collections.sort(arrayList, Collections.reverseOrder(new Comparator(priorityAccessor) { // from class: io.grpc.ServiceProviders.1 final PriorityAccessor val$priorityAccessor; { this.val$priorityAccessor = priorityAccessor; } @Override // java.util.Comparator public int compare(T t2, T t3) { int priority = this.val$priorityAccessor.getPriority(t2) - this.val$priorityAccessor.getPriority(t3); return priority != 0 ? priority : t2.getClass().getName().compareTo(t3.getClass().getName()); } })); return Collections.unmodifiableList(arrayList); } /* JADX INFO: Access modifiers changed from: package-private */ public static boolean isAndroid(ClassLoader classLoader) { try { Class.forName("android.app.Application", false, classLoader); return true; } catch (Exception unused) { return false; } } public static Iterable getCandidatesViaServiceLoader(Class cls, ClassLoader classLoader) { ServiceLoader load = ServiceLoader.load(cls, classLoader); return !load.iterator().hasNext() ? ServiceLoader.load(cls) : load; } /* JADX INFO: Access modifiers changed from: package-private */ public static Iterable getCandidatesViaHardCoded(Class cls, Iterable> iterable) { ArrayList arrayList = new ArrayList(); Iterator> it = iterable.iterator(); while (it.hasNext()) { arrayList.add(create(cls, it.next())); } return arrayList; } static T create(Class cls, Class cls2) { try { return (T) cls2.asSubclass(cls).getConstructor(new Class[0]).newInstance(new Object[0]); } catch (Throwable th) { throw new ServiceConfigurationError(String.format("Provider %s could not be instantiated %s", cls2.getName(), th), th); } } }