Android Open Source - rsmonitor-heartrate Activity Record






From Project

Back to project page rsmonitor-heartrate.

License

The source code is released under:

GNU General Public License

If you think the Android project rsmonitor-heartrate 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.renaultsport.heartrate;
//w ww  .  ja  v  a  2  s.c o  m
import java.io.File;
import java.util.Iterator;
import java.util.Set;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.preference.PreferenceManager;
import android.view.ContextThemeWrapper;
import android.view.LayoutInflater;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.googlecode.android.widgets.DateSlider.ScrollLayout;
import com.renaultsport.heartrate.utils.CaptureCamera;
import com.renaultsport.heartrate.utils.ClientThread;
import com.renaultsport.heartrate.utils.Constants;




public class ActivityRecord extends Activity implements LocationListener, View.OnClickListener, DialogInterface.OnClickListener
{
  private static int ID_SAVE_DIALOG = 20;

  private boolean  m_bIsConnected  = false;




  @SuppressLint("HandlerLeak")
  public class MainHandler extends Handler
  {
    public MainHandler () { }


    @Override
    public void handleMessage (Message message)
    {
      int value = 0;

      switch (message.what)
      {
        case Constants.CONNECTING :
          Toast.makeText (ActivityRecord.this, ActivityRecord.this.getString (R.string.connecting), Toast.LENGTH_LONG).show();
  
          m_HeartRateTextView.setText      (ActivityRecord.this.getString (R.string.reset_value));
          m_BatteryIndicatorTextView.setText  (ActivityRecord.this.getString (R.string.reset_value));
        break;
  
        case Constants.CONNECTED :
          Toast.makeText (ActivityRecord.this, ActivityRecord.this.getString (R.string.connected), Toast.LENGTH_LONG).show();
  
          m_HeartRateTextView.setText      (ActivityRecord.this.getString (R.string.reset_value));
          m_BatteryIndicatorTextView.setText  (ActivityRecord.this.getString (R.string.reset_value));
        break;
  
        case Constants.CONNECTION_FAILED :
          Toast.makeText (ActivityRecord.this, ActivityRecord.this.getString (R.string.connection_failure), Toast.LENGTH_LONG).show();
          disconnect ();
        break;
  
        case Constants.CONNECTION_LOST :
          Toast.makeText (ActivityRecord.this, ActivityRecord.this.getString (R.string.connection_lost), Toast.LENGTH_LONG).show();
          disconnect ();
        break;
  
        case Constants.BATTERY_UPDATE :
          if (m_ClientThread != null)
          {
            value = m_ClientThread.getBatteryIndicator ();
            if ((0x60 & value) == 0)  { m_BatteryIndicatorTextView.setText ("0");    }
            if ((0x60 & value) == 96)  { m_BatteryIndicatorTextView.setText ("100");  }
            if ((0x60 & value) == 64)  { m_BatteryIndicatorTextView.setText ("66");  }
            if ((0x60 & value) == 32)  { m_BatteryIndicatorTextView.setText ("33");  }
          }
        break;
  
        case Constants.HEART_RATE_UPDATE :
          if (m_ClientThread != null)
          {
            value = m_ClientThread.getHeartRate ();
            if (value != 0) { m_HeartRateTextView.setText (String.valueOf (value)); }
          }
        break;
  
        case Constants.BYTES_RECEIVED :
          if (m_ClientThread != null) { m_BytesTextView.setText (String.valueOf (m_ClientThread.getReceivedBytes ())); }
        break;
  
        case Constants.PREVIEW_DESTROYED :
          m_ButtonVideo.setBackgroundResource (R.drawable.camera_off);
        break;
  
        case Constants.CAMERA_ON :
          m_ButtonVideo.setBackgroundResource (R.drawable.camera_on);
        break;
  
        case Constants.CAMERA_OFF :
          m_ButtonVideo.setBackgroundResource (R.drawable.camera_off);
        break;
  
        case Constants.CAMERA_RECORD_START :
          m_ButtonVideo.setBackgroundResource (R.drawable.camera_reg);
        break;
  
        case Constants.CAMERA_RECORD_STOP :
          if (m_CaptureCamera.isPreview ())  { m_ButtonVideo.setBackgroundResource (R.drawable.camera_on); }
          else                { m_ButtonVideo.setBackgroundResource (R.drawable.camera_off); }
        break;
  
        default :
        break;
      }
    }
  }




