package com.cvte.tv.api;

import android.app.Application;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.text.TextUtils;
import android.util.Log;
import android.util.SparseArray;
import android.view.KeyEvent;
import com.cvte.tv.api.aidl.INotifyGlobalKeyListener;
import com.cvte.tv.api.aidl.INotifyListener;
import com.cvte.tv.api.aidl.ITvApiManager;
import com.cvte.tv.api.aidl.ITvNotifyListener;
import com.seewo.libmcuservice.LibMcuApplication;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/* JADX INFO: loaded from: classes.dex */
public final class TvApiSDKManager {
    private static TvApiSDKManager instance;
    private Handler mHandler;
    private HandlerThread mHandlerThread;
    private ITvApiManager tvapi;
    private final String TAG = "TvApiSDKManager";
    private final Object mLockNotify = new Object();
    private final Object mLockGlobal = new Object();
    private final List<TvServiceConnectListener> mConnectedListeners = new ArrayList();
    private final List<TvServiceConnection> mTvServiceConnections = new ArrayList();
    private final SparseArray<ITvNotifyListener> mNotifyArray = new SparseArray<>();
    private final SparseArray<ITvGlobalKeyEventListener> mGlobalKeyArray = new SparseArray<>();
    private INotifyListener notifyListener = new INotifyListener.Stub() { // from class: com.cvte.tv.api.TvApiSDKManager.1
        @Override // com.cvte.tv.api.aidl.INotifyListener
        public void onNotify(String str, Bundle bundle) throws RemoteException {
            synchronized (TvApiSDKManager.this.mLockNotify) {
                if (TvApiSDKManager.this.mNotifyArray.size() > 0) {
                    int size = TvApiSDKManager.this.mNotifyArray.size();
                    for (int i = 0; i < size; i++) {
                        ITvNotifyListener iTvNotifyListener = (ITvNotifyListener) TvApiSDKManager.this.mNotifyArray.valueAt(i);
                        if (iTvNotifyListener != null) {
                            try {
                                iTvNotifyListener.onTvNotify(str, bundle);
                            } catch (Exception e2) {
                                Log.e("TvApiSDKManager", "Exception", e2);
                            }
                        }
                    }
                }
            }
        }
    };
    private INotifyGlobalKeyListener notifyGlobalKeyListener = new INotifyGlobalKeyListener.Stub() { // from class: com.cvte.tv.api.TvApiSDKManager.2
        @Override // com.cvte.tv.api.aidl.INotifyGlobalKeyListener
        public boolean handleKeyEvent(KeyEvent keyEvent) throws RemoteException {
            synchronized (TvApiSDKManager.this.mLockGlobal) {
                if (TvApiSDKManager.this.mGlobalKeyArray.size() > 0) {
                    int size = TvApiSDKManager.this.mGlobalKeyArray.size();
                    for (int i = 0; i < size; i++) {
                        ITvGlobalKeyEventListener iTvGlobalKeyEventListener = (ITvGlobalKeyEventListener) TvApiSDKManager.this.mGlobalKeyArray.valueAt(i);
                        Log.d("Evan", "handleKeyEvent:" + keyEvent.getKeyCode());
                        if (iTvGlobalKeyEventListener.handleGlobalKeyEvent(keyEvent)) {
                            Log.d("Evan", "handleKeyEvent stop:" + keyEvent.getKeyCode());
                            return true;
                        }
                    }
                }
                return false;
            }
        }
    };
    private Runnable getServiceRunnable = new Runnable() { // from class: com.cvte.tv.api.TvApiSDKManager.3
        @Override // java.lang.Runnable
        public void run() {
            TvApiSDKManager.this.tvapi = ITvApiManager.Stub.asInterface(ServiceManager.getService(TvApiManagerService.TVAPI_SERVICE));
            StringBuilder sb = new StringBuilder();
            sb.append("getTvApi TvApiSDKManager_init getSystemService = ");
            sb.append(TvApiSDKManager.this.tvapi != null);
            Log.d("TvApiSDKManager", sb.toString());
            if (TvApiSDKManager.this.tvapi == null) {
                if (TvApiSDKManager.this.mHandler != null) {
                    TvApiSDKManager.this.mHandler.postDelayed(TvApiSDKManager.this.getServiceRunnable, 100L);
                    return;
                }
                return;
            }
            try {
                TvApiSDKManager.this.tvapi.addNotifyListener(TvApiSDKManager.this.notifyListener);
                TvApiSDKManager.this.tvapi.addGlobalKeyListener(TvApiSDKManager.this.notifyGlobalKeyListener);
                Iterator it = TvApiSDKManager.this.mConnectedListeners.iterator();
                while (it.hasNext()) {
                    ((TvServiceConnectListener) it.next()).OnConnected(TvApiSDKManager.this.tvapi);
                }
                TvApiSDKManager.this.mConnectedListeners.clear();
                Iterator it2 = TvApiSDKManager.this.mTvServiceConnections.iterator();
                while (it2.hasNext()) {
                    ((TvServiceConnection) it2.next()).onServiceConnected(TvApiSDKManager.this.tvapi);
                }
                TvApiSDKManager.this.mTvServiceConnections.clear();
            } catch (RemoteException e2) {
                Log.e("TvApiSDKManager", LibMcuApplication.REMOTE_EXCEPTION, e2);
            }
        }
    };

