Sungsoo Lim | bf37bd4 | 2018-12-19 14:03:47 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 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.media; |
| 18 | |
| 19 | import static android.media.Session2Command.COMMAND_CODE_CUSTOM; |
Sungsoo Lim | bf37bd4 | 2018-12-19 14:03:47 +0900 | [diff] [blame] | 20 | |
| 21 | import android.annotation.NonNull; |
| 22 | import android.annotation.Nullable; |
Sungsoo Lim | bf37bd4 | 2018-12-19 14:03:47 +0900 | [diff] [blame] | 23 | import android.os.Parcel; |
| 24 | import android.os.Parcelable; |
Sungsoo Lim | bf37bd4 | 2018-12-19 14:03:47 +0900 | [diff] [blame] | 25 | |
| 26 | import java.util.Collection; |
| 27 | import java.util.HashSet; |
| 28 | import java.util.Set; |
| 29 | |
| 30 | /** |
| 31 | * A set of {@link Session2Command} which represents a command group. |
| 32 | * <p> |
| 33 | * This API is not generally intended for third party application developers. |
Sungsoo Lim | 938e8e9 | 2018-12-28 13:37:29 +0900 | [diff] [blame] | 34 | * Use the <a href="{@docRoot}jetpack/androidx.html">AndroidX</a> |
| 35 | * <a href="{@docRoot}reference/androidx/media2/package-summary.html">Media2 Library</a> |
| 36 | * for consistent behavior across all devices. |
Sungsoo Lim | bf37bd4 | 2018-12-19 14:03:47 +0900 | [diff] [blame] | 37 | * </p> |
Sungsoo Lim | bf37bd4 | 2018-12-19 14:03:47 +0900 | [diff] [blame] | 38 | */ |
| 39 | public final class Session2CommandGroup implements Parcelable { |
| 40 | private static final String TAG = "Session2CommandGroup"; |
| 41 | |
Jeff Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame^] | 42 | public static final @android.annotation.NonNull Parcelable.Creator<Session2CommandGroup> CREATOR = |
Sungsoo Lim | bf37bd4 | 2018-12-19 14:03:47 +0900 | [diff] [blame] | 43 | new Parcelable.Creator<Session2CommandGroup>() { |
| 44 | @Override |
| 45 | public Session2CommandGroup createFromParcel(Parcel in) { |
| 46 | return new Session2CommandGroup(in); |
| 47 | } |
| 48 | |
| 49 | @Override |
| 50 | public Session2CommandGroup[] newArray(int size) { |
| 51 | return new Session2CommandGroup[size]; |
| 52 | } |
| 53 | }; |
| 54 | |
| 55 | Set<Session2Command> mCommands = new HashSet<>(); |
| 56 | |
| 57 | /** |
Sungsoo Lim | bf37bd4 | 2018-12-19 14:03:47 +0900 | [diff] [blame] | 58 | * Creates a new Session2CommandGroup with commands copied from another object. |
| 59 | * |
| 60 | * @param commands The collection of commands to copy. |
| 61 | */ |
Sungsoo Lim | 7553b35 | 2019-01-11 17:06:40 +0900 | [diff] [blame] | 62 | @SuppressWarnings("WeakerAccess") /* synthetic access */ |
Jin Seok Park | 42dd015 | 2019-01-10 11:02:48 +0900 | [diff] [blame] | 63 | Session2CommandGroup(@Nullable Collection<Session2Command> commands) { |
Sungsoo Lim | bf37bd4 | 2018-12-19 14:03:47 +0900 | [diff] [blame] | 64 | if (commands != null) { |
| 65 | mCommands.addAll(commands); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Used by parcelable creator. |
| 71 | */ |
| 72 | @SuppressWarnings("WeakerAccess") /* synthetic access */ |
| 73 | Session2CommandGroup(Parcel in) { |
Insun Kang | f86a56c | 2019-01-22 17:16:07 +0900 | [diff] [blame] | 74 | Parcelable[] commands = in.readParcelableArray(Session2Command.class.getClassLoader()); |
Sungsoo Lim | bf37bd4 | 2018-12-19 14:03:47 +0900 | [diff] [blame] | 75 | if (commands != null) { |
Insun Kang | f86a56c | 2019-01-22 17:16:07 +0900 | [diff] [blame] | 76 | for (Parcelable command : commands) { |
| 77 | mCommands.add((Session2Command) command); |
Sungsoo Lim | bf37bd4 | 2018-12-19 14:03:47 +0900 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | /** |
Sungsoo Lim | bf37bd4 | 2018-12-19 14:03:47 +0900 | [diff] [blame] | 83 | * Checks whether this command group has a command that matches given {@code command}. |
| 84 | * |
| 85 | * @param command A command to find. Shouldn't be {@code null}. |
| 86 | */ |
| 87 | public boolean hasCommand(@NonNull Session2Command command) { |
| 88 | if (command == null) { |
| 89 | throw new IllegalArgumentException("command shouldn't be null"); |
| 90 | } |
| 91 | return mCommands.contains(command); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Checks whether this command group has a command that matches given {@code commandCode}. |
| 96 | * |
| 97 | * @param commandCode A command code to find. |
| 98 | * Shouldn't be {@link Session2Command#COMMAND_CODE_CUSTOM}. |
| 99 | */ |
Sungsoo Lim | 2d4fe89 | 2019-01-02 15:36:31 +0900 | [diff] [blame] | 100 | public boolean hasCommand(int commandCode) { |
Sungsoo Lim | bf37bd4 | 2018-12-19 14:03:47 +0900 | [diff] [blame] | 101 | if (commandCode == COMMAND_CODE_CUSTOM) { |
| 102 | throw new IllegalArgumentException("Use hasCommand(Command) for custom command"); |
| 103 | } |
| 104 | for (Session2Command command : mCommands) { |
| 105 | if (command.getCommandCode() == commandCode) { |
| 106 | return true; |
| 107 | } |
| 108 | } |
| 109 | return false; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Gets all commands of this command group. |
| 114 | */ |
Sungsoo Lim | 2d4fe89 | 2019-01-02 15:36:31 +0900 | [diff] [blame] | 115 | @NonNull |
| 116 | public Set<Session2Command> getCommands() { |
Sungsoo Lim | bf37bd4 | 2018-12-19 14:03:47 +0900 | [diff] [blame] | 117 | return new HashSet<>(mCommands); |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public int describeContents() { |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | @Override |
Jin Seok Park | 42dd015 | 2019-01-10 11:02:48 +0900 | [diff] [blame] | 126 | public void writeToParcel(@NonNull Parcel dest, int flags) { |
| 127 | if (dest == null) { |
| 128 | throw new IllegalArgumentException("parcel shouldn't be null"); |
| 129 | } |
Jin Seok Park | eabc8bf | 2019-01-11 09:55:11 +0900 | [diff] [blame] | 130 | dest.writeParcelableArray(mCommands.toArray(new Session2Command[0]), 0); |
Sungsoo Lim | bf37bd4 | 2018-12-19 14:03:47 +0900 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Builds a {@link Session2CommandGroup} object. |
| 135 | */ |
| 136 | public static final class Builder { |
| 137 | private Set<Session2Command> mCommands; |
| 138 | |
| 139 | public Builder() { |
| 140 | mCommands = new HashSet<>(); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Creates a new builder for {@link Session2CommandGroup} with commands copied from another |
| 145 | * {@link Session2CommandGroup} object. |
| 146 | * @param commandGroup |
| 147 | */ |
| 148 | public Builder(@NonNull Session2CommandGroup commandGroup) { |
Jin Seok Park | 42dd015 | 2019-01-10 11:02:48 +0900 | [diff] [blame] | 149 | if (commandGroup == null) { |
| 150 | throw new IllegalArgumentException("command group shouldn't be null"); |
| 151 | } |
Sungsoo Lim | bf37bd4 | 2018-12-19 14:03:47 +0900 | [diff] [blame] | 152 | mCommands = commandGroup.getCommands(); |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Adds a command to this command group. |
| 157 | * |
| 158 | * @param command A command to add. Shouldn't be {@code null}. |
| 159 | */ |
Sungsoo Lim | 2d4fe89 | 2019-01-02 15:36:31 +0900 | [diff] [blame] | 160 | @NonNull |
| 161 | public Builder addCommand(@NonNull Session2Command command) { |
Sungsoo Lim | bf37bd4 | 2018-12-19 14:03:47 +0900 | [diff] [blame] | 162 | if (command == null) { |
| 163 | throw new IllegalArgumentException("command shouldn't be null"); |
| 164 | } |
| 165 | mCommands.add(command); |
| 166 | return this; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Adds a predefined command with given {@code commandCode} to this command group. |
| 171 | * |
| 172 | * @param commandCode A command code to add. |
| 173 | * Shouldn't be {@link Session2Command#COMMAND_CODE_CUSTOM}. |
| 174 | */ |
Sungsoo Lim | 2d4fe89 | 2019-01-02 15:36:31 +0900 | [diff] [blame] | 175 | @NonNull |
| 176 | public Builder addCommand(int commandCode) { |
Sungsoo Lim | bf37bd4 | 2018-12-19 14:03:47 +0900 | [diff] [blame] | 177 | if (commandCode == COMMAND_CODE_CUSTOM) { |
| 178 | throw new IllegalArgumentException( |
| 179 | "Use addCommand(Session2Command) for COMMAND_CODE_CUSTOM."); |
| 180 | } |
| 181 | mCommands.add(new Session2Command(commandCode)); |
| 182 | return this; |
| 183 | } |
| 184 | |
| 185 | /** |
Sungsoo Lim | bf37bd4 | 2018-12-19 14:03:47 +0900 | [diff] [blame] | 186 | * Removes a command from this group which matches given {@code command}. |
| 187 | * |
| 188 | * @param command A command to find. Shouldn't be {@code null}. |
| 189 | */ |
Sungsoo Lim | 2d4fe89 | 2019-01-02 15:36:31 +0900 | [diff] [blame] | 190 | @NonNull |
| 191 | public Builder removeCommand(@NonNull Session2Command command) { |
Sungsoo Lim | bf37bd4 | 2018-12-19 14:03:47 +0900 | [diff] [blame] | 192 | if (command == null) { |
| 193 | throw new IllegalArgumentException("command shouldn't be null"); |
| 194 | } |
| 195 | mCommands.remove(command); |
| 196 | return this; |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Removes a command from this group which matches given {@code commandCode}. |
| 201 | * |
| 202 | * @param commandCode A command code to find. |
| 203 | * Shouldn't be {@link Session2Command#COMMAND_CODE_CUSTOM}. |
| 204 | */ |
Sungsoo Lim | 2d4fe89 | 2019-01-02 15:36:31 +0900 | [diff] [blame] | 205 | @NonNull |
| 206 | public Builder removeCommand(int commandCode) { |
Sungsoo Lim | bf37bd4 | 2018-12-19 14:03:47 +0900 | [diff] [blame] | 207 | if (commandCode == COMMAND_CODE_CUSTOM) { |
| 208 | throw new IllegalArgumentException("commandCode shouldn't be COMMAND_CODE_CUSTOM"); |
| 209 | } |
| 210 | mCommands.remove(new Session2Command(commandCode)); |
| 211 | return this; |
| 212 | } |
| 213 | |
Sungsoo Lim | bf37bd4 | 2018-12-19 14:03:47 +0900 | [diff] [blame] | 214 | /** |
| 215 | * Builds {@link Session2CommandGroup}. |
| 216 | * |
| 217 | * @return a new {@link Session2CommandGroup}. |
| 218 | */ |
Sungsoo Lim | 2d4fe89 | 2019-01-02 15:36:31 +0900 | [diff] [blame] | 219 | @NonNull |
| 220 | public Session2CommandGroup build() { |
Sungsoo Lim | bf37bd4 | 2018-12-19 14:03:47 +0900 | [diff] [blame] | 221 | return new Session2CommandGroup(mCommands); |
| 222 | } |
| 223 | } |
| 224 | } |