  private MainApplication        m_Application        = null;
  private LocationManager        m_LocationManager      = null;
  private SharedPreferences      m_Preferences        = null;
  private PowerManager        m_PowerManager        = null;
  private WakeLock          m_WakeLock          = null;
  private BluetoothAdapter      m_Adapter          = null;
  private ClientThread        m_ClientThread        = null;
  private MainHandler          m_Handler          = null;
  private CaptureCamera        m_CaptureCamera        = null;

  private SurfaceView          m_CameraSurfaceView      = null;
  private ImageView          m_GpsImageView        = null;
  private ImageButton          m_ButtonVideo        = null;
  private TextView          m_HeartRateTextView      = null;
  private TextView          m_BatteryIndicatorTextView  = null;
  private TextView          m_BytesTextView        = null;
  private ImageButton          m_RecordButton        = null;
  private AlertDialog          m_GpsDialog          = null;




  @Override
  public void onCreate (Bundle bundle) 
  {
    super.onCreate (bundle);

    setContentView (R.layout.main);

    m_Application    = (MainApplication)    getApplication ();
    m_LocationManager  = (LocationManager)    getSystemService (LOCATION_SERVICE);
    m_PowerManager    = ((PowerManager)    getSystemService (POWER_SERVICE));
    m_Preferences    = (SharedPreferences)  PreferenceManager.getDefaultSharedPreferences (getApplicationContext ());

    m_CameraSurfaceView      = ((SurfaceView)  findViewById (R.id.SurfaceCamera));
    m_GpsImageView        = ((ImageView)    findViewById (R.id.IconGps));
    m_HeartRateTextView      = ((TextView)    findViewById (R.id.HeartRateTextView));
    m_BatteryIndicatorTextView  = ((TextView)    findViewById (R.id.BatteryIndicatorTextView));
    m_BytesTextView        = ((TextView)    findViewById (R.id.BytesTextView));
    m_ButtonVideo        = ((ImageButton)  findViewById (R.id.ButtonVideo));
    m_RecordButton        = ((ImageButton)  findViewById (R.id.RecordButton));

    m_CameraSurfaceView.setVisibility (SurfaceView.GONE);

    m_ButtonVideo.setOnClickListener  (this);
    m_RecordButton.setOnClickListener  (this);

    m_Handler    = new MainHandler ();
    m_WakeLock    = m_PowerManager.newWakeLock (PowerManager.FULL_WAKE_LOCK, getClass ().getName ());
    m_CaptureCamera  = CaptureCamera.getInstance (m_Handler, m_CameraSurfaceView, getResources ().getConfiguration ().orientation);
    m_GpsDialog    = createPopupGps ();

    m_Adapter = BluetoothAdapter.getDefaultAdapter ();
    if (m_Adapter == null) { Toast.makeText (this, getString (R.string.bluetooth_not_supported), Toast.LENGTH_LONG).show (); }

    m_ClientThread  = null;
  }

  @Override
  public void onResume () 
  {
    Location  location  = null;
    Intent    intent    = null;


    super.onResume();

    if (! m_Preferences.getBoolean (Constants.STATE_POPUP_GPS, false)) { m_GpsDialog.show (); }

    location = m_LocationManager.getLastKnownLocation (LocationManager.GPS_PROVIDER);
    if (location != null) { initLogoGPS (location.getAccuracy ()); }

    m_LocationManager.requestLocationUpdates (LocationManager.GPS_PROVIDER, Constants.MINIMUM_TIME_BETWEEN_UPDATE, Constants.MINIMUM_DISTANCE_CHANGE_FOR_UPDATE, this);

    if ((m_Adapter != null) && (! m_Adapter.isEnabled ()))
    {
      intent = new Intent (BluetoothAdapter.ACTION_REQUEST_ENABLE);
      startActivityForResult (intent, Constants.REQUEST_ENABLE_BT);
    }
  }

  @Override
  public void onPause () 
  {
    File videoFile = null;


    super.onPause ();

    if (m_CaptureCamera.isRecording ())
    {
      m_CaptureCamera.stopRecording ();
      videoFile = new File (m_CaptureCamera.getPathVideo ());
      if (videoFile.exists ()) { videoFile.delete (); }
    }

    m_CaptureCamera.cameraOff ();

    m_bIsConnected = false;
  }