    private TvApiSDKManager() {
        HandlerThread handlerThread = new HandlerThread("TvApiSDKManager_init");
        this.mHandlerThread = handlerThread;
        handlerThread.start();
        Handler handler = new Handler(this.mHandlerThread.getLooper());
        this.mHandler = handler;
        handler.post(this.getServiceRunnable);
    }

    public static void destroy() {
        if (Looper.getMainLooper() != Looper.myLooper()) {
            throw new IllegalThreadStateException("destroy must be called in main thread");
        }
        if (instance == null) {
            throw new IllegalStateException("you have not init the TvApiSDKManager");
        }
        instance = null;
    }

    public static TvApiSDKManager getInstance() {
        if (instance == null) {
            instance = new TvApiSDKManager();
        }
        return instance;
    }

    public static TvApiSDKManager init(Application application) {
        if (Looper.getMainLooper() == Looper.myLooper()) {
            return getInstance();
        }
        throw new IllegalThreadStateException("init must be called in main thread");
    }

    public void addGlobalKeyEventListener(Context context, ITvGlobalKeyEventListener iTvGlobalKeyEventListener) {
        if (context == null) {
            return;
        }
        String str = context.getApplicationInfo().packageName;
        Log.d("Evan", String.format("addGlobalKeyEventListener package:%s, listener:%s", str, iTvGlobalKeyEventListener));
        String globalKeyPackage = "com.cvte.tv.androidsetting";
        try {
            ITvApiManager iTvApiManager = this.tvapi;
            if (iTvApiManager != null) {
                globalKeyPackage = iTvApiManager.getGlobalKeyPackage();
                if (TextUtils.isEmpty(globalKeyPackage)) {
                    globalKeyPackage = "com.cvte.tv.androidsetting";
                }
            }
        } catch (RemoteException e2) {
            e2.printStackTrace();
        }
        if (!globalKeyPackage.equals(str) && !"com.cvte.tv.api.impl".equals(str)) {
            Log.e("Evan", "addGlobalKeyEventListener illegal access!");
            throw new IllegalAccessError("addGlobalKeyEventListener illegal access!");
        }
        if (iTvGlobalKeyEventListener == null) {
            throw new IllegalAccessError("global Key Listenter parameter must be not null!");
        }
        synchronized (this.mLockGlobal) {
            this.mGlobalKeyArray.put(System.identityHashCode(iTvGlobalKeyEventListener), iTvGlobalKeyEventListener);
        }
        Log.d("Evan", "###### mGlobalKeyArray:" + this.mGlobalKeyArray);
    }

