package com.baidu.speech.utils.analysis; import android.content.Context; import android.util.Log; import com.baidu.speech.asr.SpeechConstant; import com.baidu.speech.utils.Policy; import com.baidu.tts.loopj.AsyncHttpClient; import java.io.File; import java.io.FileInputStream; import java.io.FileWriter; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Scanner; import org.json.JSONArray; import org.json.JSONObject; public class Analysis { private static final boolean DEBUG = false; private static final String KEY_APP_ID = "appid"; private static final String KEY_APP_NAME = "app_name"; private static final String KEY_APP_SIGNATURE = "app_signature"; private static final String KEY_CUID = "wise_cuid"; private static final String KEY_ERROR = "error"; private static final String KEY_NET_TYPE = "net_type"; private static final String KEY_OS = "os"; private static final String KEY_PID = "pid"; private static final String KEY_PKG = "pkg"; private static final String KEY_PLATFORM = "platform"; private static final String KEY_RECOGNITION_RESULT = "recog_results"; private static final String KEY_RECOGNITION_RESULT_CMD_ID = "cmd_id"; private static final String KEY_RECOGNITION_RESULT_CMD_TYPE = "cmd_type"; private static final String KEY_RECOGNITION_RESULT_ERROR_CODE = "error_code"; private static final String KEY_RECOGNITION_RESULT_TIME = "time"; private static final String KEY_RECOGNITION_RESULT_VOICE_TO_TEXT = "voice_to_text_result"; private static final String KEY_RESPONSE_ERROR_CODE = "errno"; private static final String KEY_RESPONSE_UPLOAD_DATA = "data"; private static final String KEY_RESPONSE_UPLOAD_PERIOD = "secs"; private static final String KEY_SCREEN = "screen"; private static final String KEY_SDK_NAME = "sdk_name"; private static final String KEY_SDK_VERSION = "sdk_version"; private static final String KEY_TYPE = "type"; private static final String TAG = "Analysis"; public static class Item { public static final String TYPE_ASR_LONGSPEECH = "asr_longspeech"; public static final String TYPE_NORMAL = "asr_normal"; public static final String TYPE_TTS = "tts"; public static final String TYPE_WAKEUP = "wakeup"; public int appId = 0; public int decoder; public int error_code; public int pid; public String pkg; public long time; public String type; public String toString() { HashMap hashMap = new HashMap(); hashMap.put(Analysis.KEY_RECOGNITION_RESULT_TIME, Long.valueOf(this.time)); hashMap.put(Analysis.KEY_RECOGNITION_RESULT_ERROR_CODE, Integer.valueOf(this.error_code)); hashMap.put(Analysis.KEY_RECOGNITION_RESULT_CMD_TYPE, 0); hashMap.put(Analysis.KEY_RECOGNITION_RESULT_CMD_ID, 0); hashMap.put(Analysis.KEY_RECOGNITION_RESULT_VOICE_TO_TEXT, ""); hashMap.put("pid", Integer.valueOf(this.pid)); hashMap.put(SpeechConstant.DECODER, Integer.valueOf(this.decoder)); hashMap.put(Analysis.KEY_TYPE, this.type); hashMap.put(Analysis.KEY_PKG, this.pkg); hashMap.put("app_id", Integer.valueOf(this.appId)); return new JSONObject(hashMap).toString(); } } public static void asyncUploadAll(final Context context) { new Thread(new Runnable() { /* class com.baidu.speech.utils.analysis.Analysis.AnonymousClass1 */ public void run() { Analysis.syncUploadAll(context); } }).start(); } private static String buildStatUrl(Context context, String str) throws UnsupportedEncodingException { LinkedList linkedList = new LinkedList(); linkedList.add("wise_cuid=" + URLEncoder.encode(Policy.uid(context), "utf-8")); linkedList.add("sdk_version=" + URLEncoder.encode(Utility.getSdkVersion(), "utf-8")); linkedList.add("app_name=" + URLEncoder.encode(Utility.getAppName(context), "utf-8")); linkedList.add("platform=" + URLEncoder.encode(Utility.getPlatform(context), "utf-8")); linkedList.add("os=" + URLEncoder.encode(Utility.getOS(), "utf-8")); StringBuilder sb = new StringBuilder(); sb.append("net_type="); sb.append(URLEncoder.encode(Utility.getNetType(context) + "", "utf-8")); linkedList.add(sb.toString()); linkedList.add("appid=" + URLEncoder.encode(str, "utf-8")); linkedList.add("screen=" + URLEncoder.encode(Utility.getScreen(context), "utf-8")); linkedList.add("sdk_name=" + URLEncoder.encode(Utility.getSdkName(), "utf-8")); linkedList.add("app_signature=" + URLEncoder.encode(Utility.getSignatureMD5(context), "utf-8")); String join = join(linkedList, "&"); return "https://yuyin.baidu.com/voice?osname=voiceopen&action=usereventflow&" + join; } /* JADX WARNING: Removed duplicated region for block: B:36:0x00ca A[Catch:{ all -> 0x00be }] */ /* JADX WARNING: Removed duplicated region for block: B:37:0x00cb A[Catch:{ all -> 0x00be }] */ /* JADX WARNING: Removed duplicated region for block: B:40:0x00d5 */ private static String httpRequest(String str, Map<String, String> map, byte[] bArr, boolean z) throws Exception { Throwable th; Exception e; HttpURLConnection httpURLConnection = null; try { if (Log.isLoggable(TAG, 3)) { Log.i(TAG, "cur time: " + (System.currentTimeMillis() % 1000000) + ", http req: " + str); } HttpURLConnection httpURLConnection2 = (HttpURLConnection) new URL(str).openConnection(); try { httpURLConnection2.setConnectTimeout(8000); httpURLConnection2.setReadTimeout(8000); httpURLConnection2.setInstanceFollowRedirects(false); for (Map.Entry<String, String> entry : map.entrySet()) { httpURLConnection2.setRequestProperty(entry.getKey(), entry.getValue()); } if (bArr != null || z) { httpURLConnection2.setRequestMethod("POST"); } httpURLConnection2.connect(); if (bArr != null) { httpURLConnection2.getOutputStream().write(bArr); } String next = new Scanner(httpURLConnection2.getInputStream()).useDelimiter("\\A").next(); if (Log.isLoggable(TAG, 3)) { Log.i(TAG, "http res: " + next); } if (httpURLConnection2 != null) { httpURLConnection2.disconnect(); } return next; } catch (Exception e2) { e = e2; httpURLConnection = httpURLConnection2; try { if (!Log.isLoggable(TAG, 3)) { } throw e; } catch (Throwable th2) { th = th2; httpURLConnection2 = httpURLConnection; if (httpURLConnection2 != null) { } throw th; } } catch (Throwable th3) { th = th3; if (httpURLConnection2 != null) { httpURLConnection2.disconnect(); } throw th; } } catch (Exception e3) { e = e3; if (!Log.isLoggable(TAG, 3)) { Log.w(TAG, "", e); } throw e; } } private static String join(List<String> list, String str) { StringBuilder sb = new StringBuilder(); boolean z = true; for (String str2 : list) { if (z) { z = false; } else { sb.append(str); } sb.append(str2); } return sb.toString(); } public static void log(Context context, Item item) { try { File file = new File(context.getFilesDir(), "open-analysis"); file.mkdirs(); if (!file.exists()) { return; } if (item.appId != 0) { File file2 = new File(file, System.currentTimeMillis() + ".txt"); FileWriter fileWriter = new FileWriter(file2); String item2 = item.toString(); if (Log.isLoggable(TAG, 3)) { Log.i(TAG, "write to " + file2 + ", \t content: " + item2); } fileWriter.write(item.toString()); fileWriter.write("\r\n"); fileWriter.close(); } else if (Log.isLoggable(TAG, 3)) { Log.i(TAG, "appid is 0, ignore " + item); } } catch (IOException e) { e.printStackTrace(); } } /* access modifiers changed from: private */ public static synchronized void syncUploadAll(Context context) { File[] listFiles; synchronized (Analysis.class) { try { File file = new File(context.getFilesDir(), "open-analysis"); file.mkdirs(); if (file.exists() && file.isDirectory() && (listFiles = file.listFiles()) != null) { int i = 0; while (true) { if (i >= listFiles.length) { break; } else if (i > 10) { break; } else { File file2 = listFiles[i]; Scanner scanner = null; try { scanner = new Scanner(new FileInputStream(file2)).useDelimiter("\\A"); JSONObject jSONObject = new JSONObject(scanner.nextLine()); String str = "" + jSONObject.getInt("app_id"); byte[] encryptGZIP = Utility.encryptGZIP(new JSONObject().put(KEY_RECOGNITION_RESULT, new JSONArray().put(jSONObject)).toString()); if (encryptGZIP.length >= 2) { encryptGZIP[0] = 117; encryptGZIP[1] = 123; } String encryptBASE64 = Utility.encryptBASE64(encryptGZIP); String str2 = "records=" + URLEncoder.encode(encryptBASE64, "utf-8"); byte[] bytes = str2.getBytes("utf-8"); String buildStatUrl = buildStatUrl(context, str); HashMap hashMap = new HashMap(); hashMap.put(AsyncHttpClient.HEADER_CONTENT_TYPE, "application/x-www-form-urlencoded"); String httpRequest = httpRequest(buildStatUrl, hashMap, bytes, true); file2.delete(); if (Log.isLoggable(TAG, 3)) { Log.i(TAG, "POST " + buildStatUrl + "\n\t\trequest data:" + str2 + "\n\t\tresponse data:\n" + httpRequest); } if (scanner == null) { i++; } } catch (Exception e) { e.printStackTrace(); if (Log.isLoggable(TAG, 3)) { Log.i(TAG, "", e); } if (0 == 0) { } } catch (Throwable th) { if (0 != 0) { scanner.close(); } throw th; } scanner.close(); i++; } } } } catch (NullPointerException e2) { e2.printStackTrace(); } } return; } }