settings/agent/earcons/
agent.rs1use crate::agent::earcons::bluetooth_handler::BluetoothHandler;
6use crate::agent::earcons::volume_change_handler::VolumeChangeHandler;
7use crate::agent::{
8 AgentError, Context as AgentContext, Invocation, InvocationResult, Lifespan, Payload,
9};
10use crate::event::Publisher;
11use crate::service;
12use crate::service_context::{ExternalServiceProxy, ServiceContext};
13use fidl_fuchsia_media_sounds::PlayerProxy;
14use fuchsia_async as fasync;
15use futures::lock::Mutex;
16use std::collections::HashSet;
17use std::fmt::Debug;
18use std::rc::Rc;
19
20pub(crate) struct Agent {
23 publisher: Publisher,
24 sound_player_connection: Rc<Mutex<Option<ExternalServiceProxy<PlayerProxy>>>>,
25 messenger: service::message::Messenger,
26}
27
28#[derive(Clone)]
30pub(super) struct CommonEarconsParams {
31 pub(super) service_context: Rc<ServiceContext>,
32 pub(super) sound_player_added_files: Rc<Mutex<HashSet<&'static str>>>,
33 pub(super) sound_player_connection: Rc<Mutex<Option<ExternalServiceProxy<PlayerProxy>>>>,
34}
35
36impl Debug for CommonEarconsParams {
37 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
38 f.debug_struct("CommonEarconsParams")
39 .field("sound_player_added_files", &self.sound_player_added_files)
40 .field("sound_player_connection", &self.sound_player_connection)
41 .finish_non_exhaustive()
42 }
43}
44
45impl Agent {
46 pub(crate) async fn create(mut context: AgentContext) {
47 let mut agent = Agent {
48 publisher: context.get_publisher(),
49 sound_player_connection: Rc::new(Mutex::new(None)),
50 messenger: context.create_messenger().await.expect("messenger should be created"),
51 };
52
53 fasync::Task::local(async move {
54 let _ = &context;
55 while let Ok((Payload::Invocation(invocation), client)) =
56 context.receptor.next_of::<Payload>().await
57 {
58 let _ = client.reply(Payload::Complete(agent.handle(invocation).await).into());
59 }
60
61 log::info!("Earcons agent done processing requests");
62 })
63 .detach();
64 }
65
66 async fn handle(&mut self, invocation: Invocation) -> InvocationResult {
67 if Lifespan::Initialization != invocation.lifespan {
69 return Err(AgentError::UnhandledLifespan);
70 }
71
72 let common_earcons_params = CommonEarconsParams {
73 service_context: invocation.service_context,
74 sound_player_added_files: Rc::new(Mutex::new(HashSet::new())),
75 sound_player_connection: self.sound_player_connection.clone(),
76 };
77
78 if let Err(e) = VolumeChangeHandler::create(
79 self.publisher.clone(),
80 common_earcons_params.clone(),
81 self.messenger.clone(),
82 )
83 .await
84 {
85 log::error!("Could not set up VolumeChangeHandler: {:?}", e);
89 }
90
91 if BluetoothHandler::create(
92 self.publisher.clone(),
93 common_earcons_params.clone(),
94 self.messenger.clone(),
95 )
96 .await
97 .is_err()
98 {
99 log::error!("Could not set up BluetoothHandler");
103 }
104
105 Ok(())
106 }
107}