Skip to content

Commit 4f51e9f

Browse files
committed
Add GetDocument draft demo
1 parent 116504a commit 4f51e9f

1 file changed

Lines changed: 221 additions & 0 deletions

File tree

  • samples/storage/src/main/java/com/example/platform/storage/storageaccessframework
Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
/*
2+
* Copyright 2023 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.platform.storage.storageaccessframework
18+
19+
import android.text.format.Formatter
20+
import androidx.compose.foundation.layout.Column
21+
import androidx.compose.foundation.layout.Row
22+
import androidx.compose.foundation.layout.Spacer
23+
import androidx.compose.foundation.layout.fillMaxWidth
24+
import androidx.compose.foundation.layout.height
25+
import androidx.compose.foundation.layout.padding
26+
import androidx.compose.foundation.layout.size
27+
import androidx.compose.foundation.layout.width
28+
import androidx.compose.material.icons.Icons
29+
import androidx.compose.material.icons.automirrored.filled.InsertDriveFile
30+
import androidx.compose.material.icons.filled.AudioFile
31+
import androidx.compose.material.icons.filled.Description
32+
import androidx.compose.material.icons.filled.Image
33+
import androidx.compose.material.icons.filled.PictureAsPdf
34+
import androidx.compose.material.icons.filled.VideoFile
35+
import androidx.compose.material3.Button
36+
import androidx.compose.material3.ElevatedCard
37+
import androidx.compose.material3.Icon
38+
import androidx.compose.material3.MaterialTheme
39+
import androidx.compose.material3.Text
40+
import androidx.compose.runtime.Composable
41+
import androidx.compose.ui.Alignment
42+
import androidx.compose.ui.Modifier
43+
import androidx.compose.ui.graphics.vector.ImageVector
44+
import androidx.compose.ui.platform.LocalContext
45+
import androidx.compose.ui.tooling.preview.Preview
46+
import androidx.compose.ui.unit.dp
47+
48+
internal enum class FileType(val mimeType: String) {
49+
Image("image/*"),
50+
Video("video/*"),
51+
Audio("audio/*"),
52+
Text("text/*"),
53+
Pdf("application/pdf"),
54+
Any("*/*");
55+
}
56+
57+
data class FileRecord(val filename: String, val size: Long, val mimeType: String)
58+
59+
@Composable
60+
fun FileCard(
61+
file: FileRecord,
62+
icon: ImageVector,
63+
contentPreview: @Composable (() -> Unit)? = null,
64+
) {
65+
val sizeLabel = Formatter.formatShortFileSize(LocalContext.current, file.size)
66+
67+
ElevatedCard(
68+
modifier = Modifier
69+
.fillMaxWidth()
70+
.padding(16.dp),
71+
) {
72+
Row(
73+
modifier = Modifier
74+
.padding(16.dp)
75+
.fillMaxWidth(),
76+
verticalAlignment = Alignment.Top,
77+
) {
78+
Icon(
79+
imageVector = icon,
80+
contentDescription = null,
81+
modifier = Modifier.size(48.dp),
82+
)
83+
Spacer(modifier = Modifier.width(16.dp))
84+
Column {
85+
Text(file.filename, style = MaterialTheme.typography.headlineSmall, maxLines = 2)
86+
Spacer(modifier = Modifier.height(4.dp))
87+
Text("$sizeLabel · ${file.mimeType}", style = MaterialTheme.typography.bodyMedium)
88+
89+
if (contentPreview != null) {
90+
contentPreview()
91+
}
92+
}
93+
}
94+
}
95+
}
96+
97+
@Composable
98+
fun ImageFileCard(file: FileRecord, contentPreview: @Composable (() -> Unit)? = null) {
99+
FileCard(file, Icons.Default.Image) {
100+
if (contentPreview != null) {
101+
Spacer(modifier = Modifier.height(16.dp))
102+
contentPreview()
103+
} else {
104+
Spacer(modifier = Modifier.height(16.dp))
105+
Button(onClick = { /* Handle button click */ }) {
106+
Text("Read first 10 bytes")
107+
}
108+
}
109+
}
110+
}
111+
112+
@Composable
113+
fun VideoFileCard(file: FileRecord, contentPreview: @Composable (() -> Unit)? = null) {
114+
FileCard(file, Icons.Default.VideoFile) {
115+
if (contentPreview != null) {
116+
Spacer(modifier = Modifier.height(16.dp))
117+
contentPreview()
118+
} else {
119+
Spacer(modifier = Modifier.height(16.dp))
120+
Button(onClick = { /* Handle button click */ }) {
121+
Text("Read first 10 bytes")
122+
}
123+
}
124+
}
125+
}
126+
127+
@Composable
128+
fun AudioFileCard(file: FileRecord, contentPreview: @Composable (() -> Unit)? = null) {
129+
FileCard(file, Icons.Default.AudioFile) {
130+
if (contentPreview != null) {
131+
Spacer(modifier = Modifier.height(16.dp))
132+
contentPreview()
133+
} else {
134+
Spacer(modifier = Modifier.height(16.dp))
135+
Button(onClick = { /* Handle button click */ }) {
136+
Text("Read first 10 bytes")
137+
}
138+
}
139+
}
140+
}
141+
142+
@Composable
143+
fun TextFileCard(file: FileRecord, contentPreview: @Composable (() -> Unit)? = null) {
144+
FileCard(file, Icons.Default.Description) {
145+
if (contentPreview != null) {
146+
Spacer(modifier = Modifier.height(16.dp))
147+
contentPreview()
148+
} else {
149+
Spacer(modifier = Modifier.height(16.dp))
150+
Button(onClick = { /* Handle button click */ }) {
151+
Text("Read first 10 bytes")
152+
}
153+
}
154+
}
155+
}
156+
157+
@Composable
158+
fun PdfFileCard(file: FileRecord, contentPreview: @Composable (() -> Unit)? = null) {
159+
FileCard(file, Icons.Default.PictureAsPdf) {
160+
if (contentPreview != null) {
161+
Spacer(modifier = Modifier.height(16.dp))
162+
contentPreview()
163+
} else {
164+
Spacer(modifier = Modifier.height(16.dp))
165+
Button(onClick = { /* Handle button click */ }) {
166+
Text("Read first 10 bytes")
167+
}
168+
}
169+
}
170+
}
171+
172+
@Composable
173+
fun BinaryFileCard(file: FileRecord, contentPreview: @Composable (() -> Unit)? = null) {
174+
FileCard(file, Icons.AutoMirrored.Filled.InsertDriveFile) {
175+
if (contentPreview != null) {
176+
Spacer(modifier = Modifier.height(16.dp))
177+
contentPreview()
178+
} else {
179+
Spacer(modifier = Modifier.height(16.dp))
180+
Button(onClick = { /* Handle button click */ }) {
181+
Text("Read first 10 bytes")
182+
}
183+
}
184+
}
185+
}
186+
187+
@Preview
188+
@Composable
189+
fun ImageFileCard_Preview() {
190+
ImageFileCard(FileRecord("AmazingPhoto.png", 345_000, "image/png"))
191+
}
192+
193+
@Preview
194+
@Composable
195+
fun VideoFileCard_Preview() {
196+
VideoFileCard(FileRecord("All hands - meeting recording.mp4", 1_234_567_890, "image/png"))
197+
}
198+
199+
@Preview
200+
@Composable
201+
fun AudioFileCard_Preview() {
202+
AudioFileCard(FileRecord("Queen - We will rock you.mp3", 5_432_100, "audio/mp3"))
203+
}
204+
205+
@Preview
206+
@Composable
207+
fun TextFileCard_Preview() {
208+
TextFileCard(FileRecord("Android Jetpack Compose.txt", 5_678, "text/plain"))
209+
}
210+
211+
@Preview
212+
@Composable
213+
fun PdfFileCard_Preview() {
214+
PdfFileCard(FileRecord("Android Jetpack Compose.pdf", 1_234_567, "application/pdf"))
215+
}
216+
217+
@Preview
218+
@Composable
219+
fun BinaryFileCard_Preview() {
220+
BinaryFileCard(FileRecord("binary.bin", 78_420_968, "application/octet-stream"))
221+
}

0 commit comments

Comments
 (0)