diff --git a/app/build.gradle b/app/build.gradle index 6501d33..f74bbe0 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -22,6 +22,12 @@ android { dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.android.support:appcompat-v7:28.0.0' + // Required -- JUnit 4 framework + testImplementation 'junit:junit:4.12' + // Optional -- Robolectric environment + testImplementation 'androidx.test:core:1.2.0' + // Optional -- Mockito framework + testImplementation 'org.mockito:mockito-core:1.10.19' testImplementation 'junit:junit:4.12' } diff --git a/app/src/main/java/com/snatik/matches/common/Music.java b/app/src/main/java/com/snatik/matches/common/Music.java index b65ac5e..c889519 100644 --- a/app/src/main/java/com/snatik/matches/common/Music.java +++ b/app/src/main/java/com/snatik/matches/common/Music.java @@ -1,5 +1,6 @@ package com.snatik.matches.common; +import android.content.SharedPreferences; import android.media.MediaPlayer; import android.media.MediaPlayer.OnCompletionListener; @@ -8,6 +9,7 @@ public class Music { public static boolean OFF = false; + private static MediaPlayer mediaPlayer; public static void playCorrent() { if (!OFF) { @@ -26,7 +28,12 @@ public void onCompletion(MediaPlayer mp) { } public static void playBackgroundMusic() { - // TODO + mediaPlayer = MediaPlayer.create(Shared.context, R.raw.background_music); + if(isMusicOn()) { + mediaPlayer.start(); + mediaPlayer.setLooping(true); + } + OFF = false; } public static void showStar() { @@ -44,4 +51,16 @@ public void onCompletion(MediaPlayer mp) { mp.start(); } } + + public static void stopBackgroundMusic(){ + + if(mediaPlayer.isPlaying()) { + mediaPlayer.stop(); + OFF = true; + } + } + + public static boolean isMusicOn(){ + return !OFF; + } } diff --git a/app/src/main/java/com/snatik/matches/fragments/DifficultySelectFragment.java b/app/src/main/java/com/snatik/matches/fragments/DifficultySelectFragment.java index 954c8a4..1717257 100644 --- a/app/src/main/java/com/snatik/matches/fragments/DifficultySelectFragment.java +++ b/app/src/main/java/com/snatik/matches/fragments/DifficultySelectFragment.java @@ -7,6 +7,7 @@ import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.Gravity; +import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -15,12 +16,13 @@ import com.snatik.matches.R; import com.snatik.matches.common.Memory; +import com.snatik.matches.common.Music; import com.snatik.matches.common.Shared; import com.snatik.matches.events.ui.DifficultySelectedEvent; import com.snatik.matches.themes.Theme; import com.snatik.matches.ui.DifficultyView; -public class DifficultySelectFragment extends Fragment { +public class DifficultySelectFragment extends Fragment implements View.OnKeyListener { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { @@ -90,6 +92,30 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa } + @Override + public void onDestroy() { + super.onDestroy(); + } + + @Override + public boolean onKey(View v, int keyCode, KeyEvent event) { + + this.getView().setFocusableInTouchMode(true); + this.getView().requestFocus(); + this.getView().setOnKeyListener(new View.OnKeyListener() { + @Override + public boolean onKey(View v, int keyCode, KeyEvent event) { + if( keyCode == KeyEvent.KEYCODE_BACK ) + { + Music.stopBackgroundMusic(); + return true; + } + return false; + } + }); + return false; + } + private String getBestTimeForStage(int theme, int difficulty) { int bestTime = Memory.getBestTime(theme, difficulty); if (bestTime != -1) { diff --git a/app/src/main/java/com/snatik/matches/fragments/GameFragment.java b/app/src/main/java/com/snatik/matches/fragments/GameFragment.java index db65f04..9c6bc87 100644 --- a/app/src/main/java/com/snatik/matches/fragments/GameFragment.java +++ b/app/src/main/java/com/snatik/matches/fragments/GameFragment.java @@ -1,6 +1,9 @@ package com.snatik.matches.fragments; +import android.content.DialogInterface; +import android.media.MediaPlayer; import android.os.Bundle; +import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -10,6 +13,7 @@ import android.widget.TextView; import com.snatik.matches.R; +import com.snatik.matches.common.Music; import com.snatik.matches.common.Shared; import com.snatik.matches.events.engine.FlipDownCardsEvent; import com.snatik.matches.events.engine.GameWonEvent; @@ -22,7 +26,7 @@ import com.snatik.matches.utils.FontLoader; import com.snatik.matches.utils.FontLoader.Font; -public class GameFragment extends BaseFragment { +public class GameFragment extends BaseFragment{ private BoardView mBoardView; private TextView mTime; @@ -47,7 +51,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa Shared.eventBus.listen(FlipDownCardsEvent.TYPE, this); Shared.eventBus.listen(HidePairCardsEvent.TYPE, this); Shared.eventBus.listen(GameWonEvent.TYPE, this); - + return view; } diff --git a/app/src/main/java/com/snatik/matches/fragments/MenuFragment.java b/app/src/main/java/com/snatik/matches/fragments/MenuFragment.java index 620b502..8e2b854 100644 --- a/app/src/main/java/com/snatik/matches/fragments/MenuFragment.java +++ b/app/src/main/java/com/snatik/matches/fragments/MenuFragment.java @@ -5,6 +5,7 @@ import android.animation.AnimatorSet; import android.animation.ObjectAnimator; import android.animation.ValueAnimator; +import android.media.MediaPlayer; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; @@ -72,11 +73,10 @@ public void onAnimationEnd(Animator animation) { startLightsAnimation(); startTootipAnimation(); - // play background music - Music.playBackgroundMusic(); return view; } + protected void animateAllAssetsOff(AnimatorListenerAdapter adapter) { // title // 120dp + 50dp + buffer(30dp) diff --git a/app/src/main/java/com/snatik/matches/fragments/ThemeSelectFragment.java b/app/src/main/java/com/snatik/matches/fragments/ThemeSelectFragment.java index e8617be..1a31b3f 100644 --- a/app/src/main/java/com/snatik/matches/fragments/ThemeSelectFragment.java +++ b/app/src/main/java/com/snatik/matches/fragments/ThemeSelectFragment.java @@ -12,6 +12,7 @@ import com.snatik.matches.R; import com.snatik.matches.common.Memory; +import com.snatik.matches.common.Music; import com.snatik.matches.common.Shared; import com.snatik.matches.events.ui.ThemeSelectedEvent; import com.snatik.matches.themes.Theme; @@ -66,6 +67,11 @@ public void onClick(View v) { return view; } + @Override + public void onDestroy() { + super.onDestroy(); + } + private void animateShow(View view) { ObjectAnimator animatorScaleX = ObjectAnimator.ofFloat(view, "scaleX", 0.5f, 1f); ObjectAnimator animatorScaleY = ObjectAnimator.ofFloat(view, "scaleY", 0.5f, 1f); diff --git a/app/src/main/java/com/snatik/matches/model/Game.java b/app/src/main/java/com/snatik/matches/model/Game.java index ca7084a..2109695 100644 --- a/app/src/main/java/com/snatik/matches/model/Game.java +++ b/app/src/main/java/com/snatik/matches/model/Game.java @@ -9,7 +9,7 @@ */ public class Game { - /** + /** * The board configuration */ public BoardConfiguration boardConfiguration; diff --git a/app/src/main/java/com/snatik/matches/ui/BoardView.java b/app/src/main/java/com/snatik/matches/ui/BoardView.java index 0e2ff81..a2bc9fb 100644 --- a/app/src/main/java/com/snatik/matches/ui/BoardView.java +++ b/app/src/main/java/com/snatik/matches/ui/BoardView.java @@ -11,6 +11,7 @@ import android.animation.ObjectAnimator; import android.content.Context; import android.graphics.Bitmap; +import android.media.MediaPlayer; import android.os.AsyncTask; import android.util.AttributeSet; import android.view.Gravity; @@ -40,6 +41,7 @@ public class BoardView extends LinearLayout { private List flippedUp = new ArrayList(); private boolean mLocked = false; private int mSize; + private MediaPlayer mediaPlayer; public BoardView(Context context) { this(context, null); @@ -142,6 +144,8 @@ protected void onPostExecute(Bitmap result) { public void onClick(View v) { if (!mLocked && tileView.isFlippedDown()) { tileView.flipUp(); + mediaPlayer = MediaPlayer.create(Shared.context, R.raw.click_on); + mediaPlayer.start(); flippedUp.add(id); if (flippedUp.size() == 2) { mLocked = true; diff --git a/app/src/main/java/com/snatik/matches/ui/DifficultyView.java b/app/src/main/java/com/snatik/matches/ui/DifficultyView.java index b74094e..8eb89a3 100644 --- a/app/src/main/java/com/snatik/matches/ui/DifficultyView.java +++ b/app/src/main/java/com/snatik/matches/ui/DifficultyView.java @@ -31,5 +31,7 @@ public void setDifficulty(int difficulty, int stars) { int drawableResourceId = Shared.context.getResources().getIdentifier(titleResource, "drawable", Shared.context.getPackageName()); mTitle.setImageResource(drawableResourceId); } + + } diff --git a/app/src/main/java/com/snatik/matches/ui/PopupSettingsView.java b/app/src/main/java/com/snatik/matches/ui/PopupSettingsView.java index 02d3262..ace4432 100644 --- a/app/src/main/java/com/snatik/matches/ui/PopupSettingsView.java +++ b/app/src/main/java/com/snatik/matches/ui/PopupSettingsView.java @@ -1,9 +1,12 @@ package com.snatik.matches.ui; +import android.app.Activity; import android.content.Context; import android.content.Intent; +import android.media.MediaPlayer; import android.net.Uri; import android.util.AttributeSet; +import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.View; import android.widget.ImageView; @@ -16,7 +19,7 @@ import com.snatik.matches.utils.FontLoader; import com.snatik.matches.utils.FontLoader.Font; -public class PopupSettingsView extends LinearLayout { +public class PopupSettingsView extends LinearLayout{ private ImageView mSoundImage; private TextView mSoundText; @@ -27,19 +30,22 @@ public PopupSettingsView(Context context) { public PopupSettingsView(Context context, AttributeSet attrs) { super(context, attrs); + if(Music.OFF) Music.stopBackgroundMusic(); setOrientation(LinearLayout.VERTICAL); setBackgroundResource(R.drawable.settings_popup); LayoutInflater.from(getContext()).inflate(R.layout.popup_settings_view, this, true); mSoundText = (TextView) findViewById(R.id.sound_off_text); + mSoundText.setText("Sound OFF"); TextView rateView = (TextView) findViewById(R.id.rate_text); FontLoader.setTypeface(context, new TextView[] { mSoundText, rateView }, Font.GROBOLD); mSoundImage = (ImageView) findViewById(R.id.sound_image); + mSoundImage.setImageResource(R.drawable.button_music_off); View soundOff = findViewById(R.id.sound_off); soundOff.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - Music.OFF = !Music.OFF; setMusicButton(); + Music.OFF = !Music.OFF; } }); View rate = findViewById(R.id.rate); @@ -54,16 +60,18 @@ public void onClick(View v) { } } }); - setMusicButton(); + } private void setMusicButton() { if (Music.OFF) { mSoundText.setText("Sound OFF"); mSoundImage.setImageResource(R.drawable.button_music_off); + Music.stopBackgroundMusic(); } else { mSoundText.setText("Sound ON"); mSoundImage.setImageResource(R.drawable.button_music_on); + Music.playBackgroundMusic(); } } } diff --git a/app/src/main/res/raw/click_on.mp3 b/app/src/main/res/raw/click_on.mp3 new file mode 100644 index 0000000..eb4ec23 Binary files /dev/null and b/app/src/main/res/raw/click_on.mp3 differ diff --git a/app/src/test/java/com/snatik/matches/ExampleUnitTest.java b/app/src/test/java/com/snatik/matches/ExampleUnitTest.java deleted file mode 100644 index 910ddee..0000000 --- a/app/src/test/java/com/snatik/matches/ExampleUnitTest.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.snatik.matches; - -import org.junit.Test; - -import static org.junit.Assert.*; - -/** - * To work on unit tests, switch the Test Artifact in the Build Variants view. - */ -public class ExampleUnitTest { - @Test - public void addition_isCorrect() throws Exception { - assertEquals(4, 2 + 2); - } -} \ No newline at end of file diff --git a/app/src/test/java/com/snatik/matches/common/MusicTest.java b/app/src/test/java/com/snatik/matches/common/MusicTest.java new file mode 100644 index 0000000..2293e16 --- /dev/null +++ b/app/src/test/java/com/snatik/matches/common/MusicTest.java @@ -0,0 +1,32 @@ +package com.snatik.matches.common; + +import android.media.MediaPlayer; + +import com.snatik.matches.R; + +import org.junit.Test; + +import static org.junit.Assert.*; + +public class MusicTest { + + @Test + public void testPlayBackgroundMusic(){ + assertTrue(Music.isMusicOn()); + } + + @Test + public void testStopBackgroundMusic() { + Music.OFF = true; + assertFalse(Music.isMusicOn()); + } + + @Test + public void testMusicOn() { + Music.OFF = true; + assertFalse(Music.isMusicOn()); + + Music.OFF = false; + assertTrue(Music.isMusicOn()); + } +} \ No newline at end of file diff --git a/app/src/test/java/com/snatik/matches/model/BoardConfigurationTest.java b/app/src/test/java/com/snatik/matches/model/BoardConfigurationTest.java new file mode 100644 index 0000000..3309947 --- /dev/null +++ b/app/src/test/java/com/snatik/matches/model/BoardConfigurationTest.java @@ -0,0 +1,34 @@ +package com.snatik.matches.model; + +import org.junit.Test; + +import static org.junit.Assert.*; + +public class BoardConfigurationTest { + + BoardConfiguration boardConfiguration = new BoardConfiguration(5); + + @Test + public void testNumTiles() { + int numTiles = boardConfiguration.getNumTiles(); + assertEquals(numTiles, 32); + } + + @Test + public void testNumTilesInRow() { + int numTilesInRow = boardConfiguration.getNumTilesInRow(); + assertEquals(numTilesInRow, 8); + } + + @Test + public void testNumRows() { + int numRows = boardConfiguration.getNumRows(); + assertEquals(numRows, 4); + } + + @Test + public void testTime() { + int time = boardConfiguration.getTime(); + assertEquals(time, 180); + } +} \ No newline at end of file