  @Override
  protected void onActivityResult (int requestCode, int resultCode, Intent data)
  {
    super.onActivityResult (requestCode, resultCode, data);

    if (requestCode == Constants.REQUEST_ENABLE_BT) { Toast.makeText (this, getString (R.string.bluetooth_activated), Toast.LENGTH_LONG).show (); }
  }

  @Override
  public void onLocationChanged (Location location) { initLogoGPS (location.getAccuracy ()); }

  @Override
  public void onProviderDisabled (String provider) { }

  @Override
  public void onProviderEnabled (String provider) { }

  @Override
  public void onStatusChanged (String provider, int status, Bundle extras) { }

  @Override
  public void onClick (View view)
  {
    Editor    editor    = null;
    Location  location  = null;


    switch (view.getId ())
    {
      case R.id.buttonClosePopupGps :
        m_GpsDialog.dismiss ();
      break;
  
      case R.id.ButtonVideo :
        if    (! m_CaptureCamera.isPreview  ())  { m_CaptureCamera.cameraOn  (); }
        else if  (! m_CaptureCamera.isRecording  ()) { m_CaptureCamera.cameraOff  (); }
      break;
  
      case R.id.RecordButton :
        if (m_bIsConnected)
        {
          if (m_CaptureCamera.isRecording ())
          {
            m_CaptureCamera.stopRecording ();
            showDialog (ID_SAVE_DIALOG);
          }
  
          disconnect ();
        }
        else
        {
          if (m_CaptureCamera.isPreview () && (! m_CaptureCamera.isRecording ()))
          {
            try { m_CaptureCamera.startRecording (); }
            catch (Exception exception) { m_CaptureCamera.cameraOff (); }
          }
  
          location = m_LocationManager.getLastKnownLocation (LocationManager.GPS_PROVIDER);
          if (location != null)
          {
            editor = getSharedPreferences (Constants.PREFERENCES_LOCATION, Context.MODE_PRIVATE).edit ();
            editor.putFloat (Constants.PREFERENCES_LOCATION_LATITUDE,  (float) location.getLatitude  ());
            editor.putFloat (Constants.PREFERENCES_LOCATION_LONGITUDE,  (float) location.getLongitude  ());
            editor.commit ();
          }
  
          if (! m_Application.isScreenLocked ())
          {
            m_Application.setScreenLockState (getResources ().getConfiguration ().orientation);
            m_Application.lockScreen (m_Application.getScreenLockState (), this);
            m_Application.setScreenLocked (true);
          }
  
          connect ();
  
          m_bIsConnected = true;
        }
      break;
  
      default :
      break;
    }
  }

  @Override
  public void onClick (DialogInterface dialog, int id)
  {
    CheckBox  checkbox  = null;
    Editor    editor    = null;


    checkbox = (CheckBox) m_GpsDialog.findViewById (R.id.dontShowPopupGps);
    if (checkbox.isChecked ())
    {
      editor = m_Preferences.edit ();
      editor.putBoolean (Constants.STATE_POPUP_GPS, true);
      editor.commit ();

      m_GpsDialog.dismiss ();
    }
  }

  @Override
  public Dialog onCreateDialog (int id)
  {
    View  view  = null;
    Dialog  dialog  = null;
    Builder  builder  = null;


    if (id == ID_SAVE_DIALOG)
    {
      view = LayoutInflater.from (this).inflate (R.layout.success_save_video, null);

      builder  = new Builder (new ContextThemeWrapper (this, R.style.Popup));
      builder.setTitle      (getResources ().getString (R.string.success));
      builder.setView        (view);
      builder.setPositiveButton  (getResources ().getString (R.string.ok), this);

      dialog = builder.create ();
    }

    return dialog;
  }




  private void connect ()
  {
    Iterator<BluetoothDevice>  iterator    = null;
    Set<BluetoothDevice>    bondedDevices  = null;
    BluetoothDevice        device      = null;
    Boolean            found      = false;


    if ((m_Adapter != null) && (m_ClientThread == null))
    {
      bondedDevices = m_Adapter.getBondedDevices ();
      if (bondedDevices.size () > 0)
      {
        found = false;

        iterator = bondedDevices.iterator ();
        while (iterator.hasNext () && (! found))
        {
          device = (BluetoothDevice) iterator.next ();
          if (device.getName ().startsWith (Constants.DEVICE_NAME_PREFIX)) { found = true; }
        }
      }

      if (found)
      {
        Toast.makeText (this, getString (R.string.device_found), Toast.LENGTH_LONG).show();

        m_ClientThread = new ClientThread (device, m_Handler);
        m_ClientThread.start ();

        m_RecordButton.setBackgroundResource (R.drawable.logger_on);
        m_WakeLock.acquire ();
      }
      else { Toast.makeText (this, getString (R.string.device_not_found), Toast.LENGTH_LONG).show(); }
    }
  }

