panlili2024
2025-03-05 134209ad70f82051da3ce63471df0cc8f778e57d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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<BindInfoBean>() {
                                    @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();
        }
    }
}