| 1 | /* |
| 2 | * Copyright (C) 2019 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 android.service.controls.actions; |
| 18 | |
| 19 | import android.annotation.NonNull; |
| 20 | import android.annotation.Nullable; |
| 21 | import android.os.Bundle; |
| 22 | import android.service.controls.Control; |
| 23 | import android.service.controls.templates.ToggleRangeTemplate; |
| 24 | import android.service.controls.templates.ToggleTemplate; |
| 25 | |
| 26 | /** |
| 27 | * Action sent by user toggling a {@link Control} between checked/unchecked. |
| 28 | * |
| 29 | * This action is available when the {@link Control} was constructed with either a |
| 30 | * {@link ToggleTemplate} or a {@link ToggleRangeTemplate}. |
| 31 | */ |
| 32 | public final class BooleanAction extends ControlAction { |
| 33 | |
| 34 | private static final @ActionType int TYPE = TYPE_BOOLEAN; |
| 35 | private static final String KEY_NEW_STATE = "key_new_state"; |
| 36 | |
| 37 | private final boolean mNewState; |
| 38 | |
| 39 | /** |
| 40 | * @param templateId the identifier of the {@link ToggleTemplate} that produced this action. |
| 41 | * @param newState new value for the state displayed by the {@link ToggleTemplate}. |
| 42 | */ |
| 43 | public BooleanAction(@NonNull String templateId, boolean newState) { |
| 44 | this(templateId, newState, null); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * @param templateId the identifier of the template that originated this action. |
| 49 | * @param newState new value for the state displayed by the template. |
| 50 | * @param challengeValue a value sent by the user along with the action to authenticate. {@code} |
| 51 | * null is sent when no authentication is needed or has not been |
| 52 | * requested. |
| 53 | */ |
| 54 | public BooleanAction(@NonNull String templateId, boolean newState, |
| 55 | @Nullable String challengeValue) { |
| 56 | super(templateId, challengeValue); |
| 57 | mNewState = newState; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * @param b |
| 62 | * @hide |
| 63 | */ |
| 64 | BooleanAction(Bundle b) { |
| 65 | super(b); |
| 66 | mNewState = b.getBoolean(KEY_NEW_STATE); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * The new state set for the button in the corresponding {@link ToggleTemplate}. |
| 71 | * |
| 72 | * @return {@code true} if the button was toggled from unchecked to checked. |
| 73 | */ |
| 74 | public boolean getNewState() { |
| 75 | return mNewState; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * @return {@link ControlAction#TYPE_BOOLEAN} |
| 80 | */ |
| 81 | @Override |
| 82 | public int getActionType() { |
| 83 | return TYPE; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * @return |
| 88 | * @hide |
| 89 | */ |
| 90 | @Override |
| 91 | @NonNull |
| 92 | Bundle getDataBundle() { |
| 93 | Bundle b = super.getDataBundle(); |
| 94 | b.putBoolean(KEY_NEW_STATE, mNewState); |
| 95 | return b; |
| 96 | } |
| 97 | } |