blob: 73a59d00ae344e1a6bdaea8469dd48c4256f460f [file] [log] [blame]
Sungsoo Limbf37bd42018-12-19 14:03:47 +09001/*
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
17package android.media;
18
19import static android.media.Session2Command.COMMAND_CODE_CUSTOM;
Sungsoo Limbf37bd42018-12-19 14:03:47 +090020
21import android.annotation.NonNull;
22import android.annotation.Nullable;
Sungsoo Limbf37bd42018-12-19 14:03:47 +090023import android.os.Parcel;
24import android.os.Parcelable;
Sungsoo Limbf37bd42018-12-19 14:03:47 +090025
26import java.util.Collection;
27import java.util.HashSet;
28import 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 Lim938e8e92018-12-28 13:37:29 +090034 * 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 Limbf37bd42018-12-19 14:03:47 +090037 * </p>
Sungsoo Limbf37bd42018-12-19 14:03:47 +090038 */
39public final class Session2CommandGroup implements Parcelable {
40 private static final String TAG = "Session2CommandGroup";
41
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -070042 public static final @android.annotation.NonNull Parcelable.Creator<Session2CommandGroup> CREATOR =
Sungsoo Limbf37bd42018-12-19 14:03:47 +090043 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 Limbf37bd42018-12-19 14:03:47 +090058 * Creates a new Session2CommandGroup with commands copied from another object.
59 *
60 * @param commands The collection of commands to copy.
61 */
Sungsoo Lim7553b352019-01-11 17:06:40 +090062 @SuppressWarnings("WeakerAccess") /* synthetic access */
Jin Seok Park42dd0152019-01-10 11:02:48 +090063 Session2CommandGroup(@Nullable Collection<Session2Command> commands) {
Sungsoo Limbf37bd42018-12-19 14:03:47 +090064 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 Kangf86a56c2019-01-22 17:16:07 +090074 Parcelable[] commands = in.readParcelableArray(Session2Command.class.getClassLoader());
Sungsoo Limbf37bd42018-12-19 14:03:47 +090075 if (commands != null) {
Insun Kangf86a56c2019-01-22 17:16:07 +090076 for (Parcelable command : commands) {
77 mCommands.add((Session2Command) command);
Sungsoo Limbf37bd42018-12-19 14:03:47 +090078 }
79 }
80 }
81
82 /**
Sungsoo Limbf37bd42018-12-19 14:03:47 +090083 * 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 Lim2d4fe892019-01-02 15:36:31 +0900100 public boolean hasCommand(int commandCode) {
Sungsoo Limbf37bd42018-12-19 14:03:47 +0900101 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 Lim2d4fe892019-01-02 15:36:31 +0900115 @NonNull
116 public Set<Session2Command> getCommands() {
Sungsoo Limbf37bd42018-12-19 14:03:47 +0900117 return new HashSet<>(mCommands);
118 }
119
120 @Override
121 public int describeContents() {
122 return 0;
123 }
124
125 @Override
Jin Seok Park42dd0152019-01-10 11:02:48 +0900126 public void writeToParcel(@NonNull Parcel dest, int flags) {
127 if (dest == null) {
128 throw new IllegalArgumentException("parcel shouldn't be null");
129 }
Jin Seok Parkeabc8bf2019-01-11 09:55:11 +0900130 dest.writeParcelableArray(mCommands.toArray(new Session2Command[0]), 0);
Sungsoo Limbf37bd42018-12-19 14:03:47 +0900131 }
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 Park42dd0152019-01-10 11:02:48 +0900149 if (commandGroup == null) {
150 throw new IllegalArgumentException("command group shouldn't be null");
151 }
Sungsoo Limbf37bd42018-12-19 14:03:47 +0900152 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 Lim2d4fe892019-01-02 15:36:31 +0900160 @NonNull
161 public Builder addCommand(@NonNull Session2Command command) {
Sungsoo Limbf37bd42018-12-19 14:03:47 +0900162 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 Lim2d4fe892019-01-02 15:36:31 +0900175 @NonNull
176 public Builder addCommand(int commandCode) {
Sungsoo Limbf37bd42018-12-19 14:03:47 +0900177 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 Limbf37bd42018-12-19 14:03:47 +0900186 * 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 Lim2d4fe892019-01-02 15:36:31 +0900190 @NonNull
191 public Builder removeCommand(@NonNull Session2Command command) {
Sungsoo Limbf37bd42018-12-19 14:03:47 +0900192 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 Lim2d4fe892019-01-02 15:36:31 +0900205 @NonNull
206 public Builder removeCommand(int commandCode) {
Sungsoo Limbf37bd42018-12-19 14:03:47 +0900207 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 Limbf37bd42018-12-19 14:03:47 +0900214 /**
215 * Builds {@link Session2CommandGroup}.
216 *
217 * @return a new {@link Session2CommandGroup}.
218 */
Sungsoo Lim2d4fe892019-01-02 15:36:31 +0900219 @NonNull
220 public Session2CommandGroup build() {
Sungsoo Limbf37bd42018-12-19 14:03:47 +0900221 return new Session2CommandGroup(mCommands);
222 }
223 }
224}