Vitalii Tomkiv | db66ed6 | 2016-04-07 18:10:39 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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.google.android.car.kitchensink.sensor; |
| 18 | |
Steve Paik | b805820 | 2018-06-06 18:11:55 -0700 | [diff] [blame] | 19 | import static java.lang.Integer.toHexString; |
| 20 | |
Vitalii Tomkiv | db66ed6 | 2016-04-07 18:10:39 -0700 | [diff] [blame] | 21 | import android.Manifest; |
| 22 | import android.annotation.Nullable; |
| 23 | import android.car.Car; |
Steve Paik | b805820 | 2018-06-06 18:11:55 -0700 | [diff] [blame] | 24 | import android.car.hardware.CarSensorConfig; |
| 25 | import android.car.hardware.CarSensorEvent; |
| 26 | import android.car.hardware.CarSensorManager; |
Vitalii Tomkiv | db66ed6 | 2016-04-07 18:10:39 -0700 | [diff] [blame] | 27 | import android.content.pm.PackageManager; |
Vitalii Tomkiv | db66ed6 | 2016-04-07 18:10:39 -0700 | [diff] [blame] | 28 | import android.os.Bundle; |
| 29 | import android.os.Handler; |
Vitalii Tomkiv | db66ed6 | 2016-04-07 18:10:39 -0700 | [diff] [blame] | 30 | import android.text.TextUtils; |
Kai | 4805eff | 2019-01-22 18:03:37 -0800 | [diff] [blame] | 31 | import android.text.method.ScrollingMovementMethod; |
Vitalii Tomkiv | db66ed6 | 2016-04-07 18:10:39 -0700 | [diff] [blame] | 32 | import android.util.Log; |
| 33 | import android.view.LayoutInflater; |
| 34 | import android.view.View; |
| 35 | import android.view.ViewGroup; |
| 36 | import android.widget.TextView; |
| 37 | |
Anthony Chen | d12cd77 | 2018-04-16 16:20:44 -0700 | [diff] [blame] | 38 | import androidx.fragment.app.Fragment; |
| 39 | |
Srinivas Visvanathan | f854423 | 2017-03-24 11:52:53 -0700 | [diff] [blame] | 40 | import com.google.android.car.kitchensink.KitchenSinkActivity; |
Vitalii Tomkiv | db66ed6 | 2016-04-07 18:10:39 -0700 | [diff] [blame] | 41 | import com.google.android.car.kitchensink.R; |
| 42 | |
| 43 | import java.text.DateFormat; |
| 44 | import java.text.SimpleDateFormat; |
| 45 | import java.util.ArrayList; |
Vitalii Tomkiv | db66ed6 | 2016-04-07 18:10:39 -0700 | [diff] [blame] | 46 | import java.util.HashSet; |
| 47 | import java.util.List; |
| 48 | import java.util.Map; |
| 49 | import java.util.Set; |
| 50 | import java.util.concurrent.ConcurrentHashMap; |
| 51 | |
| 52 | public class SensorsTestFragment extends Fragment { |
| 53 | private static final String TAG = "CAR.SENSOR.KS"; |
| 54 | private static final boolean DBG = true; |
Steve Paik | b805820 | 2018-06-06 18:11:55 -0700 | [diff] [blame] | 55 | private static final boolean DBG_VERBOSE = true; |
Vitalii Tomkiv | db66ed6 | 2016-04-07 18:10:39 -0700 | [diff] [blame] | 56 | private static final int KS_PERMISSIONS_REQUEST = 1; |
| 57 | |
| 58 | private final static String[] REQUIRED_PERMISSIONS = new String[]{ |
| 59 | Manifest.permission.ACCESS_FINE_LOCATION, |
| 60 | Manifest.permission.ACCESS_COARSE_LOCATION, |
| 61 | Car.PERMISSION_MILEAGE, |
Kai | 68ee16b | 2018-03-22 11:57:37 -0700 | [diff] [blame] | 62 | Car.PERMISSION_ENERGY, |
Steve Paik | 289ab99 | 2017-07-11 22:40:57 -0700 | [diff] [blame] | 63 | Car.PERMISSION_SPEED, |
Kai | 68ee16b | 2018-03-22 11:57:37 -0700 | [diff] [blame] | 64 | Car.PERMISSION_CAR_DYNAMICS_STATE |
Vitalii Tomkiv | db66ed6 | 2016-04-07 18:10:39 -0700 | [diff] [blame] | 65 | }; |
| 66 | |
Jason Tholstrup | d72b535 | 2016-09-22 16:32:14 -0700 | [diff] [blame] | 67 | private final CarSensorManager.OnSensorChangedListener mOnSensorChangedListener = |
| 68 | new CarSensorManager.OnSensorChangedListener() { |
Vitalii Tomkiv | db66ed6 | 2016-04-07 18:10:39 -0700 | [diff] [blame] | 69 | @Override |
Steve Paik | b805820 | 2018-06-06 18:11:55 -0700 | [diff] [blame] | 70 | public void onSensorChanged(CarSensorEvent event) { |
Vitalii Tomkiv | db66ed6 | 2016-04-07 18:10:39 -0700 | [diff] [blame] | 71 | if (DBG_VERBOSE) { |
| 72 | Log.v(TAG, "New car sensor event: " + event); |
| 73 | } |
| 74 | synchronized (SensorsTestFragment.this) { |
| 75 | mEventMap.put(event.sensorType, event); |
| 76 | } |
Scott Randolph | 75d2f12 | 2018-07-11 22:04:55 -0700 | [diff] [blame] | 77 | refreshSensorInfoText(); |
Vitalii Tomkiv | db66ed6 | 2016-04-07 18:10:39 -0700 | [diff] [blame] | 78 | } |
| 79 | }; |
| 80 | private final Handler mHandler = new Handler(); |
| 81 | private final Map<Integer, CarSensorEvent> mEventMap = new ConcurrentHashMap<>(); |
| 82 | private final DateFormat mDateFormat = SimpleDateFormat.getDateTimeInstance(); |
| 83 | |
Srinivas Visvanathan | f854423 | 2017-03-24 11:52:53 -0700 | [diff] [blame] | 84 | private KitchenSinkActivity mActivity; |
Vitalii Tomkiv | db66ed6 | 2016-04-07 18:10:39 -0700 | [diff] [blame] | 85 | private Car mCar; |
Scott Randolph | 75d2f12 | 2018-07-11 22:04:55 -0700 | [diff] [blame] | 86 | private CarSensorManager mCarSensorManager; |
| 87 | private LocationListeners mLocationListener; |
Vitalii Tomkiv | db66ed6 | 2016-04-07 18:10:39 -0700 | [diff] [blame] | 88 | private String mNaString; |
| 89 | private int[] supportedSensors = new int[0]; |
| 90 | private Set<String> mActivePermissions = new HashSet<String>(); |
| 91 | |
Scott Randolph | 75d2f12 | 2018-07-11 22:04:55 -0700 | [diff] [blame] | 92 | private TextView mSensorInfo; |
| 93 | private TextView mLocationInfo; |
| 94 | private TextView mAccelInfo; |
| 95 | private TextView mGyroInfo; |
| 96 | private TextView mMagInfo; |
| 97 | |
Vitalii Tomkiv | db66ed6 | 2016-04-07 18:10:39 -0700 | [diff] [blame] | 98 | @Nullable |
| 99 | @Override |
| 100 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, |
| 101 | @Nullable Bundle savedInstanceState) { |
| 102 | if (DBG) { |
| 103 | Log.i(TAG, "onCreateView"); |
| 104 | } |
| 105 | |
| 106 | View view = inflater.inflate(R.layout.sensors, container, false); |
Srinivas Visvanathan | f854423 | 2017-03-24 11:52:53 -0700 | [diff] [blame] | 107 | mActivity = (KitchenSinkActivity) getHost(); |
Scott Randolph | 75d2f12 | 2018-07-11 22:04:55 -0700 | [diff] [blame] | 108 | |
Vitalii Tomkiv | db66ed6 | 2016-04-07 18:10:39 -0700 | [diff] [blame] | 109 | mSensorInfo = (TextView) view.findViewById(R.id.sensor_info); |
Kai | 4805eff | 2019-01-22 18:03:37 -0800 | [diff] [blame] | 110 | mSensorInfo.setMovementMethod(new ScrollingMovementMethod()); |
Scott Randolph | 75d2f12 | 2018-07-11 22:04:55 -0700 | [diff] [blame] | 111 | mLocationInfo = (TextView) view.findViewById(R.id.location_info); |
Kai | 4805eff | 2019-01-22 18:03:37 -0800 | [diff] [blame] | 112 | mLocationInfo.setMovementMethod(new ScrollingMovementMethod()); |
Scott Randolph | 75d2f12 | 2018-07-11 22:04:55 -0700 | [diff] [blame] | 113 | mAccelInfo = (TextView) view.findViewById(R.id.accel_info); |
| 114 | mGyroInfo = (TextView) view.findViewById(R.id.gyro_info); |
| 115 | mMagInfo = (TextView) view.findViewById(R.id.mag_info); |
| 116 | |
Vitalii Tomkiv | db66ed6 | 2016-04-07 18:10:39 -0700 | [diff] [blame] | 117 | mNaString = getContext().getString(R.string.sensor_na); |
Vitalii Tomkiv | db66ed6 | 2016-04-07 18:10:39 -0700 | [diff] [blame] | 118 | return view; |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public void onResume() { |
| 123 | super.onResume(); |
Kai | cf3387b | 2018-06-20 12:37:52 -0700 | [diff] [blame] | 124 | final Runnable r = () -> { |
| 125 | initPermissions(); |
| 126 | }; |
| 127 | ((KitchenSinkActivity) getActivity()).requestRefreshManager(r, |
| 128 | new Handler(getContext().getMainLooper())); |
Vitalii Tomkiv | db66ed6 | 2016-04-07 18:10:39 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public void onPause() { |
| 133 | super.onPause(); |
Scott Randolph | 75d2f12 | 2018-07-11 22:04:55 -0700 | [diff] [blame] | 134 | if (mCarSensorManager != null) { |
| 135 | mCarSensorManager.unregisterListener(mOnSensorChangedListener); |
| 136 | } |
| 137 | if (mLocationListener != null) { |
| 138 | mLocationListener.stopListening(); |
Vitalii Tomkiv | db66ed6 | 2016-04-07 18:10:39 -0700 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
| 142 | private void initSensors() { |
| 143 | try { |
Scott Randolph | 75d2f12 | 2018-07-11 22:04:55 -0700 | [diff] [blame] | 144 | if (mCarSensorManager == null) { |
| 145 | mCarSensorManager = |
| 146 | (CarSensorManager) ((KitchenSinkActivity) getActivity()).getSensorManager(); |
| 147 | } |
| 148 | supportedSensors = mCarSensorManager.getSupportedSensors(); |
Vitalii Tomkiv | db66ed6 | 2016-04-07 18:10:39 -0700 | [diff] [blame] | 149 | for (Integer sensor : supportedSensors) { |
Scott Randolph | 75d2f12 | 2018-07-11 22:04:55 -0700 | [diff] [blame] | 150 | mCarSensorManager.registerListener(mOnSensorChangedListener, sensor, |
Vitalii Tomkiv | db66ed6 | 2016-04-07 18:10:39 -0700 | [diff] [blame] | 151 | CarSensorManager.SENSOR_RATE_NORMAL); |
| 152 | } |
Steve Paik | b805820 | 2018-06-06 18:11:55 -0700 | [diff] [blame] | 153 | } catch (Exception e) { |
Scott Randolph | 75d2f12 | 2018-07-11 22:04:55 -0700 | [diff] [blame] | 154 | Log.e(TAG, "initSensors() exception caught SensorManager: ", e); |
| 155 | } |
| 156 | try { |
| 157 | if (mLocationListener == null) { |
| 158 | mLocationListener = new LocationListeners(getContext(), |
| 159 | new LocationInfoTextUpdateListener()); |
| 160 | } |
| 161 | mLocationListener.startListening(); |
| 162 | } catch (Exception e) { |
| 163 | Log.e(TAG, "initSensors() exception caught from LocationListeners: ", e); |
Vitalii Tomkiv | db66ed6 | 2016-04-07 18:10:39 -0700 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | |
| 167 | private void initPermissions() { |
| 168 | Set<String> missingPermissions = checkExistingPermissions(); |
| 169 | if (!missingPermissions.isEmpty()) { |
| 170 | requestPermissions(missingPermissions); |
Scott Randolph | 75d2f12 | 2018-07-11 22:04:55 -0700 | [diff] [blame] | 171 | // The callback with premission results will take care of calling initSensors for us |
Vitalii Tomkiv | db66ed6 | 2016-04-07 18:10:39 -0700 | [diff] [blame] | 172 | } else { |
| 173 | initSensors(); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | private Set<String> checkExistingPermissions() { |
| 178 | Set<String> missingPermissions = new HashSet<String>(); |
| 179 | for (String permission : REQUIRED_PERMISSIONS) { |
Srinivas Visvanathan | f854423 | 2017-03-24 11:52:53 -0700 | [diff] [blame] | 180 | if (mActivity.checkSelfPermission(permission) |
Vitalii Tomkiv | db66ed6 | 2016-04-07 18:10:39 -0700 | [diff] [blame] | 181 | == PackageManager.PERMISSION_GRANTED) { |
| 182 | mActivePermissions.add(permission); |
| 183 | } else { |
| 184 | missingPermissions.add(permission); |
| 185 | } |
| 186 | } |
| 187 | return missingPermissions; |
| 188 | } |
| 189 | |
| 190 | private void requestPermissions(Set<String> permissions) { |
| 191 | Log.d(TAG, "requesting additional permissions=" + permissions); |
| 192 | |
| 193 | requestPermissions(permissions.toArray(new String[permissions.size()]), |
| 194 | KS_PERMISSIONS_REQUEST); |
| 195 | } |
| 196 | |
| 197 | @Override |
| 198 | public void onRequestPermissionsResult(int requestCode, String[] permissions, |
| 199 | int[] grantResults) { |
| 200 | Log.d(TAG, "onRequestPermissionsResult reqCode=" + requestCode); |
| 201 | if (KS_PERMISSIONS_REQUEST == requestCode) { |
| 202 | for (int i=0; i<permissions.length; i++) { |
| 203 | if (grantResults[i] == PackageManager.PERMISSION_GRANTED) { |
| 204 | mActivePermissions.add(permissions[i]); |
| 205 | } |
| 206 | } |
| 207 | initSensors(); |
| 208 | } |
| 209 | } |
| 210 | |
Scott Randolph | 75d2f12 | 2018-07-11 22:04:55 -0700 | [diff] [blame] | 211 | private void refreshSensorInfoText() { |
Vitalii Tomkiv | db66ed6 | 2016-04-07 18:10:39 -0700 | [diff] [blame] | 212 | String summaryString; |
| 213 | synchronized (this) { |
| 214 | List<String> summary = new ArrayList<>(); |
| 215 | for (Integer i : supportedSensors) { |
| 216 | CarSensorEvent event = mEventMap.get(i); |
| 217 | switch (i) { |
Vitalii Tomkiv | db66ed6 | 2016-04-07 18:10:39 -0700 | [diff] [blame] | 218 | case CarSensorManager.SENSOR_TYPE_CAR_SPEED: |
| 219 | summary.add(getContext().getString(R.string.sensor_speed, |
| 220 | getTimestamp(event), |
Steve Paik | b805820 | 2018-06-06 18:11:55 -0700 | [diff] [blame] | 221 | event == null ? mNaString : event.getCarSpeedData(null).carSpeed)); |
Vitalii Tomkiv | db66ed6 | 2016-04-07 18:10:39 -0700 | [diff] [blame] | 222 | break; |
| 223 | case CarSensorManager.SENSOR_TYPE_RPM: |
| 224 | summary.add(getContext().getString(R.string.sensor_rpm, |
| 225 | getTimestamp(event), |
Steve Paik | b805820 | 2018-06-06 18:11:55 -0700 | [diff] [blame] | 226 | event == null ? mNaString : event.getRpmData(null).rpm)); |
Vitalii Tomkiv | db66ed6 | 2016-04-07 18:10:39 -0700 | [diff] [blame] | 227 | break; |
| 228 | case CarSensorManager.SENSOR_TYPE_ODOMETER: |
| 229 | summary.add(getContext().getString(R.string.sensor_odometer, |
| 230 | getTimestamp(event), |
Steve Paik | b805820 | 2018-06-06 18:11:55 -0700 | [diff] [blame] | 231 | event == null ? mNaString : event.getOdometerData(null).kms)); |
Vitalii Tomkiv | db66ed6 | 2016-04-07 18:10:39 -0700 | [diff] [blame] | 232 | break; |
| 233 | case CarSensorManager.SENSOR_TYPE_FUEL_LEVEL: |
Steve Paik | d0ebac2 | 2017-07-17 07:10:53 -0700 | [diff] [blame] | 234 | summary.add(getFuelLevel(event)); |
| 235 | break; |
| 236 | case CarSensorManager.SENSOR_TYPE_FUEL_DOOR_OPEN: |
| 237 | summary.add(getFuelDoorOpen(event)); |
Vitalii Tomkiv | db66ed6 | 2016-04-07 18:10:39 -0700 | [diff] [blame] | 238 | break; |
Kai | f571107 | 2018-06-07 16:25:46 -0700 | [diff] [blame] | 239 | case CarSensorManager.SENSOR_TYPE_IGNITION_STATE: |
| 240 | summary.add(getContext().getString(R.string.sensor_ignition_status, |
| 241 | getTimestamp(event), |
| 242 | event == null ? mNaString : |
| 243 | event.getIgnitionStateData(null).ignitionState)); |
| 244 | break; |
Vitalii Tomkiv | db66ed6 | 2016-04-07 18:10:39 -0700 | [diff] [blame] | 245 | case CarSensorManager.SENSOR_TYPE_PARKING_BRAKE: |
Gene Karshenboym | e73a146 | 2017-06-21 15:21:07 -0700 | [diff] [blame] | 246 | summary.add(getContext().getString(R.string.sensor_parking_brake, |
Vitalii Tomkiv | db66ed6 | 2016-04-07 18:10:39 -0700 | [diff] [blame] | 247 | getTimestamp(event), |
| 248 | event == null ? mNaString : |
Steve Paik | b805820 | 2018-06-06 18:11:55 -0700 | [diff] [blame] | 249 | event.getParkingBrakeData(null).isEngaged)); |
Vitalii Tomkiv | db66ed6 | 2016-04-07 18:10:39 -0700 | [diff] [blame] | 250 | break; |
| 251 | case CarSensorManager.SENSOR_TYPE_GEAR: |
| 252 | summary.add(getContext().getString(R.string.sensor_gear, |
| 253 | getTimestamp(event), |
Steve Paik | b805820 | 2018-06-06 18:11:55 -0700 | [diff] [blame] | 254 | event == null ? mNaString : event.getGearData(null).gear)); |
Vitalii Tomkiv | db66ed6 | 2016-04-07 18:10:39 -0700 | [diff] [blame] | 255 | break; |
| 256 | case CarSensorManager.SENSOR_TYPE_NIGHT: |
| 257 | summary.add(getContext().getString(R.string.sensor_night, |
| 258 | getTimestamp(event), |
Steve Paik | b805820 | 2018-06-06 18:11:55 -0700 | [diff] [blame] | 259 | event == null ? mNaString : event.getNightData(null).isNightMode)); |
Vitalii Tomkiv | db66ed6 | 2016-04-07 18:10:39 -0700 | [diff] [blame] | 260 | break; |
Kai | f571107 | 2018-06-07 16:25:46 -0700 | [diff] [blame] | 261 | case CarSensorManager.SENSOR_TYPE_ENV_OUTSIDE_TEMPERATURE: |
Vitalii Tomkiv | db66ed6 | 2016-04-07 18:10:39 -0700 | [diff] [blame] | 262 | String temperature = mNaString; |
Vitalii Tomkiv | db66ed6 | 2016-04-07 18:10:39 -0700 | [diff] [blame] | 263 | if (event != null) { |
Steve Paik | b805820 | 2018-06-06 18:11:55 -0700 | [diff] [blame] | 264 | CarSensorEvent.EnvironmentData env = event.getEnvironmentData(null); |
Vitalii Tomkiv | db66ed6 | 2016-04-07 18:10:39 -0700 | [diff] [blame] | 265 | temperature = Float.isNaN(env.temperature) ? temperature : |
| 266 | String.valueOf(env.temperature); |
Vitalii Tomkiv | db66ed6 | 2016-04-07 18:10:39 -0700 | [diff] [blame] | 267 | } |
| 268 | summary.add(getContext().getString(R.string.sensor_environment, |
Kai | f571107 | 2018-06-07 16:25:46 -0700 | [diff] [blame] | 269 | getTimestamp(event), temperature)); |
Vitalii Tomkiv | db66ed6 | 2016-04-07 18:10:39 -0700 | [diff] [blame] | 270 | break; |
Steve Paik | 289ab99 | 2017-07-11 22:40:57 -0700 | [diff] [blame] | 271 | case CarSensorManager.SENSOR_TYPE_WHEEL_TICK_DISTANCE: |
| 272 | if(event != null) { |
| 273 | CarSensorEvent.CarWheelTickDistanceData d = |
Steve Paik | b805820 | 2018-06-06 18:11:55 -0700 | [diff] [blame] | 274 | event.getCarWheelTickDistanceData(null); |
Steve Paik | 289ab99 | 2017-07-11 22:40:57 -0700 | [diff] [blame] | 275 | summary.add(getContext().getString(R.string.sensor_wheel_ticks, |
| 276 | getTimestamp(event), d.sensorResetCount, d.frontLeftWheelDistanceMm, |
| 277 | d.frontRightWheelDistanceMm, d.rearLeftWheelDistanceMm, |
| 278 | d.rearRightWheelDistanceMm)); |
| 279 | } else { |
| 280 | summary.add(getContext().getString(R.string.sensor_wheel_ticks, |
| 281 | getTimestamp(event), mNaString, mNaString, mNaString, mNaString, |
| 282 | mNaString)); |
| 283 | } |
Steve Paik | a281990 | 2017-07-18 15:51:26 -0700 | [diff] [blame] | 284 | // Get the config data |
Justin Paupore | 97b4df7 | 2019-02-12 19:40:39 -0800 | [diff] [blame] | 285 | CarSensorConfig c = mCarSensorManager.getSensorConfig( |
Steve Paik | a281990 | 2017-07-18 15:51:26 -0700 | [diff] [blame] | 286 | CarSensorManager.SENSOR_TYPE_WHEEL_TICK_DISTANCE); |
Justin Paupore | 97b4df7 | 2019-02-12 19:40:39 -0800 | [diff] [blame] | 287 | summary.add(getContext().getString(R.string.sensor_wheel_ticks_cfg, |
Steve Paik | a281990 | 2017-07-18 15:51:26 -0700 | [diff] [blame] | 288 | c.getInt(CarSensorConfig.WHEEL_TICK_DISTANCE_SUPPORTED_WHEELS), |
| 289 | c.getInt(CarSensorConfig.WHEEL_TICK_DISTANCE_FRONT_LEFT_UM_PER_TICK), |
| 290 | c.getInt(CarSensorConfig.WHEEL_TICK_DISTANCE_FRONT_RIGHT_UM_PER_TICK), |
| 291 | c.getInt(CarSensorConfig.WHEEL_TICK_DISTANCE_REAR_LEFT_UM_PER_TICK), |
| 292 | c.getInt(CarSensorConfig.WHEEL_TICK_DISTANCE_REAR_RIGHT_UM_PER_TICK))); |
Steve Paik | 289ab99 | 2017-07-11 22:40:57 -0700 | [diff] [blame] | 293 | break; |
| 294 | case CarSensorManager.SENSOR_TYPE_ABS_ACTIVE: |
| 295 | summary.add(getContext().getString(R.string.sensor_abs_is_active, |
| 296 | getTimestamp(event), event == null ? mNaString : |
Steve Paik | b805820 | 2018-06-06 18:11:55 -0700 | [diff] [blame] | 297 | event.getCarAbsActiveData(null).absIsActive)); |
Steve Paik | 289ab99 | 2017-07-11 22:40:57 -0700 | [diff] [blame] | 298 | break; |
| 299 | |
| 300 | case CarSensorManager.SENSOR_TYPE_TRACTION_CONTROL_ACTIVE: |
| 301 | summary.add( |
| 302 | getContext().getString(R.string.sensor_traction_control_is_active, |
| 303 | getTimestamp(event), event == null ? mNaString : |
Steve Paik | b805820 | 2018-06-06 18:11:55 -0700 | [diff] [blame] | 304 | event.getCarTractionControlActiveData(null) |
| 305 | .tractionControlIsActive)); |
Steve Paik | 289ab99 | 2017-07-11 22:40:57 -0700 | [diff] [blame] | 306 | break; |
Steve Paik | d0ebac2 | 2017-07-17 07:10:53 -0700 | [diff] [blame] | 307 | case CarSensorManager.SENSOR_TYPE_EV_BATTERY_LEVEL: |
| 308 | summary.add(getEvBatteryLevel(event)); |
| 309 | break; |
| 310 | case CarSensorManager.SENSOR_TYPE_EV_CHARGE_PORT_OPEN: |
| 311 | summary.add(getEvChargePortOpen(event)); |
| 312 | break; |
| 313 | case CarSensorManager.SENSOR_TYPE_EV_CHARGE_PORT_CONNECTED: |
| 314 | summary.add(getEvChargePortConnected(event)); |
| 315 | break; |
| 316 | case CarSensorManager.SENSOR_TYPE_EV_BATTERY_CHARGE_RATE: |
| 317 | summary.add(getEvChargeRate(event)); |
| 318 | break; |
smara | 0d26953 | 2018-03-29 13:48:13 -0700 | [diff] [blame] | 319 | case CarSensorManager.SENSOR_TYPE_ENGINE_OIL_LEVEL: |
| 320 | summary.add(getEngineOilLevel(event)); |
| 321 | break; |
Vitalii Tomkiv | db66ed6 | 2016-04-07 18:10:39 -0700 | [diff] [blame] | 322 | default: |
| 323 | // Should never happen. |
Steve Paik | b805820 | 2018-06-06 18:11:55 -0700 | [diff] [blame] | 324 | Log.w(TAG, "Unrecognized event type: " + toHexString(i)); |
Vitalii Tomkiv | db66ed6 | 2016-04-07 18:10:39 -0700 | [diff] [blame] | 325 | } |
| 326 | } |
| 327 | summaryString = TextUtils.join("\n", summary); |
| 328 | } |
| 329 | mHandler.post(new Runnable() { |
| 330 | @Override |
| 331 | public void run() { |
| 332 | mSensorInfo.setText(summaryString); |
| 333 | } |
| 334 | }); |
| 335 | } |
| 336 | |
| 337 | private String getTimestamp(CarSensorEvent event) { |
| 338 | if (event == null) { |
| 339 | return mNaString; |
| 340 | } |
Steve Paik | aceeab1 | 2018-11-02 10:52:21 -0700 | [diff] [blame] | 341 | return Double.toString(event.timestamp / (1000L * 1000L * 1000L)) + " seconds"; |
Vitalii Tomkiv | db66ed6 | 2016-04-07 18:10:39 -0700 | [diff] [blame] | 342 | } |
| 343 | |
Scott Randolph | 75d2f12 | 2018-07-11 22:04:55 -0700 | [diff] [blame] | 344 | private String getTimestampNow() { |
Steve Paik | aceeab1 | 2018-11-02 10:52:21 -0700 | [diff] [blame] | 345 | return Double.toString(System.nanoTime() / (1000L * 1000L * 1000L)) + " seconds"; |
Scott Randolph | 75d2f12 | 2018-07-11 22:04:55 -0700 | [diff] [blame] | 346 | } |
| 347 | |
Steve Paik | d0ebac2 | 2017-07-17 07:10:53 -0700 | [diff] [blame] | 348 | private String getFuelLevel(CarSensorEvent event) { |
| 349 | String fuelLevel = mNaString; |
| 350 | if(event != null) { |
Steve Paik | b805820 | 2018-06-06 18:11:55 -0700 | [diff] [blame] | 351 | fuelLevel = String.valueOf(event.getFuelLevelData(null).level); |
Steve Paik | d0ebac2 | 2017-07-17 07:10:53 -0700 | [diff] [blame] | 352 | } |
| 353 | return getContext().getString(R.string.sensor_fuel_level, getTimestamp(event), fuelLevel); |
| 354 | } |
| 355 | |
| 356 | private String getFuelDoorOpen(CarSensorEvent event) { |
| 357 | String fuelDoorOpen = mNaString; |
| 358 | if(event != null) { |
Steve Paik | b805820 | 2018-06-06 18:11:55 -0700 | [diff] [blame] | 359 | fuelDoorOpen = String.valueOf(event.getCarFuelDoorOpenData(null).fuelDoorIsOpen); |
Steve Paik | d0ebac2 | 2017-07-17 07:10:53 -0700 | [diff] [blame] | 360 | } |
| 361 | return getContext().getString(R.string.sensor_fuel_door_open, getTimestamp(event), |
| 362 | fuelDoorOpen); |
| 363 | } |
| 364 | |
Steve Paik | d0ebac2 | 2017-07-17 07:10:53 -0700 | [diff] [blame] | 365 | private String getEvBatteryLevel(CarSensorEvent event) { |
| 366 | String evBatteryLevel = mNaString; |
| 367 | if(event != null) { |
Steve Paik | b805820 | 2018-06-06 18:11:55 -0700 | [diff] [blame] | 368 | evBatteryLevel = String.valueOf(event.getCarEvBatteryLevelData(null).evBatteryLevel); |
Steve Paik | d0ebac2 | 2017-07-17 07:10:53 -0700 | [diff] [blame] | 369 | } |
| 370 | return getContext().getString(R.string.sensor_ev_battery_level, getTimestamp(event), |
| 371 | evBatteryLevel); |
| 372 | } |
| 373 | |
| 374 | private String getEvChargePortOpen(CarSensorEvent event) { |
| 375 | String evChargePortOpen = mNaString; |
| 376 | if(event != null) { |
| 377 | evChargePortOpen = String.valueOf( |
Steve Paik | b805820 | 2018-06-06 18:11:55 -0700 | [diff] [blame] | 378 | event.getCarEvChargePortOpenData(null).evChargePortIsOpen); |
Steve Paik | d0ebac2 | 2017-07-17 07:10:53 -0700 | [diff] [blame] | 379 | } |
| 380 | return getContext().getString(R.string.sensor_ev_charge_port_is_open, getTimestamp(event), |
| 381 | evChargePortOpen); |
| 382 | } |
| 383 | |
| 384 | private String getEvChargePortConnected(CarSensorEvent event) { |
| 385 | String evChargePortConnected = mNaString; |
| 386 | if(event != null) { |
| 387 | evChargePortConnected = String.valueOf( |
Steve Paik | b805820 | 2018-06-06 18:11:55 -0700 | [diff] [blame] | 388 | event.getCarEvChargePortConnectedData(null).evChargePortIsConnected); |
Steve Paik | d0ebac2 | 2017-07-17 07:10:53 -0700 | [diff] [blame] | 389 | } |
| 390 | return getContext().getString(R.string.sensor_ev_charge_port_is_connected, |
| 391 | getTimestamp(event), evChargePortConnected); |
| 392 | } |
| 393 | |
| 394 | private String getEvChargeRate(CarSensorEvent event) { |
| 395 | String evChargeRate = mNaString; |
| 396 | if(event != null) { |
Steve Paik | b805820 | 2018-06-06 18:11:55 -0700 | [diff] [blame] | 397 | evChargeRate = String.valueOf(event.getCarEvBatteryChargeRateData(null).evChargeRate); |
Steve Paik | d0ebac2 | 2017-07-17 07:10:53 -0700 | [diff] [blame] | 398 | } |
| 399 | return getContext().getString(R.string.sensor_ev_charge_rate, getTimestamp(event), |
| 400 | evChargeRate); |
| 401 | } |
smara | 0d26953 | 2018-03-29 13:48:13 -0700 | [diff] [blame] | 402 | |
| 403 | private String getEngineOilLevel(CarSensorEvent event) { |
| 404 | String engineOilLevel = mNaString; |
| 405 | if(event != null) { |
Steve Paik | b805820 | 2018-06-06 18:11:55 -0700 | [diff] [blame] | 406 | engineOilLevel = String.valueOf(event.getCarEngineOilLevelData(null).engineOilLevel); |
smara | 0d26953 | 2018-03-29 13:48:13 -0700 | [diff] [blame] | 407 | } |
| 408 | return getContext().getString(R.string.sensor_oil_level, getTimestamp(event), |
| 409 | engineOilLevel); |
Scott Randolph | 75d2f12 | 2018-07-11 22:04:55 -0700 | [diff] [blame] | 410 | } |
smara | 0d26953 | 2018-03-29 13:48:13 -0700 | [diff] [blame] | 411 | |
Scott Randolph | 75d2f12 | 2018-07-11 22:04:55 -0700 | [diff] [blame] | 412 | public class LocationInfoTextUpdateListener { |
| 413 | public void setLocationField(String value) { |
| 414 | setTimestampedTextField(mLocationInfo, value); |
| 415 | } |
| 416 | |
| 417 | public void setAccelField(String value) { |
| 418 | setTimestampedTextField(mAccelInfo, value); |
| 419 | } |
| 420 | |
| 421 | public void setGyroField(String value) { |
| 422 | setTimestampedTextField(mGyroInfo, value); |
| 423 | } |
| 424 | |
| 425 | public void setMagField(String value) { |
| 426 | setTimestampedTextField(mMagInfo, value); |
| 427 | } |
| 428 | |
| 429 | private void setTimestampedTextField(TextView text, String value) { |
| 430 | synchronized (SensorsTestFragment.this) { |
| 431 | text.setText(getTimestampNow() + ": " + value); |
| 432 | Log.d(TAG, "setText: " + value); |
| 433 | } |
| 434 | } |
smara | 0d26953 | 2018-03-29 13:48:13 -0700 | [diff] [blame] | 435 | } |
Vitalii Tomkiv | db66ed6 | 2016-04-07 18:10:39 -0700 | [diff] [blame] | 436 | } |