/* * Copyright (C) 2025 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 * * http://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.android.privatespace import android.content.Context import android.content.Intent import android.net.Uri import android.os.Environment import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.setValue import androidx.lifecycle.ViewModel import com.android.privatespace.filetransfer.FileTransferService import com.android.privatespace.filetransfer.FileTransferService.Companion.DESTINATION_PATH_EXTRA import com.android.privatespace.filetransfer.FileTransferService.Companion.KEEP_ORIGINAL_EXTRA import com.android.privatespace.filetransfer.FileTransferService.Companion.SOURCE_URIS_EXTRA import java.util.ArrayList /** A ViewModel for the PrivateSpaceActivity move files feature and its dialog. */ class PrivateSpaceViewModel : ViewModel() { var uiState by mutableStateOf(PrivateSpaceUiState.STARTED) private set private var fileUris: List = emptyList() fun showMoveFilesDialog(uris: List) { fileUris = uris if (uris.isNotEmpty()) { uiState = PrivateSpaceUiState.SHOW_MOVE_FILES_DIALOG } else { // Finish if no URIs are selected. finishFlow() } } fun moveFiles(context: Context) { val serviceIntent = getServiceIntent(context) serviceIntent.putExtra(KEEP_ORIGINAL_EXTRA, false) context.startForegroundService(serviceIntent) finishFlow() } fun copyFiles(context: Context) { val serviceIntent = getServiceIntent(context) serviceIntent.putExtra(KEEP_ORIGINAL_EXTRA, true) context.startForegroundService(serviceIntent) finishFlow() } fun finishFlow() { uiState = PrivateSpaceUiState.FINISHED } private fun getServiceIntent(context: Context): Intent { val intent = Intent(context, FileTransferService::class.java) intent.putParcelableArrayListExtra(SOURCE_URIS_EXTRA, ArrayList(fileUris)) intent.putExtra(DESTINATION_PATH_EXTRA, Environment.DIRECTORY_DOWNLOADS) return intent } }