Replace if with switch

This commit is contained in:
Andrew Rabert 2018-04-24 19:58:40 -04:00
parent 4fed3bd3a3
commit bb9e3a506e
15 changed files with 354 additions and 250 deletions

View File

@ -173,15 +173,16 @@ public class EditPlayActionActivity extends SubsonicActivity {
@Override @Override
public boolean onOptionsItemSelected(MenuItem item) { public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) { switch (item.getItemId()) {
cancel(); case android.R.id.home:
return true; cancel();
} else if (item.getItemId() == R.id.menu_accept) { return true;
accept(); case R.id.menu_accept:
return true; accept();
} else if (item.getItemId() == R.id.menu_cancel) { return true;
cancel(); case R.id.menu_cancel:
return true; cancel();
return true;
} }
return false; return false;

View File

@ -538,12 +538,13 @@ public class SubsonicFragmentActivity extends SubsonicActivity implements Downlo
} }
private SubsonicFragment getNewFragment(String fragmentType) { private SubsonicFragment getNewFragment(String fragmentType) {
if ("Playlist".equals(fragmentType)) { switch (fragmentType) {
return new SelectPlaylistFragment(); case "Playlist":
} else if ("Download".equals(fragmentType)) { return new SelectPlaylistFragment();
return new DownloadFragment(); case "Download":
} else { return new DownloadFragment();
return new SelectArtistFragment(); default:
return new SelectArtistFragment();
} }
} }

View File

