Matt Pietal | b7da66c | 2020-03-06 10:49:31 -0500 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.systemui.controls.ui |
| 18 | |
| 19 | import android.app.AlertDialog |
| 20 | import android.app.Dialog |
| 21 | import android.content.DialogInterface |
| 22 | import android.service.controls.actions.BooleanAction |
| 23 | import android.service.controls.actions.CommandAction |
| 24 | import android.service.controls.actions.ControlAction |
| 25 | import android.service.controls.actions.FloatAction |
| 26 | import android.service.controls.actions.ModeAction |
| 27 | import android.text.InputType |
| 28 | import android.util.Log |
| 29 | import android.view.WindowManager |
| 30 | import android.widget.CheckBox |
| 31 | import android.widget.EditText |
| 32 | |
| 33 | import 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 | */ |
| 40 | object 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 Pietal | e80c781 | 2020-03-20 14:02:52 -0400 | [diff] [blame] | 53 | val res = cvh.context.resources |
| 54 | setTitle(res.getString(R.string.controls_pin_verify, *arrayOf(cvh.title.getText()))) |
Matt Pietal | b7da66c | 2020-03-06 10:49:31 -0500 | [diff] [blame] | 55 | 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 | } |