  private void disconnect ()
  {
    if (m_ClientThread != null)
    {
      m_ClientThread.setActive (false);

      try { m_ClientThread.join (Constants.MAX_THREAD_WAIT); }
      catch (InterruptedException exception) { }

      m_ClientThread = null;

      m_RecordButton.setBackgroundResource (R.drawable.logger_off);
      m_WakeLock.release ();
    }
  }

  private AlertDialog createPopupGps ()
  {
    View    view    = null;
    AlertDialog  alertDialog  = null;
    Button    closeButton  = null;


    view    = LayoutInflater.from (this).inflate (R.layout.popup_gps_help, (ScrollLayout) findViewById (R.id.popupGps));
    alertDialog  = new AlertDialog.Builder (this).setView (view).create ();
    closeButton  = (Button) view.findViewById (R.id.buttonClosePopupGps);

    closeButton.setOnClickListener (this);

    return alertDialog;
  }

  private void initLogoGPS (float precision)
  {
    if    (precision <= Constants.GPS_PRECISION_3)  { m_GpsImageView.setImageResource (R.drawable.gps_3); }
    else if  (precision <= Constants.GPS_PRECISION_2)  { m_GpsImageView.setImageResource (R.drawable.gps_2); }
    else if (precision <= Constants.GPS_PRECISION_1)  { m_GpsImageView.setImageResource (R.drawable.gps_1); }
    else                        { m_GpsImageView.setImageResource (R.drawable.gps_0); }
  }
}




Java Source Code List

com.facebook.android.AsyncFacebookRunner.java
com.facebook.android.BaseDialogListener.java
com.facebook.android.BaseRequestListener.java
com.facebook.android.DialogError.java
com.facebook.android.FQLQuery.java
com.facebook.android.FacebookError.java
com.facebook.android.FacebookFunctions.java
com.facebook.android.Facebook.java
com.facebook.android.FbDialog.java
com.facebook.android.FieldsConnectionsDialog.java
com.facebook.android.FriendsGetProfilePics.java
com.facebook.android.FriendsList.java
com.facebook.android.GraphExplorer.java
com.facebook.android.IntentUriHandler.java
com.facebook.android.LoginButton.java
com.facebook.android.PermissionsDialog.java
com.facebook.android.Places.java
com.facebook.android.SessionEvents.java
com.facebook.android.SessionStore.java
com.facebook.android.TokenRefreshDialog.java
com.facebook.android.UpdateStatusResultDialog.java
com.facebook.android.UploadPhotoResultDialog.java
com.facebook.android.Util.java
com.facebook.android.Utility.java
com.googlecode.android.widgets.DateSlider.AlternativeDateSlider.java
com.googlecode.android.widgets.DateSlider.CustomDateSlider.java
com.googlecode.android.widgets.DateSlider.DateSlider.java
com.googlecode.android.widgets.DateSlider.DateTimeSlider.java
com.googlecode.android.widgets.DateSlider.DefaultDateSlider.java
com.googlecode.android.widgets.DateSlider.MonthYearDateSlider.java
com.googlecode.android.widgets.DateSlider.ScrollLayout.java
com.googlecode.android.widgets.DateSlider.TimeSlider.java
com.googlecode.android.widgets.DateSlider.TimeView.java
com.renaultsport.heartrate.ActivityMain.java
com.renaultsport.heartrate.ActivityRecord.java
com.renaultsport.heartrate.ActivitySplash.java
com.renaultsport.heartrate.ActivityUpload.java
com.renaultsport.heartrate.ActivityVideoReader.java
com.renaultsport.heartrate.ActivityWarning.java
com.renaultsport.heartrate.MainApplication.java
com.renaultsport.heartrate.utils.CaptureCamera.java
com.renaultsport.heartrate.utils.ClientThread.java
com.renaultsport.heartrate.utils.Constants.java
com.renaultsport.heartrate.utils.RunEncoder.java
team.stride.tabs.TabGroupActivity.java