Skip to main content

tauri_plugin_android_fs/models/
storage_volume.rs

1use serde::{Deserialize, Serialize};
2use crate::*;
3
4
5#[derive(Debug, Clone, Hash, PartialEq, Eq, Deserialize, Serialize)]
6#[serde(rename_all = "camelCase")]
7pub struct StorageVolume {
8
9    /// A user-visible description of the volume.  
10    /// This can be determined by the manufacturer and is often localized according to the user’s language.
11    ///
12    /// e.g.
13    /// - `Internal shared storage`
14    /// - `SDCARD`
15    /// - `SD card`
16    /// - `Virtual SD card`
17    pub description: String,
18
19    /// Indicates whether this is primary storage volume. 
20    /// A device always has one (and one only) primary storage volume. 
21    pub is_primary: bool,
22
23    /// Indicates whether this is physically removable.
24    /// If `false`, this is device's built-in storage.
25    pub is_removable: bool,
26
27    /// Indicates whether thit is stable part of the device.
28    /// 
29    /// For example, a device’s built-in storage and physical media slots under protective covers are considered stable, 
30    /// while USB flash drives connected to handheld devices are not.
31    pub is_stable: bool,
32
33    /// Indicates whether this is backed by private user data partition, 
34    /// either internal storage or [adopted storage](https://source.android.com/docs/core/storage/adoptable).
35    ///
36    /// On most recent devices, the primary storage volume will often have this set to `true`.
37    pub is_emulated: bool,
38
39    /// Indicates whether this is readonly storage volume.
40    ///
41    /// e.g. SD card with readonly mode.
42    /// 
43    /// # Remark
44    /// As far as I understand, this should never be `true` 
45    /// when either `is_primary` or `is_emulated` is true, 
46    /// or when `is_removable` is false, 
47    /// but it might not be the case due to any issues or rare cases.
48    pub is_readonly: bool,
49
50    pub is_available_for_app_storage: bool,
51
52    pub is_available_for_public_storage: bool,
53
54    pub id: StorageVolumeId
55}
56
57impl StorageVolume {
58
59    /// Returns `true` if this volume is likely a SD-card–type storage.
60    ///
61    /// This is a heuristic classification based on the following properties:
62    ///
63    /// - `is_removable == true`
64    /// - `is_stable == true`
65    ///
66    /// Such volumes are typically removable media that are considered a stable part of the device.
67    pub fn is_sd_card_like(&self) -> bool {
68        self.is_removable && self.is_stable
69    }
70
71    /// Returns `true` if this volume is likely a USB-drive–type storage.
72    ///
73    /// This is a heuristic classification based on the following properties:
74    ///
75    /// - `is_removable == true`
76    /// - `is_stable == false`
77    ///
78    /// Such volumes are typically removable media that are not considered a stable part of the device.
79    pub fn is_usb_drive_like(&self) -> bool {
80        self.is_removable && !self.is_stable
81    }
82}
83
84#[derive(Debug, Clone, Hash, PartialEq, Eq, Deserialize, Serialize)]
85#[serde(rename_all = "camelCase")]
86pub struct StorageVolumeId {
87    /// これは常に存在すると期待していい。
88    pub(crate) top_dir_path: Option<std::path::PathBuf>,
89
90    /// USB drive などの一時的な storage volume の場合は存在しない。
91    pub(crate) app_data_dir_path: Option<std::path::PathBuf>,
92
93    /// USB drive などの一時的な storage volume の場合は存在しない。
94    pub(crate) app_cache_dir_path: Option<std::path::PathBuf>,
95
96    /// USB drive などの一時的な storage volume の場合は存在しない。
97    pub(crate) app_media_dir_path: Option<std::path::PathBuf>,
98
99    /// 常に存在するとは限らない。
100    /// primary storage volume はこれが None になることが多い。
101    pub(crate) uid: Option<String>,
102
103    /// None の場合は primary storage volume を指す。
104    /// None でないから primary storage volume でないとは限らない。
105    /// Android 9 以下は常に None。
106    pub(crate) media_store_volume_name: Option<String>,
107
108    /// 常に存在するとは限らない。
109    /// Android 11 以下は常に None。
110    pub(crate) storage_uuid: Option<String>,
111}
112
113#[allow(unused)]
114impl StorageVolumeId {
115
116    pub(crate) fn app_dir_path(&self, dir: impl Into<AppDir>) -> Option<&std::path::PathBuf> {
117        match dir.into() {
118            AppDir::Data => self.app_data_dir_path.as_ref(),
119            AppDir::Cache => self.app_cache_dir_path.as_ref(),
120
121            #[allow(deprecated)]
122            AppDir::PublicMedia => self.app_media_dir_path.as_ref()
123        }
124    }
125}