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