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