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 | |
13 /** | |
14 * 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.
| |
15 */ | |
16 @JNINamespace("content") | |
17 public class MediaSession implements AudioManager.OnAudioFocusChangeListener { | |
18 | |
19 private Context mContext; | |
20 private int mFocusType; | |
21 | |
22 // Native pointer to C++ content::MediaSession. | |
23 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
| |
24 | |
25 private MediaSession(final Context context, long nativeMediaSession) { | |
26 mContext = context; | |
27 mNativeMediaSession = nativeMediaSession; | |
28 } | |
29 | |
30 @CalledByNative | |
31 private static MediaSession createMediaSession(Context context, long nativeM ediaSession) { | |
32 return new MediaSession(context, nativeMediaSession); | |
33 } | |
34 | |
35 @CalledByNative | |
36 private boolean requestAudioFocus(boolean transientFocus) { | |
37 mFocusType = transientFocus ? AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY _DUCK | |
38 : AudioManager.AUDIOFOCUS_GAIN; | |
39 return requestAudioFocusInternal(); | |
40 } | |
41 | |
42 @CalledByNative | |
43 private void abandonAudioFocus() { | |
44 AudioManager am = (AudioManager) mContext.getSystemService(Context.AUDIO _SERVICE); | |
45 am.abandonAudioFocus(this); | |
46 } | |
47 | |
48 private boolean requestAudioFocusInternal() { | |
49 AudioManager am = (AudioManager) mContext.getSystemService(Context.AUDIO _SERVICE); | |
50 | |
51 int result = am.requestAudioFocus(this, AudioManager.STREAM_MUSIC, mFocu sType); | |
52 return result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED; | |
53 } | |
54 | |
55 public void onAudioFocusChange(int focusChange) { | |
whywhat
2015/05/12 12:50:46
@Override?
mlamouri (slow - plz ping)
2015/05/19 21:56:17
Done.
| |
56 switch (focusChange) { | |
57 case AudioManager.AUDIOFOCUS_GAIN: | |
58 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
| |
59 nativeOnResume(mNativeMediaSession); | |
60 } | |
61 break; | |
62 case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT: | |
63 case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK: | |
64 nativeOnSuspend(mNativeMediaSession); | |
65 break; | |
66 case AudioManager.AUDIOFOCUS_LOSS: | |
67 abandonAudioFocus(); | |
68 nativeOnSuspend(mNativeMediaSession); | |
69 break; | |
70 } | |
71 } | |
72 | |
73 private native void nativeOnSuspend(long nativeMediaSession); | |
74 private native void nativeOnResume(long nativeMediaSession); | |
75 } | |
OLD | NEW |