Android Open Source - Fast Run Fragment






From Project

Back to project page Fast.

License

The source code is released under:

GNU General Public License

If you think the Android project Fast listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.example.fast.run;
//www  .  ja  v  a  2 s  .c o m
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.fast.Database;
import com.example.fast.MainActivity;
import com.example.fast.R;
import com.example.fast.Statistics;


/**
 * 
 * Screen seen when the user selects the Run Now option.  Displays all statistics of the current run.
 *
 */
public class RunFragment extends Fragment {
  
  private TextView gpsText;
  private TextView distance;
  private TextView speed;
  private TextView heartRate;
  private TextView connected;
  private boolean clicked = false;
  private Database database;
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    LinearLayout view = (LinearLayout) inflater.inflate(R.layout.fragment_run, container, false);
    
    return view;
  }
  

  @Override
  public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    
    final Button startButton = (Button) getActivity().findViewById(R.id.startButton);
    final Button stopButton = (Button) getActivity().findViewById(R.id.endButton);
    
    gpsText = (TextView) getActivity().findViewById(R.id.runTime);
    distance = (TextView) getActivity().findViewById(R.id.distance);
    speed = (TextView) getActivity().findViewById(R.id.speed);
    database = ((MainActivity)getActivity()).getDatabase();
    heartRate = (TextView) getActivity().findViewById(R.id.heartRateConnected);
    connected = (TextView) getActivity().findViewById(R.id.heartRateCurrent);
        
    

    
    /* Start button */
    startButton.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        if(clicked == false){
          clicked = true;
          ((MainActivity) getActivity()).startStatisticsUpdater();
          startButton.setText("Pause");
          stopButton.setVisibility(View.VISIBLE); //show the stop button
        }
        else{
          clicked = false;
          ((MainActivity) getActivity()).stopStatisticsUpdater();
          startButton.setText("Resume");
        }
      }
    });
    
    /* Stop (End) button */
    stopButton.setOnClickListener(new OnClickListener() {
      
      @Override
      public void onClick(View v) {
        clicked = false;
        /* Store stats, ask to share? */
        ((MainActivity) getActivity()).stopStatisticsUpdater();
        startButton.setText("Start");
        stopButton.setVisibility(View.GONE); //after clicked, make invisible
        
        /* Post the updated values to the DATABASE */
        /* Schema (_id): AVERAGE_SPEED, MAX_SPEED, DISTANCE, USERNAME */
            updateDatabaseRecord("TEST");
            
            /* Set the home frag text, update from DB */
            ((MainActivity) getActivity()).updateEverything();
            
      }
    });
    

  }
  
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
     setRetainInstance(true);
  }

  @Override
  public void onStart() {
    super.onStart();
  }

  @Override
  public void onPause() {
    super.onPause();
  }
  
  @Override
  public void onStop() {
    super.onStop();
  }
  
  /*
   * Updates the database with the new data once the run has been ended.
   */
  private void updateDatabaseRecord(String name){
    double avgSpeed = ((MainActivity)getActivity()).getStatistics().getAverageSpeed();
        double maxSpeed = ((MainActivity)getActivity()).getStatistics().getMaxSpeed();
         double distance = ((MainActivity)getActivity()).getStatistics().getDistanceTravelled();
         double heartrate = ((MainActivity)getActivity()).getStatistics().getAverageHeartrate();
        database.updateRecords(name, avgSpeed, maxSpeed, distance, heartrate);
  }
  
  /**
   *  Updates all textviews with new data in the runFragment
   *  */
  public void updateRunTextViews() {
    
    Statistics statistics = ((MainActivity) getActivity()).getStatistics();
    
    /* Update text in Run Fragment, if possible */
      if(((MainActivity)getActivity()).getUsingMetric()){
           distance.setText(MainActivity.format(Statistics.kilometers(statistics.getDistanceTravelled())) + " kilometers");
           speed.setText(MainActivity.format(Statistics.kilometersPerHour(statistics.getAverageSpeed())) + " km/h");
      }
      
      else{
           distance.setText(MainActivity.format(Statistics.miles(statistics.getDistanceTravelled())) + " miles");
           speed.setText(MainActivity.format(Statistics.milesPerHour(statistics.getAverageSpeed())) + " MPH");
      }
      
      heartRate.setText(MainActivity.format(((MainActivity) getActivity()).getHeartRate()) + " bpm");
      connected.setText(((MainActivity) getActivity()).getConnection());
  }
  
  public TextView getGpsText(){
    return gpsText;
  }
  
  public TextView getDistance(){
    return distance;
  }
  
  public TextView getSpeed(){
    return speed;
  }
}




Java Source Code List

com.example.fast.DatabaseHelper.java
com.example.fast.Database.java
com.example.fast.GPSTracker.java
com.example.fast.MainActivity.java
com.example.fast.StatisticsContentResolver.java
com.example.fast.Statistics.java
com.example.fast.adapter.NavDrawerListAdapter.java
com.example.fast.dummy.DummyContent.java
com.example.fast.heart.ConnectedListener.java
com.example.fast.heart.HeartFragment.java
com.example.fast.heart.HxmBluetoothHelper.java
com.example.fast.home.HomeFragment.java
com.example.fast.model.NavDrawerItem.java
com.example.fast.preferences.FastPreferences.java
com.example.fast.run.RunFragment.java
com.example.fast.share.ShareFragment.java