1 /*
<lambda>null2  * 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 com.android.settings.biometrics.fingerprint2.ui.settings.fragment
18 
19 import android.content.Context
20 import android.util.Log
21 import android.view.View
22 import androidx.lifecycle.lifecycleScope
23 import androidx.preference.PreferenceViewHolder
24 import com.android.settings.R
25 import com.android.settings.biometrics.fingerprint2.lib.model.FingerprintData
26 import com.android.settingslib.widget.TwoTargetPreference
27 import kotlinx.coroutines.delay
28 import kotlinx.coroutines.launch
29 
30 private const val TAG = "FingerprintSettingsPreference"
31 
32 class FingerprintSettingsPreference(
33   context: Context,
34   val fingerprintViewModel: FingerprintData,
35   val fragment: FingerprintSettingsV2Fragment,
36   val isLastFingerprint: Boolean,
37 ) : TwoTargetPreference(context) {
38   private lateinit var myView: View
39 
40   init {
41     key = "FINGERPRINT_" + fingerprintViewModel.fingerId
42     Log.d(TAG, "FingerprintPreference $this with frag $fragment $key")
43     title = fingerprintViewModel.name
44     isPersistent = false
45     setIcon(R.drawable.ic_fingerprint_24dp)
46     setOnPreferenceClickListener {
47       fragment.lifecycleScope.launch { fragment.onPrefClicked(fingerprintViewModel) }
48       true
49     }
50   }
51 
52   override fun onBindViewHolder(view: PreferenceViewHolder) {
53     super.onBindViewHolder(view)
54     myView = view.itemView
55     view.itemView.findViewById<View>(R.id.delete_button)?.setOnClickListener {
56       fragment.lifecycleScope.launch { fragment.onDeletePrefClicked(fingerprintViewModel) }
57     }
58   }
59 
60   /** Highlights this dialog. */
61   suspend fun highlight() {
62     fragment.activity?.getDrawable(R.drawable.preference_highlight)?.let { highlight ->
63       val centerX: Float = myView.width / 2.0f
64       val centerY: Float = myView.height / 2.0f
65       highlight.setHotspot(centerX, centerY)
66       myView.background = highlight
67       myView.isPressed = true
68       myView.isPressed = false
69       delay(300)
70       myView.background = null
71     }
72   }
73 
74   override fun getSecondTargetResId(): Int {
75     return R.layout.preference_widget_delete
76   }
77 
78   suspend fun askUserToDeleteDialog(): Boolean {
79     return FingerprintDeletionDialog.showInstance(fingerprintViewModel, isLastFingerprint, fragment)
80   }
81 
82   suspend fun askUserToRenameDialog(): Pair<FingerprintData, String>? {
83     return FingerprintSettingsRenameDialog.showInstance(fingerprintViewModel, fragment)
84   }
85 }
86