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


package com.baidu.speech;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.RemoteException;
import android.text.TextUtils;
import com.baidu.speech.aidl.EventListener;
import com.baidu.speech.aidl.EventManager;
import com.baidu.speech.aidl.EventManagerFactory;
import com.baidu.speech.aidl.EventRecognitionService;
import com.baidu.speech.asr.EventManagerAsr;
import com.baidu.speech.asr.EventManagerSlot;
import com.baidu.speech.asr.EventManagerWp;
import com.baidu.speech.asr.SpeechConstant;
import com.baidu.speech.audio.MicrophoneServer;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.json.JSONException;
import org.json.JSONObject;

public class EventManagerFactory {
    private static final String TAG = "EventManagerFactory";
    private static boolean asrUsing = false;
    private static boolean kwsLoaded = false;
    private static boolean wpUsing = false;

    /* access modifiers changed from: package-private */
    public static class EventManagerRemote2Local implements EventManager {
        final ServiceConnection conn = new ServiceConnection() {
            /* class com.baidu.speech.EventManagerFactory.EventManagerRemote2Local.AnonymousClass1 */

            public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
                com.baidu.speech.aidl.EventManagerFactory asInterface = EventManagerFactory.Stub.asInterface(iBinder);
                try {
                    if (EventManagerRemote2Local.this.remoteEM == null) {
                        EventManagerRemote2Local.this.setRemoteEM(asInterface.create(EventManagerRemote2Local.this.name));
                    }
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }

            public void onServiceDisconnected(ComponentName componentName) {
                if (EventManagerFactory.kwsLoaded && EventManagerRemote2Local.this.mLis != null) {
                    EventManagerRemote2Local.this.mLis.onEvent(SpeechConstant.CALLBACK_EVENT_ASR_UNLOADED, null, null, 0, 0);
                }
                if (EventManagerFactory.asrUsing && EventManagerRemote2Local.this.mLis != null) {
                    EventManagerRemote2Local.this.mLis.onEvent(SpeechConstant.CALLBACK_EVENT_ASR_EXIT, null, null, 0, 0);
                }
                if (EventManagerFactory.wpUsing && EventManagerRemote2Local.this.mLis != null) {
                    EventManagerRemote2Local.this.mLis.onEvent(SpeechConstant.CALLBACK_EVENT_WAKEUP_STOPED, null, null, 0, 0);
                }
                EventManagerRemote2Local.this.remoteEM = null;
            }
        };
        private Context context;
        ExecutorService executor = Executors.newCachedThreadPool();
        private EventListener mLis;
        private String name;
        private EventManager remoteEM;

        EventManagerRemote2Local(Context context2, String str) {
            this.context = context2;
            this.name = str;
        }

        @Override // com.baidu.speech.EventManager
        public void registerListener(EventListener eventListener) {
            this.mLis = eventListener;
        }

