|
1 | 1 | package net.nycjava.skylight1;
|
2 | 2 |
|
| 3 | +import java.util.Locale; |
| 4 | +import java.util.concurrent.Executors; |
| 5 | + |
3 | 6 | import net.nycjava.skylight1.dependencyinjection.Dependency;
|
4 | 7 | import net.nycjava.skylight1.dependencyinjection.DependencyInjectingObjectFactory;
|
5 | 8 | import skylight1.util.Assets;
|
6 | 9 | import skylight1.util.HighScoreService;
|
| 10 | +import skylight1.util.PhoneIdHasher; |
7 | 11 | import android.content.Intent;
|
8 | 12 | import android.media.MediaPlayer;
|
9 | 13 | import android.os.Bundle;
|
@@ -75,7 +79,49 @@ public void onCreate(final Bundle savedInstanceState) {
|
75 | 79 | }
|
76 | 80 |
|
77 | 81 | final int failedLevel = getIntent().getIntExtra(DIFFICULTY_LEVEL, 0);
|
78 |
| - new HighScoreService().recordScore(failedLevel, this); |
| 82 | + new HighScoreService().recordScore(failedLevel, false, this); |
| 83 | + |
| 84 | + //TODO: review |
| 85 | + Executors.defaultThreadFactory().newThread(new Runnable() { |
| 86 | + @Override |
| 87 | + public void run() { |
| 88 | + final String hashedPhoneId = new PhoneIdHasher().getHashedPhoneId(FailActivity.this); |
| 89 | + final int azimuthVariance = calculateAzimuth(); |
| 90 | + if(tracker!=null) { |
| 91 | + tracker.trackEvent("fail", "hashedPhoneId", hashedPhoneId, 0); //TODO: review |
| 92 | + tracker.trackEvent("fail", "locale", Locale.getDefault().toString(), 0); //TODO: review |
| 93 | + tracker.trackEvent("fail", "azimuthVariance", Integer.toString(azimuthVariance),azimuthVariance); |
| 94 | + } |
| 95 | + } |
| 96 | + private int calculateAzimuth() { |
| 97 | + final float compassReadings[] = getIntent().getFloatArrayExtra(COMPASS_READINGS); |
| 98 | + |
| 99 | + // need at least two readings to get a variance |
| 100 | + if (compassReadings.length < 2) { |
| 101 | + Log.i(TAG, "returning az = 0"); |
| 102 | + return 0; |
| 103 | + } |
| 104 | + |
| 105 | + // using two-pass algorithm from http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance |
| 106 | + float mean = 0f; |
| 107 | + for (float compassReading : compassReadings) { |
| 108 | + mean += compassReading; |
| 109 | + } |
| 110 | + mean = mean / (float) compassReadings.length; |
| 111 | + float sumOfSquares = 0f; |
| 112 | + for (float compassReading : compassReadings) { |
| 113 | + final float distance = compassReading - mean; |
| 114 | + sumOfSquares += distance * distance; |
| 115 | + } |
| 116 | + final double variance = sumOfSquares / (float) (compassReadings.length - 1); |
| 117 | + |
| 118 | + final int standardDeviation = (int) Math.sqrt(variance); |
| 119 | + |
| 120 | + Log.i(TAG, String.format("az sd is %d", standardDeviation)); |
| 121 | + return standardDeviation; |
| 122 | + } |
| 123 | + }).start(); |
| 124 | + |
79 | 125 | }
|
80 | 126 |
|
81 | 127 | @Override
|
|
0 commit comments