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