settings/agent/earcons/
agent.rs

1// Copyright 2020 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use crate::agent::earcons::bluetooth_handler::BluetoothHandler;
6use crate::agent::earcons::volume_change_handler::VolumeChangeHandler;
7use crate::audio::Request as AudioRequest;
8use fidl_fuchsia_media_sounds::PlayerProxy;
9use futures::channel::mpsc::UnboundedSender;
10use futures::lock::Mutex;
11use settings_common::inspect::event::ExternalEventPublisher;
12use settings_common::service_context::{ExternalServiceProxy, ServiceContext};
13use std::collections::HashSet;
14use std::fmt::Debug;
15use std::rc::Rc;
16
17/// The Earcons Agent is responsible for watching updates to relevant sources that need to play
18/// sounds.
19pub(crate) struct Agent {
20    external_publisher: ExternalEventPublisher,
21    sound_player_connection:
22        Rc<Mutex<Option<ExternalServiceProxy<PlayerProxy, ExternalEventPublisher>>>>,
23    audio_request_tx: Option<UnboundedSender<AudioRequest>>,
24}
25
26/// Params that are common to handlers of the earcons agent.
27#[derive(Clone)]
28pub(super) struct CommonEarconsParams {
29    pub(super) service_context: Rc<ServiceContext>,
30    pub(super) sound_player_added_files: Rc<Mutex<HashSet<&'static str>>>,
31    pub(super) sound_player_connection:
32        Rc<Mutex<Option<ExternalServiceProxy<PlayerProxy, ExternalEventPublisher>>>>,
33}
34
35impl Debug for CommonEarconsParams {
36    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
37        f.debug_struct("CommonEarconsParams")
38            .field("sound_player_added_files", &self.sound_player_added_files)
39            .field("sound_player_connection", &self.sound_player_connection)
40            .finish_non_exhaustive()
41    }
42}
43
44impl Agent {
45    pub(crate) fn new(
46        audio_request_tx: Option<UnboundedSender<AudioRequest>>,
47        external_publisher: ExternalEventPublisher,
48    ) -> Self {
49        Self {
50            external_publisher,
51            sound_player_connection: Rc::new(Mutex::new(None)),
52            audio_request_tx,
53        }
54    }
55
56    pub async fn initialize(self, service_context: Rc<ServiceContext>) {
57        let common_earcons_params = CommonEarconsParams {
58            service_context,
59            sound_player_added_files: Rc::new(Mutex::new(HashSet::new())),
60            sound_player_connection: self.sound_player_connection.clone(),
61        };
62
63        if let Err(e) = VolumeChangeHandler::spawn(
64            self.audio_request_tx.clone(),
65            self.external_publisher.clone(),
66            common_earcons_params.clone(),
67        )
68        .await
69        {
70            // For now, report back as an error to prevent issues on
71            // platforms that don't support the handler's dependencies.
72            // TODO(https://fxbug.dev/42139617): Handle with config
73            log::error!("Could not set up VolumeChangeHandler: {:?}", e);
74        }
75
76        if BluetoothHandler::spawn(
77            self.audio_request_tx,
78            self.external_publisher,
79            common_earcons_params,
80        )
81        .await
82        .is_err()
83        {
84            // For now, report back as an error to prevent issues on
85            // platforms that don't support the handler's dependencies.
86            // TODO(https://fxbug.dev/42139617): Handle with config
87            log::error!("Could not set up BluetoothHandler");
88        }
89    }
90}