1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License 15 */ 16 17 package com.android.cts.verifier.sensors; 18 19 import android.app.AlertDialog; 20 import android.content.Context; 21 import android.hardware.Sensor; 22 import android.hardware.SensorEvent; 23 import android.hardware.SensorEventListener; 24 import android.hardware.SensorManager; 25 import android.os.Bundle; 26 import android.view.View; 27 import android.widget.TextView; 28 29 import com.android.cts.verifier.PassFailButtons; 30 import com.android.cts.verifier.R; 31 32 /** 33 * CTS Verifier case for verifying correct integration of heart rate monitor. 34 * If a user is wearing a device with an HRM, the value is between <> and <> 35 */ 36 public class HeartRateMonitorTestActivity extends PassFailButtons.Activity { 37 private SensorManager mSensorManager; 38 private Sensor mSensor; 39 private SensorListener mSensorListener; 40 private AlertDialog mNoHeartRateWarningDialog; 41 private TextView mSensorText; 42 43 @Override 44 protected void onCreate(Bundle savedInstanceState) { 45 super.onCreate(savedInstanceState); 46 setContentView(R.layout.snsr_hrm); 47 setInfoResources(R.string.snsr_heartrate_test, R.string.snsr_heartrate_test_info, 0); 48 setPassFailButtonClickListeners(); 49 50 mSensorText = (TextView) findViewById(R.id.sensor_value); 51 52 mSensorManager = (SensorManager) getApplicationContext().getSystemService( 53 Context.SENSOR_SERVICE); 54 mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_HEART_RATE); 55 mSensorListener = new SensorListener(); 56 } 57 58 @Override 59 protected void onResume() { 60 super.onResume(); 61 if (!mSensorManager.registerListener(mSensorListener, mSensor, 62 SensorManager.SENSOR_DELAY_UI)) { 63 showNoHeartRateWarningDialog(); 64 setTestResultAndFinish(true); 65 } 66 } 67 68 @Override 69 protected void onPause() { 70 super.onPause(); 71 mSensorManager.unregisterListener(mSensorListener, mSensor); 72 } 73 74 private void showNoHeartRateWarningDialog() { 75 if (mNoHeartRateWarningDialog == null) { 76 mNoHeartRateWarningDialog = new AlertDialog.Builder(this) 77 .setIcon(android.R.drawable.ic_dialog_alert) 78 .setTitle(R.string.snsr_heartrate_test_no_heartrate_title) 79 .setMessage(R.string.snsr_heartrate_test_no_heartrate_message) 80 .setPositiveButton(android.R.string.ok, null) 81 .create(); 82 } 83 if (!mNoHeartRateWarningDialog.isShowing()) { 84 mNoHeartRateWarningDialog.show(); 85 } 86 } 87 88 private class SensorListener implements SensorEventListener { 89 private static final double MIN_HEART_RATE = 40; 90 private static final double MAX_HEART_RATE = 200; 91 @Override 92 public void onSensorChanged(SensorEvent sensorEvent) { 93 float value = sensorEvent.values[0]; 94 if (value > MAX_HEART_RATE || value < MIN_HEART_RATE) { 95 updateWidgets(value, sensorEvent.accuracy, R.drawable.fs_error); 96 } else { 97 updateWidgets(value, sensorEvent.accuracy, R.drawable.fs_good); 98 } 99 } 100 101 void updateWidgets(float value, float accuracy, int icon) { 102 TextView sensorText = (TextView) findViewById(R.id.sensor_value); 103 TextView sensorAccuracyText = (TextView) findViewById(R.id.sensor_accuracy_value); 104 105 sensorText.setText(String.format("%+.2f", value)); 106 sensorText.setCompoundDrawablesWithIntrinsicBounds(0, 0, icon, 0); 107 sensorAccuracyText.setText(String.format("%+.2f", accuracy)); 108 } 109 110 @Override 111 public void onAccuracyChanged(Sensor sensor, int i) { 112 } 113 } 114 } 115