CEEX v1.0.1版本的 MD5 值为:d20246587215ec3471b0d051724c6d34
以下内容为反编译后的 NetworkLineActivity.java 源代码,内容仅作参考
package io.bhex.app.account.ui;
import android.content.res.Resources;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.ceex.app.R;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import io.bhex.app.account.bean.PingResponse;
import io.bhex.app.account.presenter.NetworkLinePresenter;
import io.bhex.app.base.BaseActivity;
import io.bhex.app.utils.NumberUtils;
import io.bhex.app.web.ui.WebActivity;
import io.bhex.baselib.mvp.BaseUI;
import io.bhex.baselib.network.BParamsBuilder;
import io.bhex.baselib.network.Utils.Convert;
import io.bhex.baselib.network.params.GetParams;
import io.bhex.baselib.network.request.RequestFactory;
import io.bhex.sdk.Urls;
import io.bhex.sdk.UrlsConfig;
import io.bhex.sdk.config.bean.BackupDomainBean;
import io.bhex.sdk.config.domain.BackupDomainManager;
import java.io.IOException;
import java.net.SocketException;
import java.security.SecureRandom;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nullable;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.WebSocket;
import okhttp3.WebSocketListener;
import okio.ByteString;
public class NetworkLineActivity extends BaseActivity<NetworkLinePresenter, NetworkLinePresenter.NetworkLineUI> implements NetworkLinePresenter.NetworkLineUI {
private static final long DELAY_MILLISS = 2000;
private static final String KEY_API = "_api";
private static final String KEY_OTC = "_otc";
private static final String KEY_WS = "_ws";
private static final int SEND_PING = 1;
private LinesAdapter adapter;
private List<BackupDomainBean> backupDomainList;
private StringBuilder domainParams;
private Handler handler;
private OkHttpClient okHttpClient;
private RecyclerView recyclerView;
private TimerTask task;
private Timer timer;
private TreeMap<String, String> costTimeMap = new TreeMap<>();
private ConcurrentHashMap<String, WebSocket> webSocketMap = new ConcurrentHashMap<>();
private ConcurrentHashMap<String, Long> pingTimeMap = new ConcurrentHashMap<>();
public class LinesAdapter extends BaseQuickAdapter<BackupDomainBean, BaseViewHolder> {
private BackupDomainBean currentDomain;
public LinesAdapter(List<BackupDomainBean> list) {
super(R.layout.item_lines_layout, list);
this.currentDomain = BackupDomainManager.getInstance().getCurrentDomain();
}
@Override
public void convert(BaseViewHolder baseViewHolder, final BackupDomainBean backupDomainBean) {
String domain = backupDomainBean.getDomain();
baseViewHolder.setText(R.id.item_value, NetworkLineActivity.this.getString(R.string.string_line) + (baseViewHolder.getLayoutPosition() + 1));
String str = (String) NetworkLineActivity.this.costTimeMap.get(domain + NetworkLineActivity.KEY_API);
int i = R.color.green;
if (str == null) {
baseViewHolder.setText(R.id.item_api_cost_time, this.mContext.getResources().getString(R.string.string_placeholder));
} else if (TextUtils.isEmpty(str)) {
baseViewHolder.setText(R.id.item_api_cost_time, NetworkLineActivity.this.getString(R.string.string_time_out));
baseViewHolder.setTextColor(R.id.item_api_cost_time, NetworkLineActivity.this.getResources().getColor(R.color.red));
} else {
baseViewHolder.setText(R.id.item_api_cost_time, str + " ms");
baseViewHolder.setTextColor(R.id.item_api_cost_time, NetworkLineActivity.this.getResources().getColor(NumberUtils.sub(str, "300") <= 0.0d ? R.color.green : R.color.orange));
}
TreeMap treeMap = NetworkLineActivity.this.costTimeMap;
String str2 = (String) treeMap.get(domain + NetworkLineActivity.KEY_WS);
if (str2 != null && !str2.equals("--")) {
if (TextUtils.isEmpty(str2)) {
baseViewHolder.setText(R.id.item_ws_cost_time, NetworkLineActivity.this.getString(R.string.string_time_out));
baseViewHolder.setTextColor(R.id.item_ws_cost_time, NetworkLineActivity.this.getResources().getColor(R.color.red));
} else {
baseViewHolder.setText(R.id.item_ws_cost_time, str2 + " ms");
Resources resources = NetworkLineActivity.this.getResources();
if (NumberUtils.sub(str2, "300") > 0.0d) {
i = R.color.orange;
}
baseViewHolder.setTextColor(R.id.item_ws_cost_time, resources.getColor(i));
}
} else {
baseViewHolder.setText(R.id.item_ws_cost_time, this.mContext.getResources().getString(R.string.string_placeholder));
}
BackupDomainBean backupDomainBean2 = this.currentDomain;
if (backupDomainBean2 != null) {
if (backupDomainBean2.getDomain().equals(backupDomainBean.getDomain())) {
baseViewHolder.getConvertView().findViewById(R.id.item_selected).setBackgroundResource(R.mipmap.icon_checkbox_checked);
} else {
baseViewHolder.getConvertView().findViewById(R.id.item_selected).setBackgroundResource(R.mipmap.icon_checkbox_normal);
}
}
baseViewHolder.getConvertView().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
LinesAdapter.this.currentDomain = backupDomainBean;
BackupDomainManager.getInstance().switchDomain(backupDomainBean);
LinesAdapter.this.notifyDataSetChanged();
}
});
}
}
private void apiProbe(BackupDomainBean backupDomainBean) {
final String domain = backupDomainBean.getDomain();
if (TextUtils.isEmpty(domain)) {
return;
}
String loadApiServer = loadApiServer(backupDomainBean);
if (TextUtils.isEmpty(loadApiServer)) {
return;
}
this.okHttpClient.newCall(RequestFactory.createRequest(((GetParams.Builder) BParamsBuilder.get().url(loadApiServer + "api/time")).build())).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException iOException) {
TreeMap treeMap = NetworkLineActivity.this.costTimeMap;
treeMap.put(domain + NetworkLineActivity.KEY_API, "");
}
@Override
public void onResponse(Call call, Response response) throws IOException {
NetworkLineActivity networkLineActivity = NetworkLineActivity.this;
networkLineActivity.w();
if (networkLineActivity == null || !NetworkLineActivity.this.isAlive()) {
return;
}
long receivedResponseAtMillis = response.receivedResponseAtMillis() - response.sentRequestAtMillis();
TreeMap treeMap = NetworkLineActivity.this.costTimeMap;
treeMap.put(domain + NetworkLineActivity.KEY_API, String.valueOf(receivedResponseAtMillis));
NetworkLineActivity.this.handler.sendEmptyMessage(0);
}
});
}
private void buildOkClient() {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
TimeUnit timeUnit = TimeUnit.SECONDS;
builder.connectTimeout(5L, timeUnit).retryOnConnectionFailure(true).readTimeout(5L, timeUnit).writeTimeout(5L, timeUnit);
this.okHttpClient = builder.build();
}
private static SSLSocketFactory createSSLSocketFactory() {
try {
SSLContext sSLContext = SSLContext.getInstance("TLS");
sSLContext.init(null, new TrustManager[]{new TrustAllCerts()}, new SecureRandom());
return sSLContext.getSocketFactory();
} catch (Exception unused) {
return null;
}
}
private String loadApiServer(BackupDomainBean backupDomainBean) {
String domain = backupDomainBean.getDomain();
int level = backupDomainBean.getLevel();
if (!TextUtils.isEmpty(domain)) {
if (level == 1) {
return "https://app." + domain + "/";
} else if (level == 2) {
return "https://" + domain + "/";
}
}
return "";
}
private String loadWsServer(BackupDomainBean backupDomainBean) {
String domain = backupDomainBean.getDomain();
int level = backupDomainBean.getLevel();
if (!TextUtils.isEmpty(domain)) {
if (level == 1) {
return "wss://ws." + domain + "/";
} else if (level == 2) {
return "wss://" + domain + "/";
}
}
return "";
}
public synchronized void loopProbe(BackupDomainBean backupDomainBean) {
if (backupDomainBean == null) {
return;
}
apiProbe(backupDomainBean);
wsProbe(backupDomainBean);
}
public void sendPing(String str) {
WebSocket webSocket = this.webSocketMap.get(str);
if (webSocket != null) {
long currentTimeMillis = System.currentTimeMillis();
this.pingTimeMap.put(str, Long.valueOf(currentTimeMillis));
webSocket.send("{\"ping\":\"" + currentTimeMillis + "\",\"sendTime\":" + currentTimeMillis + "}");
}
}
private void startTimer() {
this.timer = new Timer();
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
if (NetworkLineActivity.this.backupDomainList != null) {
for (BackupDomainBean backupDomainBean : NetworkLineActivity.this.backupDomainList) {
NetworkLineActivity.this.loopProbe(backupDomainBean);
}
}
}
};
this.task = timerTask;
this.timer.schedule(timerTask, 100L, DELAY_MILLISS);
}
private void wsProbe(BackupDomainBean backupDomainBean) {
final String domain = backupDomainBean.getDomain();
if (TextUtils.isEmpty(domain)) {
return;
}
String loadWsServer = loadWsServer(backupDomainBean);
if (TextUtils.isEmpty(loadWsServer)) {
return;
}
final String str = loadWsServer + "ws/quote/v1";
Request build = new Request.Builder().url(str).build();
if (this.webSocketMap.get(str) == null) {
this.okHttpClient.newWebSocket(build, new WebSocketListener() {
@Override
public void onClosed(WebSocket webSocket, int i, String str2) {
super.onClosed(webSocket, i, str2);
NetworkLineActivity.this.webSocketMap.remove(str);
TreeMap treeMap = NetworkLineActivity.this.costTimeMap;
treeMap.put(domain + NetworkLineActivity.KEY_WS, "--");
NetworkLineActivity.this.handler.sendEmptyMessage(0);
}
@Override
public void onClosing(WebSocket webSocket, int i, String str2) {
super.onClosing(webSocket, i, str2);
}
@Override
public void onFailure(WebSocket webSocket, Throwable th, @Nullable Response response) {
super.onFailure(webSocket, th, response);
NetworkLineActivity.this.webSocketMap.remove(str);
if (th instanceof SocketException) {
TreeMap treeMap = NetworkLineActivity.this.costTimeMap;
treeMap.put(domain + NetworkLineActivity.KEY_WS, "--");
} else {
TreeMap treeMap2 = NetworkLineActivity.this.costTimeMap;
treeMap2.put(domain + NetworkLineActivity.KEY_WS, "");
}
NetworkLineActivity.this.handler.sendEmptyMessage(0);
}
@Override
public void onMessage(WebSocket webSocket, String str2) {
super.onMessage(webSocket, str2);
if (TextUtils.isEmpty(str2)) {
return;
}
long currentTimeMillis = System.currentTimeMillis();
if (((PingResponse) Convert.fromJson(str2, (Class<Object>) PingResponse.class)) != null) {
long longValue = currentTimeMillis - ((Long) NetworkLineActivity.this.pingTimeMap.get(str)).longValue();
TreeMap treeMap = NetworkLineActivity.this.costTimeMap;
treeMap.put(domain + NetworkLineActivity.KEY_WS, String.valueOf(longValue));
NetworkLineActivity.this.handler.sendEmptyMessage(0);
Message message = new Message();
message.what = 1;
message.obj = str;
NetworkLineActivity.this.handler.sendMessageDelayed(message, NetworkLineActivity.DELAY_MILLISS);
}
}
@Override
public void onOpen(WebSocket webSocket, Response response) {
super.onOpen(webSocket, response);
NetworkLineActivity.this.webSocketMap.put(str, webSocket);
long receivedResponseAtMillis = response.receivedResponseAtMillis() - response.sentRequestAtMillis();
TreeMap treeMap = NetworkLineActivity.this.costTimeMap;
treeMap.put(domain + NetworkLineActivity.KEY_WS, String.valueOf(receivedResponseAtMillis));
NetworkLineActivity.this.handler.sendEmptyMessage(0);
NetworkLineActivity.this.sendPing(str);
}
@Override
public void onMessage(WebSocket webSocket, ByteString byteString) {
super.onMessage(webSocket, byteString);
}
});
}
}
@Override
public void addEvent() {
super.addEvent();
this.c.find(R.id.btn_network_detection).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
NetworkLineActivity.this.backupDomainList.iterator();
String replaceFirst = UrlsConfig.API_SERVER_URL.replaceFirst("app.", "www.");
String substring = replaceFirst.substring(0, replaceFirst.length() - 1);
NetworkLineActivity.this.domainParams = new StringBuilder();
StringBuilder sb = NetworkLineActivity.this.domainParams;
sb.append("?domain=" + substring);
NetworkLineActivity networkLineActivity = NetworkLineActivity.this;
String string = networkLineActivity.getString(R.string.string_network_detection);
WebActivity.runActivity(networkLineActivity, string, Urls.H5_NETWORK_DETECTION + ((Object) NetworkLineActivity.this.domainParams));
}
});
}
@Override
protected BaseUI c() {
w();
return this;
}
@Override
protected int e() {
return R.layout.activity_network_line_layout;
}
@Override
public void initView() {
super.initView();
this.handler = new Handler() {
@Override
public void handleMessage(@NonNull Message message) {
super.handleMessage(message);
int i = message.what;
if (i == 0) {
NetworkLineActivity.this.adapter.setNewData(NetworkLineActivity.this.backupDomainList);
} else if (i != 1) {
} else {
NetworkLineActivity.this.sendPing((String) message.obj);
}
}
};
this.recyclerView = (RecyclerView) this.c.find(R.id.recyclerView);
buildOkClient();
List<BackupDomainBean> backupDomainList = BackupDomainManager.getInstance().getBackupDomainList();
this.backupDomainList = backupDomainList;
showLines(backupDomainList);
}
@Override
public void onPause() {
super.onPause();
TimerTask timerTask = this.task;
if (timerTask != null) {
timerTask.cancel();
}
Timer timer = this.timer;
if (timer != null) {
timer.cancel();
}
ConcurrentHashMap<String, WebSocket> concurrentHashMap = this.webSocketMap;
if (concurrentHashMap != null) {
for (String str : concurrentHashMap.keySet()) {
WebSocket webSocket = this.webSocketMap.get(str);
if (webSocket != null) {
webSocket.cancel();
}
}
this.webSocketMap.clear();
}
}
@Override
public void onResume() {
super.onResume();
startTimer();
}
public void showLines(List<BackupDomainBean> list) {
if (list == null) {
return;
}
LinesAdapter linesAdapter = this.adapter;
if (linesAdapter == null) {
LinesAdapter linesAdapter2 = new LinesAdapter(list);
this.adapter = linesAdapter2;
linesAdapter2.isFirstOnly(false);
this.recyclerView.setLayoutManager(new LinearLayoutManager(this));
this.recyclerView.setAdapter(this.adapter);
return;
}
linesAdapter.setNewData(list);
}
@Override
public NetworkLinePresenter a() {
return new NetworkLinePresenter();
}
protected NetworkLinePresenter.NetworkLineUI w() {
return this;
}
}