• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 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 
17 package com.example.android.musicplayer;
18 
19 import android.content.Context;
20 import android.media.AudioManager;
21 
22 /**
23  * Convenience class to deal with audio focus. This class deals with everything related to audio
24  * focus: it can request and abandon focus, and will intercept focus change events and deliver
25  * them to a MusicFocusable interface (which, in our case, is implemented by {@link MusicService}).
26  *
27  * This class can only be used on SDK level 8 and above, since it uses API features that are not
28  * available on previous SDK's.
29  */
30 public class AudioFocusHelper implements AudioManager.OnAudioFocusChangeListener {
31     AudioManager mAM;
32     MusicFocusable mFocusable;
33 
AudioFocusHelper(Context ctx, MusicFocusable focusable)34     public AudioFocusHelper(Context ctx, MusicFocusable focusable) {
35         mAM = (AudioManager) ctx.getSystemService(Context.AUDIO_SERVICE);
36         mFocusable = focusable;
37     }
38 
39     /** Requests audio focus. Returns whether request was successful or not. */
requestFocus()40     public boolean requestFocus() {
41         return AudioManager.AUDIOFOCUS_REQUEST_GRANTED ==
42             mAM.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
43     }
44 
45     /** Abandons audio focus. Returns whether request was successful or not. */
abandonFocus()46     public boolean abandonFocus() {
47         return AudioManager.AUDIOFOCUS_REQUEST_GRANTED == mAM.abandonAudioFocus(this);
48     }
49 
50     /**
51      * Called by AudioManager on audio focus changes. We implement this by calling our
52      * MusicFocusable appropriately to relay the message.
53      */
onAudioFocusChange(int focusChange)54     public void onAudioFocusChange(int focusChange) {
55         if (mFocusable == null) return;
56         switch (focusChange) {
57             case AudioManager.AUDIOFOCUS_GAIN:
58                 mFocusable.onGainedAudioFocus();
59                 break;
60             case AudioManager.AUDIOFOCUS_LOSS:
61             case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
62                 mFocusable.onLostAudioFocus(false);
63                 break;
64             case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
65                 mFocusable.onLostAudioFocus(true);
66                 break;
67              default:
68         }
69     }
70 }
71