android_manifest/intent_filter.rs
1use super::action::Action;
2use super::category::Category;
3use super::data::Data;
4use super::resources::{MipmapOrDrawableResource, StringResourceOrString};
5use serde::{Deserialize, Serialize};
6
7/// Specifies the types of intents that an activity, service, or broadcast receiver can
8/// respond to.
9///
10/// An intent filter declares the capabilities of its parent component — what an activity
11/// or service can do and what types of broadcasts a receiver can handle. It opens the
12/// component to receiving intents of the advertised type, while filtering out those that
13/// are not meaningful for the component. Most of the contents of the filter are
14/// described by its [`<action>`], [`<category>`], and [`<data>`] subelements.
15///
16/// For a more detailed discussion of filters, see the separate [`Intents and Intent
17/// Filters`] document, as well as the [`Intents Filters`]
18/// section in the introduction.
19///
20/// ## XML Syntax
21/// ```xml
22/// <intent-filter android:icon="drawable resource"
23/// android:label="string resource"
24/// android:priority="integer" >
25/// ...
26/// </intent-filter>
27/// ```
28///
29/// ## Contained in
30/// * [`<activity>`]
31/// * [`<activity-alias>`]
32/// * [`<service>`]
33/// * [`<receiver>`]
34/// * [`<provider>`]
35///
36/// ## Must contain
37/// * [`<action>`]
38///
39/// ## Can contain
40/// * [`<category>`]
41/// * [`<data>`]
42///
43/// ## Introduced in
44/// API Level 1
45///
46/// [`Intents and Intent Filters`]: https://developer.android.com/guide/components/intents-filters
47/// [`Intents Filters`]: https://developer.android.com/guide/topics/manifest/manifest-intro#ifs
48/// [`<activity>`]: crate::Activity
49/// [`<activity-alias>`]: crate::ActivityAlias
50/// [`<service>`]: crate::Service
51/// [`<receiver>`]: crate::Receiver
52/// [`<provider>`]: crate::Provider
53/// [`<action>`]: crate::Action
54/// [`<category>`]: crate::Category
55/// [`<data>`]: crate::Data
56#[derive(
57 Debug, Deserialize, Serialize, YaSerialize, YaDeserialize, PartialEq, Eq, Default, Clone,
58)]
59pub struct IntentFilter {
60 /// An icon that represents the parent activity, service, or broadcast receiver when
61 /// that component is presented to the user as having the capability described by
62 /// the filter.
63 ///
64 /// This attribute must be set as a reference to a drawable resource containing the
65 /// image definition. The default value is the icon set by the parent component's
66 /// icon attribute. If the parent does not specify an icon, the default is the
67 /// icon set by the [`<application>`] element.
68 ///
69 /// For more on intent filter icons, see [`Icons and Labels`] in the introduction.
70 ///
71 /// [`<application>`]: crate::Application
72 /// [`Icons and Labels`]: https://developer.android.com/guide/topics/manifest/manifest-intro#iconlabel
73 #[yaserde(attribute, prefix = "android")]
74 pub icon: Option<MipmapOrDrawableResource>,
75 /// A user-readable label for the parent component. This label, rather than the one
76 /// set by the parent component, is used when the component is presented to the
77 /// user as having the capability described by the filter. The label should be set
78 /// as a reference to a string resource, so that it can be localized like other
79 /// strings in the user interface. However, as a convenience while you're
80 /// developing the application, it can also be set as a raw string.
81 ///
82 /// The default value is the label set by the parent component. If the parent does not
83 /// specify a label, the default is the label set by the [`<application>`]
84 /// element's [`label`] attribute.s
85 ///
86 /// For more on intent filter labels, see [`Icons and Labels`] in the introduction.
87 ///
88 /// [`<application>`]: crate::Application
89 /// [`label`]: crate::Application#structfield.label
90 /// [`Icons and Labels`]: https://developer.android.com/guide/topics/manifest/manifest-intro#iconlabel
91 #[yaserde(attribute, prefix = "android")]
92 pub label: Option<StringResourceOrString>,
93 /// The priority that should be given to the parent component with regard to handling
94 /// intents of the type described by the filter. This attribute has meaning for
95 /// both activities and broadcast receivers:
96 ///
97 /// * It provides information about how able an activity is to respond to an intent
98 /// that matches the filter, relative
99 /// to other activities that could also respond to the intent. When an intent
100 /// could be handled by multiple activities with different priorities, Android
101 /// will consider only those with higher priority values as potential targets for
102 /// the intent.
103 /// * It controls the order in which broadcast receivers are executed to
104 /// receive broadcast messages. Those with higher priority values are called
105 /// before those with lower values. (The order applies only to
106 /// synchronous messages; it's ignored for asynchronous messages.)
107 ///
108 /// Use this attribute only if you really need to impose a specific order in
109 /// which the broadcasts are received, or want to force Android to prefer
110 /// one activity over others. The value must be an integer, such as
111 /// "100". Higher numbers have a higher priority. The default value is 0
112 /// In certain circumstances the requested priority is ignored and the value
113 /// is capped to 0. This occurs when:
114 ///
115 /// * A non-privileged application requests any priority > 0
116 /// * A privileged application requests a priority > 0 for [`ACTION_VIEW`],
117 /// [`ACTION_SEND`], [`ACTION_SENDTO`] or
118 /// [`ACTION_SEND_MULTIPLE`]
119 ///
120 /// Also see [`setPriority()`].
121 ///
122 /// [`ACTION_VIEW`]: https://developer.android.com/reference/android/content/Intent#ACTION_VIEW
123 /// [`ACTION_SEND`]: https://developer.android.com/reference/android/content/Intent#ACTION_SEND
124 /// [`ACTION_SENDTO`]: https://developer.android.com/reference/android/content/Intent#ACTION_SENDTO
125 /// [`ACTION_SEND_MULTIPLE`]: https://developer.android.com/reference/android/content/Intent#ACTION_SEND_MULTIPLE
126 /// [`setPriority()`]: https://developer.android.com/reference/android/content/IntentFilter#setPriority(int)
127 #[yaserde(attribute, prefix = "android")]
128 pub priority: Option<u32>,
129 /// The order in which the filter should be processed when multiple filters match.
130 /// order differs from priority in that priority applies across apps, while order
131 /// disambiguates multiple matching filters in a single app.
132 ///
133 /// When multiple filters could match, use a directed intent instead.
134 ///
135 /// The value must be an integer, such as "100". Higher numbers are matched first. The
136 /// default value is 0.
137 ///
138 /// This attribute was introduced in API Level 28.
139 #[yaserde(attribute, prefix = "android")]
140 pub order: Option<u32>,
141 /// List of `<action>` tags.
142 #[serde(default, skip_serializing_if = "Vec::is_empty")]
143 pub action: Vec<Action>,
144 /// List of `<category>` tags.
145 #[serde(default, skip_serializing_if = "Vec::is_empty")]
146 pub category: Vec<Category>,
147 /// List of `<data>` tags.
148 #[serde(default, skip_serializing_if = "Vec::is_empty")]
149 pub data: Vec<Data>,
150 /// This attribute signals to the system that it should verify whether your app
151 /// belongs to the URL domains used in your intent filters.
152 ///
153 /// See [`verify-android-applinks`] for more information.
154 ///
155 /// [`verify-android-applinks`]: https://developer.android.com/training/app-links/verify-android-applinks
156 #[yaserde(attribute, prefix = "android", rename = "autoVerify")]
157 pub auto_verify: Option<bool>,
158}
159
160#[cfg(test)]
161mod tests {
162 use super::*;
163
164 #[test]
165 fn test_intent_filter_toml_serialize_deserialize() {
166 let value = IntentFilter {
167 order: Some(100),
168 action: vec![Action {
169 name: Some("android.intent.action.MAIN".to_string()),
170 }],
171 category: vec![Category {
172 name: Some("android.intent.category.LAUNCHER".to_string()),
173 }],
174 ..Default::default()
175 };
176 let string = toml::to_string_pretty(&value).unwrap();
177 let _value: IntentFilter = toml::from_str(&string).unwrap();
178 }
179}