Skip to content

Add Storage Access Framework demos #191

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

Open
wants to merge 1 commit 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: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Browse the samples inside each topic samples folder:
- [User-interface](https://github.com/android/platform-samples/tree/main/samples/user-interface)
- More to come...

We regularly add new samples to this repository. You can find a list of all the available samples [here](https://github.com/android/platform-samples/tree/main/samples).
We are constantly adding new samples to this repository. You can find a list of all the available samples [here](https://github.com/android/platform-samples/tree/main/samples).

## How to run

Expand Down
9 changes: 9 additions & 0 deletions app/src/main/java/com/example/platform/app/SampleDemo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ import com.example.platform.shared.MinSdkBox
import com.example.platform.storage.mediastore.MediaStoreQuerySample
import com.example.platform.storage.mediastore.SelectedPhotosAccessSample
import com.example.platform.storage.photopicker.PhotoPickerSample
import com.example.platform.storage.storageaccessframework.GetContentSample
import com.example.platform.ui.appwidgets.AppWidgets
import com.example.platform.ui.constraintlayout.AdvancedArrangementFragment
import com.example.platform.ui.constraintlayout.AdvancedChainsFragment
Expand Down Expand Up @@ -566,6 +567,14 @@ val SAMPLE_DEMOS by lazy {
apiSurface = StorageApiSurface,
content = { MediaStoreQuerySample() },
),
ComposableSampleDemo(
id = "storageaccessframework-getcontent",
name = "Storage Access Framework - GET_CONTENT",
description = "Open a document using the Storage Access Framework",
documentation = "https://developer.android.com/training/data-storage/shared/documents-files#open-file",
apiSurface = StorageApiSurface,
content = { GetContentSample() },
),
ComposableSampleDemo(
id = "selected-photos-access",
name = "Selected Photos Access",
Expand Down
1 change: 1 addition & 0 deletions samples/accessibility/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ android {
dependencies {
implementation(platform(libs.androidx.compose.bom))
implementation(libs.androidx.ui)
implementation(libs.androidx.ui.tooling)
implementation(libs.androidx.ui.tooling.preview)
implementation(libs.androidx.material3)
implementation(libs.androidx.fragment)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,10 @@ import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.compose.ui.unit.dp
import androidx.core.app.AppOpsManagerCompat
//import androidx.core.content.getSystemService
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.LifecycleOwner
//import com.example.platform.base.PermissionBox
import com.example.platform.shared.PermissionBox
//import com.google.android.catalog.framework.annotations.Sample
import com.google.android.gms.location.LocationServices
import com.google.android.gms.location.Priority
import com.google.android.gms.tasks.CancellationTokenSource
Expand All @@ -62,13 +59,6 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.tasks.await

@SuppressLint("MissingPermission")
//@Sample(
// name = "Data Access",
// description = "Demonstrates how to implement data access auditing for your app to identify " +
// "unexpected data access, even from third-party SDKs and libraries.",
// documentation = "https://developer.android.com/guide/topics/data/audit-access",
//)

@RequiresApi(Build.VERSION_CODES.R)
@Composable
fun DataAccessSample() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ import androidx.fragment.app.Fragment
import java.text.DateFormat
import java.util.Date

//@Sample(
// name = "Screenshot Detection",
// description = "This sample shows how to detect that the user capture the screen in Android 14 onwards",
//)
@RequiresApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
class ScreenshotDetectionSample : Fragment() {

Expand Down
1 change: 1 addition & 0 deletions samples/storage/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ dependencies {
implementation(platform(libs.androidx.compose.bom))
implementation(libs.androidx.ui)
implementation(libs.androidx.ui.graphics)
implementation(libs.androidx.ui.tooling)
implementation(libs.androidx.ui.tooling.preview)
implementation(libs.androidx.material3)

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
package com.example.platform.storage.fileprovider

Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,10 @@ import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import coil.compose.AsyncImage
//import com.example.platform.base.PermissionBox
import com.example.platform.shared.PermissionBox
//import com.google.android.catalog.framework.annotations.Sample
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

//@Sample(
// name = "MediaStore - Query",
// description = "Query files indexed by MediaStore",
// documentation = "https://developer.android.com/training/data-storage/shared/media#media_store",
//)
@SuppressLint("MissingPermission")
@Composable
fun MediaStoreQuerySample() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,6 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

//@Sample(
// name = "Selected Photos Access",
// description = "Check and request storage permissions",
// documentation = "https://developer.android.com/about/versions/14/changes/partial-photo-video-access",
//)
//@RequiresPermission(anyOf = [READ_MEDIA_IMAGES, READ_MEDIA_VIDEO, READ_EXTERNAL_STORAGE])
@Composable
fun SelectedPhotosAccessSample() {
val context = LocalContext.current
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,6 @@ import coil.compose.AsyncImage
* picker. Check out the AndroidManifest.xml of the storage section to see the <service> declaration
* that enables backport support on Android KitKat onwards using Google Play Services
*/
@OptIn(ExperimentalMaterial3Api::class)
//@Sample(
// name = "PhotoPicker",
// description = "Select images/videos in a privacy-friendly way using the photo picker",
// documentation = "https://developer.android.com/training/data-storage/shared/photopicker",
//)
@Composable
fun PhotoPickerSample() {
var selectedMedia by remember { mutableStateOf(emptyList<Uri>()) }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
/*
* 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.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.res.painterResource
import androidx.compose.ui.semantics.contentDescription
import androidx.compose.ui.semantics.semantics
import com.example.platform.storage.R
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 kotlinx.coroutines.launch

@Composable
fun GetContentSample() {
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(
painter = painterResource(R.drawable.ic_filter_alt_24),
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