Enviando Objetos

Objetos AIDL podem ser enviados como um tipo AIDL concreto ou como a interface IBinder com tipo apagado:

birthday_service/aidl/com/example/birthdayservice/IBirthdayInfoProvider.aidl:

package com.example.birthdayservice; interface IBirthdayInfoProvider { String name(); int years(); }

birthday_service/aidl/com/example/birthdayservice/IBirthdayService.aidl:

import com.example.birthdayservice.IBirthdayInfoProvider; interface IBirthdayService { /** A mesma coisa, mas usando um objeto de ligação. */ String wishWithProvider(IBirthdayInfoProvider provider); /** A mesma coisa, mas usando `IBinder`. */ String wishWithErasedProvider(IBinder provider); }

birthday_service/src/client.rs:

/// _Struct_ Rust implementando a interface `IBirthdayInfoProvider`. struct InfoProvider { name: String, age: u8, } impl binder::Interface for InfoProvider {} impl IBirthdayInfoProvider for InfoProvider { fn name(&self) -> binder::Result<String> { Ok(self.name.clone()) } fn years(&self) -> binder::Result<i32> { Ok(self.age as i32) } } fn main() { binder::ProcessState::start_thread_pool(); let service = connect().expect("Falha ao conectar-se a BirthdayService"); // Cria um objeto de ligação para a interface `IBirthdayInfoProvider`. let provider = BnBirthdayInfoProvider::new_binder( InfoProvider { name: name.clone(), age: years as u8 }, BinderFeatures::default(), ); // Envia o objeto de ligação para o serviço. service.wishWithProvider(&provider)?; // Realiza a mesma operação, mas passando o provedor como um `SpIBinder`. service.wishWithErasedProvider(&provider.as_binder())?; }

Speaker Notes

  • Observe o uso de BnBirthdayInfoProvider. Isso serve para o mesmo propósito que BnBirthdayService que vimos anteriormente.