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 { |
| 53 | setTitle(R.string.controls_pin_verify) |
| 54 | setView(R.layout.controls_dialog_pin) |
| 55 | setPositiveButton( |
| 56 | android.R.string.ok, |
| 57 | DialogInterface.OnClickListener { dialog, _ -> |
| 58 | if (dialog is Dialog) { |
| 59 | dialog.requireViewById<EditText>(R.id.controls_pin_input) |
| 60 | val pin = dialog.requireViewById<EditText>(R.id.controls_pin_input) |
| 61 | .getText().toString() |
| 62 | cvh.action(addChallengeValue(lastAction, pin)) |
| 63 | dialog.dismiss() |
| 64 | } |
| 65 | }) |
| 66 | setNegativeButton( |
| 67 | android.R.string.cancel, |
| 68 | DialogInterface.OnClickListener { dialog, _ -> dialog.cancel() } |
| 69 | ) |
| 70 | } |
| 71 | return builder.create().apply { |
| 72 | getWindow().apply { |
| 73 | setType(WindowManager.LayoutParams.TYPE_VOLUME_OVERLAY) |
| 74 | setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE) |
| 75 | } |
| 76 | setOnShowListener(DialogInterface.OnShowListener { _ -> |
| 77 | val editText = requireViewById<EditText>(R.id.controls_pin_input) |
| 78 | requireViewById<CheckBox>(R.id.controls_pin_use_alpha).setOnClickListener { v -> |
| 79 | if ((v as CheckBox).isChecked) { |
| 80 | editText.setInputType( |
| 81 | InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD) |
| 82 | } else { |
| 83 | editText.setInputType( |
| 84 | InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_VARIATION_PASSWORD) |
| 85 | } |
| 86 | } |
| 87 | editText.requestFocus() |
| 88 | }) |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | private fun addChallengeValue(action: ControlAction, challengeValue: String): ControlAction { |
| 93 | val id = action.getTemplateId() |
| 94 | return when (action) { |
| 95 | is BooleanAction -> BooleanAction(id, action.getNewState(), challengeValue) |
| 96 | is FloatAction -> FloatAction(id, action.getNewValue(), challengeValue) |
| 97 | is CommandAction -> CommandAction(id, challengeValue) |
| 98 | is ModeAction -> ModeAction(id, action.getNewMode(), challengeValue) |
| 99 | else -> throw IllegalStateException("'action' is not a known type: $action") |
| 100 | } |
| 101 | } |
| 102 | } |