1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
use creator_tools::types::*;
use serde::{Deserialize, Serialize};
use std::path::PathBuf;

#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct AndroidMetadata {
    /// Resources directory path relatively to project path.
    #[serde(rename = "res")]
    pub resources: Option<PathBuf>,
    /// Assets directory path relatively to project path.
    pub assets: Option<PathBuf>,
    /// Build targets.
    pub build_targets: Option<Vec<AndroidTarget>>,
    /// Android manifest.
    pub manifest: AndroidManifestConfig,
}

#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct AndroidManifestConfig {
    pub apk_label: Option<String>,
    pub target_sdk_version: Option<u32>,
    pub min_sdk_version: Option<u32>,
    pub icon: Option<String>,
    pub fullscreen: Option<bool>,
    pub orientation: Option<String>,
    pub opengles_version: Option<(u8, u8)>,
    pub feature: Option<Vec<FeatureConfig>>,
    pub permission: Option<Vec<PermissionConfig>>,
    pub intent_filter: Option<Vec<IntentFilterConfig>>,
    pub application_metadatas: Option<Vec<ApplicationMetadataConfig>>,
    pub activity_metadatas: Option<Vec<ActivityMetadataConfig>>,
}

impl AndroidManifestConfig {
    pub fn into_android_manifest(
        self,
        target: &Target,
        profile: Profile,
        package_name: String,
        package_version: String,
        target_sdk_version: u32,
    ) -> AndroidManifest {
        let pkg_name = match target {
            Target::Lib => format!("rust.{}", package_name.replace("-", "_")),
            Target::Example(_) => format!("rust.example.{}", package_name.replace("-", "_")),
            _ => panic!(),
        };
        let package_label = self
            .apk_label
            .as_deref()
            .unwrap_or(&package_name)
            .to_string();
        let version_code = VersionCode::from_semver(&package_version)
            .unwrap()
            .to_code(1);
        let version_name = package_version;
        let min_sdk_version = self.min_sdk_version.unwrap_or(23);
        let opengles_version = self.opengles_version.unwrap_or((3, 1));
        let features = self
            .feature
            .clone()
            .unwrap_or_default()
            .into_iter()
            .map(Into::into)
            .collect();
        let permissions = self
            .permission
            .clone()
            .unwrap_or_default()
            .into_iter()
            .map(Into::into)
            .collect();
        let intent_filters = self
            .intent_filter
            .clone()
            .unwrap_or_default()
            .into_iter()
            .map(Into::into)
            .collect();
        let application_metadatas = self
            .application_metadatas
            .clone()
            .unwrap_or_default()
            .into_iter()
            .map(Into::into)
            .collect();
        let activity_metadatas = self
            .activity_metadatas
            .clone()
            .unwrap_or_default()
            .into_iter()
            .map(Into::into)
            .collect();
        AndroidManifest {
            package_name: pkg_name,
            package_label,
            version_name,
            version_code,
            split: None,
            target_name: package_name.replace("-", "_"),
            debuggable: profile == Profile::Debug,
            target_sdk_version,
            min_sdk_version,
            opengles_version,
            features,
            permissions,
            intent_filters,
            icon: self.icon.clone(),
            fullscreen: self.fullscreen.unwrap_or(false),
            orientation: self.orientation,
            application_metadatas,
            activity_metadatas,
        }
    }
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct FeatureConfig {
    name: String,
    required: Option<bool>,
}

impl From<FeatureConfig> for Feature {
    fn from(config: FeatureConfig) -> Self {
        Self {
            name: config.name,
            required: config.required.unwrap_or(true),
        }
    }
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct PermissionConfig {
    name: String,
    max_sdk_version: Option<u32>,
}

impl From<PermissionConfig> for Permission {
    fn from(config: PermissionConfig) -> Self {
        Self {
            name: config.name,
            max_sdk_version: config.max_sdk_version,
        }
    }
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct IntentFilterConfigData {
    pub scheme: Option<String>,
    pub host: Option<String>,
    pub prefix: Option<String>,
}

impl From<IntentFilterConfigData> for IntentFilterData {
    fn from(config: IntentFilterConfigData) -> Self {
        Self {
            scheme: config.scheme,
            host: config.host,
            prefix: config.prefix,
        }
    }
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct IntentFilterConfig {
    name: String,
    data: Vec<IntentFilterConfigData>,
    categories: Vec<String>,
}

impl From<IntentFilterConfig> for IntentFilter {
    fn from(config: IntentFilterConfig) -> Self {
        Self {
            name: config.name,
            data: config
                .data
                .into_iter()
                .map(IntentFilterData::from)
                .rev()
                .collect(),
            categories: config.categories,
        }
    }
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ApplicationMetadataConfig {
    name: String,
    value: String,
}

impl From<ApplicationMetadataConfig> for ApplicationMetadata {
    fn from(config: ApplicationMetadataConfig) -> Self {
        Self {
            name: config.name,
            value: config.value,
        }
    }
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ActivityMetadataConfig {
    name: String,
    value: String,
}

impl From<ActivityMetadataConfig> for ActivityMetadata {
    fn from(config: ActivityMetadataConfig) -> Self {
        Self {
            name: config.name,
            value: config.value,
        }
    }
}