package com.benq.platform.utils;

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.util.Log;
import com.benq.platform.utils.TouchPanelUtils;
import com.ifpdos.windowsettings.IServiceHelper;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import kotlin.jvm.internal.g;
import kotlin.jvm.internal.l;

/* JADX INFO: loaded from: classes.dex */
public final class TouchPanelUtils {
    public static final Companion Companion = new Companion(null);
    private static final long DEFAULT_RECONNECT_INTERVAL = 3000;
    private static volatile TouchPanelUtils INSTANCE = null;
    private static final int MAX_RECONNECT_ATTEMPTS = 5;
    private static final double RECONNECT_DELAY_MULTIPLIER = 1.5d;
    private static final String TAG = "TouchPanelUtils";
    private static final String TOUCH_PANEL_PACKAGE = "com.ifpdos.touchpanel";
    private static final String TOUCH_PANEL_SERVICE = "com.ifpdos.touchpanel.bussiness.service.TouchPanelService";
    private final Context context;
    private List<OnServiceConnectionListener> mConnectionCallbacks;
    private boolean mEnableAutoReconnect;
    private final Handler mHandler;
    private boolean mIsConnected;
    private boolean mIsReconnecting;
    private int mMaxReconnectAttempts;
    private int mReconnectAttempts;
    private long mReconnectInterval;
    private final ScheduledExecutorService mScheduledExecutor;
    private final TouchPanelUtils$mServiceConnection$1 mServiceConnection;
    private IServiceHelper mServiceHelper;

    public static final class Companion {
        public /* synthetic */ Companion(g gVar) {
            this();
        }

        public final TouchPanelUtils getInstance(Context context) {
            l.e(context, "context");
            TouchPanelUtils touchPanelUtils = TouchPanelUtils.INSTANCE;
            if (touchPanelUtils == null) {
                synchronized (this) {
                    touchPanelUtils = TouchPanelUtils.INSTANCE;
                    if (touchPanelUtils == null) {
                        Context applicationContext = context.getApplicationContext();
                        l.d(applicationContext, "getApplicationContext(...)");
                        touchPanelUtils = new TouchPanelUtils(applicationContext, null);
                        TouchPanelUtils.INSTANCE = touchPanelUtils;
                    }
                }
            }
            return touchPanelUtils;
        }

        private Companion() {
        }
    }

    public interface OnServiceConnectionListener {
        void onServiceConnected();

        void onServiceDisconnected();
    }

