Neda Topoljanac | 45a626c | 2021-12-15 14:58:39 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2021 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 androidx.wear.tiles.material; |
| 18 | |
| 19 | import static androidx.wear.tiles.material.ChipDefaults.COMPACT_HEIGHT; |
| 20 | import static androidx.wear.tiles.material.ChipDefaults.COMPACT_HORIZONTAL_PADDING; |
| 21 | import static androidx.wear.tiles.material.ChipDefaults.COMPACT_PRIMARY; |
| 22 | |
| 23 | import androidx.annotation.NonNull; |
| 24 | import androidx.annotation.RestrictTo; |
| 25 | import androidx.annotation.RestrictTo.Scope; |
| 26 | import androidx.wear.tiles.ActionBuilders.Action; |
| 27 | import androidx.wear.tiles.DeviceParametersBuilders.DeviceParameters; |
| 28 | import androidx.wear.tiles.DimensionBuilders.ContainerDimension; |
| 29 | import androidx.wear.tiles.DimensionBuilders.WrappedDimensionProp; |
| 30 | import androidx.wear.tiles.LayoutElementBuilders.FontStyles; |
| 31 | import androidx.wear.tiles.LayoutElementBuilders.LayoutElement; |
| 32 | import androidx.wear.tiles.proto.LayoutElementProto; |
| 33 | |
| 34 | /** |
| 35 | * Tiles component {@link CompactChip} that represents clickable object with the text. |
| 36 | * |
| 37 | * <p>The Chip is Stadium shape and has a max height designed to take no more than one line of text |
| 38 | * of {@link FontStyles#caption1} style. Width of the chip is adjustable to the text size. |
| 39 | * |
| 40 | * <p>The recommended set of {@link ChipColors} styles can be obtained from {@link ChipDefaults}., |
| 41 | * e.g. {@link ChipDefaults#COMPACT_PRIMARY} to get a color scheme for a primary {@link CompactChip} |
| 42 | * which by default will have a solid background of {@link Colors#PRIMARY} and text color of {@link |
| 43 | * Colors#ON_PRIMARY}. |
| 44 | */ |
| 45 | public class CompactChip implements LayoutElement { |
| 46 | @NonNull private final Chip mElement; |
| 47 | |
| 48 | CompactChip(@NonNull Chip element) { |
| 49 | this.mElement = element; |
| 50 | } |
| 51 | |
| 52 | /** Builder class for {@link androidx.wear.tiles.material.CompactChip}. */ |
| 53 | public static final class Builder implements LayoutElement.Builder { |
| 54 | @NonNull private final String mText; |
| 55 | @NonNull private final Action mAction; |
| 56 | @NonNull private final String mClickableId; |
| 57 | @NonNull private final DeviceParameters mDeviceParameters; |
| 58 | @NonNull private ChipColors mChipColors = COMPACT_PRIMARY; |
| 59 | |
| 60 | /** |
| 61 | * Creates a builder for the {@link CompactChip} with associated action and the given text |
| 62 | * |
| 63 | * @param text The text to be displayed in this compact chip. It shouldn't contain more than |
| 64 | * 9 characters. Any extra characters will be deleted. |
| 65 | * @param action Associated Actions for click events. When the CompactChip is clicked it |
| 66 | * will fire the associated action. |
| 67 | * @param clickableId The ID associated with the given action's clickable. |
| 68 | * @param deviceParameters The device parameters used for styling text. |
| 69 | */ |
| 70 | @SuppressWarnings("LambdaLast") |
| 71 | public Builder( |
| 72 | @NonNull String text, |
| 73 | @NonNull Action action, |
| 74 | @NonNull String clickableId, |
| 75 | @NonNull DeviceParameters deviceParameters) { |
| 76 | this.mText = text.substring(0, Math.min(text.length(), 9)); |
| 77 | this.mAction = action; |
| 78 | this.mClickableId = clickableId; |
| 79 | this.mDeviceParameters = deviceParameters; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Sets the colors for the {@link CompactChip}. If set, {@link |
| 84 | * ChipColors#getBackgroundColor()} will be used for the background of the button and {@link |
| 85 | * ChipColors#getBackgroundColor()} for the text. If not set, {@link |
| 86 | * ChipDefaults#COMPACT_PRIMARY} will be used. |
| 87 | */ |
| 88 | @NonNull |
Neda Topoljanac | 45a626c | 2021-12-15 14:58:39 +0000 | [diff] [blame] | 89 | public Builder setChipColors(@NonNull ChipColors chipColors) { |
| 90 | mChipColors = chipColors; |
| 91 | return this; |
| 92 | } |
| 93 | |
| 94 | /** Constructs and returns {@link CompactChip} with the provided content and look. */ |
| 95 | @NonNull |
| 96 | @Override |
| 97 | public CompactChip build() { |
| 98 | Chip.Builder chipBuilder = |
| 99 | new Chip.Builder(mAction, mClickableId, mDeviceParameters) |
| 100 | .setChipColors(mChipColors) |
| 101 | .setContentDescription(mText) |
| 102 | .setLeftAlign(false) // centered |
| 103 | .setWidth(new WrappedDimensionProp.Builder().build()) |
| 104 | .setHeight(COMPACT_HEIGHT) |
| 105 | .setHorizontalPadding(COMPACT_HORIZONTAL_PADDING) |
| 106 | .setPrimaryTextContent(mText) |
| 107 | .setPrimaryTextFontStyle( |
| 108 | FontStyles.caption1(mDeviceParameters).build()); |
| 109 | |
| 110 | return new CompactChip(chipBuilder.build()); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | /** Returns height of this Chip. Intended for testing purposes only. */ |
| 115 | @NonNull |
| 116 | public ContainerDimension getHeight() { |
| 117 | return mElement.getHeight(); |
| 118 | } |
| 119 | |
| 120 | /** Returns width of this Chip. Intended for testing purposes only. */ |
| 121 | @NonNull |
| 122 | public ContainerDimension getWidth() { |
| 123 | return mElement.getWidth(); |
| 124 | } |
| 125 | |
| 126 | /** Returns click event action associated with this Chip. Intended for testing purposes only. */ |
| 127 | @NonNull |
| 128 | public Action getAction() { |
| 129 | return mElement.getAction(); |
| 130 | } |
| 131 | |
| 132 | /** Returns chip color of this Chip. Intended for testing purposes only. */ |
| 133 | @NonNull |
| 134 | public ChipColors getChipColors() { |
| 135 | return mElement.getChipColors(); |
| 136 | } |
| 137 | |
| 138 | /** Returns content description of this Chip. Intended for testing purposes only. */ |
| 139 | @NonNull |
| 140 | public String getContentDescription() { |
| 141 | return mElement.getContentDescription(); |
| 142 | } |
| 143 | |
| 144 | /** Returns content of this Chip. Intended for testing purposes only. */ |
| 145 | @NonNull |
| 146 | public LayoutElement getContent() { |
| 147 | return mElement.getContent(); |
| 148 | } |
| 149 | |
| 150 | /** @hide */ |
| 151 | @RestrictTo(Scope.LIBRARY_GROUP) |
| 152 | @NonNull |
| 153 | @Override |
| 154 | public LayoutElementProto.LayoutElement toLayoutElementProto() { |
| 155 | return mElement.toLayoutElementProto(); |
| 156 | } |
| 157 | } |