智能工厂.apk(点击下载) / VoiceRecognitionService.java


package com.baidu.speech;

import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.RemoteException;
import android.speech.RecognitionService;
import android.util.AndroidRuntimeException;
import com.baidu.speech.asr.SpeechConstant;
import com.fasterxml.jackson.core.util.MinimalPrettyPrinter;
import com.nordnetab.chcp.main.js.PluginResultHelper;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public final class VoiceRecognitionService extends RecognitionService {
    public static final int EVENT_ENGINE_SWITCH = 12;
    private static final int EVENT_ERROR = 11;
    private static final int EVENT_THIRD_DATA = 12;
    public static final String TAG = "VoiceRecognitionService";
    public static final String VERSION_NAME = "3.4.1.107";
    private static final Logger logger = Logger.getLogger(TAG);
    private boolean internal;
    private EventManager mEventManagerAsr;
    private Bundle mFinalBundle;
    private boolean mLongSpeech;
    private MyListener mUsingListener;

    class MyListener implements EventListener {
        RecognitionService.Callback mListener;

        MyListener() {
        }

        private final void callbackOnEvent(RecognitionService.Callback callback, int i, Bundle bundle) {
            try {
                Field declaredField = callback.getClass().getDeclaredField("mListener");
                declaredField.setAccessible(true);
                Class.forName("android.speech.IRecognitionListener").getMethod("onEvent", Integer.TYPE, Bundle.class).invoke(declaredField.get(callback), Integer.valueOf(i), bundle);
            } catch (Exception e) {
                e.printStackTrace();
                VoiceRecognitionService.logger.log(Level.WARNING, "", (Throwable) e);
            }
        }

        @Override // com.baidu.speech.EventListener
        public void onEvent(String str, String str2, byte[] bArr, int i, int i2) {
            VoiceRecognitionService voiceRecognitionService;
            RecognitionService.Callback callback = this.mListener;
            if (callback != null) {
                try {
                    if (SpeechConstant.CALLBACK_EVENT_ASR_READY.equals(str)) {
                        callback.readyForSpeech(new Bundle());
                    } else if (SpeechConstant.CALLBACK_EVENT_ASR_BEGIN.equals(str)) {
                        callback.beginningOfSpeech();
                    } else if (SpeechConstant.CALLBACK_EVENT_ASR_AUDIO.equals(str)) {
                        callback.bufferReceived(bArr);
                    } else if (SpeechConstant.CALLBACK_EVENT_ASR_VOLUME.equals(str)) {
                        callback.rmsChanged((float) new JSONObject(str2).optDouble("volume"));
                    } else if (SpeechConstant.CALLBACK_EVENT_ASR_END.equals(str)) {
                        callback.endOfSpeech();
                    } else if (SpeechConstant.CALLBACK_EVENT_ASR_PARTIAL.equals(str)) {
                        JSONObject jSONObject = new JSONObject(str2);
                        String optString = jSONObject.optString("result_type");
                        Bundle fromJson = VoiceRecognitionService.fromJson(jSONObject);
                        if (optString != null && optString != "") {
                            if (optString.equals("partial_result")) {
                                callback.partialResults(fromJson);
                            } else if (optString.equals("final_result")) {
                                VoiceRecognitionService.this.mFinalBundle = fromJson;
                            } else if (optString.equals("third_result")) {
                                Bundle bundle = new Bundle();
                                bundle.putByteArray("third_data", bArr);
                                callbackOnEvent(callback, 12, bundle);
                            }
                        }
                    } else {
                        if (SpeechConstant.CALLBACK_EVENT_ASR_FINISH.equals(str)) {
                            JSONObject jSONObject2 = new JSONObject(str2);
                            int i3 = jSONObject2.getInt(PluginResultHelper.JsParams.General.ERROR);
                            if (i3 != 0) {
                                callback.error(i3);
                                Bundle bundle2 = new Bundle();
                                bundle2.putInt(PluginResultHelper.JsParams.General.ERROR, jSONObject2.getInt("sub_error"));
                                bundle2.putString("reason", jSONObject2.getString("desc"));
                                callbackOnEvent(callback, 11, bundle2);
                                return;
                            } else if (!VoiceRecognitionService.this.mLongSpeech) {
                                callback.results(VoiceRecognitionService.this.mFinalBundle);
                                voiceRecognitionService = VoiceRecognitionService.this;
                            } else {
                                return;
                            }
                        } else if (SpeechConstant.CALLBACK_EVENT_ASR_LONG_SPEECH.equals(str)) {
                            callback.results(VoiceRecognitionService.this.mFinalBundle);
                            voiceRecognitionService = VoiceRecognitionService.this;
                        } else {
                            return;
                        }
                        voiceRecognitionService.mFinalBundle = null;
                    }
                } catch (RemoteException e) {
                    e.printStackTrace();
                } catch (JSONException e2) {
                    e2.printStackTrace();
                }
            }
        }

        public void setCallbackListener(RecognitionService.Callback callback) {
            this.mListener = callback;
        }
    }

    private JSONObject convertIntentToJson(Intent intent) {
        HashMap hashMap = new HashMap();
        intent.getStringExtra("a");
        Bundle extras = intent.getExtras();
        for (String str : extras.keySet()) {
            Object obj = extras.get(str);
            if (!str.equals("args") || !(obj instanceof String)) {
                hashMap.put(str, obj);
            } else {
                String[] split = ((String) obj).split("--");
                for (String str2 : split) {
                    int indexOf = str2.trim().indexOf(MinimalPrettyPrinter.DEFAULT_ROOT_VALUE_SEPARATOR);
                    if (indexOf < 0) {
                        indexOf = str2.indexOf("\t");
                    }
                    if (indexOf < 0) {
                        indexOf = str2.indexOf("=");
                    }
                    if (indexOf > 0) {
                        hashMap.put(str2.substring(0, indexOf).trim(), str2.substring(indexOf + 1).trim());
                    }
                }
            }
        }
        return new JSONObject(hashMap);
    }

    public static Bundle fromJson(JSONObject jSONObject) {
        Bundle bundle = new Bundle();
        Iterator<String> keys = jSONObject.keys();
        while (keys.hasNext()) {
            String next = keys.next();
            JSONArray optJSONArray = jSONObject.optJSONArray(next);
            String optString = jSONObject.optString(next);
            int i = 0;
            if (optJSONArray != null && optJSONArray.length() <= 0) {
                bundle.putStringArray(next, new String[0]);
            } else if (optJSONArray != null && optJSONArray.optString(0) != null) {
                ArrayList<String> arrayList = new ArrayList<>();
                while (i < optJSONArray.length()) {
                    arrayList.add(optJSONArray.optString(i));
                    i++;
                }
                bundle.putStringArrayList(next, arrayList);
            } else if (optJSONArray != null && !Double.isNaN(optJSONArray.optDouble(0))) {
                double[] dArr = new double[optJSONArray.length()];
                while (i < optJSONArray.length()) {
                    dArr[i] = optJSONArray.optDouble(i);
                    i++;
                }
                bundle.putDoubleArray(next, dArr);
            } else if (optString != null) {
                bundle.putString(next, optString);
            }
        }
        return bundle;
    }

    public static String getSdkVersion() {
        return VERSION_NAME;
    }

    /* access modifiers changed from: protected */
    public void onCancel(RecognitionService.Callback callback) {
        this.mEventManagerAsr.send("asr.cancel", "{}", null, 0, 0);
    }

    public void onCreate() {
        super.onCreate();
        synchronized (VoiceRecognitionService.class) {
            if (this.mEventManagerAsr == null) {
                this.mEventManagerAsr = EventManagerFactory.create(getApplicationContext(), "asr");
                this.mUsingListener = new MyListener();
                this.mEventManagerAsr.registerListener(this.mUsingListener);
                SpeechConstant.PUBLIC_DECODER = false;
            }
        }
        logger.info(String.format("onCreate(), hashcode=%s", Integer.valueOf(hashCode())));
        try {
            Class.forName("com.baidu.android.voicedemo.SettingMore");
            this.internal = true;
        } catch (Exception unused) {
        }
        Logger logger2 = logger;
        logger2.info("internal=" + this.internal);
        try {
            if (getPackageManager().getServiceInfo(new ComponentName(getPackageName(), getClass().getName()), 128).exported) {
                throw new AndroidRuntimeException(getClass().getName() + ", 'android:exported' should be false, please modify AndroidManifest.xml");
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }

    public void onDestroy() {
        this.mEventManagerAsr.send(SpeechConstant.ASR_KWS_UNLOAD_ENGINE, "{}", null, 0, 0);
        super.onDestroy();
    }

    /* access modifiers changed from: protected */
    public void onStartListening(Intent intent, RecognitionService.Callback callback) {
        if (!intent.hasExtra(SpeechConstant.AUDIO_MILLS)) {
            intent.putExtra(SpeechConstant.AUDIO_MILLS, System.currentTimeMillis());
        }
        this.mLongSpeech = intent.getIntExtra(SpeechConstant.VAD_ENDPOINT_TIMEOUT, -1) == 0;
        JSONObject convertIntentToJson = convertIntentToJson(intent);
        try {
            this.mUsingListener.setCallbackListener(callback);
            if (intent.getIntExtra(SpeechConstant.DECODER, 0) != 0) {
                this.mEventManagerAsr.send(SpeechConstant.ASR_KWS_LOAD_ENGINE, convertIntentToJson.toString(4), null, 0, 0);
            }
            this.mEventManagerAsr.send(SpeechConstant.ASR_START, convertIntentToJson.toString(4), null, 0, 0);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    /* access modifiers changed from: protected */
    public void onStopListening(RecognitionService.Callback callback) {
        this.mEventManagerAsr.send(SpeechConstant.ASR_STOP, "{}", null, 0, 0);
    }
}