    public void addNotifyHandle(ITvNotifyListener iTvNotifyListener) {
        synchronized (this.mLockNotify) {
            this.mNotifyArray.put(System.identityHashCode(iTvNotifyListener), iTvNotifyListener);
        }
    }

    public int getLocalLibraryVersionCode(Context context) {
        return 1490089988;
    }

    public String getLocalLibraryVersionName(Context context) {
        return "1.0.14";
    }

    @Deprecated
    public void getTvApi(TvServiceConnectListener tvServiceConnectListener) {
        if (this.tvapi != null) {
            if (tvServiceConnectListener != null) {
                Log.d("TvApiSDKManager", "getTvApi OnConnected 2");
                tvServiceConnectListener.OnConnected(this.tvapi);
                return;
            }
            return;
        }
        this.tvapi = ITvApiManager.Stub.asInterface(ServiceManager.getService(TvApiManagerService.TVAPI_SERVICE));
        StringBuilder sb = new StringBuilder();
        sb.append("getTvApi 2 getSystemService = ");
        sb.append(this.tvapi != null);
        Log.d("TvApiSDKManager", sb.toString());
        ITvApiManager iTvApiManager = this.tvapi;
        if (iTvApiManager == null) {
            this.mConnectedListeners.add(tvServiceConnectListener);
            return;
        }
        try {
            iTvApiManager.addNotifyListener(this.notifyListener);
            this.tvapi.addGlobalKeyListener(this.notifyGlobalKeyListener);
        } catch (RemoteException e2) {
            Log.e("TvApiSDKManager", LibMcuApplication.REMOTE_EXCEPTION, e2);
        }
        if (tvServiceConnectListener != null) {
            Log.d("TvApiSDKManager", "getTvApi OnConnected 1");
            tvServiceConnectListener.OnConnected(this.tvapi);
        }
    }

    public void removeGlobalKeyEventListener(ITvGlobalKeyEventListener iTvGlobalKeyEventListener) {
        if (iTvGlobalKeyEventListener == null) {
            throw new IllegalAccessError("global Key Listenter parameter must be not null!");
        }
        synchronized (this.mLockGlobal) {
            this.mGlobalKeyArray.remove(System.identityHashCode(iTvGlobalKeyEventListener));
            Log.d("Evan", "removeGlobalKeyEventListener");
        }
    }

    public void removeNotifyHandle(ITvNotifyListener iTvNotifyListener) {
        synchronized (this.mLockNotify) {
            if (iTvNotifyListener != null) {
                this.mNotifyArray.remove(System.identityHashCode(iTvNotifyListener));
            }
        }
    }

    @Deprecated
    public void getTvApi(TvServiceConnection tvServiceConnection) {
        if (this.tvapi != null) {
            if (tvServiceConnection != null) {
                Log.d("TvApiSDKManager", "getTvApi OnConnected 2");
                tvServiceConnection.onServiceConnected(this.tvapi);
                return;
            }
            return;
        }
        this.tvapi = ITvApiManager.Stub.asInterface(ServiceManager.getService(TvApiManagerService.TVAPI_SERVICE));
        StringBuilder sb = new StringBuilder();
        sb.append("getTvApi getSystemService = ");
        sb.append(this.tvapi != null);
        Log.d("TvApiSDKManager", sb.toString());
        if (this.tvapi == null) {
            this.mTvServiceConnections.add(tvServiceConnection);
        } else if (tvServiceConnection != null) {
            Log.d("TvApiSDKManager", "getTvApi OnConnected 1");
            tvServiceConnection.onServiceConnected(this.tvapi);
        }
    }

    public ITvApiManager getTvApi() {
        long jCurrentTimeMillis = System.currentTimeMillis();
        while (this.tvapi == null && System.currentTimeMillis() - jCurrentTimeMillis < 2000) {
            try {
                Thread.sleep(100L);
            } catch (InterruptedException e2) {
                e2.printStackTrace();
            }
        }
        return this.tvapi;
    }
}
