-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
77304e4
commit 45f3c6c
Showing
6 changed files
with
220 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 27 additions & 1 deletion
28
app/src/main/java/tech/torque/popper/utils/HighScoreHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,32 @@ | ||
package tech.torque.popper.utils; | ||
|
||
import android.content.Context; | ||
import android.content.SharedPreferences; | ||
|
||
public class HighScoreHelper { | ||
|
||
} | ||
private static final String PREFS_GLOBAL = "prefs_global"; | ||
private static final String PREF_TOP_SCORE = "pref_top_score"; | ||
|
||
private static SharedPreferences getPreferences(Context context) { | ||
return context.getSharedPreferences( | ||
PREFS_GLOBAL, Context.MODE_PRIVATE); | ||
} | ||
|
||
// Setters and getters for global preferences | ||
public static boolean isTopScore(Context context, int newScore) { | ||
int topScore = getPreferences(context).getInt(PREF_TOP_SCORE, 0); | ||
return newScore > topScore; | ||
} | ||
|
||
public static int getTopScore(Context context) { | ||
return getPreferences(context).getInt(PREF_TOP_SCORE, 0); | ||
} | ||
|
||
public static void setTopScore(Context context, int score) { | ||
SharedPreferences.Editor editor = getPreferences(context).edit(); | ||
editor.putInt(PREF_TOP_SCORE, score); | ||
editor.apply(); | ||
} | ||
|
||
} |
48 changes: 48 additions & 0 deletions
48
app/src/main/java/tech/torque/popper/utils/SimpleAlertDialog.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package tech.torque.popper.utils; | ||
|
||
import android.app.Dialog; | ||
import android.os.Bundle; | ||
import android.support.annotation.NonNull; | ||
import android.support.v4.app.DialogFragment; | ||
import android.support.v7.app.AlertDialog; | ||
|
||
public class SimpleAlertDialog extends DialogFragment { | ||
|
||
private static final String TITLE_KEY = "title_key"; | ||
private static final String MESSAGE_KEY = "message_key"; | ||
|
||
public SimpleAlertDialog() { | ||
} | ||
|
||
public static SimpleAlertDialog newInstance(String title, String message) { | ||
|
||
Bundle args = new Bundle(); | ||
args.putString(TITLE_KEY, title); | ||
args.putString(MESSAGE_KEY, message); | ||
|
||
SimpleAlertDialog fragment = new SimpleAlertDialog(); | ||
fragment.setArguments(args); | ||
return fragment; | ||
} | ||
|
||
@NonNull | ||
@Override | ||
public Dialog onCreateDialog(Bundle savedInstanceState) { | ||
|
||
Bundle args = getArguments(); | ||
if (args == null) throw new AssertionError(); | ||
|
||
String title = args.getString(TITLE_KEY); | ||
String prompt = args.getString(MESSAGE_KEY); | ||
|
||
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()) | ||
.setTitle(title) | ||
.setMessage(prompt) | ||
.setCancelable(false); | ||
|
||
builder.setPositiveButton(android.R.string.ok, null); | ||
return builder.create(); | ||
|
||
} | ||
|
||
} |
75 changes: 75 additions & 0 deletions
75
app/src/main/java/tech/torque/popper/utils/SoundHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package tech.torque.popper.utils; | ||
|
||
|
||
import android.app.Activity; | ||
import android.content.Context; | ||
import android.media.AudioAttributes; | ||
import android.media.AudioManager; | ||
import android.media.MediaPlayer; | ||
import android.media.SoundPool; | ||
import android.view.View; | ||
|
||
import tech.torque.popper.R; | ||
|
||
public class SoundHelper { | ||
|
||
private MediaPlayer mMusicPlayer; | ||
|
||
private SoundPool mSoundPool; | ||
private int mSoundID; | ||
private boolean mLoaded; | ||
private float mVolume; | ||
|
||
public SoundHelper(Activity activity) { | ||
|
||
AudioManager audioManager = (AudioManager) activity.getSystemService(Context.AUDIO_SERVICE); | ||
float actVolume = (float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC); | ||
float maxVolume = (float) audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC); | ||
mVolume = actVolume / maxVolume; | ||
|
||
activity.setVolumeControlStream(AudioManager.STREAM_MUSIC); | ||
|
||
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) { | ||
AudioAttributes audioAttrib = new AudioAttributes.Builder() | ||
.setUsage(AudioAttributes.USAGE_GAME) | ||
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) | ||
.build(); | ||
mSoundPool = new SoundPool.Builder().setAudioAttributes(audioAttrib).setMaxStreams(6).build(); | ||
} else { | ||
//noinspection deprecation | ||
mSoundPool = new SoundPool(6, AudioManager.STREAM_MUSIC, 0); | ||
} | ||
|
||
mSoundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() { | ||
@Override | ||
public void onLoadComplete(SoundPool soundPool, int sampleId, int status) { | ||
mLoaded = true; | ||
} | ||
}); | ||
mSoundID = mSoundPool.load(activity, R.raw.balloon_pop, 1); | ||
} | ||
|
||
public void playSound() { | ||
if (mLoaded) { | ||
mSoundPool.play(mSoundID, mVolume, mVolume, 1, 0, 1f); | ||
} | ||
} | ||
|
||
public void prepareMusicPlayer(Context context) { | ||
mMusicPlayer = MediaPlayer.create(context.getApplicationContext(), R.raw.pleasant_music); | ||
mMusicPlayer.setVolume(.5f, .5f); | ||
mMusicPlayer.setLooping(true); | ||
} | ||
|
||
public void playMusic() { | ||
if(mMusicPlayer != null) { | ||
mMusicPlayer.start(); | ||
} | ||
} | ||
|
||
public void pauseMusic() { | ||
if(mMusicPlayer != null && mMusicPlayer.isPlaying()) { | ||
mMusicPlayer.pause(); | ||
} | ||
} | ||
} |
Binary file not shown.
Binary file not shown.