Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package org.chromium.content.browser; | |
| 6 | |
| 7 import android.content.Context; | |
| 8 import android.media.AudioManager; | |
| 9 | |
| 10 import org.chromium.base.CalledByNative; | |
| 11 import org.chromium.base.JNINamespace; | |
| 12 import org.chromium.base.Log; | |
| 13 | |
| 14 /** | |
| 15 * MediaSession is the Java counterpart of content::MediaSession. | |
| 16 * It is being used to communicate from content::MediaSession (C++) to the | |
| 17 * Android system. A MediaSession is implementing OnAudioFocusChangeListener, | |
| 18 * making it an audio focus holder for Android. Thus two instances of | |
| 19 * MediaSession can't have audio focus at the same time. | |
| 20 * A MediaSession will use the type requested from its C++ counterpart and will | |
| 21 * resume its play using the same type if it were to happen, for example, when | |
| 22 * it got temporarily suspended by a transient sound like a notification. | |
| 23 */ | |
| 24 @JNINamespace("content") | |
| 25 public class MediaSession implements AudioManager.OnAudioFocusChangeListener { | |
| 26 private static final String TAG = "MediaSession"; | |
| 27 | |
| 28 private Context mContext; | |
| 29 private int mFocusType; | |
| 30 | |
| 31 // Native pointer to C++ content::MediaSession. | |
| 32 private final long mNativeMediaSession; | |
| 33 | |
| 34 private MediaSession(final Context context, long nativeMediaSession) { | |
| 35 mContext = context; | |
| 36 mNativeMediaSession = nativeMediaSession; | |
| 37 } | |
| 38 | |
| 39 @CalledByNative | |
| 40 private static MediaSession createMediaSession(Context context, long nativeM ediaSession) { | |
| 41 return new MediaSession(context, nativeMediaSession); | |
| 42 } | |
| 43 | |
| 44 @CalledByNative | |
| 45 private boolean requestAudioFocus(boolean transientFocus) { | |
| 46 mFocusType = transientFocus ? AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY _DUCK | |
| 47 : AudioManager.AUDIOFOCUS_GAIN; | |
|
Ted C
2015/05/27 00:59:08
java is 8 from the start of the previous line
mlamouri (slow - plz ping)
2015/05/28 16:20:40
Done.
| |
| 48 return requestAudioFocusInternal(); | |
| 49 } | |
| 50 | |
| 51 @CalledByNative | |
| 52 private void abandonAudioFocus() { | |
| 53 AudioManager am = (AudioManager) mContext.getSystemService(Context.AUDIO _SERVICE); | |
| 54 am.abandonAudioFocus(this); | |
| 55 } | |
| 56 | |
| 57 private boolean requestAudioFocusInternal() { | |
| 58 AudioManager am = (AudioManager) mContext.getSystemService(Context.AUDIO _SERVICE); | |
| 59 | |
| 60 int result = am.requestAudioFocus(this, AudioManager.STREAM_MUSIC, mFocu sType); | |
| 61 return result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED; | |
| 62 } | |
| 63 | |
| 64 @Override | |
| 65 public void onAudioFocusChange(int focusChange) { | |
| 66 switch (focusChange) { | |
| 67 case AudioManager.AUDIOFOCUS_GAIN: | |
| 68 if (requestAudioFocusInternal()) { | |
| 69 nativeOnResume(mNativeMediaSession); | |
| 70 } | |
| 71 break; | |
| 72 case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT: | |
| 73 case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK: | |
| 74 nativeOnSuspend(mNativeMediaSession); | |
| 75 break; | |
| 76 case AudioManager.AUDIOFOCUS_LOSS: | |
| 77 abandonAudioFocus(); | |
| 78 nativeOnSuspend(mNativeMediaSession); | |
| 79 break; | |
| 80 default: | |
| 81 Log.w(TAG, "onAudioFocusChange called with unexpected value " + focusChange); | |
| 82 break; | |
| 83 } | |
| 84 } | |
| 85 | |
| 86 private native void nativeOnSuspend(long nativeMediaSession); | |
| 87 private native void nativeOnResume(long nativeMediaSession); | |
| 88 } | |
| OLD | NEW |