package com.shukria.kiosklauncher.util import android.content.ComponentName import android.content.Context import android.content.Intent import android.content.ServiceConnection import android.os.IBinder import android.os.RemoteException import android.util.Log import com.morefun.yapi.engine.DeviceServiceEngine import com.morefun.yapi.engine.OnUninstallAppListener import com.morefun.yapi.engine.DeviceInfoConstrants object YSDKManager { private const val TAG = "YSDKManager" private const val SERVICE_ACTION = "com.morefun.ysdk.service" private const val SERVICE_PACKAGE = "com.morefun.ysdk" @Volatile private var engine: DeviceServiceEngine? = null private var isServiceMissing = false private var appContext: Context? = null private val onReadyListeners = mutableListOf<() -> Unit>() private val connection = object : ServiceConnection { override fun onServiceConnected(name: ComponentName, binder: IBinder) { engine = DeviceServiceEngine.Stub.asInterface(binder) Log.i(TAG, "YSDK DeviceServiceEngine connected") // Notify waiting listeners triggerReadyListeners() binder.linkToDeath({ Log.w(TAG, "YSDK service died — reconnecting") engine = null appContext?.let { bind(it) } }, 0) } override fun onServiceDisconnected(name: ComponentName) { Log.w(TAG, "YSDK service disconnected — reconnecting") engine = null appContext?.let { bind(it) } } } fun bind(context: Context) { appContext = context.applicationContext val intent = Intent(SERVICE_ACTION).apply { `package` = SERVICE_PACKAGE } try { val bound = context.applicationContext.bindService(intent, connection, Context.BIND_AUTO_CREATE) Log.i(TAG, "bindService result=$bound") if (!bound) { Log.w(TAG, "YSDK service not found on this device (Standard Android detected)") isServiceMissing = true triggerReadyListeners() } } catch (e: Exception) { Log.e(TAG, "Failed to bind YSDK service: ${e.message}") isServiceMissing = true triggerReadyListeners() } } private fun triggerReadyListeners() { synchronized(onReadyListeners) { onReadyListeners.forEach { it() } onReadyListeners.clear() } } fun installApp(apkPath: String, packageName: String): Boolean { val svc = engine ?: run { Log.w(TAG, "installApp: YSDK not connected") return false } return try { svc.installApp(apkPath, "", packageName) Log.i(TAG, "installApp called: pkg=$packageName path=$apkPath") true } catch (e: RemoteException) { Log.e(TAG, "installApp RemoteException: ${e.message}") false } } fun uninstallApp(packageName: String): Boolean { val svc = engine ?: run { Log.w(TAG, "uninstallApp: YSDK not connected") return false } return try { svc.uninstallApp(packageName, object : OnUninstallAppListener.Stub() { override fun onUninstallAppResult(code: Int) { Log.i(TAG, "uninstallApp result: pkg=$packageName code=$code") } }) Log.i(TAG, "uninstallApp called: pkg=$packageName") true } catch (e: RemoteException) { Log.e(TAG, "uninstallApp RemoteException: ${e.message}") false } } fun isConnected() = engine != null || isServiceMissing fun runWhenReady(action: () -> Unit) { if (isConnected()) { action() } else { synchronized(onReadyListeners) { onReadyListeners.add(action) } } } fun getDevInfo(): android.os.Bundle? { val svc = engine ?: run { Log.w(TAG, "getDevInfo: YSDK not connected") return null } return try { svc.devInfo } catch (e: Exception) { Log.e(TAG, "getDevInfo Exception: ${e.message}") null } } fun setWifiEnabled(enabled: Boolean): Boolean { val svc = engine ?: run { Log.w(TAG, "setWifiEnabled: YSDK not connected") return false } // Try Int first try { val bundleInt = android.os.Bundle().apply { putInt(DeviceInfoConstrants.SETTING_WIFI_SWITCH, if (enabled) 1 else 0) } val retInt = svc.setProperties(bundleInt) Log.i(TAG, "setWifiEnabled($enabled) setProperties (Int) returned $retInt") if (retInt == 0) return true } catch (e: Exception) { Log.e(TAG, "setWifiEnabled(Int) failed: ${e.message}") } // Try String fallback try { val bundleStr = android.os.Bundle().apply { putString(DeviceInfoConstrants.SETTING_WIFI_SWITCH, if (enabled) "1" else "0") } val retStr = svc.setProperties(bundleStr) Log.i(TAG, "setWifiEnabled($enabled) setProperties (String) returned $retStr") if (retStr == 0) return true } catch (e: Exception) { Log.e(TAG, "setWifiEnabled(String) failed: ${e.message}") } return false } }