Chromium Code Reviews| Index: content/public/android/java/src/org/chromium/content/browser/MediaSession.java |
| diff --git a/content/public/android/java/src/org/chromium/content/browser/MediaSession.java b/content/public/android/java/src/org/chromium/content/browser/MediaSession.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..bb199ae3d374babe2dc10f3ffc05a07d98bd43e6 |
| --- /dev/null |
| +++ b/content/public/android/java/src/org/chromium/content/browser/MediaSession.java |
| @@ -0,0 +1,75 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.content.browser; |
| + |
| +import android.content.Context; |
| +import android.media.AudioManager; |
| + |
| +import org.chromium.base.CalledByNative; |
| +import org.chromium.base.JNINamespace; |
| + |
| +/** |
| + * MediaSession is the Java counterpart of content::MediaSession. |
|
whywhat
2015/05/12 12:50:46
I believe more comment is due. E.g. explain the fa
mlamouri (slow - plz ping)
2015/05/19 21:56:17
Done.
|
| + */ |
| +@JNINamespace("content") |
| +public class MediaSession implements AudioManager.OnAudioFocusChangeListener { |
| + |
| + private Context mContext; |
| + private int mFocusType; |
| + |
| + // Native pointer to C++ content::MediaSession. |
| + private long mNativeMediaSession; |
|
whywhat
2015/05/12 12:50:46
make it final.
have you made it certain that the C
mlamouri (slow - plz ping)
2015/05/19 21:56:17
Yes, the java object is held as ScopedJavaGlobalRe
|
| + |
| + private MediaSession(final Context context, long nativeMediaSession) { |
| + mContext = context; |
| + mNativeMediaSession = nativeMediaSession; |
| + } |
| + |
| + @CalledByNative |
| + private static MediaSession createMediaSession(Context context, long nativeMediaSession) { |
| + return new MediaSession(context, nativeMediaSession); |
| + } |
| + |
| + @CalledByNative |
| + private boolean requestAudioFocus(boolean transientFocus) { |
| + mFocusType = transientFocus ? AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK |
| + : AudioManager.AUDIOFOCUS_GAIN; |
| + return requestAudioFocusInternal(); |
| + } |
| + |
| + @CalledByNative |
| + private void abandonAudioFocus() { |
| + AudioManager am = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE); |
| + am.abandonAudioFocus(this); |
| + } |
| + |
| + private boolean requestAudioFocusInternal() { |
| + AudioManager am = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE); |
| + |
| + int result = am.requestAudioFocus(this, AudioManager.STREAM_MUSIC, mFocusType); |
| + return result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED; |
| + } |
| + |
| + public void onAudioFocusChange(int focusChange) { |
|
whywhat
2015/05/12 12:50:46
@Override?
mlamouri (slow - plz ping)
2015/05/19 21:56:17
Done.
|
| + switch (focusChange) { |
| + case AudioManager.AUDIOFOCUS_GAIN: |
| + if (requestAudioFocusInternal()) { |
|
whywhat
2015/05/12 12:50:46
nit:
does it all fit in one line?
{} are not neede
mlamouri (slow - plz ping)
2015/05/19 21:56:17
Chromium is using Android coding style in Java. Wh
|
| + nativeOnResume(mNativeMediaSession); |
| + } |
| + break; |
| + case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT: |
| + case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK: |
| + nativeOnSuspend(mNativeMediaSession); |
| + break; |
| + case AudioManager.AUDIOFOCUS_LOSS: |
| + abandonAudioFocus(); |
| + nativeOnSuspend(mNativeMediaSession); |
| + break; |
| + } |
| + } |
| + |
| + private native void nativeOnSuspend(long nativeMediaSession); |
| + private native void nativeOnResume(long nativeMediaSession); |
| +} |