package com.huawei.hms.common.internal; import android.os.Handler; import android.os.HandlerThread; import android.os.Looper; import android.os.Message; import com.huawei.hms.common.HuaweiApi; import com.huawei.hms.common.internal.BaseHmsClient; import com.huawei.hms.core.aidl.IAIDLInvoke; import com.huawei.hms.support.log.HMSLog; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.Map; import java.util.Queue; import java.util.concurrent.ConcurrentLinkedQueue; /* loaded from: classes2.dex */ public class RequestManager implements Handler.Callback { public static final int NOTIFY_CONNECT_FAILED = 10012; public static final int NOTIFY_CONNECT_SUCCESS = 10011; public static final int NOTIFY_CONNECT_SUSPENDED = 10013; private static volatile RequestManager b; private static Handler c; private static final Object a = new Object(); private static Queue d = new ConcurrentLinkedQueue(); private static Map e = new LinkedHashMap(); private RequestManager(Looper looper) { c = new Handler(looper, this); } public static void addRequestToQueue(HuaweiApi.RequestHandler requestHandler) { d.add(requestHandler); } private void b() { while (!d.isEmpty()) { HuaweiApi.RequestHandler poll = d.poll(); if (poll != null) { Object client = poll.getClient(); if (client instanceof BaseHmsClient) { BaseHmsClient baseHmsClient = (BaseHmsClient) client; baseHmsClient.setService(IAIDLInvoke.Stub.asInterface(baseHmsClient.getAdapter().getServiceBinder())); poll.onConnected(); } } } } private void c() { HMSLog.i("RequestManager", "NOTIFY_CONNECT_SUSPENDED."); while (!d.isEmpty()) { d.poll().onConnectionSuspended(1); } d(); } private void d() { StringBuilder sb = new StringBuilder("notifyRunningRequestConnectSuspend, connectedReqMap.size(): "); sb.append(e.size()); HMSLog.i("RequestManager", sb.toString()); Iterator> it = e.entrySet().iterator(); while (it.hasNext()) { try { it.next().getValue().onConnectionSuspended(1); } catch (RuntimeException e2) { StringBuilder sb2 = new StringBuilder("NOTIFY_CONNECT_SUSPENDED Exception: "); sb2.append(e2.getMessage()); HMSLog.e("RequestManager", sb2.toString()); } it.remove(); } } public static RequestManager getInstance() { synchronized (a) { if (b == null) { HandlerThread handlerThread = new HandlerThread("RequestManager"); handlerThread.start(); b = new RequestManager(handlerThread.getLooper()); } } return b; } @Override // android.os.Handler.Callback public boolean handleMessage(Message message) { if (message == null) { return false; } HMSLog.i("RequestManager", "RequestManager handleMessage."); switch (message.what) { case NOTIFY_CONNECT_SUCCESS /* 10011 */: b(); return true; case NOTIFY_CONNECT_FAILED /* 10012 */: a(message); return true; case NOTIFY_CONNECT_SUSPENDED /* 10013 */: c(); return true; default: StringBuilder sb = new StringBuilder("handleMessage unknown msg:"); sb.append(message.what); HMSLog.i("RequestManager", sb.toString()); return false; } } private void a(Message message) { HMSLog.i("RequestManager", "NOTIFY_CONNECT_FAILED."); try { BaseHmsClient.ConnectionResultWrapper connectionResultWrapper = (BaseHmsClient.ConnectionResultWrapper) message.obj; HuaweiApi.RequestHandler request = connectionResultWrapper.getRequest(); d.remove(request); request.onConnectionFailed(connectionResultWrapper.getConnectionResult()); } catch (RuntimeException e2) { StringBuilder sb = new StringBuilder(" handle Failed"); sb.append(e2.getMessage()); HMSLog.e("RequestManager", sb.toString()); } } public static void removeReqByTransId(String str) { if (c == null) { return; } HMSLog.i("RequestManager", "removeReqByTransId"); c.post(new Runnable(str) { // from class: com.huawei.hms.common.internal.RequestManager.2 final String a; { this.a = str; } @Override // java.lang.Runnable public void run() { RequestManager.e.remove(this.a); } }); } public static void addToConnectedReqMap(String str, HuaweiApi.RequestHandler requestHandler) { if (c == null) { return; } HMSLog.i("RequestManager", "addToConnectedReqMap"); c.post(new Runnable(str, requestHandler) { // from class: com.huawei.hms.common.internal.RequestManager.1 final String a; final HuaweiApi.RequestHandler b; { this.a = str; this.b = requestHandler; } @Override // java.lang.Runnable public void run() { RequestManager.e.put(this.a, this.b); } }); } public static Handler getHandler() { return c; } }