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.templates; 18 19 import android.annotation.NonNull; 20 import android.os.Bundle; 21 import android.service.controls.Control; 22 import android.service.controls.actions.BooleanAction; 23 24 import com.android.internal.util.Preconditions; 25 26 /** 27 * A template for a {@link Control} with a single button that can be toggled between two states. 28 * 29 * The states for the toggle correspond to the states in {@link ControlButton#isChecked()}. 30 * An action on this template will originate a {@link BooleanAction} to change that state. 31 * 32 * @see BooleanAction 33 */ 34 public final class ToggleTemplate extends ControlTemplate { 35 36 private static final @TemplateType int TYPE = TYPE_TOGGLE; 37 private static final String KEY_BUTTON = "key_button"; 38 private final @NonNull ControlButton mButton; 39 40 /** 41 * @param templateId the identifier for this template object 42 * @param button a {@link ControlButton} that can show the current state and toggle it 43 */ ToggleTemplate(@onNull String templateId, @NonNull ControlButton button)44 public ToggleTemplate(@NonNull String templateId, @NonNull ControlButton button) { 45 super(templateId); 46 Preconditions.checkNotNull(button); 47 mButton = button; 48 } 49 50 /** 51 * @param b 52 * @hide 53 */ ToggleTemplate(Bundle b)54 ToggleTemplate(Bundle b) { 55 super(b); 56 mButton = b.getParcelable(KEY_BUTTON, android.service.controls.templates.ControlButton.class); 57 } 58 isChecked()59 public boolean isChecked() { 60 return mButton.isChecked(); 61 } 62 63 @NonNull getContentDescription()64 public CharSequence getContentDescription() { 65 return mButton.getActionDescription(); 66 } 67 68 /** 69 * @return {@link ControlTemplate#TYPE_TOGGLE} 70 */ 71 @Override getTemplateType()72 public int getTemplateType() { 73 return TYPE; 74 } 75 76 /** 77 * @return 78 * @hide 79 */ 80 @Override 81 @NonNull getDataBundle()82 Bundle getDataBundle() { 83 Bundle b = super.getDataBundle(); 84 b.putParcelable(KEY_BUTTON, mButton); 85 return b; 86 } 87 } 88