Skip to content

Commit

Permalink
#11 Added hold play mode
Browse files Browse the repository at this point in the history
  • Loading branch information
zackarhino committed Dec 11, 2020
1 parent 846e37a commit e763b99
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 14 deletions.
7 changes: 7 additions & 0 deletions .idea/dictionaries/zacka.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -115,29 +115,27 @@ public boolean onOptionsItemSelected(@NonNull MenuItem item) {
editor.putBoolean(getString(R.string.key_show_button), false); // Hiding the button
editor.apply();
setFabVisibility(false);
Toast.makeText(this, "Play mode: " + sharedPreferences.getInt(getString(R.string.key_play_mode), -1), Toast.LENGTH_SHORT).show();
break;
case R.id.action_play_mode_2:
// Play mode 2: One Shot
editor.putInt(getString(R.string.key_play_mode), ToneFragment.PLAY_MODES.ONE_SHOT);
editor.putBoolean(getString(R.string.key_show_button), true); // Showing the button
editor.apply();
ToneFragment.setFabListener(ToneFragment.PLAY_MODES.ONE_SHOT);
setFabVisibility(true);
Toast.makeText(this, "Play mode: " + sharedPreferences.getInt(getString(R.string.key_play_mode), -1), Toast.LENGTH_SHORT).show();
break;
case R.id.action_play_mode_3:
// Play mode 3: Hold
editor.putInt(getString(R.string.key_play_mode), ToneFragment.PLAY_MODES.HOLD);
editor.putBoolean(getString(R.string.key_show_button), true); // Showing the button
editor.apply();
Toast.makeText(this, "Play mode: " + sharedPreferences.getInt(getString(R.string.key_play_mode), -1), Toast.LENGTH_SHORT).show();
ToneFragment.setFabListener(ToneFragment.PLAY_MODES.HOLD);
break;
case R.id.action_party_mode:
// Flip the boolean
editor.putBoolean(getString(R.string.key_party_mode), !sharedPreferences.getBoolean(getString(R.string.key_party_mode), false));
editor.apply();
setFabVisibility(true);
Toast.makeText(this, "Party mode: " + sharedPreferences.getBoolean(getString(R.string.key_party_mode), false), Toast.LENGTH_SHORT).show();
break;
default:
Log.d("Menu", "Unrecognized menu item");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@
import android.content.res.ColorStateList;
import android.content.res.Configuration;
import android.os.Bundle;

import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

import android.os.Debug;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
Expand All @@ -35,7 +33,7 @@ public class ToneFragment extends Fragment {

SeekBar volume;
SeekBar pitch;
float frequency = 440;
static float frequency = 440;

public static final float MIN_FREQ = 110;
public static final float MAX_FREQ = 880;
Expand Down Expand Up @@ -81,7 +79,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
volume = view.findViewById(R.id.volume);
pitch = view.findViewById(R.id.pitch);

// Listeners
// Setting listeners
MainActivity.fab.setOnClickListener(new OneShotListener());
volume.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
Expand All @@ -107,7 +105,7 @@ public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if(frequency <= MIN_FREQ){
frequency = MIN_FREQ;
}

// Restart the tone if play_mode is continuous
if(getActivity().getPreferences(MODE_PRIVATE).getInt(getString(R.string.key_play_mode), -1) == PLAY_MODES.CONTINUOUS){
startTone(frequency);
}
Expand Down Expand Up @@ -136,11 +134,9 @@ public void onStopTrackingTouch(SeekBar seekBar) {}
* This allows regular buttons to function similarly to RadioButtons
*/
class ButtonSelectorListener implements View.OnClickListener{

@SuppressLint("NonConstantResourceId")
@Override
public void onClick(View v) {

switch (v.getId()){
case R.id.shapeSine:
setSelectedButton(sineButton);
Expand All @@ -165,13 +161,51 @@ public void onClick(View v) {
}
}

class OneShotListener implements View.OnClickListener{
/**
* One Shot play mode listener for fab
* @author Zachary Allard
*/
static class OneShotListener implements View.OnClickListener{
@Override
public void onClick(View v) {
playTone(frequency, 1000);
}
}

/**
* Hold play mode listener for fab
* @author Zachary Allard
*/
static class HoldListener implements View.OnTouchListener {
@Override
public boolean onTouch(View v, MotionEvent event) {
v.performClick();
if(event.getAction() == MotionEvent.ACTION_DOWN){
Log.d("Hold", "Touch down event");
startTone(frequency);
}else if(event.getAction() == MotionEvent.ACTION_UP) {
Log.d("Hold", "Touch up event");
stopTone();
}
return true;
}
}

// Methods
@SuppressLint("ClickableViewAccessibility")
public static void setFabListener(int listenerType){
// Unsetting any listeners
MainActivity.fab.setOnClickListener(null);
MainActivity.fab.setOnTouchListener(null);
switch (listenerType){
case PLAY_MODES.ONE_SHOT:
MainActivity.fab.setOnClickListener(new OneShotListener());
break;
case PLAY_MODES.HOLD:
MainActivity.fab.setOnTouchListener(new HoldListener());
break;
}
}
/**
* Method to set the selected button to cut down on duplicate code
* @param button The button to selected
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values/themes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<!-- Primary brand color. -->
<item name="colorPrimary">@color/licc_blue</item>
<item name="colorPrimaryVariant">@color/licc_dark_blue</item>
<item name="colorOnPrimary">#ff33b5e5</item>
<item name="colorOnPrimary">@color/black</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/eggshell</item>
<item name="colorSecondaryVariant">@color/eggshell</item>
Expand Down

0 comments on commit e763b99

Please sign in to comment.