Skip to content

Commit

Permalink
Fix possible NPE when showing volume or power sync dialogs
Browse files Browse the repository at this point in the history
If the corresponding preference for the synced player has not been fetched or does not exist.
  • Loading branch information
kaaholst committed Feb 5, 2024
1 parent 734f605 commit 92113d3
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.List;
import java.util.Map;

import uk.org.ngo.squeezer.Util;
import uk.org.ngo.squeezer.framework.ItemListActivity;
import uk.org.ngo.squeezer.itemlist.dialog.DefeatDestructiveTouchToPlayDialog;
import uk.org.ngo.squeezer.itemlist.dialog.PlayTrackAlbumDialog;
Expand Down Expand Up @@ -176,8 +177,8 @@ public void setDefeatDestructiveTTP(@NonNull String option) {
}

@Override
public String getSyncVolume() {
return currentSyncGroup.getItem(0).getPlayerState().prefs.get(Player.Pref.SYNC_VOLUME);
public int getSyncVolume() {
return getGroupPref(Player.Pref.SYNC_VOLUME);
}

@Override
Expand All @@ -188,8 +189,8 @@ public void setSyncVolume(@NonNull String option) {
}

@Override
public String getSyncPower() {
return currentSyncGroup.getItem(0).getPlayerState().prefs.get(Player.Pref.SYNC_POWER);
public int getSyncPower() {
return getGroupPref(Player.Pref.SYNC_POWER);
}

@Override
Expand All @@ -199,6 +200,14 @@ public void setSyncPower(@NonNull String option) {
}
}

private int getGroupPref(Player.Pref pref) {
for (int i = 0; i < currentSyncGroup.getItemCount(); i++) {
int prefValue = Util.getInt(currentSyncGroup.getItem(i).getPlayerState().prefs.get(pref), -1);
if (prefValue != -1) return prefValue;
}
return 0;
}

@Override
protected <T extends Item> void updateAdapter(int count, int start, List<T> items, Class<T> dataType) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class SyncPowerDialog extends BaseChoicesDialog {
*/
public interface SyncPowerDialogHost {
FragmentManager getSupportFragmentManager();
String getSyncPower();
int getSyncPower();
void setSyncPower(@NonNull String option);
}

Expand All @@ -53,7 +53,7 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {
return createDialog(
getString(R.string.SETUP_SYNCPOWER),
getString(R.string.SETUP_SYNCPOWER_DESC),
Integer.parseInt(host.getSyncPower()),
host.getSyncPower(),
options
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class SyncVolumeDialog extends BaseChoicesDialog {
*/
public interface SyncVolumeDialogHost {
FragmentManager getSupportFragmentManager();
String getSyncVolume();
int getSyncVolume();
void setSyncVolume(@NonNull String option);
}

Expand All @@ -53,7 +53,7 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {
return createDialog(
getString(R.string.SETUP_SYNCVOLUME),
getString(R.string.SETUP_SYNCVOLUME_DESC),
Integer.parseInt(host.getSyncVolume()),
host.getSyncVolume(),
options
);
}
Expand Down

0 comments on commit 92113d3

Please sign in to comment.