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_PASSED
28 
29 class ExtensionValidationResultAdapter(
30     private val testType: String,
31     private val layoutInflater: LayoutInflater,
32     private val extensionResultMap: LinkedHashMap<Int, Pair<Int, String>>
33 ) : BaseAdapter() {
34 
getCountnull35     override fun getCount(): Int {
36         return extensionResultMap.size
37     }
38 
getItemnull39     override fun getItem(position: Int): MutableMap.MutableEntry<Int, Pair<Int, String>> {
40         return extensionResultMap.entries.elementAt(position)
41     }
42 
getItemIdnull43     override fun getItemId(position: Int): Long {
44         return position.toLong()
45     }
46 
getViewnull47     override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
48         val textView: TextView
49         val detailsView: TextView
50 
51         val v =
52             if (convertView == null) {
53                 val layout = android.R.layout.simple_list_item_2
54                 val view = layoutInflater.inflate(layout, parent, false)
55                 textView = view.findViewById(android.R.id.text1)
56                 detailsView = view.findViewById(android.R.id.text2)
57                 view
58             } else {
59                 textView = convertView.findViewById(android.R.id.text1)
60                 detailsView = convertView.findViewById(android.R.id.text2)
61                 convertView
62             }
63 
64         val item = getItem(position)
65 
66         var backgroundResource = 0
67         var iconResource = 0
68 
69         when (item.value.first) {
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         textView.text = TestResults.getExtensionModeStringFromId(testType, item.key)
85         textView.setPadding(padding, 0, padding, 0)
86         textView.compoundDrawablePadding = padding
87         textView.setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, iconResource, 0)
88 
89         detailsView.text = item.value.second
90         detailsView.setPadding(padding, 0, padding, 0)
91 
92         v.setBackgroundResource(backgroundResource)
93         return v
94     }
95 }
96