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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
//! Bindings for `AConfiguration`.
//!
//! See also the [NDK docs](https://developer.android.com/ndk/reference/group/configuration) for
//! `AConfiguration`, as well as the [docs for providing
//! resources](https://developer.android.com/guide/topics/resources/providing-resources.html),
//! which explain many of the configuration values.

use num_enum::{IntoPrimitive, TryFromPrimitive};
use std::convert::TryInto;
use std::ptr::NonNull;

/// A native `AConfiguration *`.
///
/// This stores information about configuration.
pub struct Configuration {
    ptr: NonNull<ffi::AConfiguration>,
}

unsafe impl Send for Configuration {}
unsafe impl Sync for Configuration {}

impl Drop for Configuration {
    fn drop(&mut self) {
        unsafe { ffi::AConfiguration_delete(self.ptr.as_ptr()) }
    }
}

impl Clone for Configuration {
    fn clone(&self) -> Self {
        let mut new = Self::new();
        new.copy(self);
        new
    }
}

impl PartialEq for Configuration {
    fn eq(&self, other: &Self) -> bool {
        self.diff(other).0 == 0
    }
}
impl Eq for Configuration {}

impl Configuration {
    /// Construct a `Configuration` from a pointer.
    ///
    /// By calling this function, you assert that it is a valid pointer to a native
    /// `AConfiguration`, and give ownership of it to the `Configuration` instance.
    pub unsafe fn from_ptr(ptr: NonNull<ffi::AConfiguration>) -> Self {
        Self { ptr }
    }

    /// Create a new `Configuration`, with the same contents as the `AConfiguration` referenced by
    /// the pointer.
    ///
    /// This is useful if you have a pointer, but not ownership of it.
    pub unsafe fn clone_from_ptr(ptr: NonNull<ffi::AConfiguration>) -> Self {
        let conf = Self::new();
        ffi::AConfiguration_copy(conf.ptr.as_ptr(), ptr.as_ptr());
        conf
    }

    /// The pointer to the native `AConfiguration`.  Keep in mind that the `Configuration` object
    /// still has ownership, and will free it when dropped.
    pub fn ptr(&self) -> NonNull<ffi::AConfiguration> {
        self.ptr
    }

    /// Create a new `Configuration`, with none of the values set.
    pub fn new() -> Self {
        unsafe {
            Self {
                ptr: NonNull::new(ffi::AConfiguration_new()).unwrap(),
            }
        }
    }

    /// `dest.copy(&src)` copies the contents of `src` to `dest`
    pub fn copy(&mut self, other: &Self) {
        unsafe { ffi::AConfiguration_copy(self.ptr.as_ptr(), other.ptr.as_ptr()) }
    }

    /// Information about what fields differ between the two configurations
    pub fn diff(&self, other: &Self) -> DiffResult {
        unsafe {
            DiffResult(ffi::AConfiguration_diff(self.ptr.as_ptr(), other.ptr.as_ptr()) as u32)
        }
    }

    /// Returns false if anything in `self` conflicts with `requested`
    pub fn matches(&self, requested: &Self) -> bool {
        unsafe { ffi::AConfiguration_match(self.ptr.as_ptr(), requested.ptr.as_ptr()) != 0 }
    }

    /// Returns the country code. It will always be two letters.
    pub fn country(&self) -> String {
        let mut result = "  ".to_owned();
        unsafe {
            ffi::AConfiguration_getCountry(self.ptr.as_ptr(), result.as_mut_ptr() as *mut _);
        }
        result
    }

    /// Returns the screen density class.
    pub fn density(&self) -> Density {
        unsafe {
            (ffi::AConfiguration_getDensity(self.ptr.as_ptr()) as u32)
                .try_into()
                .unwrap()
        }
    }

    /// Returns the keyboard type.
    pub fn keyboard(&self) -> Keyboard {
        unsafe {
            (ffi::AConfiguration_getKeyboard(self.ptr.as_ptr()) as u32)
                .try_into()
                .unwrap()
        }
    }

    /// Returns the language, as a `String` of two characters, if a language is set
    pub fn language(&self) -> Option<String> {
        let mut chars = [0u8; 2];
        unsafe {
            ffi::AConfiguration_getLanguage(self.ptr.as_ptr(), chars[..].as_mut_ptr() as *mut _);
        }
        if chars[0] == 0 {
            None
        } else {
            Some(std::str::from_utf8(&chars[..]).unwrap().to_owned())
        }
    }

