136 lines
5.9 KiB
Java
136 lines
5.9 KiB
Java
package com.huawei.hms.common.internal;
|
|
|
|
import android.app.Activity;
|
|
import android.content.Context;
|
|
import android.os.Parcelable;
|
|
import android.text.TextUtils;
|
|
import com.huawei.hms.adapter.BaseAdapter;
|
|
import com.huawei.hms.common.internal.AnyClient;
|
|
import com.huawei.hms.common.internal.BaseHmsClient;
|
|
import com.huawei.hms.core.aidl.IMessageEntity;
|
|
import com.huawei.hms.support.api.entity.core.CommonCode;
|
|
import com.huawei.hms.support.log.HMSLog;
|
|
import java.lang.ref.WeakReference;
|
|
import org.json.JSONObject;
|
|
|
|
/* loaded from: classes2.dex */
|
|
public abstract class HmsClient extends BaseHmsClient implements AnyClient {
|
|
public HmsClient(Context context, ClientSettings clientSettings, BaseHmsClient.OnConnectionFailedListener onConnectionFailedListener, BaseHmsClient.ConnectionCallbacks connectionCallbacks) {
|
|
super(context, clientSettings, onConnectionFailedListener, connectionCallbacks);
|
|
}
|
|
|
|
@Override // com.huawei.hms.common.internal.AnyClient
|
|
public void post(IMessageEntity iMessageEntity, String str, AnyClient.CallBack callBack) {
|
|
if (callBack == null) {
|
|
HMSLog.e("HmsClient", "callback is invalid, discard.");
|
|
return;
|
|
}
|
|
if ((iMessageEntity instanceof RequestHeader) && str != null) {
|
|
if (!isConnected()) {
|
|
StringBuilder sb = new StringBuilder("No connection now, the connection status:");
|
|
sb.append(getConnectionStatus());
|
|
HMSLog.i("HmsClient", sb.toString());
|
|
if (getConnectionStatus() != 6) {
|
|
HMSLog.e("HmsClient", "post failed for not connected.");
|
|
callBack.onCallback(new ResponseHeader(1, CommonCode.ErrorCode.INTERNAL_ERROR, "Not Connected"), new JSONObject().toString());
|
|
return;
|
|
} else {
|
|
HMSLog.i("HmsClient", "in timeout-disconnect status, need to bind again.");
|
|
a();
|
|
}
|
|
}
|
|
RequestHeader requestHeader = (RequestHeader) iMessageEntity;
|
|
HMSLog.i("HmsClient", "post msg ".concat(String.valueOf(requestHeader)));
|
|
Activity cpActivity = getClientSettings().getCpActivity();
|
|
boolean z = cpActivity == null;
|
|
if (z) {
|
|
StringBuilder sb2 = new StringBuilder("Activity is null for ");
|
|
sb2.append(getClientSettings().getAppID());
|
|
HMSLog.i("HmsClient", sb2.toString());
|
|
}
|
|
(z ? new BaseAdapter(this) : new BaseAdapter(this, cpActivity)).baseRequest(requestHeader.toJson(), str, requestHeader.getParcelable(), new a(this, callBack));
|
|
return;
|
|
}
|
|
HMSLog.e("HmsClient", "arguments is invalid.");
|
|
callBack.onCallback(new ResponseHeader(1, CommonCode.ErrorCode.ARGUMENTS_INVALID, "Args is invalid"), new JSONObject().toString());
|
|
}
|
|
|
|
public void updateSessionId(String str) {
|
|
if (TextUtils.isEmpty(this.sessionId)) {
|
|
this.sessionId = str;
|
|
}
|
|
}
|
|
|
|
/* loaded from: classes2.dex */
|
|
static class a implements BaseAdapter.BaseCallBack {
|
|
private final AnyClient.CallBack a;
|
|
private final WeakReference<HmsClient> b;
|
|
|
|
a(HmsClient hmsClient, AnyClient.CallBack callBack) {
|
|
this.a = callBack;
|
|
this.b = new WeakReference<>(hmsClient);
|
|
}
|
|
|
|
private void a(String str) {
|
|
HmsClient hmsClient = this.b.get();
|
|
if (hmsClient != null) {
|
|
hmsClient.updateSessionId(str);
|
|
}
|
|
}
|
|
|
|
@Override // com.huawei.hms.adapter.BaseAdapter.BaseCallBack
|
|
public void onComplete(String str, String str2, Parcelable parcelable) {
|
|
if (parcelable == null) {
|
|
a(str, str2);
|
|
} else {
|
|
a(str, str2, parcelable);
|
|
}
|
|
}
|
|
|
|
@Override // com.huawei.hms.adapter.BaseAdapter.BaseCallBack
|
|
public void onError(String str) {
|
|
if (this.a == null) {
|
|
return;
|
|
}
|
|
ResponseWrap responseWrap = new ResponseWrap(new ResponseHeader());
|
|
if (responseWrap.fromJson(str)) {
|
|
HMSLog.i("HmsClient", "receive msg ".concat(String.valueOf(responseWrap)));
|
|
ResponseHeader responseHeader = responseWrap.getResponseHeader();
|
|
a(responseHeader.getSessionId());
|
|
this.a.onCallback(responseHeader, responseWrap.getBody());
|
|
return;
|
|
}
|
|
this.a.onCallback(new ResponseHeader(1, CommonCode.ErrorCode.ARGUMENTS_INVALID, "response header json error"), new JSONObject().toString());
|
|
}
|
|
|
|
private void a(String str, String str2) {
|
|
if (this.a == null) {
|
|
return;
|
|
}
|
|
ResponseHeader responseHeader = new ResponseHeader();
|
|
if (responseHeader.fromJson(str)) {
|
|
HMSLog.i("HmsClient", "receive msg ".concat(String.valueOf(responseHeader)));
|
|
a(responseHeader.getSessionId());
|
|
this.a.onCallback(responseHeader, str2);
|
|
return;
|
|
}
|
|
this.a.onCallback(new ResponseHeader(1, CommonCode.ErrorCode.ARGUMENTS_INVALID, "response header json error"), new JSONObject().toString());
|
|
}
|
|
|
|
private void a(String str, String str2, Parcelable parcelable) {
|
|
if (this.a == null) {
|
|
return;
|
|
}
|
|
ResponseHeader responseHeader = new ResponseHeader();
|
|
if (responseHeader.fromJson(str)) {
|
|
responseHeader.setParcelable(parcelable);
|
|
HMSLog.i("HmsClient", "receive msg ".concat(String.valueOf(responseHeader)));
|
|
a(responseHeader.getSessionId());
|
|
this.a.onCallback(responseHeader, str2);
|
|
return;
|
|
}
|
|
this.a.onCallback(new ResponseHeader(1, CommonCode.ErrorCode.ARGUMENTS_INVALID, "response header json error"), new JSONObject().toString());
|
|
}
|
|
}
|
|
}
|