package com.cvte.tv.api;

import android.app.Application;
import android.content.Context;
import android.os.Bundle;
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 java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

/* JADX INFO: loaded from: classes.dex */
public final class TvApiSDKManager {
    private static TvApiSDKManager instance;
    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<WeakReference<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 e) {
                                Log.e("TvApiSDKManager", "Exception", e);
                            }
                        }
                    }
                }
            }
        }
    };
    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 ITvApiManager tvapi = ITvApiManager.Stub.asInterface(ServiceManager.getService(TvApiManagerService.TVAPI_SERVICE));

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

    private TvApiSDKManager() {
        Log.d("TvApiSDKManager", "getTvApi TvApiSDKManager getSystemService = " + (this.tvapi != null));
        ITvApiManager iTvApiManager = this.tvapi;
        if (iTvApiManager != null) {
            try {
                iTvApiManager.addNotifyListener(this.notifyListener);
                this.tvapi.addGlobalKeyListener(this.notifyGlobalKeyListener);
            } catch (RemoteException e) {
                Log.e("TvApiSDKManager", "RemoteException", e);
            }
        }
    }

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

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

    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 void addNotifyHandle(ITvNotifyListener iTvNotifyListener) {
        synchronized (this.mLockNotify) {
            this.mNotifyArray.put(System.identityHashCode(iTvNotifyListener), iTvNotifyListener);
        }
    }

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

    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 e) {
            e.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 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");
        }
    }

    @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));
        Log.d("TvApiSDKManager", "getTvApi 2 getSystemService = " + (this.tvapi != null));
        ITvApiManager iTvApiManager = this.tvapi;
        if (iTvApiManager != null) {
            try {
                iTvApiManager.addNotifyListener(this.notifyListener);
                this.tvapi.addGlobalKeyListener(this.notifyGlobalKeyListener);
            } catch (RemoteException e) {
                Log.e("TvApiSDKManager", "RemoteException", e);
            }
        }
        if (tvServiceConnectListener != null) {
            Log.d("TvApiSDKManager", "getTvApi OnConnected 1");
            tvServiceConnectListener.OnConnected(this.tvapi);
        }
    }

    @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));
        Log.d("TvApiSDKManager", "getTvApi getSystemService = " + (this.tvapi != null));
        if (this.tvapi == null || tvServiceConnection == null) {
            return;
        }
        Log.d("TvApiSDKManager", "getTvApi OnConnected 1");
        tvServiceConnection.onServiceConnected(this.tvapi);
    }

    public ITvApiManager getTvApi() {
        if (this.tvapi == null) {
            this.tvapi = ITvApiManager.Stub.asInterface(ServiceManager.getService(TvApiManagerService.TVAPI_SERVICE));
        }
        return this.tvapi;
    }

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