Kotlin 到 Java 迁移

Kotlin to Java migration

我无法翻译(从 Kotlin 到 Java)或理解在以下文件的内部 class 中找到的这条语句 service.connectionListener = { addConnection(it) }

class MainActivity : AppCompatActivity() {
    private val serviceConnection = CallServiceConnection()
    private var connection: TComConnection? = null
    private val tcomManager: TComManager by lazy { TComManager(applicationContext) }
    private val requiredPermissions =
        arrayOf(
            android.Manifest.permission.READ_PHONE_STATE,
            android.Manifest.permission.READ_CALL_LOG
        )

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        drop_btn.setOnClickListener { closeConnection() }

        answer_btn.setOnClickListener {
            val conn = connection
            if (conn != null) {
                conn.setActive()
            } else {
                Toast.makeText(applicationContext, "there is no call", Toast.LENGTH_SHORT).show()
            }
        }

        incoming_btn.setOnClickListener {
            if (!hasPermissions()) {
                requestPermissions(requiredPermissions, 1)
                Toast.makeText(applicationContext, "don't have permissions", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }

            try {
                if (tcomManager.registerAccount()) {
                    tcomManager.addIncomingCall()
                } else {
                    Toast.makeText(applicationContext, "account isn't registered", Toast.LENGTH_SHORT).show()
                }
            } catch (e: Exception) {
                Toast.makeText(applicationContext, e.message, Toast.LENGTH_SHORT).show()
            }
        }

        outgoing_btn.setOnClickListener {
            if (!hasPermissions()) {
                requestPermissions(requiredPermissions, 1)
                Toast.makeText(applicationContext, "don't have permissions", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }

            try {
                if (tcomManager.registerAccount()) {
                    tcomManager.addOutgoingCall()
                } else {
                    Toast.makeText(applicationContext, "account isn't registered", Toast.LENGTH_SHORT).show()
                }
            } catch (e: Exception) {
                Toast.makeText(applicationContext, e.message, Toast.LENGTH_SHORT).show()
            }
        }
    }

    override fun onStart() {
        super.onStart()

        Intent(this, CallService::class.java).also { intent ->
            bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE)
        }
    }


    override fun onStop() {
        super.onStop()
        closeConnection()
        unbindService(serviceConnection)
    }


    private fun hasPermissions(): Boolean {
        return requiredPermissions.all { checkSelfPermission(it) == PackageManager.PERMISSION_GRANTED }
    }


    private fun closeConnection() {
        connection?.let {
            if (!it.isClosed()) {
                it.setDisconnected(DisconnectCause(DisconnectCause.CANCELED))
            }
            it.listener = {}
        }

        val stateText = "state: no call"
        state_label.text = stateText
    }

    private fun addConnection(newConnection: TComConnection) {
        newConnection.listener = {
            val stateText = "state: ${Connection.stateToString(it)}"
            state_label.text = stateText
        }
        connection = newConnection
        val stateText = "state: ${Connection.stateToString(newConnection.state)}"
        state_label.text = stateText
    }


    inner class CallServiceConnection : ServiceConnection {
        private var callService: CallService? = null
        override fun onServiceConnected(name: ComponentName?, binder: IBinder?) {
            val callSrvBinder = binder as CallService.CallServiceBinder
            val service = callSrvBinder.getCallService()
            service.connectionListener = { addConnection(it) }
            callService = service
        }

        override fun onServiceDisconnected(name: ComponentName?) {
            callService?.connectionListener = {}
        }
    }
}

我无法翻译上面的语句(已注释掉):

public class CallServiceConnection implements ServiceConnection {
        private CallService callService = null;

        @Override
        public void onServiceConnected(ComponentName name, IBinder binder) {
            CallService.CallServiceBinder callSrvBinder = (CallService.CallServiceBinder)binder;
            CallService service = callSrvBinder.getCallService();
            //service.addConnection(); // <--------
            callService = service;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            callService.addConnection(null);
        }
    }

Kotlin 中的这段代码:

service.connectionListener = { addConnection(it) }

对应Java中的这段代码:

service.setConnectionListener(connection -> {
    addConnection(connection);
    return Unit.INSTANCE;
});

假设Service class是用Kotlin写的,看起来像这样:

class Service {
    var connectionListener: (Connection) -> Unit = {}
}

更新:

如果您也想重写 Service class,您的代码将是:

service.connectionListener = connection -> {
    addConnection(connection);
    return null;
};

Service class 在 Java:

class Service {
    Function<Connection, Void> connectionListener = connection -> null;
}