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 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 appContext: Context? = null private val connection = object : ServiceConnection { override fun onServiceConnected(name: ComponentName, binder: IBinder) { engine = DeviceServiceEngine.Stub.asInterface(binder) Log.i(TAG, "YSDK DeviceServiceEngine connected") 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 } val bound = context.applicationContext.bindService(intent, connection, Context.BIND_AUTO_CREATE) Log.i(TAG, "bindService result=$bound") } 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 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 } } }