• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 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 com.android.quicksearchbox.ui
18 
19 import android.content.Context
20 import android.graphics.drawable.Drawable
21 import android.util.AttributeSet
22 import android.view.ViewDebug
23 import android.widget.Checkable
24 import android.widget.ImageView
25 import android.widget.RelativeLayout
26 import android.widget.TextView
27 import com.android.quicksearchbox.R
28 
29 /** A corpus in the corpus selection list. */
30 class CorpusView : RelativeLayout, Checkable {
31   private var mIcon: ImageView? = null
32   private var mLabel: TextView? = null
33   private var mChecked = false
34 
35   constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {}
36   constructor(context: Context?) : super(context) {}
37 
38   @Override
onFinishInflatenull39   protected override fun onFinishInflate() {
40     super.onFinishInflate()
41     mIcon = findViewById(R.id.source_icon) as ImageView?
42     mLabel = findViewById(R.id.source_label) as TextView?
43   }
44 
setLabelnull45   fun setLabel(label: CharSequence?) {
46     mLabel?.setText(label)
47   }
48 
setIconnull49   fun setIcon(icon: Drawable?) {
50     mIcon?.setImageDrawable(icon)
51   }
52 
53   @Override
54   @ViewDebug.ExportedProperty
isCheckednull55   override fun isChecked(): Boolean {
56     return mChecked
57   }
58 
59   @Override
setCheckednull60   override fun setChecked(checked: Boolean) {
61     if (mChecked != checked) {
62       mChecked = checked
63       refreshDrawableState()
64     }
65   }
66 
67   @Override
togglenull68   override fun toggle() {
69     isChecked = !mChecked
70   }
71 
72   @Override
onCreateDrawableStatenull73   protected override fun onCreateDrawableState(extraSpace: Int): IntArray {
74     val drawableState: IntArray = super.onCreateDrawableState(extraSpace + 1)
75     if (isChecked) {
76       mergeDrawableStates(drawableState, CHECKED_STATE_SET)
77     }
78     return drawableState
79   }
80 
81   companion object {
82     private val CHECKED_STATE_SET = intArrayOf(android.R.attr.state_checked)
83   }
84 }
85