package com.duoyou.api.oaid.impl; import android.app.Application; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.IBinder; import com.duoyou.api.oaid.IGetter; import com.duoyou.api.oaid.OAIDException; import com.duoyou.api.oaid.OAIDLog; public class OAIDService implements ServiceConnection { private final RemoteCaller caller; private final Context context; private final IGetter getter; @FunctionalInterface public interface RemoteCaller { String callRemoteInterface(IBinder iBinder); } private OAIDService(Context context2, IGetter iGetter, RemoteCaller remoteCaller) { this.context = !(context2 instanceof Application) ? context2.getApplicationContext() : context2; this.getter = iGetter; this.caller = remoteCaller; } public static void bind(Context context2, Intent intent, IGetter iGetter, RemoteCaller remoteCaller) { new OAIDService(context2, iGetter, remoteCaller).bind(intent); } private void bind(Intent intent) { try { if (this.context.bindService(intent, this, 1)) { OAIDLog.print("Service has been bound: " + intent); return; } throw new OAIDException("Service binding failed"); } catch (Exception e) { this.getter.onOAIDGetError(e); } } public void onServiceConnected(ComponentName componentName, IBinder iBinder) { OAIDLog.print("Service has been connected: " + componentName.getClassName()); try { String callRemoteInterface = this.caller.callRemoteInterface(iBinder); if (callRemoteInterface == null || callRemoteInterface.length() == 0) { throw new OAIDException("OAID/AAID acquire failed"); } OAIDLog.print("OAID/AAID acquire success: " + callRemoteInterface); this.getter.onOAIDGetComplete(callRemoteInterface); try { this.context.unbindService(this); OAIDLog.print("Service has been unbound: " + componentName.getClassName()); } catch (Exception e) { OAIDLog.print(e); } } catch (Exception e2) { OAIDLog.print(e2); this.getter.onOAIDGetError(e2); this.context.unbindService(this); OAIDLog.print("Service has been unbound: " + componentName.getClassName()); } catch (Throwable th) { try { this.context.unbindService(this); OAIDLog.print("Service has been unbound: " + componentName.getClassName()); } catch (Exception e3) { OAIDLog.print(e3); } throw th; } } public void onServiceDisconnected(ComponentName componentName) { OAIDLog.print("Service has been disconnected: " + componentName.getClassName()); } }