android_tools/adb/
adb_shell_dpm.rs

1use crate::error::*;
2use std::process::Command;
3
4#[derive(Clone, Default)]
5pub struct AdbShellDpm {
6    name: Option<String>,
7    user_id: Option<String>,
8    set_active_admin: bool,
9    set_profile_owner: bool,
10    set_device_owner: bool,
11    remove_active_admin: bool,
12    clear_freeze_period_record: bool,
13    force_network_logs: bool,
14    force_security_logs: bool,
15}
16
17impl AdbShellDpm {
18    pub fn new() -> Self {
19        Self {
20            ..Default::default()
21        }
22    }
23
24    pub fn name(&mut self, name: String) -> &mut Self {
25        self.name = Some(name);
26        self
27    }
28
29    pub fn user_id(&mut self, user_id: String) -> &mut Self {
30        self.user_id = Some(user_id);
31        self
32    }
33
34    /// Sets component as active admin.
35    ///
36    /// Options are:
37    /// * `--user` user_id: Specify the target user. You can also pass `--user
38    /// current` to select the current user.
39    pub fn set_active_admin(&mut self, set_active_admin: bool) -> &mut Self {
40        self.set_active_admin = set_active_admin;
41        self
42    }
43
44    /// Sets component as active admin and its package as profile owner for
45    /// an existing user.
46    ///
47    /// Options are:
48    /// * `--user user_id`: Specify the target user. You can also pass --user
49    /// current to select the current user.
50    /// * `--name name`: Specify the human-readable organization name.
51    pub fn set_profile_owner(&mut self, set_profile_owner: bool) -> &mut Self {
52        self.set_profile_owner = set_profile_owner;
53        self
54    }
55
56    /// Sets component as active admin and its package as device owner.
57    ///
58    /// Options are:
59    /// * `--user user_id`: Specify the target user. You can also pass --user
60    /// current to select the current user.
61    /// * `--name name`: Specify the human-readable organization name.
62    pub fn set_device_owner(&mut self, set_device_owner: bool) -> &mut Self {
63        self.set_device_owner = set_device_owner;
64        self
65    }
66
67    /// Disables an active admin. The app must declare `android:testOnly` in the
68    /// manifest. This command also removes device and profile owners.
69    ///
70    /// Options are:
71    /// * `--user user_id`: Specify the target user. You can also pass --user
72    /// current to select the current user.
73    pub fn remove_active_admin(&mut self, remove_active_admin: bool) -> &mut Self {
74        self.remove_active_admin = remove_active_admin;
75        self
76    }
77
78    /// Clears the device's record of previously-set freeze periods for system OTA
79    /// updates. This is useful to avoid the device's scheduling restrictions when
80    /// developing apps that manage freeze-periods. See [`Manage system updates`].
81    ///
82    /// Supported on devices running Android 9.0 (API level 28) and higher.
83    pub fn clear_freeze_period_record(&mut self, clear_freeze_period_record: bool) -> &mut Self {
84        self.clear_freeze_period_record = clear_freeze_period_record;
85        self
86    }
87
88    /// Forces the system to make any existing network logs ready for retrieval by
89    /// a DPC. If there are connection or DNS logs available, the DPC receives the
90    /// onNetworkLogsAvailable() callback. See Network activity logging.
91    ///
92    /// This command is rate-limited. Supported on devices running Android 9.0
93    /// (API level 28) and higher.
94    pub fn force_network_logs(&mut self, force_network_logs: bool) -> &mut Self {
95        self.force_network_logs = force_network_logs;
96        self
97    }
98
99    /// Forces the system to make any existing security logs available to the DPC.
100    /// If there are logs available, the DPC receives the onSecurityLogsAvailable()
101    /// callback. See Log enterprise device activity.
102    ///
103    /// This command is rate-limited. Supported on devices running Android 9.0
104    /// (API level 28) and higher.
105    pub fn force_security_logs(&mut self, force_security_logs: bool) -> &mut Self {
106        self.force_security_logs = force_security_logs;
107        self
108    }
109
110    pub fn run(&self) -> Result<()> {
111        let mut dpm = Command::new("adb");
112        dpm.arg("shell");
113        dpm.arg("dpm");
114        if let Some(name) = &self.name {
115            dpm.arg("-name").arg(name);
116        }
117        if let Some(user_id) = &self.user_id {
118            dpm.arg("-user").arg(user_id);
119        }
120        if self.set_active_admin {
121            dpm.arg("set-active-admin");
122        }
123        if self.set_profile_owner {
124            dpm.arg("set-profile-owner");
125        }
126        if self.set_device_owner {
127            dpm.arg("set-device-owner");
128        }
129        if self.remove_active_admin {
130            dpm.arg("remove-active-admin");
131        }
132        if self.clear_freeze_period_record {
133            dpm.arg("clear-freeze-period-record");
134        }
135        if self.force_network_logs {
136            dpm.arg("force-network-logs");
137        }
138        if self.force_security_logs {
139            dpm.arg("force-security-logs");
140        }
141        dpm.output_err(true)?;
142        Ok(())
143    }
144}