    /// Returns the layout direction
    pub fn layout_direction(&self) -> LayoutDir {
        unsafe {
            (ffi::AConfiguration_getLayoutDirection(self.ptr.as_ptr()) as u32)
                .try_into()
                .unwrap()
        }
    }

    /// Returns the mobile country code.
    pub fn mcc(&self) -> i32 {
        unsafe { ffi::AConfiguration_getMcc(self.ptr.as_ptr()) }
    }

    /// Returns the mobile network code, if one is defined
    pub fn mnc(&self) -> Option<i32> {
        unsafe {
            match ffi::AConfiguration_getMnc(self.ptr.as_ptr()) {
                0 => None,
                x if x == ffi::ACONFIGURATION_MNC_ZERO as i32 => Some(0),
                x => Some(x),
            }
        }
    }

    pub fn nav_hidden(&self) -> NavHidden {
        unsafe {
            (ffi::AConfiguration_getNavHidden(self.ptr.as_ptr()) as u32)
                .try_into()
                .unwrap()
        }
    }

    pub fn navigation(&self) -> Navigation {
        unsafe {
            (ffi::AConfiguration_getNavigation(self.ptr.as_ptr()) as u32)
                .try_into()
                .unwrap()
        }
    }

    pub fn orientation(&self) -> Orientation {
        unsafe {
            (ffi::AConfiguration_getOrientation(self.ptr.as_ptr()) as u32)
                .try_into()
                .unwrap()
        }
    }

    pub fn screen_height_dp(&self) -> Option<i32> {
        unsafe {
            let height = ffi::AConfiguration_getScreenHeightDp(self.ptr.as_ptr());
            if height == ffi::ACONFIGURATION_SCREEN_HEIGHT_DP_ANY as i32 {
                None
            } else {
                Some(height)
            }
        }
    }

    pub fn screen_width_dp(&self) -> Option<i32> {
        unsafe {
            let width = ffi::AConfiguration_getScreenWidthDp(self.ptr.as_ptr());
            if width == ffi::ACONFIGURATION_SCREEN_WIDTH_DP_ANY as i32 {
                None
            } else {
                Some(width)
            }
        }
    }

    pub fn screen_long(&self) -> ScreenLong {
        unsafe {
            (ffi::AConfiguration_getScreenLong(self.ptr.as_ptr()) as u32)
                .try_into()
                .unwrap()
        }
    }

    pub fn screen_round(&self) -> ScreenRound {
        unsafe {
            (ffi::AConfiguration_getScreenRound(self.ptr.as_ptr()) as u32)
                .try_into()
                .unwrap()
        }
    }

    pub fn screen_size(&self) -> ScreenSize {
        unsafe {
            (ffi::AConfiguration_getScreenSize(self.ptr.as_ptr()) as u32)
                .try_into()
                .unwrap()
        }
    }

    pub fn sdk_version(&self) -> i32 {
        unsafe { ffi::AConfiguration_getSdkVersion(self.ptr.as_ptr()) }
    }

    pub fn smallest_screen_width_dp(&self) -> Option<i32> {
        unsafe {
            let width = ffi::AConfiguration_getSmallestScreenWidthDp(self.ptr.as_ptr());
            if width == ffi::ACONFIGURATION_SMALLEST_SCREEN_WIDTH_DP_ANY as i32 {
                None
            } else {
                Some(width)
            }
        }
    }

    pub fn touchscreen(&self) -> Touchscreen {
        unsafe {
            (ffi::AConfiguration_getTouchscreen(self.ptr.as_ptr()) as u32)
                .try_into()
                .unwrap()
        }
    }

    pub fn ui_mode_night(&self) -> UiModeNight {
        unsafe {
            (ffi::AConfiguration_getUiModeNight(self.ptr.as_ptr()) as u32)
                .try_into()
                .unwrap()
        }
    }

    pub fn ui_mode_type(&self) -> UiModeType {
        unsafe {
            (ffi::AConfiguration_getUiModeType(self.ptr.as_ptr()) as u32)
                .try_into()
                .unwrap()
        }
    }
}

/// A bitfield representing the differences between two `Configuration`s
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct DiffResult(pub u32);

