Trip On v2.1.2版本的 MD5 值为:55953a6deb5e434bd7a356ea51d31456
以下内容为反编译后的 AutoCheck.java 源代码,内容仅作参考
package com.baidu.aip.core.mini;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import androidx.core.content.ContextCompat;
import com.baidu.speech.asr.SpeechConstant;
import com.facebook.common.util.UriUtil;
import com.taobao.weex.el.parse.Operators;
import io.dcloud.application.DCLoudApplicationImpl;
import io.dcloud.common.util.StringUtil;
import io.dcloud.feature.R;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeSet;
import org.json.JSONObject;
public class AutoCheck {
private static final String TAG = "AutoCheck";
public static final boolean isOnlineLited = false;
private Context context;
private boolean enableOffline;
private Handler handler;
private boolean hasError;
private String name;
private boolean isFinished = false;
private LinkedHashMap<String, Check> checks = new LinkedHashMap<>();
public AutoCheck(Context context, final Handler handler, boolean enableOffline) {
this.context = context;
this.handler = handler;
this.enableOffline = enableOffline;
}
public void checkAsr(final Map<String, Object> params) {
new Thread(new Runnable() {
@Override
public void run() {
AutoCheck checkAsrInternal = AutoCheck.this.checkAsrInternal(params);
AutoCheck.this.name = DCLoudApplicationImpl.self().getContext().getString(R.string.dcloud_feature_speech_baidu_text_recognition);
synchronized (checkAsrInternal) {
AutoCheck.this.isFinished = true;
AutoCheck.this.handler.sendMessage(AutoCheck.this.handler.obtainMessage(10086, checkAsrInternal));
}
}
}).start();
}
public String obtainErrorMessage() {
return formatString(new PrintConfig());
}
public String obtainDebugMessage() {
PrintConfig printConfig = new PrintConfig();
printConfig.withInfo = true;
return formatString(printConfig);
}
public String obtainAllMessage() {
PrintConfig printConfig = new PrintConfig();
printConfig.withLog = true;
printConfig.withInfo = true;
printConfig.withLogOnSuccess = true;
return formatString(printConfig);
}
private String formatString(PrintConfig config) {
StringBuilder sb = new StringBuilder();
this.hasError = false;
for (Map.Entry<String, Check> entry : this.checks.entrySet()) {
Check value = entry.getValue();
String key = entry.getKey();
if (value.hasError()) {
if (!this.hasError) {
this.hasError = true;
}
sb.append(DCLoudApplicationImpl.self().getContext().getString(R.string.dcloud_feature_speech_baidu_sb_text_error));
sb.append(key);
sb.append(" 】 ");
sb.append(value.getErrorMessage());
sb.append("\n");
if (value.hasFix()) {
sb.append(DCLoudApplicationImpl.self().getContext().getString(R.string.dcloud_feature_speech_baidu_sb_text_fix_method));
sb.append(key);
sb.append(" 】 ");
sb.append(value.getFixMessage());
sb.append("\n");
}
} else if (config.withEachCheckInfo) {
sb.append(DCLoudApplicationImpl.self().getContext().getString(R.string.dcloud_feature_speech_baidu_sb_text_no_error_report));
sb.append(key);
sb.append(" 】 ");
sb.append("\n");
}
if (config.withInfo && value.hasInfo()) {
sb.append(DCLoudApplicationImpl.self().getContext().getString(R.string.dcloud_feature_speech_baidu_sb_text_check_again));
sb.append(key);
sb.append("】 ");
sb.append(value.getInfoMessage());
sb.append("\n");
}
if (config.withLog && (config.withLogOnSuccess || this.hasError)) {
if (value.hasLog()) {
sb.append("【log】:" + value.getLogMessage());
sb.append("\n");
}
}
}
if (!this.hasError) {
sb.append("【" + this.name + DCLoudApplicationImpl.self().getContext().getString(R.string.dcloud_feature_speech_baidu_sb_text_success_no_error));
}
return sb.toString();
}
public AutoCheck checkAsrInternal(Map<String, Object> params) {
commonSetting(params);
this.checks.put(DCLoudApplicationImpl.self().getContext().getString(R.string.dcloud_feature_speech_baidu_text_vertify_out_audio), new FileCheck(this.context, params, SpeechConstant.IN_FILE));
this.checks.put(DCLoudApplicationImpl.self().getContext().getString(R.string.dcloud_feature_speech_baidu_text_vertify_offline_bsg), new FileCheck(this.context, params, SpeechConstant.ASR_OFFLINE_ENGINE_GRAMMER_FILE_PATH));
for (Map.Entry<String, Check> entry : this.checks.entrySet()) {
Check value = entry.getValue();
value.check();
if (value.hasError()) {
break;
}
}
return this;
}
private void commonSetting(Map<String, Object> params) {
this.checks.put(DCLoudApplicationImpl.self().getContext().getString(R.string.dcloud_feature_speech_baidu_text_check_android_permission), new PermissionCheck(this.context));
this.checks.put(DCLoudApplicationImpl.self().getContext().getString(R.string.dcloud_feature_speech_baidu_text_check_android_so), new JniCheck(this.context));
try {
AppInfoCheck appInfoCheck = new AppInfoCheck(this.context, params);
this.checks.put(DCLoudApplicationImpl.self().getContext().getString(R.string.dcloud_feature_speech_baidu_text_check_appid_appkey), appInfoCheck);
if (this.enableOffline) {
this.checks.put(DCLoudApplicationImpl.self().getContext().getString(R.string.dcloud_feature_speech_baidu_text_check_package_name), new ApplicationIdCheck(this.context, appInfoCheck.appId));
}
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
}
public static class PrintConfig {
public boolean withEachCheckInfo;
public boolean withInfo;
public boolean withLog;
public boolean withLogOnSuccess;
private PrintConfig() {
this.withEachCheckInfo = false;
this.withInfo = false;
this.withLog = false;
this.withLogOnSuccess = false;
}
}
public static class PermissionCheck extends Check {
private Context context;
public PermissionCheck(Context context) {
this.context = context;
}
@Override
public void check() {
String[] strArr = {"android.permission.RECORD_AUDIO", "android.permission.ACCESS_NETWORK_STATE", "android.permission.INTERNET", "android.permission.READ_PHONE_STATE"};
ArrayList arrayList = new ArrayList();
for (int i = 0; i < 4; i++) {
String str = strArr[i];
if (ContextCompat.checkSelfPermission(this.context, str) != 0) {
arrayList.add(str);
}
}
if (arrayList.isEmpty()) {
return;
}
this.errorMessage = DCLoudApplicationImpl.self().getContext().getString(R.string.dcloud_feature_speech_baidu_text_loss_permission) + arrayList;
this.fixMessage = DCLoudApplicationImpl.self().getContext().getString(R.string.dcloud_feature_speech_baidu_text_copy_permission_from_manifest);
}
}
public static class JniCheck extends Check {
private Context context;
private String[] soNames = {"libBaiduSpeechSDK.so", "libvad.dnn.so", "libbd_easr_s1_merge_normal_20151216.dat.so", "libbdEASRAndroid.so", "libbdSpilWakeup.so"};
public JniCheck(Context context) {
this.context = context;
}
@Override
public void check() {
String[] strArr;
String str = this.context.getApplicationInfo().nativeLibraryDir;
appendLogMessage(DCLoudApplicationImpl.self().getContext().getString(R.string.dcloud_feature_speech_baidu_text_jni_dir) + str);
File[] listFiles = new File(str).listFiles();
TreeSet treeSet = new TreeSet();
if (listFiles != null) {
for (File file : listFiles) {
treeSet.add(file.getName());
}
}
for (String str2 : this.soNames) {
if (!treeSet.contains(str2)) {
this.errorMessage = DCLoudApplicationImpl.self().getContext().getString(R.string.dcloud_feature_speech_baidu_text_jni_error_msg_one) + str + DCLoudApplicationImpl.self().getContext().getString(R.string.dcloud_feature_speech_baidu_text_jni_error_msg_two) + str2 + DCLoudApplicationImpl.self().getContext().getString(R.string.dcloud_feature_speech_baidu_text_jni_error_msg_three) + treeSet.toString();
this.fixMessage = DCLoudApplicationImpl.self().getContext().getString(R.string.dcloud_feature_speech_baidu_text_jni_fix_message);
return;
}
}
}
}
public static class AppInfoCheck extends Check {
private String appId;
private String appKey;
private String secretKey;
public AppInfoCheck(Context context, Map<String, Object> params) throws PackageManager.NameNotFoundException {
Bundle bundle = context.getPackageManager().getApplicationInfo(context.getPackageName(), 128).metaData;
if (params.get("appid") != null) {
this.appId = params.get("appid").toString();
} else {
int i = bundle.getInt("com.baidu.speech.APP_ID", 0);
if (i > 0) {
this.appId = "" + i;
}
}
if (params.get("key") != null) {
this.appKey = params.get("key").toString();
} else {
this.appKey = bundle.getString("com.baidu.speech.API_KEY", "");
}
if (params.get(SpeechConstant.SECRET) != null) {
this.secretKey = params.get(SpeechConstant.SECRET).toString();
} else {
this.secretKey = bundle.getString("com.baidu.speech.SECRET_KEY", "");
}
}
@Override
public void check() {
appendLogMessage("try to check appId " + this.appId + " ,appKey=" + this.appKey + " ,secretKey" + this.secretKey);
String str = this.appId;
if (str == null || str.isEmpty()) {
this.errorMessage = DCLoudApplicationImpl.self().getContext().getString(R.string.dcloud_feature_speech_baidu_text_appid_empty);
this.fixMessage = DCLoudApplicationImpl.self().getContext().getString(R.string.dcloud_feature_speech_baidu_text_appid_fill);
return;
}
String str2 = this.appKey;
if (str2 == null || str2.isEmpty()) {
this.errorMessage = DCLoudApplicationImpl.self().getContext().getString(R.string.dcloud_feature_speech_baidu_text_appkey_empty);
this.fixMessage = DCLoudApplicationImpl.self().getContext().getString(R.string.dcloud_feature_speech_baidu_text_appkey_fill);
return;
}
String str3 = this.secretKey;
if (str3 == null || str3.isEmpty()) {
this.errorMessage = DCLoudApplicationImpl.self().getContext().getString(R.string.dcloud_feature_speech_baidu_text_secretkey_empty);
this.fixMessage = DCLoudApplicationImpl.self().getContext().getString(R.string.dcloud_feature_speech_baidu_text_secretkey_fill);
return;
}
try {
checkOnline();
} catch (UnknownHostException e) {
this.infoMessage = DCLoudApplicationImpl.self().getContext().getString(R.string.dcloud_feature_speech_baidu_error_no_network) + e.getMessage();
} catch (Exception e2) {
this.errorMessage = e2.getClass().getCanonicalName() + ":" + e2.getMessage();
this.fixMessage = DCLoudApplicationImpl.self().getContext().getString(R.string.dcloud_feature_speech_baidu_fixmessage_check_appid_appkey_again);
}
}
public void checkOnline() throws Exception {
String readLine;
HttpURLConnection httpURLConnection = (HttpURLConnection) new URL("http://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=" + this.appKey + "&client_secret=" + this.secretKey).openConnection();
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setConnectTimeout(1000);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
StringBuilder sb = new StringBuilder();
do {
readLine = bufferedReader.readLine();
if (readLine != null) {
sb.append(readLine);
continue;
}
} while (readLine != null);
String sb2 = sb.toString();
if (!sb2.contains("audio_voice_assistant_get")) {
this.errorMessage = "appid:" + this.appId + DCLoudApplicationImpl.self().getContext().getString(R.string.dcloud_feature_speech_baidu_error_no_audio_voice_assistant_get);
this.fixMessage = "secretKey";
return;
}
appendLogMessage("openapi return " + sb2);
JSONObject jSONObject = new JSONObject(sb2);
String optString = jSONObject.optString("error");
if (optString != null && !optString.isEmpty()) {
this.errorMessage = StringUtil.format(DCLoudApplicationImpl.self().getContext().getString(R.string.dcloud_feature_speech_baidu_error_appkey_message_format), optString, sb);
this.fixMessage = DCLoudApplicationImpl.self().getContext().getString(R.string.dcloud_feature_speech_baidu_fixmessage_check_appid_appkey_again);
return;
}
String string = jSONObject.getString("access_token");
if (string != null) {
if (string.endsWith(Operators.SUB + this.appId)) {
return;
}
}
this.errorMessage = StringUtil.format(DCLoudApplicationImpl.self().getContext().getString(R.string.dcloud_feature_speech_baidu_error_appkey_message_format), this.appId, string);
this.fixMessage = DCLoudApplicationImpl.self().getContext().getString(R.string.dcloud_feature_speech_baidu_fixmessage_check_appid_appkey_again);
}
}
public static class ApplicationIdCheck extends Check {
private String appId;
private Context context;
public ApplicationIdCheck(Context context, String appId) {
this.appId = appId;
this.context = context;
}
@Override
public void check() {
this.infoMessage = StringUtil.format(DCLoudApplicationImpl.self().getContext().getString(R.string.dcloud_feature_speech_baidu_sdk_fix_message_step), this.appId) + getApplicationId();
}
private String getApplicationId() {
return this.context.getPackageName();
}
}
public static class FileCheck extends Check {
private boolean allowAssets;
private boolean allowRes;
private Context context;
private String key;
private Map<String, Object> params;
public FileCheck(Context context, Map<String, Object> params, String key) {
this.allowRes = false;
this.allowAssets = true;
this.context = context;
this.params = params;
this.key = key;
if (key.equals(SpeechConstant.IN_FILE)) {
this.allowRes = true;
this.allowAssets = false;
}
}
@Override
public void check() {
if (this.params.containsKey(this.key)) {
String obj = this.params.get(this.key).toString();
if (this.allowAssets && obj.startsWith("assets")) {
String substring = obj.substring(10);
if (!":///".equals(obj.substring(6, 10)) || substring.isEmpty()) {
this.errorMessage = StringUtil.format(DCLoudApplicationImpl.self().getContext().getString(R.string.dcloud_feature_speech_baidu_msg_param_error_format), this.key, obj);
this.fixMessage = DCLoudApplicationImpl.self().getContext().getString(R.string.dcloud_feature_speech_baidu_msg_param_error_format_fix);
}
try {
this.context.getAssets().open(substring);
} catch (IOException e) {
this.errorMessage = DCLoudApplicationImpl.self().getContext().getString(R.string.dcloud_feature_speech_baidu_msg_asset_file_loss) + substring;
this.fixMessage = DCLoudApplicationImpl.self().getContext().getString(R.string.dcloud_feature_speech_baidu_msg_asset_file_loss_fix);
e.printStackTrace();
}
appendLogMessage(DCLoudApplicationImpl.self().getContext().getString(R.string.dcloud_feature_speech_baidu_msg_asset_check_done) + substring);
}
if (this.allowRes && obj.startsWith(UriUtil.LOCAL_RESOURCE_SCHEME)) {
String substring2 = obj.substring(7);
if (!":///".equals(obj.substring(3, 7)) || substring2.isEmpty()) {
this.errorMessage = StringUtil.format(DCLoudApplicationImpl.self().getContext().getString(R.string.dcloud_feature_speech_baidu_msg_param_error_format), this.key, obj);
this.fixMessage = DCLoudApplicationImpl.self().getContext().getString(R.string.dcloud_feature_speech_baidu_msg_param_error_format_fix_two);
}
if (getClass().getClassLoader().getResourceAsStream(substring2) == null) {
this.errorMessage = DCLoudApplicationImpl.self().getContext().getString(R.string.dcloud_feature_speech_baidu_msg_res_file_loss) + substring2;
this.fixMessage = DCLoudApplicationImpl.self().getContext().getString(R.string.dcloud_feature_speech_baidu_msg_res_file_loss_fix);
}
appendLogMessage(DCLoudApplicationImpl.self().getContext().getString(R.string.dcloud_feature_speech_baidu_msg_res_check_done) + substring2);
}
if (obj.startsWith("/")) {
if (!new File(obj).canRead()) {
this.errorMessage = DCLoudApplicationImpl.self().getContext().getString(R.string.dcloud_feature_speech_baidu_msg_file_path_not_exist) + obj;
this.fixMessage = DCLoudApplicationImpl.self().getContext().getString(R.string.dcloud_feature_speech_baidu_msg_file_path_not_exist_fix);
}
appendLogMessage(DCLoudApplicationImpl.self().getContext().getString(R.string.dcloud_feature_speech_baidu_msg_file_path_check_done) + obj);
}
}
}
}
public static abstract class Check {
protected String errorMessage = null;
protected String fixMessage = null;
protected String infoMessage = null;
protected StringBuilder logMessage = new StringBuilder();
public abstract void check();
public boolean hasError() {
return this.errorMessage != null;
}
public boolean hasFix() {
return this.fixMessage != null;
}
public boolean hasInfo() {
return this.infoMessage != null;
}
public boolean hasLog() {
return !this.logMessage.toString().isEmpty();
}
public void appendLogMessage(String message) {
StringBuilder sb = this.logMessage;
sb.append(message + "\n");
}
public String getErrorMessage() {
return this.errorMessage;
}
public String getFixMessage() {
return this.fixMessage;
}
public String getInfoMessage() {
return this.infoMessage;
}
public String getLogMessage() {
return this.logMessage.toString();
}
}
}