Skip to content

Commit

Permalink
Merge pull request #26 from pserwylo/fix-25-soundset-preferences
Browse files Browse the repository at this point in the history
Preferences don't use the directory prefix when refering to soundsets
  • Loading branch information
pserwylo authored May 14, 2021
2 parents a7576d8 + 44ac12c commit f27539d
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 26 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ android {
applicationId "com.nicobrailo.pianoli"
minSdkVersion 21
targetSdkVersion 28
versionCode 11
versionName "1.11"
versionCode 12
versionName "1.12"
}

buildTypes {
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/nicobrailo/pianoli/ConfigActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

public class ConfigActivity extends AppCompatActivity {

private static final String SOUNDSET_DIR_PREFIX = "soundset_";
public static final String SOUNDSET_DIR_PREFIX = "soundset_";

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -46,7 +46,7 @@ protected void onCreate(Bundle savedInstanceState) {
sound_set_list_view.setItemChecked(selected_index, true);

sound_set_list_view.setOnItemClickListener((parent, view1, position, id) -> {
final String selected_soundset = SOUNDSET_DIR_PREFIX + available_sound_sets.get(position);
final String selected_soundset = available_sound_sets.get(position);
Log.i("PianOli::Activity", "Selected " + selected_soundset);
Preferences.setSelectedSoundSet(this, selected_soundset);
});
Expand Down
42 changes: 21 additions & 21 deletions app/src/main/java/com/nicobrailo/pianoli/Piano.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,30 +128,30 @@ void selectSoundset(final Context context, String soundSetName) {
KeySoundIdx = new int[24];
final AssetManager am = context.getAssets();
try {
KeySoundIdx[0] = KeySound.load(am.openFd("sounds/" + soundSetName + "/n01.mp3"), 1);
KeySoundIdx[1] = KeySound.load(am.openFd("sounds/" + soundSetName + "/n02.mp3"), 1);
KeySoundIdx[2] = KeySound.load(am.openFd("sounds/" + soundSetName + "/n03.mp3"), 1);
KeySoundIdx[3] = KeySound.load(am.openFd("sounds/" + soundSetName + "/n04.mp3"), 1);
KeySoundIdx[4] = KeySound.load(am.openFd("sounds/" + soundSetName + "/n05.mp3"), 1);
KeySoundIdx[0] = KeySound.load(am.openFd("sounds/" + ConfigActivity.SOUNDSET_DIR_PREFIX + soundSetName + "/n01.mp3"), 1);
KeySoundIdx[1] = KeySound.load(am.openFd("sounds/" + ConfigActivity.SOUNDSET_DIR_PREFIX + soundSetName + "/n02.mp3"), 1);
KeySoundIdx[2] = KeySound.load(am.openFd("sounds/" + ConfigActivity.SOUNDSET_DIR_PREFIX + soundSetName + "/n03.mp3"), 1);
KeySoundIdx[3] = KeySound.load(am.openFd("sounds/" + ConfigActivity.SOUNDSET_DIR_PREFIX + soundSetName + "/n04.mp3"), 1);
KeySoundIdx[4] = KeySound.load(am.openFd("sounds/" + ConfigActivity.SOUNDSET_DIR_PREFIX + soundSetName + "/n05.mp3"), 1);
KeySoundIdx[5] = KeySound.load(context, R.raw.no_note, 1);
KeySoundIdx[6] = KeySound.load(am.openFd("sounds/" + soundSetName + "/n06.mp3"), 1);
KeySoundIdx[7] = KeySound.load(am.openFd("sounds/" + soundSetName + "/n07.mp3"), 1);
KeySoundIdx[8] = KeySound.load(am.openFd("sounds/" + soundSetName + "/n08.mp3"), 1);
KeySoundIdx[9] = KeySound.load(am.openFd("sounds/" + soundSetName + "/n09.mp3"), 1);
KeySoundIdx[10] = KeySound.load(am.openFd("sounds/" + soundSetName + "/n10.mp3"), 1);
KeySoundIdx[11] = KeySound.load(am.openFd("sounds/" + soundSetName + "/n11.mp3"), 1);
KeySoundIdx[12] = KeySound.load(am.openFd("sounds/" + soundSetName + "/n12.mp3"), 1);
KeySoundIdx[6] = KeySound.load(am.openFd("sounds/" + ConfigActivity.SOUNDSET_DIR_PREFIX + soundSetName + "/n06.mp3"), 1);
KeySoundIdx[7] = KeySound.load(am.openFd("sounds/" + ConfigActivity.SOUNDSET_DIR_PREFIX + soundSetName + "/n07.mp3"), 1);
KeySoundIdx[8] = KeySound.load(am.openFd("sounds/" + ConfigActivity.SOUNDSET_DIR_PREFIX + soundSetName + "/n08.mp3"), 1);
KeySoundIdx[9] = KeySound.load(am.openFd("sounds/" + ConfigActivity.SOUNDSET_DIR_PREFIX + soundSetName + "/n09.mp3"), 1);
KeySoundIdx[10] = KeySound.load(am.openFd("sounds/" + ConfigActivity.SOUNDSET_DIR_PREFIX + soundSetName + "/n10.mp3"), 1);
KeySoundIdx[11] = KeySound.load(am.openFd("sounds/" + ConfigActivity.SOUNDSET_DIR_PREFIX + soundSetName + "/n11.mp3"), 1);
KeySoundIdx[12] = KeySound.load(am.openFd("sounds/" + ConfigActivity.SOUNDSET_DIR_PREFIX + soundSetName + "/n12.mp3"), 1);
KeySoundIdx[13] = KeySound.load(context, R.raw.no_note, 1);
KeySoundIdx[14] = KeySound.load(am.openFd("sounds/" + soundSetName + "/n13.mp3"), 1);
KeySoundIdx[15] = KeySound.load(am.openFd("sounds/" + soundSetName + "/n14.mp3"), 1);
KeySoundIdx[16] = KeySound.load(am.openFd("sounds/" + soundSetName + "/n15.mp3"), 1);
KeySoundIdx[17] = KeySound.load(am.openFd("sounds/" + soundSetName + "/n16.mp3"), 1);
KeySoundIdx[18] = KeySound.load(am.openFd("sounds/" + soundSetName + "/n17.mp3"), 1);
KeySoundIdx[19] = KeySound.load(am.openFd("sounds/" + soundSetName + "/n18.mp3"), 1);
KeySoundIdx[14] = KeySound.load(am.openFd("sounds/" + ConfigActivity.SOUNDSET_DIR_PREFIX + soundSetName + "/n13.mp3"), 1);
KeySoundIdx[15] = KeySound.load(am.openFd("sounds/" + ConfigActivity.SOUNDSET_DIR_PREFIX + soundSetName + "/n14.mp3"), 1);
KeySoundIdx[16] = KeySound.load(am.openFd("sounds/" + ConfigActivity.SOUNDSET_DIR_PREFIX + soundSetName + "/n15.mp3"), 1);
KeySoundIdx[17] = KeySound.load(am.openFd("sounds/" + ConfigActivity.SOUNDSET_DIR_PREFIX + soundSetName + "/n16.mp3"), 1);
KeySoundIdx[18] = KeySound.load(am.openFd("sounds/" + ConfigActivity.SOUNDSET_DIR_PREFIX + soundSetName + "/n17.mp3"), 1);
KeySoundIdx[19] = KeySound.load(am.openFd("sounds/" + ConfigActivity.SOUNDSET_DIR_PREFIX + soundSetName + "/n18.mp3"), 1);
KeySoundIdx[20] = KeySound.load(context, R.raw.no_note, 1);
KeySoundIdx[21] = KeySound.load(am.openFd("sounds/" + soundSetName + "/n19.mp3"), 1);
KeySoundIdx[22] = KeySound.load(am.openFd("sounds/" + soundSetName + "/n20.mp3"), 1);
KeySoundIdx[23] = KeySound.load(am.openFd("sounds/" + soundSetName + "/n21.mp3"), 1);
KeySoundIdx[21] = KeySound.load(am.openFd("sounds/" + ConfigActivity.SOUNDSET_DIR_PREFIX + soundSetName + "/n19.mp3"), 1);
KeySoundIdx[22] = KeySound.load(am.openFd("sounds/" + ConfigActivity.SOUNDSET_DIR_PREFIX + soundSetName + "/n20.mp3"), 1);
KeySoundIdx[23] = KeySound.load(am.openFd("sounds/" + ConfigActivity.SOUNDSET_DIR_PREFIX + soundSetName + "/n21.mp3"), 1);
} catch (IOException e) {
Log.d("PianOli::Piano", "Failed to load sounds");
e.printStackTrace();
Expand Down
30 changes: 29 additions & 1 deletion app/src/main/java/com/nicobrailo/pianoli/Preferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,45 @@

import android.content.Context;
import android.preference.PreferenceManager;
import android.util.Log;

public class Preferences {

private static final String TAG = "Preferences";
private static final String DEFAULT_SOUNDSET = "piano";
private final static String PREF_SELECTED_SOUND_SET = "selectedSoundSet";

/**
* The sound set is the name of the folder in assets/sounds/soundset_[NAME]
* (note that the soundset_ prefix is stripped from the directory name before being recorded here).
*/
public static String selectedSoundSet(Context context) {
return PreferenceManager.getDefaultSharedPreferences(context).getString(PREF_SELECTED_SOUND_SET, DEFAULT_SOUNDSET);
final String soundsetName = PreferenceManager.getDefaultSharedPreferences(context).getString(PREF_SELECTED_SOUND_SET, DEFAULT_SOUNDSET);

// Should never return null, but the linter has picked up that getString() can strictly speaking
// return null if a null was saved into preferences in the past, so may as well be defensive here.
if (soundsetName == null) {
Log.w(TAG, "Hmm, we have a null soundset for some unknown reason. Defaulting to \"" + DEFAULT_SOUNDSET + "\".");
setSelectedSoundSet(context, DEFAULT_SOUNDSET);
return DEFAULT_SOUNDSET;
}

// When fixing issue #25, the preference was always prefixed with the directory name.
// This will not play any sound, so lets take the liberty of updating the preference to the correct
// format for them. This can be removed in the future if we like after most people will have migrated
// to the newer version.
if (soundsetName.startsWith(ConfigActivity.SOUNDSET_DIR_PREFIX)) {
String updatedSoundsetName = soundsetName.substring(ConfigActivity.SOUNDSET_DIR_PREFIX.length());
Log.i(TAG, "Migrating from existing soundset \"" + soundsetName + "\" to new format: \"" + updatedSoundsetName + "\"");
setSelectedSoundSet(context, updatedSoundsetName);
return updatedSoundsetName;
}

return soundsetName;
}

public static void setSelectedSoundSet(Context context, String soundSet) {
Log.d(TAG, "Selecting soundset \"" + soundSet + "\"");
PreferenceManager.getDefaultSharedPreferences(context)
.edit()
.putString(PREF_SELECTED_SOUND_SET, soundSet)
Expand Down
1 change: 1 addition & 0 deletions fastlane/metadata/android/en-US/changelogs/12.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix issue where no sound is played on first install, requiring users to visit the settings screen (thanks to yamatt for reporting).

0 comments on commit f27539d

Please sign in to comment.