override fun onMessageReceived()

in RuntimePermissionsWear/Application/src/main/java/com/example/android/wearable/runtimepermissions/MainPhoneActivity.kt [237:284]


    override fun onMessageReceived(messageEvent: MessageEvent) {
        Log.d(TAG, "onMessageReceived(): $messageEvent")
        when (messageEvent.path) {
            Constants.MESSAGE_PATH_PHONE -> {
                val dataMap = DataMap.fromByteArray(messageEvent.data)
                when (dataMap.getInt(Constants.KEY_COMM_TYPE, 0)) {
                    Constants.COMM_TYPE_RESPONSE_PERMISSION_REQUIRED -> {
                        wearBodySensorsPermissionApproved = false
                        updateWearButtonOnUiThread()

                        // Because our request for remote data requires a remote permission, we now launch
                        // a splash activity informing the user we need those permissions (along with
                        // other helpful information to approve).
                        requestPermissionOnWearLauncher.launch(Unit)
                    }
                    Constants.COMM_TYPE_RESPONSE_USER_APPROVED_PERMISSION -> {
                        wearBodySensorsPermissionApproved = true
                        updateWearButtonOnUiThread()
                        logToUi(getString(R.string.user_approved_remote_permission))
                        lifecycleScope.launch {
                            sendMessage(
                                DataMap().apply {
                                    putInt(
                                        Constants.KEY_COMM_TYPE,
                                        Constants.COMM_TYPE_REQUEST_DATA
                                    )
                                }
                            )
                        }
                    }
                    Constants.COMM_TYPE_RESPONSE_USER_DENIED_PERMISSION -> {
                        wearBodySensorsPermissionApproved = false
                        updateWearButtonOnUiThread()
                        logToUi(getString(R.string.user_denied_remote_permission))
                    }
                    Constants.COMM_TYPE_RESPONSE_DATA -> {
                        wearBodySensorsPermissionApproved = true
                        val sensorSummary = dataMap.getString(Constants.KEY_PAYLOAD)!!
                        updateWearButtonOnUiThread()
                        logToUi(sensorSummary)
                    }
                    else -> {
                        Log.d(TAG, "Unrecognized communication type received.")
                    }
                }
            }
        }
    }