settings/do_not_disturb/
do_not_disturb_fidl_handler.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::base::{SettingInfo, SettingType};
6use crate::do_not_disturb::types::DoNotDisturbInfo;
7use crate::handler::base::Request;
8use crate::ingress::{request, watch, Scoped};
9use crate::job::source::{Error as JobError, ErrorResponder};
10use crate::job::Job;
11use fidl::endpoints::{ControlHandle, Responder};
12use fidl_fuchsia_settings::{
13    DoNotDisturbRequest, DoNotDisturbSetResponder, DoNotDisturbSetResult, DoNotDisturbSettings,
14    DoNotDisturbWatchResponder,
15};
16
17impl From<SettingInfo> for DoNotDisturbSettings {
18    fn from(response: SettingInfo) -> Self {
19        if let SettingInfo::DoNotDisturb(info) = response {
20            fidl_fuchsia_settings::DoNotDisturbSettings {
21                user_initiated_do_not_disturb: info.user_dnd,
22                night_mode_initiated_do_not_disturb: info.night_mode_dnd,
23                ..Default::default()
24            }
25        } else {
26            panic!("incorrect value sent to do_not_disturb");
27        }
28    }
29}
30
31impl ErrorResponder for DoNotDisturbSetResponder {
32    fn id(&self) -> &'static str {
33        "DoNotDisturb_Set"
34    }
35
36    fn respond(self: Box<Self>, error: fidl_fuchsia_settings::Error) -> Result<(), fidl::Error> {
37        self.send(Err(error))
38    }
39}
40
41impl request::Responder<Scoped<DoNotDisturbSetResult>> for DoNotDisturbSetResponder {
42    fn respond(self, Scoped(response): Scoped<DoNotDisturbSetResult>) {
43        let _ = self.send(response);
44    }
45}
46
47impl watch::Responder<DoNotDisturbSettings, zx::Status> for DoNotDisturbWatchResponder {
48    fn respond(self, response: Result<DoNotDisturbSettings, zx::Status>) {
49        match response {
50            Ok(settings) => {
51                let _ = self.send(&settings);
52            }
53            Err(error) => {
54                self.control_handle().shutdown_with_epitaph(error);
55            }
56        }
57    }
58}
59
60fn to_request(settings: DoNotDisturbSettings) -> Request {
61    let dnd_info = DoNotDisturbInfo {
62        user_dnd: settings.user_initiated_do_not_disturb,
63        night_mode_dnd: settings.night_mode_initiated_do_not_disturb,
64    };
65    Request::SetDnD(dnd_info)
66}
67
68impl TryFrom<DoNotDisturbRequest> for Job {
69    type Error = JobError;
70    fn try_from(req: DoNotDisturbRequest) -> Result<Self, Self::Error> {
71        // Support future expansion of FIDL
72        #[allow(unreachable_patterns)]
73        match req {
74            DoNotDisturbRequest::Set { settings, responder } => {
75                Ok(request::Work::new(SettingType::DoNotDisturb, to_request(settings), responder)
76                    .into())
77            }
78            DoNotDisturbRequest::Watch { responder } => {
79                Ok(watch::Work::new_job(SettingType::DoNotDisturb, responder))
80            }
81            _ => {
82                log::warn!("Received a call to an unsupported API: {:?}", req);
83                Err(JobError::Unsupported)
84            }
85        }
86    }
87}