package com.hdl.sdk.connect.cloud.broadcast; import android.text.TextUtils; import android.util.Log; import com.hdl.sdk.connect.bean.response.BindInfoBean; import com.hdl.sdk.connect.cloud.HDLException; import com.hdl.sdk.connect.cloud.HDLResponse; import com.hdl.sdk.connect.config.HDLCloudConfig; import com.hdl.sdk.sourceos.bind.BindHomeService; import com.hdl.sdk.sourceos.qrcode.QRCode; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; /** * Created by Tong on 2022/2/12. * token失效处理 */ public class HDLCloudBroadcast extends CloudBroadcast { private final ExecutorService service = Executors.newSingleThreadExecutor(); private volatile Future mBindTokenTask; private static final String TAG = HDLCloudBroadcast.class.getSimpleName(); /** * 登录失效了 */ @Override public void onLogOut() { super.onLogOut(); refreshBindingToken(); } /** * 刷新绑定Token */ public void refreshBindingToken() { Log.e(TAG, "refreshBindingToken"); if (mBindTokenTask != null) { try { mBindTokenTask.cancel(true); } catch (Throwable e) { e.printStackTrace(); } } final String code = QRCode.getBindQRCode(); if (!TextUtils.isEmpty(code)) { String[] split = code.split("_"); if (split.length == 3) { try { mBindTokenTask = service.submit(new Runnable() { @Override public void run() { try { try { //防止频繁休眠10秒 Thread.sleep(10 * 1000); } catch (Exception e) { e.printStackTrace(); } BindHomeService.isBind(split[0], split[1], split[2], new HDLResponse() { @Override public void onResponse(BindInfoBean response) { if (response != null) { HDLCloudConfig.getInstance().setToken(response.getToken()); HDLCloudConfig.getInstance().setRefreshToken(response.getRefreshToken()); } Log.e(TAG, "refreshBindingToken---> succeed"); } @Override public void onFailure(HDLException e) { Log.e(TAG, "refreshBindingToken error---> " + e); /*if (e instanceof ApiException) { if (e.getCode() != CloudCode.NO_TOKEN) { } }*/ } }); } catch (Exception e) { e.printStackTrace(); } } }); } catch (Exception e) { e.printStackTrace(); } } } } @Override public void onOtherError(int code, String message) { super.onOtherError(code, message); Log.d(TAG, "onOtherError: code" + code + "," + message); if (code == 125102 || code == 10401) { //思必驰失效了, 住宅和当前用户不匹配 Log.d(TAG, "onOtherError: 思必驰失效了"); refreshBindingToken(); } } }