1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
/*
* Copyright 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.media;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;
import java.util.Objects;
/**
* Define a command that a {@link MediaController2} can send to a {@link MediaSession2}.
* <p>
* If {@link #getCommandCode()} isn't {@link #COMMAND_CODE_CUSTOM}), it's predefined command.
* If {@link #getCommandCode()} is {@link #COMMAND_CODE_CUSTOM}), it's custom command and
* {@link #getCustomCommand()} shouldn't be {@code null}.
* <p>
* This API is not generally intended for third party application developers.
* Use the <a href="{@docRoot}jetpack/androidx.html">AndroidX</a>
* <a href="{@docRoot}reference/androidx/media2/package-summary.html">Media2 Library</a>
* for consistent behavior across all devices.
*/
public final class Session2Command implements Parcelable {
/**
* Command code for the custom command which can be defined by string action in the
* {@link Session2Command}.
*/
public static final int COMMAND_CODE_CUSTOM = 0;
/**
* Result code representing that the command is skipped or canceled. For an example, a seek
* command can be skipped if it is followed by another seek command.
*/
public static final int RESULT_INFO_SKIPPED = 1;
/**
* Result code representing that the command is successfully completed.
*/
public static final int RESULT_SUCCESS = 0;
/**
* Result code represents that call is ended with an unknown error.
*/
public static final int RESULT_ERROR_UNKNOWN_ERROR = -1;
public static final Parcelable.Creator<Session2Command> CREATOR =
new Parcelable.Creator<Session2Command>() {
@Override
public Session2Command createFromParcel(Parcel in) {
return new Session2Command(in);
}
@Override
public Session2Command[] newArray(int size) {
return new Session2Command[size];
}
};
private final int mCommandCode;
// Nonnull if it's custom command
private final String mCustomCommand;
private final Bundle mExtras;
/**
* Constructor for creating a command predefined in AndroidX media2.
*
* @param commandCode A command code for a command predefined in AndroidX media2.
*/
public Session2Command(int commandCode) {
if (commandCode == COMMAND_CODE_CUSTOM) {
throw new IllegalArgumentException("commandCode shouldn't be COMMAND_CODE_CUSTOM");
}
mCommandCode = commandCode;
mCustomCommand = null;
mExtras = null;
}
/**
* Constructor for creating a custom command.
*
* @param action The action of this custom command.
* @param extras An extra bundle for this custom command.
*/
public Session2Command(@NonNull String action, @Nullable Bundle extras) {
if (action == null) {
throw new IllegalArgumentException("action shouldn't be null");
}
mCommandCode = COMMAND_CODE_CUSTOM;
mCustomCommand = action;
mExtras = extras;
}
/**
* Used by parcelable creator.
*/
@SuppressWarnings("WeakerAccess") /* synthetic access */
Session2Command(Parcel in) {
mCommandCode = in.readInt();
mCustomCommand = in.readString();
mExtras = in.readBundle();
}
/**
* Gets the command code of a predefined command.
* This will return {@link #COMMAND_CODE_CUSTOM} for a custom command.
*/
public int getCommandCode() {
return mCommandCode;
}
/**
* Gets the action of a custom command.
* This will return {@code null} for a predefined command.
*/
@Nullable
public String getCustomCommand() {
return mCustomCommand;
}
/**
* Gets the extra bundle of a custom command.
* This will return {@code null} for a predefined command.
*/
@Nullable
public Bundle getExtras() {
return mExtras;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(@NonNull Parcel dest, int flags) {
if (dest == null) {
throw new IllegalArgumentException("parcel shouldn't be null");
}
dest.writeInt(mCommandCode);
dest.writeString(mCustomCommand);
dest.writeBundle(mExtras);
}
@Override
public boolean equals(@Nullable Object obj) {
if (!(obj instanceof Session2Command)) {
return false;
}
Session2Command other = (Session2Command) obj;
return mCommandCode == other.mCommandCode
&& TextUtils.equals(mCustomCommand, other.mCustomCommand);
}
@Override
public int hashCode() {
return Objects.hash(mCustomCommand, mCommandCode);
}
/**
* Contains the result of {@link Session2Command}.
*/
public static final class Result {
private final int mResultCode;
private final Bundle mResultData;
/**
* Constructor of {@link Result}.
*
* @param resultCode result code
* @param resultData result data
*/
public Result(int resultCode, @Nullable Bundle resultData) {
mResultCode = resultCode;
mResultData = resultData;
}
/**
* Returns the result code.
*/
public int getResultCode() {
return mResultCode;
}
/**
* Returns the result data.
*/
@Nullable
public Bundle getResultData() {
return mResultData;
}
}
}
|