@ -78,12 +78,17 @@ public class SearchAdapter extends ExpandableSectionAdapter<Serializable> {
@Override @Override
public UpdateView.UpdateViewHolder onCreateSectionViewHolder(int viewType) { public UpdateView.UpdateViewHolder onCreateSectionViewHolder(int viewType) {
UpdateView updateView = null; UpdateView updateView = null;
if (viewType == VIEW_TYPE_ALBUM_CELL || viewType == VIEW_TYPE_ALBUM_LINE) { switch (viewType) {
updateView = new AlbumView(context, viewType == VIEW_TYPE_ALBUM_CELL); case VIEW_TYPE_ALBUM_CELL:
} else if (viewType == VIEW_TYPE_SONG) { case VIEW_TYPE_ALBUM_LINE:
updateView = new SongView(context); updateView = new AlbumView(context, viewType == VIEW_TYPE_ALBUM_CELL);
} else if (viewType == VIEW_TYPE_ARTIST) { break;
updateView = new ArtistView(context); case VIEW_TYPE_SONG:
updateView = new SongView(context);
break;
case VIEW_TYPE_ARTIST:
updateView = new ArtistView(context);
break;
} }
return new UpdateView.UpdateViewHolder(updateView); return new UpdateView.UpdateViewHolder(updateView);
@ -92,14 +97,19 @@ public class SearchAdapter extends ExpandableSectionAdapter<Serializable> {
@Override @Override
public void onBindViewHolder(UpdateView.UpdateViewHolder holder, Serializable item, int viewType) { public void onBindViewHolder(UpdateView.UpdateViewHolder holder, Serializable item, int viewType) {
UpdateView view = holder.getUpdateView(); UpdateView view = holder.getUpdateView();
if (viewType == VIEW_TYPE_ALBUM_CELL || viewType == VIEW_TYPE_ALBUM_LINE) { switch (viewType) {
AlbumView albumView = (AlbumView) view; case VIEW_TYPE_ALBUM_CELL:
albumView.setObject((Entry) item, imageLoader); case VIEW_TYPE_ALBUM_LINE:
} else if (viewType == VIEW_TYPE_SONG) { AlbumView albumView = (AlbumView) view;
SongView songView = (SongView) view; albumView.setObject((Entry) item, imageLoader);
songView.setObject((Entry) item, true); break;
} else if (viewType == VIEW_TYPE_ARTIST) { case VIEW_TYPE_SONG:
view.setObject(item); SongView songView = (SongView) view;
songView.setObject((Entry) item, true);
break;
case VIEW_TYPE_ARTIST:
view.setObject(item);
break;
} }
} }

View File

@ -75,53 +75,71 @@ public class MainFragment extends SelectRecyclerFragment<Integer> {
} }
private void showAlbumList(String type) { private void showAlbumList(String type) {
if ("genres".equals(type)) { switch (type) {
SubsonicFragment fragment = new SelectGenreFragment(); case "genres": {
replaceFragment(fragment); SubsonicFragment fragment = new SelectGenreFragment();
} else if ("years".equals(type)) { replaceFragment(fragment);
SubsonicFragment fragment = new SelectYearFragment(); break;
replaceFragment(fragment);
} else {
// Clear out recently added count when viewing
if ("newest".equals(type)) {
SharedPreferences.Editor editor = Util.getPreferences(context).edit();
editor.putInt(Constants.PREFERENCES_KEY_RECENT_COUNT + Util.getActiveServer(context), 0);
editor.apply();
} }
case "years": {
SubsonicFragment fragment = new SelectYearFragment();
replaceFragment(fragment);
break;
}
default: {
// Clear out recently added count when viewing
if ("newest".equals(type)) {
SharedPreferences.Editor editor = Util.getPreferences(context).edit();
editor.putInt(Constants.PREFERENCES_KEY_RECENT_COUNT + Util.getActiveServer(context), 0);
editor.apply();
}
SubsonicFragment fragment = new SelectDirectoryFragment(); SubsonicFragment fragment = new SelectDirectoryFragment();
Bundle args = new Bundle(); Bundle args = new Bundle();
args.putString(Constants.INTENT_EXTRA_NAME_ALBUM_LIST_TYPE, type); args.putString(Constants.INTENT_EXTRA_NAME_ALBUM_LIST_TYPE, type);
args.putInt(Constants.INTENT_EXTRA_NAME_ALBUM_LIST_SIZE, 20); args.putInt(Constants.INTENT_EXTRA_NAME_ALBUM_LIST_SIZE, 20);
args.putInt(Constants.INTENT_EXTRA_NAME_ALBUM_LIST_OFFSET, 0); args.putInt(Constants.INTENT_EXTRA_NAME_ALBUM_LIST_OFFSET, 0);
fragment.setArguments(args); fragment.setArguments(args);
replaceFragment(fragment); replaceFragment(fragment);
break;
}
} }
} }
@Override @Override
public void onItemClicked(UpdateView<Integer> updateView, Integer item) { public void onItemClicked(UpdateView<Integer> updateView, Integer item) {
if (item == R.string.main_albums_random) { switch (item) {
showAlbumList("random"); case R.string.main_albums_random:
} else if (item == R.string.main_albums_recent) { showAlbumList("random");
showAlbumList("recent"); break;
} else if (item == R.string.main_albums_frequent) { case R.string.main_albums_recent:
showAlbumList("frequent"); showAlbumList("recent");
} else if (item == R.string.main_albums_genres) { break;
showAlbumList("genres"); case R.string.main_albums_frequent:
} else if (item == R.string.main_albums_year) { showAlbumList("frequent");
showAlbumList("years"); break;
} else if (item == R.string.main_albums_alphabetical) { case R.string.main_albums_genres:
showAlbumList("alphabeticalByName"); showAlbumList("genres");
} else if (item == R.string.main_songs_newest) { break;
showAlbumList(SONGS_NEWEST); case R.string.main_albums_year:
} else if (item == R.string.main_songs_top_played) { showAlbumList("years");
showAlbumList(SONGS_TOP_PLAYED); break;
} else if (item == R.string.main_songs_recent) { case R.string.main_albums_alphabetical:
showAlbumList(SONGS_RECENT); showAlbumList("alphabeticalByName");
} else if (item == R.string.main_songs_frequent) { break;
showAlbumList(SONGS_FREQUENT); case R.string.main_songs_newest:
showAlbumList(SONGS_NEWEST);
break;
case R.string.main_songs_top_played:
showAlbumList(SONGS_TOP_PLAYED);
break;
case R.string.main_songs_recent:
showAlbumList(SONGS_RECENT);
break;
case R.string.main_songs_frequent:
showAlbumList(SONGS_FREQUENT);
break;
} }
} }

View File

@ -315,25 +315,37 @@ public class SelectDirectoryFragment extends SubsonicFragment implements Section
} }
private void getAlbumList(final String albumListType, final int size, final boolean refresh) { private void getAlbumList(final String albumListType, final int size, final boolean refresh) {
if ("random".equals(albumListType)) { switch (albumListType) {
setTitle(R.string.main_albums_random); case "random":
} else if ("recent".equals(albumListType)) { setTitle(R.string.main_albums_random);
setTitle(R.string.main_albums_recent); break;
} else if ("frequent".equals(albumListType)) { case "recent":
setTitle(R.string.main_albums_frequent); setTitle(R.string.main_albums_recent);
} else if ("genres".equals(albumListType) || "years".equals(albumListType)) { break;
setTitle(albumListExtra); case "frequent":
} else if ("alphabeticalByName".equals(albumListType)) { setTitle(R.string.main_albums_frequent);
setTitle(R.string.main_albums_alphabetical); break;
case "genres":
case "years":
setTitle(albumListExtra);
break;
case "alphabeticalByName":
setTitle(R.string.main_albums_alphabetical);
break;
} }
if (MainFragment.SONGS_NEWEST.equals(albumListType)) { switch (albumListType) {
setTitle(R.string.main_songs_newest); case MainFragment.SONGS_NEWEST:
} else if (MainFragment.SONGS_TOP_PLAYED.equals(albumListType)) { setTitle(R.string.main_songs_newest);
setTitle(R.string.main_songs_top_played); break;
} else if (MainFragment.SONGS_RECENT.equals(albumListType)) { case MainFragment.SONGS_TOP_PLAYED:
setTitle(R.string.main_songs_recent); setTitle(R.string.main_songs_top_played);
} else if (MainFragment.SONGS_FREQUENT.equals(albumListType)) { break;
setTitle(R.string.main_songs_frequent); case MainFragment.SONGS_RECENT:
setTitle(R.string.main_songs_recent);
break;
case MainFragment.SONGS_FREQUENT:
setTitle(R.string.main_songs_frequent);
break;
} }
new LoadTask() { new LoadTask() {

View File

@ -116,14 +116,19 @@ public class SettingsFragment extends PreferenceCompatFragment implements Shared
Bundle args = new Bundle(); Bundle args = new Bundle();
int xml = 0; int xml = 0;
if ("appearance".equals(name)) { switch (name) {
xml = R.xml.settings_appearance; case "appearance":
} else if ("cache".equals(name)) { xml = R.xml.settings_appearance;
xml = R.xml.settings_cache; break;
} else if ("playback".equals(name)) { case "cache":
xml = R.xml.settings_playback; xml = R.xml.settings_cache;
} else if ("servers".equals(name)) { break;
xml = R.xml.settings_servers; case "playback":
xml = R.xml.settings_playback;
break;
case "servers":
xml = R.xml.settings_servers;
break;
} }
if (xml != 0) { if (xml != 0) {
@ -142,28 +147,37 @@ public class SettingsFragment extends PreferenceCompatFragment implements Shared
update(); update();
if (Constants.PREFERENCES_KEY_HIDE_MEDIA.equals(key)) { switch (key) {
setHideMedia(sharedPreferences.getBoolean(key, true)); case Constants.PREFERENCES_KEY_HIDE_MEDIA:
} else if (Constants.PREFERENCES_KEY_MEDIA_BUTTONS.equals(key)) { setHideMedia(sharedPreferences.getBoolean(key, true));
setMediaButtonsEnabled(sharedPreferences.getBoolean(key, true)); break;
} else if (Constants.PREFERENCES_KEY_CACHE_LOCATION.equals(key)) { case Constants.PREFERENCES_KEY_MEDIA_BUTTONS:
setCacheLocation(sharedPreferences.getString(key, "")); setMediaButtonsEnabled(sharedPreferences.getBoolean(key, true));
} else if (Constants.PREFERENCES_KEY_SYNC_MOST_RECENT.equals(key)) { break;
SyncUtil.removeMostRecentSyncFiles(context); case Constants.PREFERENCES_KEY_CACHE_LOCATION:
} else if (Constants.PREFERENCES_KEY_REPLAY_GAIN.equals(key) || Constants.PREFERENCES_KEY_REPLAY_GAIN_BUMP.equals(key) || Constants.PREFERENCES_KEY_REPLAY_GAIN_UNTAGGED.equals(key)) { setCacheLocation(sharedPreferences.getString(key, ""));
DownloadService downloadService = DownloadService.getInstance(); break;
if (downloadService != null) { case Constants.PREFERENCES_KEY_SYNC_MOST_RECENT:
downloadService.reapplyVolume(); SyncUtil.removeMostRecentSyncFiles(context);
} break;
} else if (Constants.PREFERENCES_KEY_START_ON_HEADPHONES.equals(key)) { case Constants.PREFERENCES_KEY_REPLAY_GAIN:
Intent serviceIntent = new Intent(); case Constants.PREFERENCES_KEY_REPLAY_GAIN_BUMP:
serviceIntent.setClassName(context.getPackageName(), HeadphoneListenerService.class.getName()); case Constants.PREFERENCES_KEY_REPLAY_GAIN_UNTAGGED:
DownloadService downloadService = DownloadService.getInstance();
if (downloadService != null) {
downloadService.reapplyVolume();
}
break;
case Constants.PREFERENCES_KEY_START_ON_HEADPHONES:
Intent serviceIntent = new Intent();
serviceIntent.setClassName(context.getPackageName(), HeadphoneListenerService.class.getName());
if (sharedPreferences.getBoolean(key, false)) { if (sharedPreferences.getBoolean(key, false)) {
context.startService(serviceIntent); context.startService(serviceIntent);
} else { } else {
context.stopService(serviceIntent); context.stopService(serviceIntent);
} }
break;
} }
scheduleBackup(); scheduleBackup();

View File

@ -884,13 +884,19 @@ public class DownloadService extends Service {
* Plays or resumes the playback, depending on the current player state. * Plays or resumes the playback, depending on the current player state.
*/ */
public synchronized void togglePlayPause() { public synchronized void togglePlayPause() {
if (playerState == PAUSED || playerState == COMPLETED || playerState == STOPPED) { switch (playerState) {
start(); case PAUSED:
} else if (playerState == STOPPED || playerState == IDLE) { case COMPLETED:
autoPlayStart = true; case STOPPED:
play(); start();
} else if (playerState == STARTED) { break;
pause(); case IDLE:
autoPlayStart = true;
play();
break;
case STARTED:
pause();
break;
} }
} }

View File

@ -71,19 +71,26 @@ public class DownloadServiceLifecycleSupport {
eventHandler.post(() -> { eventHandler.post(() -> {
String action = intent.getAction(); String action = intent.getAction();
Log.i(TAG, "intentReceiver.onReceive: " + action); Log.i(TAG, "intentReceiver.onReceive: " + action);
if (DownloadService.CMD_PLAY.equals(action)) { switch (action) {
downloadService.play(); case DownloadService.CMD_PLAY:
} else if (DownloadService.CMD_NEXT.equals(action)) { downloadService.play();
downloadService.next(); break;
} else if (DownloadService.CMD_PREVIOUS.equals(action)) { case DownloadService.CMD_NEXT:
downloadService.previous(); downloadService.next();
} else if (DownloadService.CMD_TOGGLEPAUSE.equals(action)) { break;
downloadService.togglePlayPause(); case DownloadService.CMD_PREVIOUS:
} else if (DownloadService.CMD_PAUSE.equals(action)) { downloadService.previous();
downloadService.pause(); break;
} else if (DownloadService.CMD_STOP.equals(action)) { case DownloadService.CMD_TOGGLEPAUSE:
downloadService.pause(); downloadService.togglePlayPause();
downloadService.seekTo(0); break;
case DownloadService.CMD_PAUSE:
downloadService.pause();
break;
case DownloadService.CMD_STOP:
downloadService.pause();
downloadService.seekTo(0);
break;
} }
}); });
} }

View File

@ -44,17 +44,23 @@ public class EntryListParser extends MusicDirectoryEntryParser {
eventType = nextParseEvent(); eventType = nextParseEvent();
if (eventType == XmlPullParser.START_TAG) { if (eventType == XmlPullParser.START_TAG) {
String name = getElementName(); String name = getElementName();
if ("album".equals(name)) { switch (name) {
MusicDirectory.Entry entry = parseEntry(""); case "album": {
if (get("isDir") == null) { MusicDirectory.Entry entry = parseEntry("");
entry.setDirectory(true); if (get("isDir") == null) {
entry.setDirectory(true);
}
dir.addChild(entry);
break;
} }
dir.addChild(entry); case "song": {
} else if ("song".equals(name)) { MusicDirectory.Entry entry = parseEntry("");
MusicDirectory.Entry entry = parseEntry(""); dir.addChild(entry);
dir.addChild(entry); break;
} else if ("error".equals(name)) { }
handleError(); case "error":
handleError();
break;
} }
} }
} while (eventType != XmlPullParser.END_DOCUMENT); } while (eventType != XmlPullParser.END_DOCUMENT);

View File

@ -51,14 +51,18 @@ public class GenreParser extends AbstractParser {
eventType = nextParseEvent(); eventType = nextParseEvent();
if (eventType == XmlPullParser.START_TAG) { if (eventType == XmlPullParser.START_TAG) {
String name = getElementName(); String name = getElementName();
if ("genre".equals(name)) { switch (name) {
genre = new Genre(); case "genre":
genre.setSongCount(getInteger("songCount")); genre = new Genre();
genre.setAlbumCount(getInteger("albumCount")); genre.setSongCount(getInteger("songCount"));
} else if ("error".equals(name)) { genre.setAlbumCount(getInteger("albumCount"));
handleError(); break;
} else { case "error":
genre = null; handleError();
break;
default:
genre = null;
break;
} }
} else if (eventType == XmlPullParser.TEXT) { } else if (eventType == XmlPullParser.TEXT) {
if (genre != null) { if (genre != null) {

View File

@ -65,42 +65,50 @@ public class IndexesParser extends MusicDirectoryEntryParser {
eventType = nextParseEvent(); eventType = nextParseEvent();
if (eventType == XmlPullParser.START_TAG) { if (eventType == XmlPullParser.START_TAG) {
String name = getElementName(); String name = getElementName();
if ("indexes".equals(name) || "artists".equals(name)) { switch (name) {
changed = true; case "indexes":
ignoredArticles = get("ignoredArticles"); case "artists":
} else if ("index".equals(name)) { changed = true;
index = get("name"); ignoredArticles = get("ignoredArticles");
break;
case "index":
index = get("name");
} else if ("artist".equals(name)) { break;
Artist artist = new Artist(); case "artist":
artist.setId(get("id")); Artist artist = new Artist();
artist.setName(get("name")); artist.setId(get("id"));
artist.setIndex(index); artist.setName(get("name"));
artist.setIndex(index);
// Combine the id's for the two artists // Combine the id's for the two artists
if (artistList.containsKey(artist.getName())) { if (artistList.containsKey(artist.getName())) {
Artist originalArtist = artistList.get(artist.getName()); Artist originalArtist = artistList.get(artist.getName());
originalArtist.setId(originalArtist.getId() + ";" + artist.getId()); originalArtist.setId(originalArtist.getId() + ";" + artist.getId());
} else { } else {
artistList.put(artist.getName(), artist); artistList.put(artist.getName(), artist);
artists.add(artist); artists.add(artist);
} }
if (artists.size() % 10 == 0) { if (artists.size() % 10 == 0) {
String msg = getContext().getResources().getString(R.string.parser_artist_count, artists.size()); String msg = getContext().getResources().getString(R.string.parser_artist_count, artists.size());
updateProgress(progressListener, msg); updateProgress(progressListener, msg);
} }
} else if ("shortcut".equals(name)) { break;
Artist shortcut = new Artist(); case "shortcut":
shortcut.setId(get("id")); Artist shortcut = new Artist();
shortcut.setName(get("name")); shortcut.setId(get("id"));
shortcut.setIndex("*"); shortcut.setName(get("name"));
shortcuts.add(shortcut); shortcut.setIndex("*");
} else if ("child".equals(name)) { shortcuts.add(shortcut);
MusicDirectory.Entry entry = parseEntry(""); break;
entries.add(entry); case "child":
} else if ("error".equals(name)) { MusicDirectory.Entry entry = parseEntry("");
handleError(); entries.add(entry);
break;
case "error":
handleError();
break;
} }
} }
} while (eventType != XmlPullParser.END_DOCUMENT); } while (eventType != XmlPullParser.END_DOCUMENT);

View File

@ -44,13 +44,17 @@ public class PlaylistParser extends MusicDirectoryEntryParser {
eventType = nextParseEvent(); eventType = nextParseEvent();
if (eventType == XmlPullParser.START_TAG) { if (eventType == XmlPullParser.START_TAG) {
String name = getElementName(); String name = getElementName();
if ("entry".equals(name)) { switch (name) {
dir.addChild(parseEntry("")); case "entry":
} else if ("error".equals(name)) { dir.addChild(parseEntry(""));
handleError(); break;
} else if ("playlist".equals(name)) { case "error":
dir.setName(get("name")); handleError();
dir.setId(get("id")); break;
case "playlist":
dir.setName(get("name"));
dir.setId(get("id"));
break;
} }
} }
} while (eventType != XmlPullParser.END_DOCUMENT); } while (eventType != XmlPullParser.END_DOCUMENT);

View File

@ -50,19 +50,24 @@ public class SearchResult2Parser extends MusicDirectoryEntryParser {
eventType = nextParseEvent(); eventType = nextParseEvent();
if (eventType == XmlPullParser.START_TAG) { if (eventType == XmlPullParser.START_TAG) {
String name = getElementName(); String name = getElementName();
if ("artist".equals(name)) { switch (name) {
Artist artist = new Artist(); case "artist":
artist.setId(get("id")); Artist artist = new Artist();
artist.setName(get("name")); artist.setId(get("id"));
artists.add(artist); artist.setName(get("name"));
} else if ("album".equals(name)) { artists.add(artist);
MusicDirectory.Entry entry = parseEntry(""); break;
entry.setDirectory(true); case "album":
albums.add(entry); MusicDirectory.Entry entry = parseEntry("");
} else if ("song".equals(name)) { entry.setDirectory(true);
songs.add(parseEntry("")); albums.add(entry);
} else if ("error".equals(name)) { break;
handleError(); case "song":
songs.add(parseEntry(""));
break;
case "error":
handleError();
break;
} }
} }
} while (eventType != XmlPullParser.END_DOCUMENT); } while (eventType != XmlPullParser.END_DOCUMENT);

View File

@ -60,29 +60,32 @@ public final class ThemeUtil {
private static int getThemeRes(Context context, String theme) { private static int getThemeRes(Context context, String theme) {
if (context instanceof SubsonicFragmentActivity || context instanceof SettingsActivity) { if (context instanceof SubsonicFragmentActivity || context instanceof SettingsActivity) {
if (Util.getPreferences(context).getBoolean(Constants.PREFERENCES_KEY_COLOR_ACTION_BAR, true)) { if (Util.getPreferences(context).getBoolean(Constants.PREFERENCES_KEY_COLOR_ACTION_BAR, true)) {
if (THEME_DARK.equals(theme)) { switch (theme) {
return R.style.Theme_Audinaut_Dark_No_Actionbar; case THEME_DARK:
} else if (THEME_BLACK.equals(theme)) { return R.style.Theme_Audinaut_Dark_No_Actionbar;
return R.style.Theme_Audinaut_Black_No_Actionbar; case THEME_BLACK:
} else { return R.style.Theme_Audinaut_Black_No_Actionbar;
return R.style.Theme_Audinaut_Light_No_Actionbar; default:
return R.style.Theme_Audinaut_Light_No_Actionbar;
} }
} else { } else {
if (THEME_DARK.equals(theme)) { switch (theme) {
return R.style.Theme_Audinaut_Dark_No_Color; case THEME_DARK:
} else if (THEME_BLACK.equals(theme)) { return R.style.Theme_Audinaut_Dark_No_Color;
return R.style.Theme_Audinaut_Black_No_Color; case THEME_BLACK:
} else { return R.style.Theme_Audinaut_Black_No_Color;
return R.style.Theme_Audinaut_Light_No_Color; default:
return R.style.Theme_Audinaut_Light_No_Color;
} }
} }
} else { } else {
if (THEME_DARK.equals(theme)) { switch (theme) {
return R.style.Theme_Audinaut_Dark; case THEME_DARK:
} else if (THEME_BLACK.equals(theme)) { return R.style.Theme_Audinaut_Dark;
return R.style.Theme_Audinaut_Black; case THEME_BLACK:
} else { return R.style.Theme_Audinaut_Black;
return R.style.Theme_Audinaut_Light; default:
return R.style.Theme_Audinaut_Light;
} }
} }
} }

View File

@ -968,33 +968,38 @@ public final class Util {
audioManager.requestAudioFocus(focusListener = new OnAudioFocusChangeListener() { audioManager.requestAudioFocus(focusListener = new OnAudioFocusChangeListener() {
public void onAudioFocusChange(int focusChange) { public void onAudioFocusChange(int focusChange) {
DownloadService downloadService = (DownloadService) context; DownloadService downloadService = (DownloadService) context;
if ((focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT || focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK)) { switch (focusChange) {
if (downloadService.getPlayerState() == PlayerState.STARTED) { case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
Log.i(TAG, "Temporary loss of focus"); case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
SharedPreferences prefs = getPreferences(context); if (downloadService.getPlayerState() == PlayerState.STARTED) {
int lossPref = Integer.parseInt(prefs.getString(Constants.PREFERENCES_KEY_TEMP_LOSS, "1")); Log.i(TAG, "Temporary loss of focus");
if (lossPref == 2 || (lossPref == 1 && focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK)) { SharedPreferences prefs = getPreferences(context);
lowerFocus = true; int lossPref = Integer.parseInt(prefs.getString(Constants.PREFERENCES_KEY_TEMP_LOSS, "1"));
downloadService.setVolume(0.1f); if (lossPref == 2 || (lossPref == 1 && focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK)) {
} else if (lossPref == 0 || (lossPref == 1 && focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT)) { lowerFocus = true;
pauseFocus = true; downloadService.setVolume(0.1f);
downloadService.pause(true); } else if (lossPref == 0 || (lossPref == 1 && focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT)) {
pauseFocus = true;
downloadService.pause(true);
}
} }
} break;
} else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) { case AudioManager.AUDIOFOCUS_GAIN:
if (pauseFocus) { if (pauseFocus) {
pauseFocus = false; pauseFocus = false;
downloadService.start(); downloadService.start();
} }
if (lowerFocus) { if (lowerFocus) {
lowerFocus = false; lowerFocus = false;
downloadService.setVolume(1.0f); downloadService.setVolume(1.0f);
} }
} else if (focusChange == AudioManager.AUDIOFOCUS_LOSS) { break;
Log.i(TAG, "Permanently lost focus"); case AudioManager.AUDIOFOCUS_LOSS:
focusListener = null; Log.i(TAG, "Permanently lost focus");
downloadService.pause(); focusListener = null;
audioManager.abandonAudioFocus(this); downloadService.pause();
audioManager.abandonAudioFocus(this);
break;
} }
} }
}, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); }, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);