1 /*
2  * Copyright 2021 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.extensions.validation
18 
19 import android.view.LayoutInflater
20 import android.view.View
21 import android.view.ViewGroup
22 import android.widget.BaseAdapter
23 import android.widget.TextView
24 import androidx.camera.integration.extensions.R
25 import androidx.camera.integration.extensions.TestResultType.TEST_RESULT_FAILED
26 import androidx.camera.integration.extensions.TestResultType.TEST_RESULT_NOT_SUPPORTED
27 import androidx.camera.integration.extensions.TestResultType.TEST_RESULT_NOT_TESTED
28 import androidx.camera.integration.extensions.TestResultType.TEST_RESULT_PARTIALLY_TESTED
29 import androidx.camera.integration.extensions.TestResultType.TEST_RESULT_PASSED
30 import androidx.camera.integration.extensions.validation.CameraValidationResultActivity.Companion.getLensFacingStringFromInt
31 
32 class CameraValidationResultAdapter(
33     private val layoutInflater: LayoutInflater,
34     private val cameraLensFacingMap: LinkedHashMap<String, Int>,
35     private val cameraExtensionResultMap:
36         LinkedHashMap<Pair<String, String>, LinkedHashMap<Int, Pair<Int, String>>>
37 ) : BaseAdapter() {
38 
getCountnull39     override fun getCount(): Int {
40         return cameraExtensionResultMap.size
41     }
42 
getItemnull43     override fun getItem(
44         position: Int
45     ): MutableMap.MutableEntry<Pair<String, String>, LinkedHashMap<Int, Pair<Int, String>>> {
46         return cameraExtensionResultMap.entries.elementAt(position)
47     }
48 
getItemIdnull49     override fun getItemId(position: Int): Long {
50         return position.toLong()
51     }
52 
getViewnull53     override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
54         val textView: TextView =
55             if (convertView == null) {
56                 val layout = android.R.layout.simple_list_item_1
57                 layoutInflater.inflate(layout, parent, false) as TextView
58             } else {
59                 convertView as TextView
60             }
61 
62         val item = getItem(position)
63         val (testType, cameraId) = item.key
64 
65         val testResult = getTestResult(testType, cameraId)
66         var backgroundResource = 0
67         var iconResource = 0
68 
69         when (testResult) {
70             TEST_RESULT_PASSED -> {
71                 backgroundResource = R.drawable.test_pass_gradient
72                 iconResource = R.drawable.outline_check_circle
73             }
74             TEST_RESULT_FAILED -> {
75                 backgroundResource = R.drawable.test_fail_gradient
76                 iconResource = R.drawable.outline_error
77             }
78             TEST_RESULT_NOT_SUPPORTED -> {
79                 backgroundResource = R.drawable.test_disable_gradient
80             }
81         }
82 
83         val padding = 10
84         val lensFacingName = cameraLensFacingMap[cameraId]?.let { getLensFacingStringFromInt(it) }
85         textView.text = "[$testType][$cameraId][$lensFacingName]"
86         textView.setPadding(padding, 0, padding, 0)
87         textView.compoundDrawablePadding = padding
88         textView.setBackgroundResource(backgroundResource)
89         textView.setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, iconResource, 0)
90         return textView
91     }
92 
getTestResultnull93     private fun getTestResult(testType: String, cameraId: String): Int {
94         var notTestedCount = 0
95         var passCount = 0
96         var failCount = 0
97 
98         cameraExtensionResultMap[Pair(testType, cameraId)]?.forEach {
99             when (it.value.first) {
100                 TEST_RESULT_NOT_TESTED -> {
101                     notTestedCount++
102                 }
103                 TEST_RESULT_PASSED -> {
104                     passCount++
105                 }
106                 TEST_RESULT_FAILED -> {
107                     failCount++
108                 }
109             }
110         }
111 
112         return if (passCount == 0 && failCount == 0 && notTestedCount == 0) {
113             TEST_RESULT_NOT_SUPPORTED
114         } else if (passCount != 0 && failCount == 0 && notTestedCount == 0) {
115             TEST_RESULT_PASSED
116         } else if (failCount != 0 && notTestedCount == 0) {
117             TEST_RESULT_FAILED
118         } else if (passCount == 0 && failCount == 0 && notTestedCount != 0) {
119             TEST_RESULT_NOT_TESTED
120         } else {
121             TEST_RESULT_PARTIALLY_TESTED
122         }
123     }
124 }
125