impl DiffResult {
    pub fn mcc(self) -> bool {
        self.0 & ffi::ACONFIGURATION_MCC != 0
    }
    pub fn mnc(self) -> bool {
        self.0 & ffi::ACONFIGURATION_MNC != 0
    }
    pub fn locale(self) -> bool {
        self.0 & ffi::ACONFIGURATION_LOCALE != 0
    }
    pub fn touchscreen(self) -> bool {
        self.0 & ffi::ACONFIGURATION_TOUCHSCREEN != 0
    }
    pub fn keyboard(self) -> bool {
        self.0 & ffi::ACONFIGURATION_KEYBOARD != 0
    }
    pub fn keyboard_hidden(self) -> bool {
        self.0 & ffi::ACONFIGURATION_KEYBOARD_HIDDEN != 0
    }
    pub fn navigation(self) -> bool {
        self.0 & ffi::ACONFIGURATION_NAVIGATION != 0
    }
    pub fn orientation(self) -> bool {
        self.0 & ffi::ACONFIGURATION_ORIENTATION != 0
    }
    pub fn density(self) -> bool {
        self.0 & ffi::ACONFIGURATION_DENSITY != 0
    }
    pub fn screen_size(self) -> bool {
        self.0 & ffi::ACONFIGURATION_SCREEN_SIZE != 0
    }
    pub fn version(self) -> bool {
        self.0 & ffi::ACONFIGURATION_VERSION != 0
    }
    pub fn screen_layout(self) -> bool {
        self.0 & ffi::ACONFIGURATION_SCREEN_LAYOUT != 0
    }
    pub fn ui_mode(self) -> bool {
        self.0 & ffi::ACONFIGURATION_UI_MODE != 0
    }
    pub fn smallest_screen_size(self) -> bool {
        self.0 & ffi::ACONFIGURATION_SMALLEST_SCREEN_SIZE != 0
    }
    pub fn layout_dir(self) -> bool {
        self.0 & ffi::ACONFIGURATION_LAYOUTDIR != 0
    }
    pub fn screen_round(self) -> bool {
        self.0 & ffi::ACONFIGURATION_SCREEN_ROUND != 0
    }
    pub fn color_mode(self) -> bool {
        self.0 & ffi::ACONFIGURATION_COLOR_MODE != 0
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, TryFromPrimitive, IntoPrimitive)]
#[repr(u32)]
pub enum Orientation {
    Any = ffi::ACONFIGURATION_ORIENTATION_ANY,
    Port = ffi::ACONFIGURATION_ORIENTATION_PORT,
    Land = ffi::ACONFIGURATION_ORIENTATION_LAND,
    Square = ffi::ACONFIGURATION_ORIENTATION_SQUARE,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, TryFromPrimitive, IntoPrimitive)]
#[repr(u32)]
pub enum Touchscreen {
    Any = ffi::ACONFIGURATION_TOUCHSCREEN_ANY,
    NoTouch = ffi::ACONFIGURATION_TOUCHSCREEN_NOTOUCH,
    Stylus = ffi::ACONFIGURATION_TOUCHSCREEN_STYLUS,
    Finger = ffi::ACONFIGURATION_TOUCHSCREEN_FINGER,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, TryFromPrimitive, IntoPrimitive)]
#[repr(u32)]
pub enum Density {
    Default = ffi::ACONFIGURATION_DENSITY_DEFAULT,
    Low = ffi::ACONFIGURATION_DENSITY_LOW,
    Medium = ffi::ACONFIGURATION_DENSITY_MEDIUM,
    TV = ffi::ACONFIGURATION_DENSITY_TV,
    High = ffi::ACONFIGURATION_DENSITY_HIGH,
    XHigh = ffi::ACONFIGURATION_DENSITY_XHIGH,
    XXHigh = ffi::ACONFIGURATION_DENSITY_XXHIGH,
    XXXHigh = ffi::ACONFIGURATION_DENSITY_XXXHIGH,
    Any = ffi::ACONFIGURATION_DENSITY_ANY,
    None = ffi::ACONFIGURATION_DENSITY_NONE,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, TryFromPrimitive, IntoPrimitive)]
#[repr(u32)]
pub enum Keyboard {
    Any = ffi::ACONFIGURATION_KEYBOARD_ANY,
    NoKeys = ffi::ACONFIGURATION_KEYBOARD_NOKEYS,
    Qwerty = ffi::ACONFIGURATION_KEYBOARD_QWERTY,
    TwelveKey = ffi::ACONFIGURATION_KEYBOARD_12KEY,
}

// FIXME is it a bitmask?
// FIXME are they all bitmasks?
#[derive(Debug, Clone, Copy, PartialEq, Eq, TryFromPrimitive, IntoPrimitive)]
#[repr(u32)]
pub enum Navigation {
    Any = ffi::ACONFIGURATION_NAVIGATION_ANY,
    NoNav = ffi::ACONFIGURATION_NAVIGATION_NONAV,
    DPad = ffi::ACONFIGURATION_NAVIGATION_DPAD,
    Trackball = ffi::ACONFIGURATION_NAVIGATION_TRACKBALL,
    Wheel = ffi::ACONFIGURATION_NAVIGATION_WHEEL,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, TryFromPrimitive, IntoPrimitive)]