    public /* synthetic */ TouchPanelUtils(Context context, g gVar) {
        this(context);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public final void scheduleReconnect() {
        int i2;
        if (!this.mEnableAutoReconnect || (i2 = this.mReconnectAttempts) >= this.mMaxReconnectAttempts) {
            Log.w(TAG, "Auto reconnect disabled or max attempts reached: attempts=" + this.mReconnectAttempts + ", max=" + this.mMaxReconnectAttempts);
            return;
        }
        this.mIsReconnecting = true;
        this.mReconnectAttempts = i2 + 1;
        long jPow = (long) (this.mReconnectInterval * Math.pow(RECONNECT_DELAY_MULTIPLIER, i2));
        Log.d(TAG, "Scheduling reconnect attempt " + this.mReconnectAttempts + " in " + jPow + "ms");
        this.mHandler.postDelayed(new Runnable() { // from class: com.benq.platform.utils.a
            @Override // java.lang.Runnable
            public final void run() {
                TouchPanelUtils.scheduleReconnect$lambda$0(this.f3147b);
            }
        }, jPow);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static final void scheduleReconnect$lambda$0(TouchPanelUtils this$0) {
        l.e(this$0, "this$0");
        if (!this$0.mIsReconnecting || this$0.mIsConnected) {
            return;
        }
        Log.d(TAG, "Executing reconnect attempt " + this$0.mReconnectAttempts);
        try {
            Intent intent = new Intent();
            intent.setComponent(new ComponentName("com.ifpdos.touchpanel", TOUCH_PANEL_SERVICE));
            if (this$0.context.bindService(intent, this$0.mServiceConnection, 1)) {
                Log.d(TAG, "Reconnect attempt " + this$0.mReconnectAttempts + " successful");
                this$0.mIsReconnecting = false;
            } else {
                Log.e(TAG, "Reconnect attempt " + this$0.mReconnectAttempts + " failed");
                if (this$0.mReconnectAttempts < this$0.mMaxReconnectAttempts) {
                    this$0.scheduleReconnect();
                } else {
                    Log.w(TAG, "Max reconnect attempts reached, stopping auto reconnect");
                    this$0.mIsReconnecting = false;
                }
            }
        } catch (Exception e2) {
            Log.e(TAG, "Error during reconnect attempt " + this$0.mReconnectAttempts + ": " + e2.getMessage());
            if (this$0.mReconnectAttempts < this$0.mMaxReconnectAttempts) {
                this$0.scheduleReconnect();
            } else {
                this$0.mIsReconnecting = false;
            }
        }
    }

    public static /* synthetic */ void setReconnectConfig$default(TouchPanelUtils touchPanelUtils, long j2, int i2, boolean z2, int i3, Object obj) {
        if ((i3 & 4) != 0) {
            z2 = true;
        }
        touchPanelUtils.setReconnectConfig(j2, i2, z2);
    }

    public final void addConnectionListener(OnServiceConnectionListener listener) {
        l.e(listener, "listener");
        if (this.mConnectionCallbacks.contains(listener)) {
            return;
        }
        this.mConnectionCallbacks.add(listener);
    }

    public final void connectService() {
        if (this.mIsConnected) {
            Log.d(TAG, "Service already connected");
            return;
        }
        if (this.mIsReconnecting) {
            Log.d(TAG, "Service is already reconnecting");
            return;
        }
        try {
            Intent intent = new Intent();
            intent.setComponent(new ComponentName("com.ifpdos.touchpanel", TOUCH_PANEL_SERVICE));
            if (this.context.bindService(intent, this.mServiceConnection, 1)) {
                Log.d(TAG, "TouchPanelService binding initiated");
                this.mReconnectAttempts = 0;
            } else {
                Log.e(TAG, "Failed to bind TouchPanelService");
                if (this.mEnableAutoReconnect) {
                    scheduleReconnect();
                }
            }
        } catch (Exception e2) {
            Log.e(TAG, "Error binding TouchPanelService: " + e2.getMessage());
            if (this.mEnableAutoReconnect) {
                scheduleReconnect();
            }
        }
    }

    public final void disconnectService() {
        if (!this.mIsConnected) {
            Log.d(TAG, "Service not connected");
            return;
        }
        stopReconnect();
        try {
            this.context.unbindService(this.mServiceConnection);
            this.mIsConnected = false;
            this.mServiceHelper = null;
            this.mReconnectAttempts = 0;
            Log.d(TAG, "TouchPanelService disconnected");
        } catch (Exception e2) {
            Log.e(TAG, "Error disconnecting TouchPanelService: " + e2.getMessage());
        }
    }

    public final int getBackLight() {
        try {
            IServiceHelper iServiceHelper = this.mServiceHelper;
            if (iServiceHelper != null) {
                return iServiceHelper.getBackLight();
            }
            return -1;
        } catch (RemoteException e2) {
            Log.e(TAG, "Error getting backlight: " + e2.getMessage());
            return -1;
        }
    }

    public final int getEyeOption() {
        try {
            IServiceHelper iServiceHelper = this.mServiceHelper;
            if (iServiceHelper != null) {
                return iServiceHelper.getEyeOption();
            }
            return -1;
        } catch (RemoteException e2) {
            Log.e(TAG, "Error getting eye option: " + e2.getMessage());
            return -1;
        }
    }

    public final int getPictureMode() {
        try {
            IServiceHelper iServiceHelper = this.mServiceHelper;
            if (iServiceHelper != null) {
                return iServiceHelper.getPictureMode();
            }
            return -1;
        } catch (RemoteException e2) {
            Log.e(TAG, "Error getting picture mode: " + e2.getMessage());
            return -1;
        }
    }

    public final int getReconnectAttempts() {
        return this.mReconnectAttempts;
    }

    public final int getSaturation() {
        try {
            IServiceHelper iServiceHelper = this.mServiceHelper;
            if (iServiceHelper != null) {
                return iServiceHelper.getSaturation();
            }
            return -1;
        } catch (RemoteException e2) {
            Log.e(TAG, "Error getting saturation: " + e2.getMessage());
            return -1;
        }
    }

    public final int getSharpness() {
        try {
            IServiceHelper iServiceHelper = this.mServiceHelper;
            if (iServiceHelper != null) {
                return iServiceHelper.getSharpness();
            }
            return -1;
        } catch (RemoteException e2) {
            Log.e(TAG, "Error getting sharpness: " + e2.getMessage());
            return -1;
        }
    }

    public final boolean isAirQualitySensor() {
        try {
            IServiceHelper iServiceHelper = this.mServiceHelper;
            if (iServiceHelper != null) {
                return iServiceHelper.isAirQualitySensor();
            }
            return false;
        } catch (RemoteException e2) {
            Log.e(TAG, "Error checking air quality sensor status: " + e2.getMessage());
            return false;
        }
    }

    public final boolean isAutoLightEnable() {
        try {
            IServiceHelper iServiceHelper = this.mServiceHelper;
            if (iServiceHelper != null) {
                return iServiceHelper.isAutoLightEnable();
            }
            return false;
        } catch (RemoteException e2) {
            Log.e(TAG, "Error checking auto light enable status: " + e2.getMessage());
            return false;
        }
    }

    public final boolean isConnected() {
        return this.mIsConnected && this.mServiceHelper != null;
    }

    public final boolean isEyeEnable() {
        try {
            IServiceHelper iServiceHelper = this.mServiceHelper;
            if (iServiceHelper != null) {
                return iServiceHelper.isEyeEnable();
            }
            return false;
        } catch (RemoteException e2) {
            Log.e(TAG, "Error checking eye enable status: " + e2.getMessage());
            return false;
        }
    }

    public final boolean isInWifiDongle() {
        try {
            IServiceHelper iServiceHelper = this.mServiceHelper;
            if (iServiceHelper != null) {
                return iServiceHelper.isInWifiDongle();
            }
            return false;
        } catch (RemoteException e2) {
            Log.e(TAG, "Error checking WiFi dongle status: " + e2.getMessage());
            return false;
        }
    }

    public final boolean isReconnecting() {
        return this.mIsReconnecting;
    }

    public final boolean isSupportSoundMode() {
        try {
            IServiceHelper iServiceHelper = this.mServiceHelper;
            if (iServiceHelper != null) {
                return iServiceHelper.isSupportSoundMode();
            }
            return false;
        } catch (RemoteException e2) {
            Log.e(TAG, "Error checking sound mode support: " + e2.getMessage());
            return false;
        }
    }

    public final void reconnect() {
        if (this.mIsConnected) {
            Log.d(TAG, "Service already connected, no need to reconnect");
            return;
        }
        Log.d(TAG, "Manual reconnect initiated");
        this.mReconnectAttempts = 0;
        connectService();
    }

    public final void removeConnectionListener(OnServiceConnectionListener listener) {
        l.e(listener, "listener");
        this.mConnectionCallbacks.remove(listener);
    }

    public final boolean reset(int i2) {
        try {
            IServiceHelper iServiceHelper = this.mServiceHelper;
            if (iServiceHelper != null) {
                return iServiceHelper.reset(i2);
            }
            return false;
        } catch (RemoteException e2) {
            Log.e(TAG, "Error resetting settings: " + e2.getMessage());
            return false;
        }
    }

    public final void setAirQualitySensor(boolean z2) {
        try {
            IServiceHelper iServiceHelper = this.mServiceHelper;
            if (iServiceHelper != null) {
                iServiceHelper.setAirQualitySensor(z2);
            }
        } catch (RemoteException e2) {
            Log.e(TAG, "Error setting air quality sensor: " + e2.getMessage());
        }
    }

    public final void setAutoLightStatus(boolean z2) {
        try {
            IServiceHelper iServiceHelper = this.mServiceHelper;
            if (iServiceHelper != null) {
                iServiceHelper.setAutoLightStatus(z2);
            }
        } catch (RemoteException e2) {
            Log.e(TAG, "Error setting auto light status: " + e2.getMessage());
        }
    }

    public final boolean setBackLight(int i2) {
        try {
            IServiceHelper iServiceHelper = this.mServiceHelper;
            if (iServiceHelper != null) {
                return iServiceHelper.setBackLight(i2);
            }
            return false;
        } catch (RemoteException e2) {
            Log.e(TAG, "Error setting backlight: " + e2.getMessage());
            return false;
        }
    }

    public final boolean setBrightness(int i2) {
        try {
            IServiceHelper iServiceHelper = this.mServiceHelper;
            if (iServiceHelper != null) {
                return iServiceHelper.setBrightness(i2);
            }
            return false;
        } catch (RemoteException e2) {
            Log.e(TAG, "Error setting brightness: " + e2.getMessage());
            return false;
        }
    }

    public final boolean setContrast(int i2) {
        try {
            IServiceHelper iServiceHelper = this.mServiceHelper;
            if (iServiceHelper != null) {
                return iServiceHelper.setContrast(i2);
            }
            return false;
        } catch (RemoteException e2) {
            Log.e(TAG, "Error setting contrast: " + e2.getMessage());
            return false;
        }
    }

    public final boolean setEyeOption(int i2) {
        try {
            IServiceHelper iServiceHelper = this.mServiceHelper;
            if (iServiceHelper != null) {
                return iServiceHelper.setEyeOption(i2);
            }
            return false;
        } catch (RemoteException e2) {
            Log.e(TAG, "Error setting eye option: " + e2.getMessage());
            return false;
        }
    }

    public final void setEyeStatus(boolean z2) {
        try {
            IServiceHelper iServiceHelper = this.mServiceHelper;
            if (iServiceHelper != null) {
                iServiceHelper.setEyeStatus(z2);
            }
        } catch (RemoteException e2) {
            Log.e(TAG, "Error setting eye status: " + e2.getMessage());
        }
    }

    public final boolean setHue(int i2) {
        try {
            IServiceHelper iServiceHelper = this.mServiceHelper;
            if (iServiceHelper != null) {
                return iServiceHelper.setHue(i2);
            }
            return false;
        } catch (RemoteException e2) {
            Log.e(TAG, "Error setting hue: " + e2.getMessage());
            return false;
        }
    }

    public final boolean setPictureMode(int i2) {
        try {
            IServiceHelper iServiceHelper = this.mServiceHelper;
            if (iServiceHelper != null) {
                return iServiceHelper.setPictureMode(i2);
            }
            return false;
        } catch (RemoteException e2) {
            Log.e(TAG, "Error setting picture mode: " + e2.getMessage());
            return false;
        }
    }

    public final void setReconnectConfig(long j2, int i2, boolean z2) {
        this.mReconnectInterval = j2;
        this.mMaxReconnectAttempts = i2;
        this.mEnableAutoReconnect = z2;
        Log.d(TAG, "Reconnect config updated: interval=" + j2 + ", maxAttempts=" + i2 + ", autoReconnect=" + z2);
    }

    public final boolean setSaturation(int i2) {
        try {
            IServiceHelper iServiceHelper = this.mServiceHelper;
            if (iServiceHelper != null) {
                return iServiceHelper.setSaturation(i2);
            }
            return false;
        } catch (RemoteException e2) {
            Log.e(TAG, "Error setting saturation: " + e2.getMessage());
            return false;
        }
    }

    public final boolean setSharpness(int i2) {
        try {
            IServiceHelper iServiceHelper = this.mServiceHelper;
            if (iServiceHelper != null) {
                return iServiceHelper.setSharpness(i2);
            }
            return false;
        } catch (RemoteException e2) {
            Log.e(TAG, "Error setting sharpness: " + e2.getMessage());
            return false;
        }
    }

    public final void startDongle(boolean z2) {
        try {
            IServiceHelper iServiceHelper = this.mServiceHelper;
            if (iServiceHelper != null) {
                iServiceHelper.startDongle(z2);
            }
        } catch (RemoteException e2) {
            Log.e(TAG, "Error starting/stopping dongle: " + e2.getMessage());
        }
    }

    public final void stopReconnect() {
        this.mIsReconnecting = false;
        this.mReconnectAttempts = 0;
        this.mHandler.removeCallbacksAndMessages(null);
        this.mScheduledExecutor.shutdown();
        Log.d(TAG, "Reconnect stopped");
    }

    /* JADX WARN: Type inference failed for: r3v6, types: [com.benq.platform.utils.TouchPanelUtils$mServiceConnection$1] */
    private TouchPanelUtils(Context context) {
        this.context = context;
        this.mConnectionCallbacks = new ArrayList();
        this.mReconnectInterval = DEFAULT_RECONNECT_INTERVAL;
        this.mMaxReconnectAttempts = 5;
        this.mEnableAutoReconnect = true;
        this.mHandler = new Handler(Looper.getMainLooper());
        this.mScheduledExecutor = Executors.newSingleThreadScheduledExecutor();
        this.mServiceConnection = new ServiceConnection() { // from class: com.benq.platform.utils.TouchPanelUtils$mServiceConnection$1
            @Override // android.content.ServiceConnection
            public void onServiceConnected(ComponentName name, IBinder service) {
                l.e(name, "name");
                l.e(service, "service");
                Log.d("TouchPanelUtils", "TouchPanelService connected");
                try {
                    this.this$0.mServiceHelper = IServiceHelper.Stub.asInterface(service);
                    this.this$0.mIsConnected = true;
                    Iterator it = this.this$0.mConnectionCallbacks.iterator();
                    while (it.hasNext()) {
                        ((TouchPanelUtils.OnServiceConnectionListener) it.next()).onServiceConnected();
                    }
                    Log.d("TouchPanelUtils", "IServiceHelper interface obtained successfully");
                } catch (Exception e2) {
                    Log.e("TouchPanelUtils", "Error getting IServiceHelper interface: " + e2.getMessage());
                    this.this$0.mIsConnected = false;
                    this.this$0.mServiceHelper = null;
                }
            }

            @Override // android.content.ServiceConnection
            public void onServiceDisconnected(ComponentName name) {
                l.e(name, "name");
                Log.d("TouchPanelUtils", "TouchPanelService disconnected");
                this.this$0.mServiceHelper = null;
                this.this$0.mIsConnected = false;
                Iterator it = this.this$0.mConnectionCallbacks.iterator();
                while (it.hasNext()) {
                    ((TouchPanelUtils.OnServiceConnectionListener) it.next()).onServiceDisconnected();
                }
                if (!this.this$0.mEnableAutoReconnect || this.this$0.mIsReconnecting) {
                    return;
                }
                Log.d("TouchPanelUtils", "Service disconnected, initiating auto reconnect");
                this.this$0.scheduleReconnect();
            }
        };
    }
}
