1b30a3010f51cb3631011d84ac948496f044fc7f5Bruno Oliveira/* 2b30a3010f51cb3631011d84ac948496f044fc7f5Bruno Oliveira * Copyright (C) 2011 The Android Open Source Project 3b30a3010f51cb3631011d84ac948496f044fc7f5Bruno Oliveira * 4b30a3010f51cb3631011d84ac948496f044fc7f5Bruno Oliveira * Licensed under the Apache License, Version 2.0 (the "License"); 5b30a3010f51cb3631011d84ac948496f044fc7f5Bruno Oliveira * you may not use this file except in compliance with the License. 6b30a3010f51cb3631011d84ac948496f044fc7f5Bruno Oliveira * You may obtain a copy of the License at 7b30a3010f51cb3631011d84ac948496f044fc7f5Bruno Oliveira * 8b30a3010f51cb3631011d84ac948496f044fc7f5Bruno Oliveira * http://www.apache.org/licenses/LICENSE-2.0 9b30a3010f51cb3631011d84ac948496f044fc7f5Bruno Oliveira * 10b30a3010f51cb3631011d84ac948496f044fc7f5Bruno Oliveira * Unless required by applicable law or agreed to in writing, software 11b30a3010f51cb3631011d84ac948496f044fc7f5Bruno Oliveira * distributed under the License is distributed on an "AS IS" BASIS, 12b30a3010f51cb3631011d84ac948496f044fc7f5Bruno Oliveira * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13b30a3010f51cb3631011d84ac948496f044fc7f5Bruno Oliveira * See the License for the specific language governing permissions and 14b30a3010f51cb3631011d84ac948496f044fc7f5Bruno Oliveira * limitations under the License. 15b30a3010f51cb3631011d84ac948496f044fc7f5Bruno Oliveira */ 16b30a3010f51cb3631011d84ac948496f044fc7f5Bruno Oliveira 17b30a3010f51cb3631011d84ac948496f044fc7f5Bruno Oliveirapackage com.example.android.musicplayer; 18b30a3010f51cb3631011d84ac948496f044fc7f5Bruno Oliveira 19b30a3010f51cb3631011d84ac948496f044fc7f5Bruno Oliveiraimport android.content.BroadcastReceiver; 20b30a3010f51cb3631011d84ac948496f044fc7f5Bruno Oliveiraimport android.content.Context; 21b30a3010f51cb3631011d84ac948496f044fc7f5Bruno Oliveiraimport android.content.Intent; 225986e12034e38fd165d2f100c7e404bbf61b2649Roman Nurikimport android.util.Log; 235986e12034e38fd165d2f100c7e404bbf61b2649Roman Nurikimport android.view.KeyEvent; 24b30a3010f51cb3631011d84ac948496f044fc7f5Bruno Oliveiraimport android.widget.Toast; 25b30a3010f51cb3631011d84ac948496f044fc7f5Bruno Oliveira 26b30a3010f51cb3631011d84ac948496f044fc7f5Bruno Oliveira/** 27b30a3010f51cb3631011d84ac948496f044fc7f5Bruno Oliveira * Receives broadcasted intents. In particular, we are interested in the 285986e12034e38fd165d2f100c7e404bbf61b2649Roman Nurik * android.media.AUDIO_BECOMING_NOISY and android.intent.action.MEDIA_BUTTON intents, which is 295986e12034e38fd165d2f100c7e404bbf61b2649Roman Nurik * broadcast, for example, when the user disconnects the headphones. This class works because we are 305986e12034e38fd165d2f100c7e404bbf61b2649Roman Nurik * declaring it in a <receiver> tag in AndroidManifest.xml. 31b30a3010f51cb3631011d84ac948496f044fc7f5Bruno Oliveira */ 32b30a3010f51cb3631011d84ac948496f044fc7f5Bruno Oliveirapublic class MusicIntentReceiver extends BroadcastReceiver { 33b30a3010f51cb3631011d84ac948496f044fc7f5Bruno Oliveira @Override 345986e12034e38fd165d2f100c7e404bbf61b2649Roman Nurik public void onReceive(Context context, Intent intent) { 35b30a3010f51cb3631011d84ac948496f044fc7f5Bruno Oliveira if (intent.getAction().equals(android.media.AudioManager.ACTION_AUDIO_BECOMING_NOISY)) { 365986e12034e38fd165d2f100c7e404bbf61b2649Roman Nurik Toast.makeText(context, "Headphones disconnected.", Toast.LENGTH_SHORT).show(); 37b30a3010f51cb3631011d84ac948496f044fc7f5Bruno Oliveira 38b30a3010f51cb3631011d84ac948496f044fc7f5Bruno Oliveira // send an intent to our MusicService to telling it to pause the audio 395986e12034e38fd165d2f100c7e404bbf61b2649Roman Nurik context.startService(new Intent(MusicService.ACTION_PAUSE)); 405986e12034e38fd165d2f100c7e404bbf61b2649Roman Nurik 415986e12034e38fd165d2f100c7e404bbf61b2649Roman Nurik } else if (intent.getAction().equals(Intent.ACTION_MEDIA_BUTTON)) { 425986e12034e38fd165d2f100c7e404bbf61b2649Roman Nurik KeyEvent keyEvent = (KeyEvent) intent.getExtras().get(Intent.EXTRA_KEY_EVENT); 435986e12034e38fd165d2f100c7e404bbf61b2649Roman Nurik if (keyEvent.getAction() != KeyEvent.ACTION_DOWN) 445986e12034e38fd165d2f100c7e404bbf61b2649Roman Nurik return; 455986e12034e38fd165d2f100c7e404bbf61b2649Roman Nurik 465986e12034e38fd165d2f100c7e404bbf61b2649Roman Nurik switch (keyEvent.getKeyCode()) { 475986e12034e38fd165d2f100c7e404bbf61b2649Roman Nurik case KeyEvent.KEYCODE_HEADSETHOOK: 485986e12034e38fd165d2f100c7e404bbf61b2649Roman Nurik case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE: 495986e12034e38fd165d2f100c7e404bbf61b2649Roman Nurik context.startService(new Intent(MusicService.ACTION_TOGGLE_PLAYBACK)); 505986e12034e38fd165d2f100c7e404bbf61b2649Roman Nurik break; 515986e12034e38fd165d2f100c7e404bbf61b2649Roman Nurik case KeyEvent.KEYCODE_MEDIA_PLAY: 525986e12034e38fd165d2f100c7e404bbf61b2649Roman Nurik context.startService(new Intent(MusicService.ACTION_PLAY)); 535986e12034e38fd165d2f100c7e404bbf61b2649Roman Nurik break; 545986e12034e38fd165d2f100c7e404bbf61b2649Roman Nurik case KeyEvent.KEYCODE_MEDIA_PAUSE: 555986e12034e38fd165d2f100c7e404bbf61b2649Roman Nurik context.startService(new Intent(MusicService.ACTION_PAUSE)); 565986e12034e38fd165d2f100c7e404bbf61b2649Roman Nurik break; 575986e12034e38fd165d2f100c7e404bbf61b2649Roman Nurik case KeyEvent.KEYCODE_MEDIA_STOP: 585986e12034e38fd165d2f100c7e404bbf61b2649Roman Nurik context.startService(new Intent(MusicService.ACTION_STOP)); 595986e12034e38fd165d2f100c7e404bbf61b2649Roman Nurik break; 605986e12034e38fd165d2f100c7e404bbf61b2649Roman Nurik case KeyEvent.KEYCODE_MEDIA_NEXT: 615986e12034e38fd165d2f100c7e404bbf61b2649Roman Nurik context.startService(new Intent(MusicService.ACTION_SKIP)); 625986e12034e38fd165d2f100c7e404bbf61b2649Roman Nurik break; 635986e12034e38fd165d2f100c7e404bbf61b2649Roman Nurik case KeyEvent.KEYCODE_MEDIA_PREVIOUS: 645986e12034e38fd165d2f100c7e404bbf61b2649Roman Nurik // TODO: ensure that doing this in rapid succession actually plays the 655986e12034e38fd165d2f100c7e404bbf61b2649Roman Nurik // previous song 665986e12034e38fd165d2f100c7e404bbf61b2649Roman Nurik context.startService(new Intent(MusicService.ACTION_REWIND)); 675986e12034e38fd165d2f100c7e404bbf61b2649Roman Nurik break; 685986e12034e38fd165d2f100c7e404bbf61b2649Roman Nurik } 69b30a3010f51cb3631011d84ac948496f044fc7f5Bruno Oliveira } 70b30a3010f51cb3631011d84ac948496f044fc7f5Bruno Oliveira } 71b30a3010f51cb3631011d84ac948496f044fc7f5Bruno Oliveira} 72