#[repr(u32)]
pub enum KeysHidden {
    Any = ffi::ACONFIGURATION_KEYSHIDDEN_ANY,
    No = ffi::ACONFIGURATION_KEYSHIDDEN_NO,
    Yes = ffi::ACONFIGURATION_KEYSHIDDEN_YES,
    Soft = ffi::ACONFIGURATION_KEYSHIDDEN_SOFT,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, TryFromPrimitive, IntoPrimitive)]
#[repr(u32)]
pub enum NavHidden {
    Any = ffi::ACONFIGURATION_NAVHIDDEN_ANY,
    No = ffi::ACONFIGURATION_NAVHIDDEN_NO,
    Yes = ffi::ACONFIGURATION_NAVHIDDEN_YES,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, TryFromPrimitive, IntoPrimitive)]
#[repr(u32)]
pub enum ScreenSize {
    Any = ffi::ACONFIGURATION_SCREENSIZE_ANY,
    Small = ffi::ACONFIGURATION_SCREENSIZE_SMALL,
    Normal = ffi::ACONFIGURATION_SCREENSIZE_NORMAL,
    Large = ffi::ACONFIGURATION_SCREENSIZE_LARGE,
    XLarge = ffi::ACONFIGURATION_SCREENSIZE_XLARGE,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, TryFromPrimitive, IntoPrimitive)]
#[repr(u32)]
pub enum ScreenLong {
    Any = ffi::ACONFIGURATION_SCREENLONG_ANY,
    No = ffi::ACONFIGURATION_SCREENLONG_NO,
    Yes = ffi::ACONFIGURATION_SCREENLONG_YES,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, TryFromPrimitive, IntoPrimitive)]
#[repr(u32)]
pub enum ScreenRound {
    Any = ffi::ACONFIGURATION_SCREENROUND_ANY,
    No = ffi::ACONFIGURATION_SCREENROUND_NO,
    Yes = ffi::ACONFIGURATION_SCREENROUND_YES,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, TryFromPrimitive, IntoPrimitive)]
#[repr(u32)]
pub enum WideColorGamut {
    Any = ffi::ACONFIGURATION_WIDE_COLOR_GAMUT_ANY,
    No = ffi::ACONFIGURATION_WIDE_COLOR_GAMUT_NO,
    Yes = ffi::ACONFIGURATION_WIDE_COLOR_GAMUT_YES,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, TryFromPrimitive, IntoPrimitive)]
#[repr(u32)]
pub enum HDR {
    Any = ffi::ACONFIGURATION_HDR_ANY,
    No = ffi::ACONFIGURATION_HDR_NO,
    Yes = ffi::ACONFIGURATION_HDR_YES,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, TryFromPrimitive, IntoPrimitive)]
#[repr(u32)]
pub enum LayoutDir {
    Any = ffi::ACONFIGURATION_LAYOUTDIR_ANY,
    Ltr = ffi::ACONFIGURATION_LAYOUTDIR_LTR,
    Rtl = ffi::ACONFIGURATION_LAYOUTDIR_RTL,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, TryFromPrimitive, IntoPrimitive)]
#[repr(u32)]
pub enum UiModeType {
    Any = ffi::ACONFIGURATION_UI_MODE_TYPE_ANY,
    Normal = ffi::ACONFIGURATION_UI_MODE_TYPE_NORMAL,
    Desk = ffi::ACONFIGURATION_UI_MODE_TYPE_DESK,
    Car = ffi::ACONFIGURATION_UI_MODE_TYPE_CAR,
    Television = ffi::ACONFIGURATION_UI_MODE_TYPE_TELEVISION,
    Applicance = ffi::ACONFIGURATION_UI_MODE_TYPE_APPLIANCE,
    Watch = ffi::ACONFIGURATION_UI_MODE_TYPE_WATCH,
    VrHeadset = ffi::ACONFIGURATION_UI_MODE_TYPE_VR_HEADSET,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, TryFromPrimitive, IntoPrimitive)]
#[repr(u32)]
pub enum UiModeNight {
    Any = ffi::ACONFIGURATION_UI_MODE_NIGHT_ANY,
    No = ffi::ACONFIGURATION_UI_MODE_NIGHT_NO,
    Yes = ffi::ACONFIGURATION_UI_MODE_NIGHT_YES,
}