1 /*
2  * Copyright (C) 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  *      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 
17 package android.server.wm.intent;
18 
19 import android.os.Environment;
20 import android.server.wm.intent.Persistence;
21 
22 import com.google.common.collect.Lists;
23 
24 import org.json.JSONException;
25 
26 import java.io.File;
27 import java.io.IOException;
28 import java.nio.file.Files;
29 import java.nio.file.Path;
30 
31 public class IoUtils {
32     private static final int JSON_INDENTATION_LEVEL = 4;
33 
writeToDocumentsStorage( Persistence.TestCase testCase, int number, String dirName)34     public static void writeToDocumentsStorage(
35             Persistence.TestCase testCase, int number, String dirName)
36             throws JSONException, IOException {
37         File documentsDirectory =
38                 Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);
39         Path testsFilePath = documentsDirectory.toPath().resolve(dirName);
40 
41         if (!Files.exists(testsFilePath)) {
42             Files.createDirectories(testsFilePath);
43         }
44         Files.write(
45                 testsFilePath.resolve("test-" + number + ".json"),
46                 Lists.newArrayList(testCase.toJson().toString(JSON_INDENTATION_LEVEL)));
47     }
48 }
49