Advertisement
Guest User

Untitled

a guest
Apr 16th, 2015
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.37 KB | None | 0 0
  1. package com.example.stropheum.speedcalculatortest;
  2.  
  3. import android.content.Context;
  4. import android.location.Location;
  5. import android.location.LocationListener;
  6. import android.location.LocationManager;
  7. import android.support.v7.app.ActionBarActivity;
  8. import android.os.Bundle;
  9. import android.view.KeyEvent;
  10. import android.view.Menu;
  11. import android.view.MenuItem;
  12. import android.widget.TextView;
  13. import android.os.Vibrator;
  14.  
  15.  
  16. public class SpeedAlarmActivity extends ActionBarActivity {
  17.  
  18.     final int MILLI_TO_SEC = 1000;
  19.     final int SEC_TO_HOUR = 3600;
  20.     Vibrator vibrator;
  21.  
  22.     // Allow 15 seconds of error for time calculations
  23.     final double MILE_TIME_ERROR = 0.25;
  24.  
  25.     @Override
  26.     protected void onCreate(Bundle savedInstanceState) {
  27.         super.onCreate(savedInstanceState);
  28.  
  29.         vibrator  = (Vibrator) this.getSystemService(Context.VIBRATOR_SERVICE);
  30.  
  31.         setContentView(R.layout.activity_speed_alarm);
  32.  
  33.         LocationManager locationManager =
  34.                 (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
  35.  
  36.         LocationListener locationListener = new LocationListener() {
  37.             double lonNew, lonOld;
  38.             double latNew, latOld;
  39.  
  40.             // Values to track display interval
  41.             long paceStartTime, paceDeltaTime;
  42.             long speedStartTime, speedDeltaTime;
  43.  
  44.             // Values to track computation interval
  45.             long timeNew;
  46.             long timeOld = System.currentTimeMillis();
  47.             long deltaTime;
  48.  
  49.             double distance, speed, mileTime;
  50.             double goalMileTime = 2;
  51.  
  52.             int ticks = 0;
  53.             double speedSum = 0;
  54.  
  55.             boolean firstRun = true;
  56.  
  57.             String paceText;
  58.  
  59.  
  60.             public void onLocationChanged(Location location) {
  61.                 // Calculate change in time
  62.                 if (firstRun) {
  63.                     paceStartTime = System.currentTimeMillis();
  64.                     speedStartTime = System.currentTimeMillis();
  65.                     //timeOld = System.currentTimeMillis();
  66.                     lonOld = Math.toRadians(location.getLongitude());
  67.                     latOld = Math.toRadians(location.getLongitude());
  68.                     updateGmt(goalMileTime);
  69.                     paceText = "Begin!";
  70.                     updatePaceText(paceText);
  71.                     firstRun = false;
  72.                 }
  73.                 timeNew = System.currentTimeMillis();
  74.                 deltaTime = Math.abs(timeNew - timeOld);
  75.  
  76.                 // Compute new positional coordinates
  77.                 lonNew = Math.toRadians(location.getLongitude());
  78.                 latNew = Math.toRadians(location.getLatitude());
  79.  
  80.                 distance = haversine(latOld, lonOld, latNew, lonNew);
  81.                 if (distance > 1000) { distance = 0; }
  82.  
  83.                 // Add current speed and count number of ticks
  84.                 speedSum += (distance / deltaTime) * MILLI_TO_SEC * SEC_TO_HOUR;
  85.                 ticks++;
  86.                 paceDeltaTime = System.currentTimeMillis() - paceStartTime;
  87.                 speedDeltaTime = System.currentTimeMillis() - speedStartTime;
  88.  
  89.                 if (paceDeltaTime >= 30000) { // Update all values and pace text
  90.                     speed = speedSum / ticks;
  91.                     mileTime = 60 / speed;
  92.  
  93.                     checkPace();
  94.  
  95.                     // Update values on screen
  96.                     if (speed > 10000) {
  97.                         speed = 0;
  98.                     }
  99.                     updateSpeed(speed);
  100.                     updateEmt(mileTime);
  101.                     updatePaceText(paceText);
  102.  
  103.                     ticks = 0;
  104.                     speedSum = 0;
  105.                     paceStartTime = System.currentTimeMillis();
  106.                 } else if (speedDeltaTime >= 5000) { // Update values without interfering with average
  107.                     speed = speedSum / ticks;
  108.                     mileTime = 60 / speed;
  109.  
  110.                     if (speed > 10000) {
  111.                         speed = 0;
  112.                     }
  113.                     updateSpeed(speed);
  114.                     updateEmt(mileTime);
  115.  
  116.                     // If player resumes proper pace, reset interval timer and prompt pace
  117.                     if (mileTime > goalMileTime - MILE_TIME_ERROR &&
  118.                         mileTime < goalMileTime + MILE_TIME_ERROR) {
  119.                         checkPace();
  120.                         //paceStartTime = System.currentTimeMillis();
  121.                     }
  122.  
  123.                     speedStartTime = System.currentTimeMillis();
  124.                 }
  125.  
  126.             // Store old Coordinates and time
  127.             latOld = latNew;
  128.             lonOld = lonNew;
  129.             timeOld = timeNew;
  130.  
  131.         }
  132.  
  133.  
  134.             public void onStatusChanged(String Provider, int status, Bundle extras) {}
  135.  
  136.             public void onProviderEnabled(String provider) {}
  137.  
  138.             public void onProviderDisabled(String provider) {}
  139.  
  140.             /**
  141.              * Updates the current speed of the user
  142.              * @param speed the current speed value
  143.              */
  144.             private void updateSpeed(double speed) {
  145.                 final TextView speedVal = (TextView) findViewById(R.id.SpeedVal);
  146.                 speedVal.setText(String.format("%.2f", speed));
  147.             }
  148.  
  149.             /**
  150.              * Updates the current estimated mile time
  151.              * @param mileTime current EMT
  152.              */
  153.             private void updateEmt(double mileTime) {
  154.                 int minutes = (int)mileTime;
  155.                 int seconds = (int)(((mileTime * 100) % 100) * 0.6);
  156.                 final TextView emtVal = (TextView) findViewById(R.id.emtVal);
  157.                 emtVal.setText(String.format("%d:%02d", minutes, seconds));
  158.             }
  159.  
  160.             /**
  161.              * Updates the current goal mile time
  162.              * @param goalMileTime new goal mile time
  163.              */
  164.             private void updateGmt(double goalMileTime) {
  165.                 int minutes = (int)goalMileTime;
  166.                 int seconds = (int)(((mileTime * 100) % 100) * 0.6);
  167.                 final TextView gmtVal = (TextView) findViewById(R.id.gmtVal);
  168.                 gmtVal.setText(String.format("%d:%02d", minutes, seconds));
  169.             }
  170.  
  171.             /**
  172.              * Updates the current pace text
  173.              * @param paceText indicator for user;s current speed in relation to goal time
  174.              */
  175.             private void updatePaceText(String paceText) {
  176.                 final TextView pace = (TextView) findViewById(R.id.paceView);
  177.                 pace.setText(paceText);
  178.             }
  179.  
  180.             /**
  181.              * Checks current pace and assigns appropriate text
  182.              */
  183.             private void checkPace() {
  184.                 if (mileTime > goalMileTime + MILE_TIME_ERROR) {
  185.                     paceText = "Speed up";
  186.                     vibrator.vibrate(300);
  187.                     try {
  188.                         Thread.sleep(300);
  189.                     } catch (Exception e) {}
  190.                     vibrator.vibrate(300);
  191.                     try {
  192.                         Thread.sleep(300);
  193.                     } catch (Exception e) {}
  194.                     vibrator.vibrate(300);
  195.                 } else if (mileTime < goalMileTime - MILE_TIME_ERROR) {
  196.                     paceText = "Slow Down";
  197.                     vibrator.vibrate(1000);
  198.                 } else {
  199.                     paceText = "Perfect Pace!";
  200.                 }
  201.             }
  202.         };
  203.  
  204.         locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
  205.  
  206.     }
  207.  
  208.     /**
  209.      * Computes distance (in miles) between two coordinates using the haversine formula
  210.      * @param lat1 latitude  of previous location
  211.      * @param lon1 longitude of previous location
  212.      * @param lat2 latitude  of current  location
  213.      * @param lon2 longitude of current  location
  214.      * @return distance in miles
  215.      */
  216.     private double haversine(double lat1, double lon1, double lat2, double lon2) {
  217.         final double EARTH_RADIUS_M = 3959;
  218.  
  219.         double dLon, dLat, a, c, distance;
  220.  
  221.         // Calculate distance traveled using Haversine formula
  222.         dLon = lon2 - lon1;
  223.         dLat = lat2 - lat1;
  224.  
  225.         a = Math.sin(dLat/2.0) * Math.sin(dLat/2.0) + Math.cos(lat1) * Math.cos(lat2) * Math.sin(dLon/2.0) * Math.sin(dLon/2.0);
  226.         System.out.println("a = " + a);
  227.  
  228.         c = 2.0 * Math.atan(Math.sqrt(a));
  229.         System.out.println("c = " + c);
  230.         distance = EARTH_RADIUS_M * c;
  231.  
  232.         return distance;
  233.     }
  234.  
  235.  
  236.     @Override
  237.     public boolean onCreateOptionsMenu(Menu menu) {
  238.         // Inflate the menu; this adds items to the action bar if it is present.
  239.         getMenuInflater().inflate(R.menu.menu_speed_alarm, menu);
  240.         return true;
  241.     }
  242.  
  243.     @Override
  244.     public boolean onOptionsItemSelected(MenuItem item) {
  245.         // Handle action bar item clicks here. The action bar will
  246.         // automatically handle clicks on the Home/Up button, so long
  247.         // as you specify a parent activity in AndroidManifest.xml.
  248.         int id = item.getItemId();
  249.  
  250.         //noinspection SimplifiableIfStatement
  251.         if (id == R.id.action_settings) {
  252.             return true;
  253.         }
  254.  
  255.         return super.onOptionsItemSelected(item);
  256.     }
  257.  
  258.     @Override
  259.     public void onBackPressed() {
  260.         finish();
  261.         return;
  262.     }
  263.  
  264. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement