1 /*
<lambda>null2  * Copyright 2022 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 androidx.camera.integration.diagnose
18 
19 import android.content.Context
20 import androidx.annotation.WorkerThread
21 import androidx.camera.view.LifecycleCameraController
22 import java.io.File
23 
24 /** Diagnosis object that runs diagnosis test on device and save result to a file. */
25 class Diagnosis {
26 
27     @WorkerThread
28     suspend fun diagnose(
29         context: Context,
30         tasks: List<DiagnosisTask>,
31         cameraController: LifecycleCameraController,
32         isAggregated: Boolean
33     ): File? {
34 
35         if (tasks.isEmpty()) {
36             return null
37         }
38 
39         val zipFileName = "diagnose_report_${System.currentTimeMillis()}.zip"
40         val dataStore = DataStore(context, zipFileName)
41 
42         tasks.forEach { task ->
43             task.runDiagnosisTask(cameraController, dataStore, context)
44             if (!isAggregated) {
45                 dataStore.flushTextToTextFile("${task.getTaskName()}")
46             }
47         }
48         if (isAggregated) {
49             dataStore.flushTextToTextFile("text_report")
50         }
51 
52         return dataStore.flushZip()
53     }
54 
55     companion object {
56         private const val TAG = "Diagnosis"
57     }
58 }
59