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