        @Override // com.baidu.speech.EventManager
        public void send(final String str, final String str2, byte[] bArr, final int i, final int i2) {
            Context context2 = this.context;
            context2.bindService(new Intent(context2, EventRecognitionService.class), this.conn, 1);
            final byte[] bArr2 = bArr == null ? new byte[0] : bArr;
            if (SpeechConstant.ASR_START.equals(str) || SpeechConstant.ASR_KWS_LOAD_ENGINE.equals(str)) {
                boolean unused = EventManagerFactory.asrUsing = true;
            } else if (SpeechConstant.WAKEUP_START.equals(str)) {
                boolean unused2 = EventManagerFactory.wpUsing = true;
            } else if (SpeechConstant.ASR_KWS_LOAD_ENGINE.equals(str)) {
                boolean unused3 = EventManagerFactory.kwsLoaded = true;
            }
            new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
                /* class com.baidu.speech.EventManagerFactory.EventManagerRemote2Local.AnonymousClass2 */

                public void run() {
                    JSONObject jSONObject;
                    if (EventManagerRemote2Local.this.remoteEM == null) {
                        new Handler(Looper.getMainLooper()).postDelayed(this, 10);
                        return;
                    }
                    String str = str2;
                    if (SpeechConstant.ASR_START.equals(str) || SpeechConstant.WAKEUP_START.equals(str)) {
                        try {
                            jSONObject = new JSONObject(str2);
                        } catch (Exception unused) {
                            jSONObject = new JSONObject();
                        }
                        try {
                            String optString = jSONObject.optString(SpeechConstant.IN_FILE);
                            if (!jSONObject.has("audio.socketport") && !TextUtils.isEmpty(optString)) {
                                int i = 1;
                                if (jSONObject.has(SpeechConstant.AUDIO_SOURCE)) {
                                    i = jSONObject.optInt(SpeechConstant.AUDIO_SOURCE);
                                }
                                jSONObject.put("audio.socketport", MicrophoneServer.create(optString, i));
                                str = jSONObject.toString();
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                    try {
                        EventManagerRemote2Local.this.remoteEM.registerListener(new EventListener.Stub() {
                            /* class com.baidu.speech.EventManagerFactory.EventManagerRemote2Local.AnonymousClass2.AnonymousClass1 */

                            /* JADX WARNING: Removed duplicated region for block: B:26:0x004f  */
                            /* JADX WARNING: Removed duplicated region for block: B:27:0x0069  */
                            @Override // com.baidu.speech.aidl.EventListener
                            public void onEvent(final String str, final String str2, final byte[] bArr, final int i, final int i2) throws RemoteException {
                                JSONObject jSONObject;
                                JSONException e;
                                boolean z = false;
                                if (SpeechConstant.CALLBACK_EVENT_ASR_EXIT.equals(str)) {
                                    boolean unused = EventManagerFactory.asrUsing = false;
                                } else if (SpeechConstant.CALLBACK_EVENT_WAKEUP_STOPED.equals(str)) {
                                    boolean unused2 = EventManagerFactory.wpUsing = false;
                                } else if (SpeechConstant.CALLBACK_EVENT_ASR_UNLOADED.equals(str)) {
                                    boolean unused3 = EventManagerFactory.kwsLoaded = false;
                                }
                                if (SpeechConstant.CALLBACK_EVENT_WAKEUP_STOPED.equals(str)) {
                                    try {
                                        if (str2 == null) {
                                            str2 = "{}";
                                        }
                                        jSONObject = new JSONObject(str2);
                                        try {
                                            z = jSONObject.optBoolean("_free");
                                            jSONObject.remove("_free");
                                        } catch (JSONException e2) {
                                            e = e2;
                                        }
                                    } catch (JSONException e3) {
                                        e = e3;
                                        jSONObject = null;
                                        e.printStackTrace();
                                        final String jSONObject2 = jSONObject.toString();
                                        if (!z) {
                                        }
                                    }
                                    final String jSONObject22 = jSONObject.toString();
                                    if (!z) {
                                        new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
                                            /* class com.baidu.speech.EventManagerFactory.EventManagerRemote2Local.AnonymousClass2.AnonymousClass1.AnonymousClass1 */

                                            public void run() {
                                                EventManagerRemote2Local.this.mLis.onEvent(str, jSONObject22, bArr, i, i2);
                                            }
                                        }, 200);
                                    } else {
                                        new Handler(Looper.getMainLooper()).post(new Runnable() {
                                            /* class com.baidu.speech.EventManagerFactory.EventManagerRemote2Local.AnonymousClass2.AnonymousClass1.AnonymousClass2 */

                                            public void run() {
                                                EventManagerRemote2Local.this.mLis.onEvent(str, jSONObject22, bArr, i, i2);
                                            }
                                        });
                                    }
                                } else {
                                    new Handler(Looper.getMainLooper()).post(new Runnable() {
                                        /* class com.baidu.speech.EventManagerFactory.EventManagerRemote2Local.AnonymousClass2.AnonymousClass1.AnonymousClass3 */

                                        public void run() {
                                            EventManagerRemote2Local.this.mLis.onEvent(str, str2, bArr, i, i2);
                                        }
                                    });
                                }
                            }
                        });
                        EventManagerRemote2Local.this.remoteEM.send(str, str, bArr2, i, i2);
                    } catch (RemoteException e2) {
                        e2.printStackTrace();
                        EventManagerRemote2Local.this.remoteEM = null;
                    }
                }
            }, 0);
        }

        public void setRemoteEM(EventManager eventManager) {
            this.remoteEM = eventManager;
        }

        @Override // com.baidu.speech.EventManager
        public void unregisterListener(EventListener eventListener) {
            this.mLis = null;
        }
    }

    public static final EventManager create(Context context, String str) {
        return create(context, str, false);
    }

    public static final EventManager create(Context context, String str, boolean z) {
        if (!(context == null || str == null || str.equals(""))) {
            Context applicationContext = context.getApplicationContext();
            if (z) {
                return new EventManagerRemote2Local(applicationContext, str);
            }
            if (str.equals("asr")) {
                return new EventManagerAsr(applicationContext);
            }
            if (str.equals("wp")) {
                return new EventManagerWp(applicationContext);
            }
            if (str.equals("slot")) {
                return new EventManagerSlot(applicationContext);
            }
        }
        return null;
    }
}