Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Storage Access Framework demos #191

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions samples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ Drag and Drop using the views
This sample demonstrates editing an UltraHDR image and the resulting gainmap as well. Spatial edit operations like crop, rotate, scale are supported
- [Find devices sample](connectivity/bluetooth/ble/src/main/java/com/example/platform/connectivity/bluetooth/ble/FindBLEDevicesSample.kt):
This example will demonstrate how to scanning for Low Energy Devices
- [GetDocument](storage/src/main/java/com/example/platform/storage/storageaccessframework/GetDocument.kt):
Open a document using the Storage Access Framework
- [Haptics - 1. Vibration effects](user-interface/haptics/src/main/java/com/example/platform/ui/haptics/Haptics.kt):
Shows various vibration effects.
- [Hyphenation](user-interface/text/src/main/java/com/example/platform/ui/text/Hyphenation.kt):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
/*
* Copyright 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.platform.storage.storageaccessframework

import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.rememberScrollState
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Check
import androidx.compose.material.icons.filled.FilterAlt
import androidx.compose.material.icons.outlined.Check
import androidx.compose.material3.DropdownMenu
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.ExtendedFloatingActionButton
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.ListItem
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Switch
import androidx.compose.material3.SwitchDefaults
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.semantics.contentDescription
import androidx.compose.ui.semantics.semantics
import com.example.platform.storage.storageaccessframework.shared.AudioFileCard
import com.example.platform.storage.storageaccessframework.shared.BinaryFileCard
import com.example.platform.storage.storageaccessframework.shared.FileRecord
import com.example.platform.storage.storageaccessframework.shared.FileType
import com.example.platform.storage.storageaccessframework.shared.ImageFileCard
import com.example.platform.storage.storageaccessframework.shared.PdfFileCard
import com.example.platform.storage.storageaccessframework.shared.TextFileCard
import com.example.platform.storage.storageaccessframework.shared.VideoFileCard
import com.google.android.catalog.framework.annotations.Sample
import kotlinx.coroutines.launch

@Sample(
name = "GetDocument",
description = "Open a document using the Storage Access Framework",
documentation = "https://developer.android.com/training/data-storage/shared/documents-files#open-file",
)
@Composable
fun GetDocument() {
val coroutineScope = rememberCoroutineScope()
val context = LocalContext.current
var selectedFilter by remember { mutableStateOf(FileType.Any) }
var selectMultiple by remember { mutableStateOf(false) }
var expanded by remember { mutableStateOf(false) }
var selectedFiles by remember { mutableStateOf(emptyList<FileRecord>()) }

val getSingleDocument =
rememberLauncherForActivityResult(ActivityResultContracts.GetContent()) { uri ->
coroutineScope.launch {
selectedFiles = uri?.let { uri ->
FileRecord.fromUri(uri, context)?.let { listOf(it) }
} ?: emptyList()
}
}

val getMultipleDocuments =
rememberLauncherForActivityResult(ActivityResultContracts.GetMultipleContents()) { uris ->
coroutineScope.launch {
selectedFiles = uris.mapNotNull { uri ->
FileRecord.fromUri(uri, context)
}
}
}

Scaffold(
modifier = Modifier.fillMaxSize(),
floatingActionButton = {
ExtendedFloatingActionButton(
onClick = {
if (selectMultiple) {
getMultipleDocuments.launch(selectedFilter.mimeType)
} else {
getSingleDocument.launch(selectedFilter.mimeType)
}
},
) {
Text(if (selectMultiple) "Select Files" else "Select File")
}
},
) { paddingValues ->
LazyColumn(Modifier.padding(paddingValues)) {
item {
ListItem(
headlineContent = { Text("File type filter") },
supportingContent = {
Text(selectedFilter.name)
},
trailingContent = {
val scrollState = rememberScrollState()
Box(
modifier = Modifier
.wrapContentSize(Alignment.TopStart),
) {
IconButton(onClick = { expanded = true }) {
Icon(
Icons.Default.FilterAlt,
contentDescription = "Localized description",
)
}
DropdownMenu(
expanded = expanded,
onDismissRequest = { expanded = false },
scrollState = scrollState,
) {
FileType.entries.forEach { fileType ->
DropdownMenuItem(
text = { Text(fileType.name) },
onClick = { selectedFilter = fileType },
leadingIcon = {
if (selectedFilter == fileType) {
Icon(
Icons.Outlined.Check,
contentDescription = "Selected",
)
}
},
)
}
}
LaunchedEffect(expanded) {
if (expanded) {
// Scroll to show the bottom menu items.
scrollState.scrollTo(scrollState.maxValue)
}
}
}
},
)
HorizontalDivider()
}
item {
ListItem(
headlineContent = { Text("Select multiple files?") },
trailingContent = {
Switch(
modifier = Modifier.semantics {
contentDescription = "Select multiple files"
},
checked = selectMultiple,
onCheckedChange = { selectMultiple = it },
thumbContent = {
if (selectMultiple) {
Icon(
imageVector = Icons.Filled.Check,
contentDescription = null,
modifier = Modifier.size(SwitchDefaults.IconSize),
)
}
},
)
},
)
HorizontalDivider()
}
items(selectedFiles) { file ->
when (file.fileType) {
FileType.Image -> ImageFileCard(file)
FileType.Video -> VideoFileCard(file)
FileType.Audio -> AudioFileCard(file)
FileType.Text -> TextFileCard(file)
FileType.Pdf -> PdfFileCard(file)
FileType.Any -> BinaryFileCard(file)
}
}
}
}
}
Loading
Loading