1/*
2 * Copyright (C) 2014 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 android.annotation.SystemApi;
20import android.os.Parcel;
21import android.os.Parcelable;
22
23import java.util.Objects;
24
25/**
26 * @hide
27 * A class to encapsulate information about an audio focus owner or request.
28 */
29@SystemApi
30public final class AudioFocusInfo implements Parcelable {
31
32    private AudioAttributes mAttributes;
33    private String mClientId;
34    private String mPackageName;
35    private int mGainRequest;
36    private int mLossReceived;
37    private int mFlags;
38
39
40    /**
41     * Class constructor
42     * @param aa
43     * @param clientId
44     * @param packageName
45     * @param gainRequest
46     * @param lossReceived
47     * @param flags
48     */
49    AudioFocusInfo(AudioAttributes aa, String clientId, String packageName,
50            int gainRequest, int lossReceived, int flags) {
51        mAttributes = aa == null ? new AudioAttributes.Builder().build() : aa;
52        mClientId = clientId == null ? "" : clientId;
53        mPackageName = packageName == null ? "" : packageName;
54        mGainRequest = gainRequest;
55        mLossReceived = lossReceived;
56        mFlags = flags;
57    }
58
59
60    /**
61     * The audio attributes for the audio focus request.
62     * @return non-null {@link AudioAttributes}.
63     */
64    @SystemApi
65    public AudioAttributes getAttributes() { return mAttributes; }
66
67    @SystemApi
68    public String getClientId() { return mClientId; }
69
70    @SystemApi
71    public String getPackageName() { return mPackageName; }
72
73    /**
74     * The type of audio focus gain request.
75     * @return one of {@link AudioManager#AUDIOFOCUS_GAIN},
76     *     {@link AudioManager#AUDIOFOCUS_GAIN_TRANSIENT},
77     *     {@link AudioManager#AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK},
78     *     {@link AudioManager#AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE}.
79     */
80    @SystemApi
81    public int getGainRequest() { return mGainRequest; }
82
83    /**
84     * The type of audio focus loss that was received by the
85     * {@link AudioManager.OnAudioFocusChangeListener} if one was set.
86     * @return 0 if focus wasn't lost, or one of {@link AudioManager#AUDIOFOCUS_LOSS},
87     *   {@link AudioManager#AUDIOFOCUS_LOSS_TRANSIENT} or
88     *   {@link AudioManager#AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK}.
89     */
90    @SystemApi
91    public int getLossReceived() { return mLossReceived; }
92
93    /** @hide */
94    void clearLossReceived() { mLossReceived = 0; }
95
96    /**
97     * The flags set in the audio focus request.
98     * @return 0 or a combination of {link AudioManager#AUDIOFOCUS_FLAG_DELAY_OK},
99     *     {@link AudioManager#AUDIOFOCUS_FLAG_PAUSES_ON_DUCKABLE_LOSS}, and
100     *     {@link AudioManager#AUDIOFOCUS_FLAG_LOCK}.
101     */
102    @SystemApi
103    public int getFlags() { return mFlags; }
104
105    @Override
106    public int describeContents() {
107        return 0;
108    }
109
110    @Override
111    public void writeToParcel(Parcel dest, int flags) {
112        mAttributes.writeToParcel(dest, flags);
113        dest.writeString(mClientId);
114        dest.writeString(mPackageName);
115        dest.writeInt(mGainRequest);
116        dest.writeInt(mLossReceived);
117        dest.writeInt(mFlags);
118    }
119
120    @SystemApi
121    @Override
122    public int hashCode() {
123        return Objects.hash(mAttributes, mClientId, mPackageName, mGainRequest, mFlags);
124    }
125
126    @SystemApi
127    @Override
128    public boolean equals(Object obj) {
129        if (this == obj)
130            return true;
131        if (obj == null)
132            return false;
133        if (getClass() != obj.getClass())
134            return false;
135        AudioFocusInfo other = (AudioFocusInfo) obj;
136        if (!mAttributes.equals(other.mAttributes)) {
137            return false;
138        }
139        if (!mClientId.equals(other.mClientId)) {
140            return false;
141        }
142        if (!mPackageName.equals(other.mPackageName)) {
143            return false;
144        }
145        if (mGainRequest != other.mGainRequest) {
146            return false;
147        }
148        if (mLossReceived != other.mLossReceived) {
149            return false;
150        }
151        if (mFlags != other.mFlags) {
152            return false;
153        }
154        return true;
155    }
156
157    public static final Parcelable.Creator<AudioFocusInfo> CREATOR
158            = new Parcelable.Creator<AudioFocusInfo>() {
159
160        public AudioFocusInfo createFromParcel(Parcel in) {
161            return new AudioFocusInfo(
162                    AudioAttributes.CREATOR.createFromParcel(in), //AudioAttributes aa
163                    in.readString(), //String clientId
164                    in.readString(), //String packageName
165                    in.readInt(), //int gainRequest
166                    in.readInt(), //int lossReceived
167                    in.readInt() //int flags
168                    );
169        }
170
171        public AudioFocusInfo[] newArray(int size) {
172            return new AudioFocusInfo[size];
173        }
174    };
175}
176