blob: 15c2a0afe8191db0844b66c20e0079cc7683dccc [file] [log] [blame]
Matt Pietalb7da66c2020-03-06 10:49:31 -05001/*
2 * Copyright (C) 2020 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
17package com.android.systemui.controls.ui
18
19import android.app.AlertDialog
20import android.app.Dialog
21import android.content.DialogInterface
22import android.service.controls.actions.BooleanAction
23import android.service.controls.actions.CommandAction
24import android.service.controls.actions.ControlAction
25import android.service.controls.actions.FloatAction
26import android.service.controls.actions.ModeAction
27import android.text.InputType
28import android.util.Log
29import android.view.WindowManager
30import android.widget.CheckBox
31import android.widget.EditText
32
33import com.android.systemui.R
34
35/**
36 * Creates all dialogs for challengeValues that can occur from a call to
37 * {@link ControlsProviderService#performControlAction}. The types of challenge
38 * responses are listed in {@link ControlAction.ResponseResult}.
39 */
40object ChallengeDialogs {
41
42 fun createPinDialog(cvh: ControlViewHolder): Dialog? {
43 val lastAction = cvh.lastAction
44 if (lastAction == null) {
45 Log.e(ControlsUiController.TAG,
46 "PIN Dialog attempted but no last action is set. Will not show")
47 return null
48 }
49 val builder = AlertDialog.Builder(
50 cvh.context,
51 android.R.style.Theme_DeviceDefault_Dialog_Alert
52 ).apply {
Matt Pietale80c7812020-03-20 14:02:52 -040053 val res = cvh.context.resources
54 setTitle(res.getString(R.string.controls_pin_verify, *arrayOf(cvh.title.getText())))
Matt Pietalb7da66c2020-03-06 10:49:31 -050055 setView(R.layout.controls_dialog_pin)
56 setPositiveButton(
57 android.R.string.ok,
58 DialogInterface.OnClickListener { dialog, _ ->
59 if (dialog is Dialog) {
60 dialog.requireViewById<EditText>(R.id.controls_pin_input)
61 val pin = dialog.requireViewById<EditText>(R.id.controls_pin_input)
62 .getText().toString()
63 cvh.action(addChallengeValue(lastAction, pin))
64 dialog.dismiss()
65 }
66 })
67 setNegativeButton(
68 android.R.string.cancel,
69 DialogInterface.OnClickListener { dialog, _ -> dialog.cancel() }
70 )
71 }
72 return builder.create().apply {
73 getWindow().apply {
74 setType(WindowManager.LayoutParams.TYPE_VOLUME_OVERLAY)
75 setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE)
76 }
77 setOnShowListener(DialogInterface.OnShowListener { _ ->
78 val editText = requireViewById<EditText>(R.id.controls_pin_input)
79 requireViewById<CheckBox>(R.id.controls_pin_use_alpha).setOnClickListener { v ->
80 if ((v as CheckBox).isChecked) {
81 editText.setInputType(
82 InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD)
83 } else {
84 editText.setInputType(
85 InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_VARIATION_PASSWORD)
86 }
87 }
88 editText.requestFocus()
89 })
90 }
91 }
92
93 private fun addChallengeValue(action: ControlAction, challengeValue: String): ControlAction {
94 val id = action.getTemplateId()
95 return when (action) {
96 is BooleanAction -> BooleanAction(id, action.getNewState(), challengeValue)
97 is FloatAction -> FloatAction(id, action.getNewValue(), challengeValue)
98 is CommandAction -> CommandAction(id, challengeValue)
99 is ModeAction -> ModeAction(id, action.getNewMode(), challengeValue)
100 else -> throw IllegalStateException("'action' is not a known type: $action")
101 }
102 }
103}