• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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  *      http://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 package com.android.privatespace
17 
18 import android.content.Context
19 import android.content.Intent
20 import android.net.Uri
21 import android.os.Environment
22 import androidx.compose.runtime.getValue
23 import androidx.compose.runtime.mutableStateOf
24 import androidx.compose.runtime.setValue
25 import androidx.lifecycle.ViewModel
26 import com.android.privatespace.filetransfer.FileTransferService
27 import com.android.privatespace.filetransfer.FileTransferService.Companion.DESTINATION_PATH_EXTRA
28 import com.android.privatespace.filetransfer.FileTransferService.Companion.KEEP_ORIGINAL_EXTRA
29 import com.android.privatespace.filetransfer.FileTransferService.Companion.SOURCE_URIS_EXTRA
30 import java.util.ArrayList
31 
32 /** A ViewModel for the PrivateSpaceActivity move files feature and its dialog. */
33 class PrivateSpaceViewModel : ViewModel() {
34     var uiState by mutableStateOf(PrivateSpaceUiState.STARTED)
35         private set
36 
37     private var fileUris: List<Uri> = emptyList()
38 
showMoveFilesDialognull39     fun showMoveFilesDialog(uris: List<Uri>) {
40         fileUris = uris
41         if (uris.isNotEmpty()) {
42             uiState = PrivateSpaceUiState.SHOW_MOVE_FILES_DIALOG
43         } else {
44             // Finish if no URIs are selected.
45             finishFlow()
46         }
47     }
48 
moveFilesnull49     fun moveFiles(context: Context) {
50         val serviceIntent = getServiceIntent(context)
51         serviceIntent.putExtra(KEEP_ORIGINAL_EXTRA, false)
52         context.startForegroundService(serviceIntent)
53         finishFlow()
54     }
55 
copyFilesnull56     fun copyFiles(context: Context) {
57         val serviceIntent = getServiceIntent(context)
58         serviceIntent.putExtra(KEEP_ORIGINAL_EXTRA, true)
59         context.startForegroundService(serviceIntent)
60         finishFlow()
61     }
62 
finishFlownull63     fun finishFlow() {
64         uiState = PrivateSpaceUiState.FINISHED
65     }
66 
getServiceIntentnull67     private fun getServiceIntent(context: Context): Intent {
68         val intent = Intent(context, FileTransferService::class.java)
69         intent.putParcelableArrayListExtra(SOURCE_URIS_EXTRA, ArrayList(fileUris))
70         intent.putExtra(DESTINATION_PATH_EXTRA, Environment.DIRECTORY_DOWNLOADS)
71         return intent
72     }
73 }
74