Skip to content

Commit

Permalink
defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
arpruss committed Sep 7, 2019
1 parent 2db23b4 commit efcec54
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 116 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
applicationId "omegacentauri.mobi.simplestopwatch"
minSdkVersion 10
targetSdkVersion 28
versionCode 7
versionName "1.07"
versionCode 9
versionName "1.09"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
157 changes: 82 additions & 75 deletions app/src/main/java/omegacentauri/mobi/simplestopwatch/MyChrono.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.RectF;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.media.ToneGenerator;
import android.media.audiofx.LoudnessEnhancer;
import android.os.Build;
Expand Down Expand Up @@ -35,7 +37,6 @@

public class MyChrono {
private final Activity context;
private ToneGenerator toneG = null;
private String currentLapViewText;
BigTextView mainView;
TextView fractionView;
Expand All @@ -58,11 +59,16 @@ public class MyChrono {
public int precision = 100;
private TextToSpeech tts = null;
private boolean ttsMode;
int STREAM = AudioManager.STREAM_ALARM;;
private boolean boostAudio = false;
private static final int GAIN = 2000;
HashMap<String,String> ttsParams = new HashMap<String,String>();
private LoudnessEnhancer loudnessEnhancer = null;
private LoudnessEnhancer loudnessEnhancer2 = null;
private static final long SHORT_TONE_LENGTH = 75;
private static final long LONG_TONE_LENGTH = 600;
private static final float TONE_FREQUENCY = 2000;
private AudioTrack shortTone;
private AudioTrack longTone;

@SuppressLint("NewApi")
public MyChrono(Activity context, SharedPreferences options, BigTextView mainView, TextView fractionView, TextView lapView) {
Expand All @@ -77,10 +83,9 @@ public MyChrono(Activity context, SharedPreferences options, BigTextView mainVie
this.lapView.setText("");
this.currentLapViewText = "";
this.lapView.setMovementMethod(new ScrollingMovementMethod());
toneG = new ToneGenerator(AudioManager.STREAM_ALARM, 100);
tts = null;

Log.v("chrono", "maxSize " +this.maxSize);
StopWatch.debug("maxSize " +this.maxSize);

updateHandler = new Handler() {
public void handleMessage(Message m) {
Expand All @@ -94,7 +99,7 @@ public long getTime() {
}

private void announce(long t) {
if (quiet)
if (quiet || !active || paused)
return;
if (t < -3000 || t >= 1000) {
lastAnnounced = floorDiv(t, 1000)*1000;
Expand All @@ -111,19 +116,20 @@ else if (-2000 <= t) {
else {
msg = "3";
}
Log.v("chrono", "say: "+msg);
StopWatch.debug("say: "+msg);
tts.speak(msg,TextToSpeech.QUEUE_FLUSH, ttsParams);
}
else {
if (toneG != null)
toneG.startTone(ToneGenerator.TONE_DTMF_S, 50);
shortTone.stop();
shortTone.reloadStaticData();
shortTone.play();
}
lastAnnounced = floorDiv(t, 1000)*1000;
}
else if (t >= 0) {
if (toneG != null) {
toneG.startTone(ToneGenerator.TONE_DTMF_S, 600);
}
longTone.stop();
longTone.reloadStaticData();
longTone.play();
lastAnnounced = 0;
}
}
Expand Down Expand Up @@ -279,6 +285,15 @@ else if (active) {
updateViews();
}

private short[] sinewave(float frequency, long duration) {
int numSamples = (int)(44.100 * duration);
double alpha = frequency / 44100 * 2 * Math.PI;
short[] samples = new short[numSamples];
for (int i = 0 ; i < numSamples ; i++)
samples[i] = (short) (32767. * Math.sin(alpha * i));
return samples;
}

public void firstButton() {
if (active && paused) {
baseTime += SystemClock.elapsedRealtime() - pauseTime;
Expand Down Expand Up @@ -308,70 +323,63 @@ else if (!active) {
}

public void setAudio(String soundMode) {
boostAudio = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && options.getBoolean(Options.PREF_BOOST, false);
STREAM = Options.getStream(options);

if (soundMode.equals("none")) {
quiet = true;
ttsMode = false;
return;
}

quiet = false;

short[] tone = sinewave(TONE_FREQUENCY, LONG_TONE_LENGTH);
longTone = new AudioTrack(STREAM, 44100, AudioFormat.CHANNEL_OUT_MONO,
AudioFormat.ENCODING_PCM_16BIT, tone.length * 2, AudioTrack.MODE_STATIC);
longTone.write(tone, 0, tone.length);
int sessionId = longTone.getAudioSessionId();
int shortLength = Math.min(tone.length, (int) (44.100 * SHORT_TONE_LENGTH));
shortTone = new AudioTrack(STREAM, 44100, AudioFormat.CHANNEL_OUT_MONO,
AudioFormat.ENCODING_PCM_16BIT, shortLength * 2, AudioTrack.MODE_STATIC, sessionId);
shortTone.write(tone, 0, shortLength);
AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
am.setStreamVolume(STREAM, am.getStreamMaxVolume(STREAM), 0);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && boostAudio) {
try {
StopWatch.debug("trying to boost");
loudnessEnhancer = new LoudnessEnhancer(sessionId);
loudnessEnhancer.setTargetGain(GAIN);
loudnessEnhancer.setEnabled(true);
if (!loudnessEnhancer.getEnabled()) {
loudnessEnhancer = null;
}
else {
StopWatch.debug("loudness success");
}
}
catch(Exception e) {
}
}

if (soundMode.equals("voice")) {
quiet = false;
ttsMode = true;
if (tts == null)
tts = new TextToSpeech(context, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if(status == TextToSpeech.SUCCESS){
/* int result=tts.setLanguage(Locale.US);
if(result==TextToSpeech.LANG_MISSING_DATA ||
result==TextToSpeech.LANG_NOT_SUPPORTED){
Log.e("chrono", "Language is not supported");
}
else{
Log.v("chrono", "success");
*/
}
else {
tts = null;
}
}
});
loudnessEnhancer = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && boostAudio) {
try {
Log.v("chrono", "trying to boost");
int id = ((AudioManager) context.getSystemService(Context.AUDIO_SERVICE)).generateAudioSessionId();
if (id != 0) {
loudnessEnhancer = new LoudnessEnhancer(id);
loudnessEnhancer.setTargetGain(GAIN);
loudnessEnhancer.setEnabled(true);
if (loudnessEnhancer.getEnabled()) {
ttsParams.put(TextToSpeech.Engine.KEY_PARAM_SESSION_ID, "" + id);
Log.v("chrono", "loudness!");
} else {
Log.v("chrono", "no loudness!");
loudnessEnhancer = null;
}
tts = new TextToSpeech(context, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if(status != TextToSpeech.SUCCESS){
tts = null;
}
}
catch (Exception e) {
Log.v("chrono", "failed boost");
loudnessEnhancer = null;
}
}
ttsParams.put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(AudioManager.STREAM_ALARM));
});
ttsParams.put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(STREAM));
if (loudnessEnhancer != null)
ttsParams.put(TextToSpeech.Engine.KEY_PARAM_SESSION_ID, String.valueOf(sessionId));
}
else if (soundMode.equals("beeps")) {
quiet = false;
ttsMode = false;
}
else {
quiet = true;
}

/* if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && boostAudio && !quiet) {
Log.v("chrono", "tone session "+toneG.getAudioSessionId());
try ...
loudnessEnhancer2 = new LoudnessEnhancer(toneG.getAudioSessionId());
loudnessEnhancer2.setTargetGain(GAIN);
loudnessEnhancer2.setEnabled(true);
if (!loudnessEnhancer2.getEnabled())
loudnessEnhancer2 = null;
} */
}

public void restore() {
Expand All @@ -383,7 +391,6 @@ public void restore() {
lapData = options.getString(Options.PREF_LAPS, "");
lastLapTime = options.getLong(Options.PREF_LAST_LAP_TIME, 0);
lastAnnounced = options.getLong(Options.PREF_LAST_ANNOUNCED, 0);
boostAudio = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && options.getBoolean(Options.PREF_BOOST, false);
setAudio(options.getString(Options.PREF_SOUND, "voice"));

precision = Integer.parseInt(options.getString(Options.PREFS_PRECISION, "100"));
Expand Down Expand Up @@ -443,7 +450,7 @@ public static void clearSaved(SharedPreferences pref) {
SharedPreferences.Editor ed = pref.edit();
ed.putBoolean(Options.PREFS_ACTIVE, false);
ed.apply();
Log.v("chrono", "cleared "+Options.PREFS_ACTIVE);
StopWatch.debug("cleared "+Options.PREFS_ACTIVE);
}

public static void detectBoot(SharedPreferences options) {
Expand Down Expand Up @@ -491,13 +498,13 @@ public void destroy() {
loudnessEnhancer.setEnabled(false);
loudnessEnhancer = null;
}
if (loudnessEnhancer2 != null) {
loudnessEnhancer2.setEnabled(false);
loudnessEnhancer2 = null;
if (shortTone != null) {
shortTone.release();
shortTone = null;
}
if (toneG != null) {
toneG.release();
toneG = null;
if (longTone != null) {
longTone.release();
longTone = null;
}
if (tts != null) {
tts.shutdown();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.media.AudioManager;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.provider.MediaStore;
import android.text.Html;
import android.util.Log;

Expand Down Expand Up @@ -41,6 +43,8 @@ public class Options extends PreferenceActivity {
public static final String PREF_LAST_ANNOUNCED = "lastAnnounced";
public static final String PREF_SOUND = "sounds";
public static final String PREF_BOOST = "boost";
public static final String PREF_ALARM = "alarm";
public static final String PREF_VOLUME = "volume";
static Map<String, int[]> colorMap = new HashMap<String,int[]>();
static final int[] defaultColor = {Color.WHITE, Color.BLACK};

Expand All @@ -56,7 +60,7 @@ private static void addColor(String name, int fg, int bg) {
}

static MiniFont getFont(SharedPreferences options) {
String f = options.getString(PREF_FONT, "regular");
String f = options.getString(PREF_FONT, "medium");
if (f.equals("regular")) {
Log.v("chrono", "regular");
return new SansDigitsColon();
Expand Down Expand Up @@ -146,6 +150,13 @@ public void showLicenses() {
alertDialog.show();
}

public static int getStream(SharedPreferences options) {
if (options.getBoolean(Options.PREF_ALARM, true))
return AudioManager.STREAM_ALARM;
else
return AudioManager.STREAM_MUSIC;
}

/* public static class MyPreferenceFragment extends PreferenceFragment
{
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import android.widget.TextView;

public class StopWatch extends Activity {
private static final boolean DEBUG = true;
SharedPreferences options;
long baseTime = 0;
long pausedTime = 0;
Expand Down Expand Up @@ -151,6 +152,8 @@ void setTheme() {
protected void onResume() {
super.onResume();

setVolumeControlStream(Options.getStream(options));

String o = options.getString(Options.PREFS_ORIENTATION, "automatic");
if (o.equals("landscape"))
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
Expand All @@ -159,7 +162,7 @@ else if (o.equals("portrait"))
else
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);

Log.v("chrono", "theme");
debug("theme");
setTheme();
int orientation = getResources().getConfiguration().orientation;
chrono.post(new Runnable() {
Expand All @@ -169,7 +172,7 @@ public void run() {
}
});

Log.v("chrono", "onResume");
debug("onResume");

stopwatch.restore();
stopwatch.updateViews();
Expand All @@ -178,7 +181,7 @@ public void run() {

@Override
public void onConfigurationChanged(Configuration newConfig) {
Log.v("chrono", "onConfChanged");
debug("onConfChanged");
super.onConfigurationChanged(newConfig);
stopwatch.updateViews();
}
Expand Down Expand Up @@ -225,15 +228,24 @@ public void onButtonReset(View v) {
pressReset();
}

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (!options.getBoolean(Options.PREF_VOLUME, true))
return false;
return keyCode == KeyEvent.KEYCODE_VOLUME_UP || keyCode == KeyEvent.KEYCODE_VOLUME_DOWN;
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getAction() != KeyEvent.ACTION_DOWN)
if (!options.getBoolean(Options.PREF_VOLUME, true))
return false;
if (keyCode == KeyEvent.KEYCODE_VOLUME_UP /*|| keyCode == KeyEvent.KEYCODE_A*/) {
if (event.getAction() != KeyEvent.ACTION_DOWN)
return keyCode == KeyEvent.KEYCODE_VOLUME_UP || keyCode == KeyEvent.KEYCODE_VOLUME_DOWN;
if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
pressStart();
return true;
}
else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN /*|| keyCode == KeyEvent.KEYCODE_C*/) {
else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
pressReset();
return true;
}
Expand All @@ -249,4 +261,9 @@ public void onDestroy() {
super.onDestroy();
stopwatch.destroy();
}

public static void debug(String s) {
if (DEBUG)
Log.v("chrono", s);
}
}
Loading

0 comments on commit efcec54

Please sign in to comment.