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


package com.baidu.speech.aidl;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import com.baidu.speech.EventListener;
import com.baidu.speech.aidl.EventManager;
import com.baidu.speech.aidl.EventManagerFactory;
import com.baidu.speech.asr.SpeechConstant;
import java.util.ArrayList;
import java.util.Iterator;
import org.altbeacon.beacon.service.RangedBeacon;
import org.json.JSONException;
import org.json.JSONObject;

public class EventRecognitionService extends Service {
    private static final String TAG = "EventRecognitionService";
    private int RESTART_TIME = 3600000;
    private boolean asrUsing = false;
    private IBinder iBinder = new EventManagerFactory.Stub() {
        /* class com.baidu.speech.aidl.EventRecognitionService.AnonymousClass1 */

        @Override // com.baidu.speech.aidl.EventManagerFactory
        public EventManager create(final String str) throws RemoteException {
            AnonymousClass1 r0 = new EventManager.Stub() {
                /* class com.baidu.speech.aidl.EventRecognitionService.AnonymousClass1.AnonymousClass1 */
                com.baidu.speech.EventManager impl = com.baidu.speech.EventManagerFactory.create(EventRecognitionService.this.getApplicationContext(), str, false);
                public ArrayList<EventListenerProxy> usingLis = new ArrayList<>();

                @Override // com.baidu.speech.aidl.EventManager
                public void registerListener(EventListener eventListener) throws RemoteException {
                    EventListenerProxy eventListenerProxy = new EventListenerProxy(eventListener);
                    unregisterListener(eventListener);
                    this.usingLis.add(eventListenerProxy);
                    this.impl.registerListener(eventListenerProxy);
                }

                @Override // com.baidu.speech.aidl.EventManager
                public void send(String str, String str2, byte[] bArr, int i, int i2) throws RemoteException {
                    if (SpeechConstant.ASR_START.equals(str) || SpeechConstant.ASR_KWS_LOAD_ENGINE.equals(str)) {
                        EventRecognitionService.this.asrUsing = true;
                    } else if (SpeechConstant.WAKEUP_START.equals(str)) {
                        EventRecognitionService.this.wpUsing = true;
                    }
                    this.impl.send(str, str2, bArr, i, i2);
                }

                @Override // com.baidu.speech.aidl.EventManager
                public void unregisterListener(EventListener eventListener) throws RemoteException {
                    Iterator<EventListenerProxy> it = this.usingLis.iterator();
                    while (it.hasNext()) {
                        this.impl.unregisterListener(it.next());
                    }
                    this.usingLis.clear();
                }
            };
            EventRecognitionService.this.startTime = System.currentTimeMillis();
            return r0;
        }
    };
    private long startTime = 0;
    private boolean wpUsing = false;

    /* access modifiers changed from: package-private */
    public class EventListenerProxy implements EventListener {
        private EventListener listener;

        public EventListenerProxy(EventListener eventListener) {
            this.listener = eventListener;
        }

        @Override // com.baidu.speech.EventListener
        public void onEvent(String str, String str2, byte[] bArr, int i, int i2) {
            try {
                long currentTimeMillis = System.currentTimeMillis() - EventRecognitionService.this.startTime;
                if (bArr == null) {
                    bArr = new byte[0];
                }
                if (SpeechConstant.CALLBACK_EVENT_ASR_EXIT.equals(str)) {
                    EventRecognitionService.this.asrUsing = false;
                } else if (SpeechConstant.CALLBACK_EVENT_WAKEUP_STOPED.equals(str)) {
                    EventRecognitionService.this.wpUsing = false;
                }
                if (EventRecognitionService.this.asrUsing || EventRecognitionService.this.wpUsing) {
                    this.listener.onEvent(str, str2, bArr, i, i2);
                    return;
                }
                boolean z = currentTimeMillis > ((long) EventRecognitionService.this.RESTART_TIME);
                try {
                    JSONObject jSONObject = new JSONObject(str2 == null ? "{}" : str2);
                    jSONObject.put("_free", z);
                    str2 = jSONObject.toString();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                this.listener.onEvent(str, str2, bArr, i, i2);
                if (z) {
                    Log.d(EventRecognitionService.TAG, "wake up idle, exit!");
                    System.exit(0);
                }
            } catch (RemoteException e2) {
                e2.printStackTrace();
            }
        }
    }

    public EventRecognitionService() {
        new Handler().postDelayed(new Runnable() {
            /* class com.baidu.speech.aidl.EventRecognitionService.AnonymousClass2 */

            public void run() {
            }
        }, RangedBeacon.DEFAULT_MAX_TRACKING_AGE);
    }

    public IBinder onBind(Intent intent) {
        return this.iBinder;
    }
}