Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(860)

Unified Diff: content/public/android/java/src/org/chromium/content/browser/MediaSession.java

Issue 1110833004: Move audio focus control from media/ to content/ and make it per WebContents. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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);
+}

Powered by Google App Engine
This is Rietveld 408576698