List of usage examples for android.hardware Sensor TYPE_PRESSURE
int TYPE_PRESSURE
To view the source code for android.hardware Sensor TYPE_PRESSURE.
Click Source Link
From source file:it.unime.mobility4ckan.MainActivity.java
private void createListView() { overviewList.clear();/*from w w w. j a v a2 s .c o m*/ Overview overviewTitle = new Overview(getApplicationContext()); overviewTitle.setSensorName("Nome Sensore"); overviewTitle.setCurrentValue("Valore Corrente"); overviewTitle.setValueSend("Valore Inviato"); overviewList.add(overviewTitle); Overview overview0 = new Overview(getApplicationContext()); overview0.setSensorName("Speed"); overview0.setCurrentValue("" + mySensor.getCurrentSpeed()); overview0.setValueSend(lastSpeedValue); overviewList.add(overview0); for (int k = 0; k < sensorList.size(); k++) { Overview overview = new Overview(getApplicationContext()); switch (sensorList.get(k).getType()) { case Sensor.TYPE_AMBIENT_TEMPERATURE: // Gradi Celsius (C) overview.setSensorName("Temperature"); overview.setCurrentValue("" + mySensor.getCurrentTemp()); overview.setValueSend(lastTempValue); overviewList.add(overview); break; case Sensor.TYPE_PRESSURE: overview.setSensorName("Pressure"); overview.setCurrentValue("" + mySensor.getCurrentPressure()); overview.setValueSend(lastPressureValue); overviewList.add(overview); break; case Sensor.TYPE_LIGHT: // lx overview.setSensorName("Light"); overview.setCurrentValue("" + mySensor.getCurrentLight()); overview.setValueSend(lastLightValue); overviewList.add(overview); break; case Sensor.TYPE_ACCELEROMETER: // m/s2 overview.setSensorName("Accelerometer"); overview.setCurrentValue("" + mySensor.getCurrentAcceleration()); overview.setValueSend(lastAccelerationValue); overviewList.add(overview); break; case Sensor.TYPE_GYROSCOPE: // rad/s overview.setSensorName("Gyroscope"); overview.setCurrentValue("" + mySensor.getCurrentGyroscope()); overview.setValueSend(lastGyroscopeValue); overviewList.add(overview); break; case Sensor.TYPE_MAGNETIC_FIELD: // T overview.setSensorName("Magnetic Field"); overview.setCurrentValue("" + mySensor.getCurrentMagnetic()); overview.setValueSend(lastMagneticValue); overviewList.add(overview); break; case Sensor.TYPE_PROXIMITY: // cm overview.setSensorName("Proximity"); overview.setCurrentValue("" + mySensor.getCurrentProximity()); overview.setValueSend(lastProximityValue); overviewList.add(overview); break; case Sensor.TYPE_ROTATION_VECTOR: // unita di misura sconosciuta overview.setSensorName("Rotation Vector"); overview.setCurrentValue("" + mySensor.getCurrentRotation()); overview.setValueSend(lastRotationValue); overviewList.add(overview); break; case Sensor.TYPE_GRAVITY: // m/s2 overview.setSensorName("Gravity"); overview.setCurrentValue("" + mySensor.getCurrentGravity()); overview.setValueSend(lastGravityValue); overviewList.add(overview); break; case Sensor.TYPE_LINEAR_ACCELERATION: // m/s2 overview.setSensorName("Linear Acceleration"); overview.setCurrentValue("" + mySensor.getCurrentLinearAcceleration()); overview.setValueSend(lastLinearAccelerationValue); overviewList.add(overview); break; case Sensor.TYPE_RELATIVE_HUMIDITY: // % overview.setSensorName("Relative Humidity"); overview.setCurrentValue("" + mySensor.getCurrentHumidity()); overview.setValueSend(lastRelativeHumidity); overviewList.add(overview); break; default: break; } } listView.setAdapter(new OverviewAdapter(getApplicationContext(), overviewList)); }
From source file:ch.bfh.sensordataprocessor.sensor.SensorDisplayFragment.java
/** * @see android.hardware.SensorEventListener#onSensorChanged(android.hardware.SensorEvent) *///from ww w. j a va 2s .c om @Override public void onSensorChanged(SensorEvent event) { onAccuracyChanged(event.sensor, event.accuracy); timestampLabel.setVisibility(View.VISIBLE); timestamp.setVisibility(View.VISIBLE); timestamp.setText(String.valueOf(event.timestamp)); timestampUnits.setVisibility(View.VISIBLE); switch (event.sensor.getType()) { case Sensor.TYPE_ACCELEROMETER: showEventData("Acceleration - gravity on axis", ACCELERATION_UNITS, event.values[0], event.values[1], event.values[2]); break; case Sensor.TYPE_MAGNETIC_FIELD: showEventData("Abient Magnetic Field", "uT", event.values[0], event.values[1], event.values[2]); break; case Sensor.TYPE_GYROSCOPE: showEventData("Angular speed around axis", "radians/sec", event.values[0], event.values[1], event.values[2]); break; case Sensor.TYPE_LIGHT: showEventData("Ambient light", "lux", event.values[0]); break; case Sensor.TYPE_PRESSURE: showEventData("Atmospheric pressure", "hPa", event.values[0]); break; case Sensor.TYPE_PROXIMITY: showEventData("Distance", "cm", event.values[0]); break; case Sensor.TYPE_GRAVITY: showEventData("Gravity", ACCELERATION_UNITS, event.values[0], event.values[1], event.values[2]); break; case Sensor.TYPE_LINEAR_ACCELERATION: showEventData("Acceleration (not including gravity)", ACCELERATION_UNITS, event.values[0], event.values[1], event.values[2]); break; case Sensor.TYPE_ROTATION_VECTOR: showEventData("Rotation Vector", null, event.values[0], event.values[1], event.values[2]); xAxisLabel.setText("x*sin(" + THETA + "/2)"); yAxisLabel.setText("y*sin(" + THETA + "/2)"); zAxisLabel.setText("z*sin(" + THETA + "/2)"); if (event.values.length == 4) { cosLabel.setVisibility(View.VISIBLE); cos.setVisibility(View.VISIBLE); cos.setText(String.valueOf(event.values[3])); } break; case Sensor.TYPE_ORIENTATION: showEventData("Angle", "Degrees", event.values[0], event.values[1], event.values[2]); xAxisLabel.setText(R.string.azimuthLabel); yAxisLabel.setText(R.string.pitchLabel); zAxisLabel.setText(R.string.rollLabel); break; case Sensor.TYPE_RELATIVE_HUMIDITY: showEventData("Relatice ambient air humidity", "%", event.values[0]); break; case Sensor.TYPE_AMBIENT_TEMPERATURE: showEventData("Ambien temperature", "degree Celcius", event.values[0]); break; } }
From source file:de.tudarmstadt.dvs.myhealthassistant.myhealthhub.services.InternalSensorService.java
@Override public void onSensorChanged(SensorEvent event) { if (pref == null) pref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); int mSensorType = event.sensor.getType(); if (mSensorType == Sensor.TYPE_ACCELEROMETER) { int x = Math.round(event.values[0]); int y = Math.round(event.values[1]); int z = Math.round(event.values[2]); // using ACCELEROMETER_KNEE only as a demonstration, // in practice new event can be created AccDeviceSensorEvent accEvt = new AccDeviceSensorEvent("eventID", getTimestamp(), "producerID", SensorReadingEvent.ACCELEROMETER_ON_DEVICE, getTimestamp(), x, y, z, x, y, z); sendToChannel(accEvt, AbstractChannel.RECEIVER); // for external // apps//from w w w . j a v a2 s .c o m // subscription gotAccEvent(getTimestamp(), Sensor.TYPE_ACCELEROMETER, x, y, z); // for // on device // graphViews // using } else if (mSensorType == Sensor.TYPE_LIGHT) { float x = event.values[0]; // using ambientLightEvent only as a demonstration, // in practice new event can be created AmbientLightEvent lightEvnt = new AmbientLightEvent("eventID", getTimestamp(), "producerID", SensorReadingEvent.AMBIENT_LIGHT, getTimestamp(), "location", "object", x, AmbientLightEvent.UNIT_LUX); sendToChannel(lightEvnt, AbstractChannel.RECEIVER); gotLightEvent(getTimestamp(), Sensor.TYPE_LIGHT, x); } else if (mSensorType == Sensor.TYPE_PRESSURE) { float x = event.values[0]; AmbientPressureEvent temEvnt = new AmbientPressureEvent("eventID", getTimestamp(), "producerID", SensorReadingEvent.AMBIENT_PRESSURE, getTimestamp(), "location", "object", x, AmbientPressureEvent.UNIT_HPA); sendToChannel(temEvnt, AbstractChannel.RECEIVER); gotLightEvent(getTimestamp(), Sensor.TYPE_PRESSURE, x); } else if (mSensorType == Sensor.TYPE_PROXIMITY) { float x = event.values[0]; ProximityEvent evnt = new ProximityEvent("eventID", getTimestamp(), "producerID", SensorReadingEvent.PROXIMITY, getTimestamp(), "location", "object", x, ""); sendToChannel(evnt, AbstractChannel.RECEIVER); gotLightEvent(getTimestamp(), Sensor.TYPE_PROXIMITY, x); } else if (mSensorType == Sensor.TYPE_AMBIENT_TEMPERATURE) { float x = event.values[0]; TemperatureEvent evnt = new TemperatureEvent("eventID", getTimestamp(), "producerID", SensorReadingEvent.ROOM_TEMPERATURE, getTimestamp(), "location", "object", x, TemperatureEvent.UNIT_CELSIUS); sendToChannel(evnt, AbstractChannel.RECEIVER); gotLightEvent(getTimestamp(), Sensor.TYPE_AMBIENT_TEMPERATURE, x); } else if (mSensorType == Sensor.TYPE_RELATIVE_HUMIDITY) { float x = event.values[0]; HumidityEvent evnt = new HumidityEvent("eventID", getTimestamp(), "producerID", SensorReadingEvent.HUMIDITY, getTimestamp(), "location", "object", x); sendToChannel(evnt, AbstractChannel.RECEIVER); gotLightEvent(getTimestamp(), Sensor.TYPE_RELATIVE_HUMIDITY, x); } else if (mSensorType == Sensor.TYPE_GYROSCOPE) { int x = Math.round(event.values[0]); int y = Math.round(event.values[1]); int z = Math.round(event.values[2]); GyroscopeSensorEvent gSEvnt = new GyroscopeSensorEvent("eventID", getTimestamp(), "producerID", SensorReadingEvent.GYROSCOPE, getTimestamp(), x, y, z, x, y, z); sendToChannel(gSEvnt, AbstractChannel.RECEIVER); gotAccEvent(getTimestamp(), Sensor.TYPE_GYROSCOPE, x, y, z); } else if (mSensorType == Sensor.TYPE_MAGNETIC_FIELD) { int x = Math.round(event.values[0]); int y = Math.round(event.values[1]); int z = Math.round(event.values[2]); MagneticSensorEvent gSEvnt = new MagneticSensorEvent("eventID", getTimestamp(), "producerID", SensorReadingEvent.MAGNETIC_FIELD, getTimestamp(), x, y, z, x, y, z); sendToChannel(gSEvnt, AbstractChannel.RECEIVER); gotAccEvent(getTimestamp(), Sensor.TYPE_MAGNETIC_FIELD, x, y, z); } else if (mSensorType == Sensor.TYPE_LINEAR_ACCELERATION) { int x = Math.round(event.values[0]); int y = Math.round(event.values[1]); int z = Math.round(event.values[2]); // using ACCELEROMETER_KNEE only as a demonstration, // in practice new event can be created AccDeviceSensorEvent accEvt = new AccDeviceSensorEvent("eventID", getTimestamp(), "producerID", SensorReadingEvent.ACCELEROMETER_ON_DEVICE, getTimestamp(), x, y, z, x, y, z); sendToChannel(accEvt, AbstractChannel.RECEIVER); gotAccEvent(getTimestamp(), Sensor.TYPE_LINEAR_ACCELERATION, x, y, z); } }
From source file:com.vonglasow.michael.satstat.SensorSectionFragment.java
/** * Called by {@link MainActivity} when a sensor's reading changes. Updates sensor display. *//*from w w w.ja v a 2 s .c om*/ public void onSensorChanged(SensorEvent event) { switch (event.sensor.getType()) { case Sensor.TYPE_ACCELEROMETER: accX.setText(String.format("%." + mAccSensorRes + "f", event.values[0])); accY.setText(String.format("%." + mAccSensorRes + "f", event.values[1])); accZ.setText(String.format("%." + mAccSensorRes + "f", event.values[2])); accTotal.setText(String.format("%." + mAccSensorRes + "f", Math.sqrt( Math.pow(event.values[0], 2) + Math.pow(event.values[1], 2) + Math.pow(event.values[2], 2)))); accStatus.setTextColor(getResources().getColor(accuracyToColor(event.accuracy))); break; case Sensor.TYPE_ORIENTATION: orAzimuth.setText(String.format("%.0f%s", event.values[0], getString(R.string.unit_degree))); orAziText.setText(MainActivity.formatOrientation(this.getContext(), event.values[0])); orPitch.setText(String.format("%.0f%s", event.values[1], getString(R.string.unit_degree))); orRoll.setText(String.format("%.0f%s", event.values[2], getString(R.string.unit_degree))); orStatus.setTextColor(getResources().getColor(accuracyToColor(event.accuracy))); break; case Sensor.TYPE_GYROSCOPE: rotX.setText(String.format("%." + mGyroSensorRes + "f", event.values[0])); rotY.setText(String.format("%." + mGyroSensorRes + "f", event.values[1])); rotZ.setText(String.format("%." + mGyroSensorRes + "f", event.values[2])); rotTotal.setText(String.format("%." + mGyroSensorRes + "f", Math.sqrt( Math.pow(event.values[0], 2) + Math.pow(event.values[1], 2) + Math.pow(event.values[2], 2)))); rotStatus.setTextColor(getResources().getColor(accuracyToColor(event.accuracy))); break; case Sensor.TYPE_MAGNETIC_FIELD: magX.setText(String.format("%." + mMagSensorRes + "f", event.values[0])); magY.setText(String.format("%." + mMagSensorRes + "f", event.values[1])); magZ.setText(String.format("%." + mMagSensorRes + "f", event.values[2])); magTotal.setText(String.format("%." + mMagSensorRes + "f", Math.sqrt( Math.pow(event.values[0], 2) + Math.pow(event.values[1], 2) + Math.pow(event.values[2], 2)))); magStatus.setTextColor(getResources().getColor(accuracyToColor(event.accuracy))); break; case Sensor.TYPE_LIGHT: light.setText(String.format("%." + mLightSensorRes + "f", event.values[0])); lightStatus.setTextColor(getResources().getColor(accuracyToColor(event.accuracy))); break; case Sensor.TYPE_PROXIMITY: proximity.setText(String.format("%." + mProximitySensorRes + "f", event.values[0])); proximityStatus.setTextColor(getResources().getColor(accuracyToColor(event.accuracy))); break; case Sensor.TYPE_PRESSURE: metPressure.setText(String.format("%." + mPressureSensorRes + "f", event.values[0])); pressureStatus.setTextColor(getResources().getColor(accuracyToColor(event.accuracy))); break; case Sensor.TYPE_RELATIVE_HUMIDITY: metHumid.setText(String.format("%." + mHumiditySensorRes + "f", event.values[0])); humidStatus.setTextColor(getResources().getColor(accuracyToColor(event.accuracy))); break; case Sensor.TYPE_AMBIENT_TEMPERATURE: metTemp.setText(String.format("%." + mTempSensorRes + "f", event.values[0])); tempStatus.setTextColor(getResources().getColor(accuracyToColor(event.accuracy))); break; } }
From source file:tv.piratemedia.flightcontroller.BluetoothComputerFragment.java
public void setupAtimiter() { SensorManager mSensorManager = (SensorManager) getActivity().getSystemService(Context.SENSOR_SERVICE); List<Sensor> sensors = mSensorManager.getSensorList(Sensor.TYPE_PRESSURE); SensorEventListener sensorListener = new SensorEventListener() { @Override//www . j a v a 2 s .co m public void onSensorChanged(SensorEvent event) { if (currentPreasure > event.values[0] - 0.5 || currentPreasure < event.values[0] + 0.5) { currentPreasure = event.values[0]; currentAltitude = SensorManager.getAltitude(SensorManager.PRESSURE_STANDARD_ATMOSPHERE, currentPreasure); JSONObject msg = new JSONObject(); try { msg.put("action", "altitude_update"); msg.put("value", currentAltitude); } catch (JSONException e) { e.printStackTrace(); } sendTextMessage(msg.toString()); } } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { } }; if (sensors.size() > 0) { sensor = sensors.get(0); mSensorManager.registerListener(sensorListener, sensor, SensorManager.SENSOR_DELAY_NORMAL); } else { } }
From source file:it.unime.mobility4ckan.MainActivity.java
void sendTask(final boolean shouldUpdateCountdown) { if (!isDeviceCurrentSensorsRegistered && !isRegistering) { new RegisterDevice(this).execute(datasetName); isRegistering = true;/*from w ww. j ava2s . com*/ return; } if (!isDeviceCurrentSensorsRegistered || !isGPSReady) { return; } String currentSpeedValue = "" + mySensor.getCurrentSpeed(); getSensorDataToSend("speedDatastoreUUID", "Speed", currentSpeedValue); lastSpeedValue = currentSpeedValue; for (int k = 0; k < sensorList.size(); k++) { switch (sensorList.get(k).getType()) { case Sensor.TYPE_AMBIENT_TEMPERATURE: // Gradi Celsius (C) String currentTempValue = "" + mySensor.getCurrentTemp(); getSensorDataToSend("temperatureDatastoreUUID", "Temperature", currentTempValue); lastTempValue = currentTempValue; break; case Sensor.TYPE_PRESSURE: String currentPressureValue = "" + mySensor.getCurrentPressure(); getSensorDataToSend("pressureDatastoreUUID", "Pressure", currentPressureValue); lastPressureValue = currentPressureValue; break; case Sensor.TYPE_LIGHT: // lx String currentLightValue = "" + mySensor.getCurrentLight(); getSensorDataToSend("lightDatastoreUUID", "Light", currentLightValue); lastLightValue = currentLightValue; break; case Sensor.TYPE_ACCELEROMETER: // m/s2 String currentAccelerationValue = mySensor.getCurrentAcceleration(); getSensorDataToSend("accelerometerDatastoreUUID", "Accelerometer", currentAccelerationValue); lastAccelerationValue = currentAccelerationValue; break; case Sensor.TYPE_GYROSCOPE: // rad/s String currentGyroscopeValue = mySensor.getCurrentGyroscope(); getSensorDataToSend("gyroscopeDatastoreUUID", "Gyroscope", currentGyroscopeValue); lastGyroscopeValue = currentGyroscopeValue; break; case Sensor.TYPE_MAGNETIC_FIELD: // T String currentMagneticValue = mySensor.getCurrentMagnetic(); getSensorDataToSend("magneticFieldDatastoreUUID", "MagneticField", currentMagneticValue); lastMagneticValue = currentMagneticValue; break; case Sensor.TYPE_PROXIMITY: // cm String currentProximityValue = "" + mySensor.getCurrentProximity(); getSensorDataToSend("proximityDatastoreUUID", "Proximity", currentProximityValue); lastProximityValue = currentProximityValue; break; case Sensor.TYPE_ROTATION_VECTOR: // unita di misura sconosciuta String currentRotationValue = mySensor.getCurrentRotation(); getSensorDataToSend("rotationVector", "RotationVector", currentRotationValue); lastRotationValue = currentRotationValue; break; case Sensor.TYPE_GRAVITY: // m/s2 String currentGravityValue = mySensor.getCurrentGravity(); getSensorDataToSend("gravity", "Gravity", currentGravityValue); lastGravityValue = currentGravityValue; break; case Sensor.TYPE_LINEAR_ACCELERATION: // m/s2 String currentLinearAccelerationValue = mySensor.getCurrentLinearAcceleration(); getSensorDataToSend("linearAcceleration", "LinearAcceleration", currentLinearAccelerationValue); lastLinearAccelerationValue = currentLinearAccelerationValue; break; case Sensor.TYPE_RELATIVE_HUMIDITY: // % String currentRelativeHumidity = "" + mySensor.getCurrentHumidity(); getSensorDataToSend("relativeHumidity", "RelativeHumidity", currentRelativeHumidity); lastRelativeHumidity = currentRelativeHumidity; break; default: break; } } runOnUiThread(new Runnable() { @Override public void run() { if (shouldUpdateCountdown) { new CountDownTimer(countdown, 1000) { public void onTick(long millisUntilFinished) { countdownText.setText("" + millisUntilFinished / 1000); } public void onFinish() { } }.start(); } } }); }
From source file:com.vonglasow.michael.satstat.ui.MainActivity.java
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); defaultUEH = Thread.getDefaultUncaughtExceptionHandler(); Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() { public void uncaughtException(Thread t, Throwable e) { Context c = getApplicationContext(); File dumpDir = c.getExternalFilesDir(null); DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss", Locale.ROOT); fmt.setTimeZone(TimeZone.getTimeZone("UTC")); String fileName = String.format("satstat-%s.log", fmt.format(new Date(System.currentTimeMillis()))); File dumpFile = new File(dumpDir, fileName); PrintStream s;/*from w ww . ja v a2s . c om*/ try { InputStream buildInStream = getResources().openRawResource(R.raw.build); s = new PrintStream(dumpFile); s.append("SatStat build: "); int i; try { i = buildInStream.read(); while (i != -1) { s.write(i); i = buildInStream.read(); } buildInStream.close(); } catch (IOException e1) { e1.printStackTrace(); } s.append("\n\n"); e.printStackTrace(s); s.flush(); s.close(); Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); Uri contentUri = Uri.fromFile(dumpFile); mediaScanIntent.setData(contentUri); c.sendBroadcast(mediaScanIntent); } catch (FileNotFoundException e2) { e2.printStackTrace(); } defaultUEH.uncaughtException(t, e); } }); mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); mSharedPreferences.registerOnSharedPreferenceChangeListener(this); prefUnitType = mSharedPreferences.getBoolean(Const.KEY_PREF_UNIT_TYPE, prefUnitType); prefKnots = mSharedPreferences.getBoolean(Const.KEY_PREF_KNOTS, prefKnots); prefCoord = Integer .valueOf(mSharedPreferences.getString(Const.KEY_PREF_COORD, Integer.toString(prefCoord))); prefUtc = mSharedPreferences.getBoolean(Const.KEY_PREF_UTC, prefUtc); prefCid = mSharedPreferences.getBoolean(Const.KEY_PREF_CID, prefCid); prefCid2 = mSharedPreferences.getBoolean(Const.KEY_PREF_CID2, prefCid2); prefWifiSort = Integer .valueOf(mSharedPreferences.getString(Const.KEY_PREF_WIFI_SORT, Integer.toString(prefWifiSort))); prefMapOffline = mSharedPreferences.getBoolean(Const.KEY_PREF_MAP_OFFLINE, prefMapOffline); prefMapPath = mSharedPreferences.getString(Const.KEY_PREF_MAP_PATH, prefMapPath); ActionBar actionBar = getSupportActionBar(); setContentView(R.layout.activity_main); // Find out default screen orientation Configuration config = getResources().getConfiguration(); WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE); int rot = wm.getDefaultDisplay().getRotation(); isWideScreen = (config.orientation == Configuration.ORIENTATION_LANDSCAPE && (rot == Surface.ROTATION_0 || rot == Surface.ROTATION_180) || config.orientation == Configuration.ORIENTATION_PORTRAIT && (rot == Surface.ROTATION_90 || rot == Surface.ROTATION_270)); Log.d(TAG, "isWideScreen=" + Boolean.toString(isWideScreen)); // Create the adapter that will return a fragment for each of the // primary sections of the app. mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); // Set up the ViewPager with the sections adapter. mViewPager = (ViewPager) findViewById(R.id.pager); mViewPager.setAdapter(mSectionsPagerAdapter); Context ctx = new ContextThemeWrapper(getApplication(), R.style.AppTheme); mTabLayout = new TabLayout(ctx); LinearLayout.LayoutParams mTabLayoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); mTabLayout.setLayoutParams(mTabLayoutParams); for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) { TabLayout.Tab newTab = mTabLayout.newTab(); newTab.setIcon(mSectionsPagerAdapter.getPageIcon(i)); mTabLayout.addTab(newTab); } actionBar.setDisplayShowCustomEnabled(true); actionBar.setCustomView(mTabLayout); mTabLayout.setOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager)); mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabLayout)); // This is needed by the mapsforge library. AndroidGraphicFactory.createInstance(this.getApplication()); // Get system services for event delivery locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); mOrSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION); mAccSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); mGyroSensor = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE); mMagSensor = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD); mLightSensor = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT); mProximitySensor = sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY); mPressureSensor = sensorManager.getDefaultSensor(Sensor.TYPE_PRESSURE); mHumiditySensor = sensorManager.getDefaultSensor(Sensor.TYPE_RELATIVE_HUMIDITY); mTempSensor = sensorManager.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE); telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE); }
From source file:com.vonglasow.michael.satstat.ui.MainActivity.java
/** * Called when a sensor's reading changes. Updates sensor display and rotates sky plot according * to bearing./*from w ww . j av a2 s . c o m*/ */ public void onSensorChanged(SensorEvent event) { //to enforce sensor rate boolean isRateElapsed = false; switch (event.sensor.getType()) { case Sensor.TYPE_ACCELEROMETER: isRateElapsed = (event.timestamp / 1000) - mAccLast >= iSensorRate; // if Z acceleration is greater than X/Y combined, lock rotation, else unlock if (Math.pow(event.values[2], 2) > Math.pow(event.values[0], 2) + Math.pow(event.values[1], 2)) { // workaround (SCREEN_ORIENTATION_LOCK is unsupported on API < 18) if (isWideScreen) setRequestedOrientation( OR_FROM_ROT_WIDE[this.getWindowManager().getDefaultDisplay().getRotation()]); else setRequestedOrientation( OR_FROM_ROT_TALL[this.getWindowManager().getDefaultDisplay().getRotation()]); } else { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); } break; case Sensor.TYPE_ORIENTATION: isRateElapsed = (event.timestamp / 1000) - mOrLast >= iSensorRate; break; case Sensor.TYPE_GYROSCOPE: isRateElapsed = (event.timestamp / 1000) - mGyroLast >= iSensorRate; break; case Sensor.TYPE_MAGNETIC_FIELD: isRateElapsed = (event.timestamp / 1000) - mMagLast >= iSensorRate; break; case Sensor.TYPE_LIGHT: isRateElapsed = (event.timestamp / 1000) - mLightLast >= iSensorRate; break; case Sensor.TYPE_PROXIMITY: isRateElapsed = (event.timestamp / 1000) - mProximityLast >= iSensorRate; break; case Sensor.TYPE_PRESSURE: isRateElapsed = (event.timestamp / 1000) - mPressureLast >= iSensorRate; break; case Sensor.TYPE_RELATIVE_HUMIDITY: isRateElapsed = (event.timestamp / 1000) - mHumidityLast >= iSensorRate; break; case Sensor.TYPE_AMBIENT_TEMPERATURE: isRateElapsed = (event.timestamp / 1000) - mTempLast >= iSensorRate; break; } if (!isRateElapsed) return; switch (event.sensor.getType()) { case Sensor.TYPE_ACCELEROMETER: mAccLast = event.timestamp / 1000; break; case Sensor.TYPE_ORIENTATION: mOrLast = event.timestamp / 1000; break; case Sensor.TYPE_GYROSCOPE: mGyroLast = event.timestamp / 1000; break; case Sensor.TYPE_MAGNETIC_FIELD: mMagLast = event.timestamp / 1000; break; case Sensor.TYPE_LIGHT: mLightLast = event.timestamp / 1000; break; case Sensor.TYPE_PROXIMITY: mProximityLast = event.timestamp / 1000; break; case Sensor.TYPE_PRESSURE: mPressureLast = event.timestamp / 1000; break; case Sensor.TYPE_RELATIVE_HUMIDITY: mHumidityLast = event.timestamp / 1000; break; case Sensor.TYPE_AMBIENT_TEMPERATURE: mTempLast = event.timestamp / 1000; break; } if (sensorSectionFragment != null) { sensorSectionFragment.onSensorChanged(event); } if (gpsSectionFragment != null) { gpsSectionFragment.onSensorChanged(event); } }
From source file:com.vonglasow.michael.satstat.MainActivity.java
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); defaultUEH = Thread.getDefaultUncaughtExceptionHandler(); Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() { public void uncaughtException(Thread t, Throwable e) { Context c = getApplicationContext(); File dumpDir = c.getExternalFilesDir(null); File dumpFile = new File(dumpDir, "satstat-" + System.currentTimeMillis() + ".log"); PrintStream s;//from ww w. j ava 2 s . c om try { InputStream buildInStream = getResources().openRawResource(R.raw.build); s = new PrintStream(dumpFile); s.append("SatStat build: "); int i; try { i = buildInStream.read(); while (i != -1) { s.write(i); i = buildInStream.read(); } buildInStream.close(); } catch (IOException e1) { e1.printStackTrace(); } s.append("\n\n"); e.printStackTrace(s); s.flush(); s.close(); } catch (FileNotFoundException e2) { e2.printStackTrace(); } defaultUEH.uncaughtException(t, e); } }); mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); mSharedPreferences.registerOnSharedPreferenceChangeListener(this); final ActionBar actionBar = getActionBar(); setContentView(R.layout.activity_main); actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); // Find out default screen orientation Configuration config = getResources().getConfiguration(); WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE); int rot = wm.getDefaultDisplay().getRotation(); isWideScreen = (config.orientation == Configuration.ORIENTATION_LANDSCAPE && (rot == Surface.ROTATION_0 || rot == Surface.ROTATION_180) || config.orientation == Configuration.ORIENTATION_PORTRAIT && (rot == Surface.ROTATION_90 || rot == Surface.ROTATION_270)); Log.d("MainActivity", "isWideScreen=" + Boolean.toString(isWideScreen)); // compact action bar int dpX = (int) (this.getResources().getDisplayMetrics().widthPixels / this.getResources().getDisplayMetrics().density); /* * This is a crude way to ensure a one-line action bar with tabs * (not a drop-down list) and home (incon) and title only if there * is space, depending on screen width: * divide screen in units of 64 dp * each tab requires 1 unit, home and menu require slightly less, * title takes up approx. 2.5 units in portrait, * home and title are about 2 units wide in landscape */ if (dpX < 192) { // just enough space for drop-down list and menu actionBar.setDisplayShowHomeEnabled(false); actionBar.setDisplayShowTitleEnabled(false); } else if (dpX < 320) { // not enough space for four tabs, but home will fit next to list actionBar.setDisplayShowHomeEnabled(true); actionBar.setDisplayShowTitleEnabled(false); } else if (dpX < 384) { // just enough space for four tabs actionBar.setDisplayShowHomeEnabled(false); actionBar.setDisplayShowTitleEnabled(false); } else if ((dpX < 448) || ((config.orientation == Configuration.ORIENTATION_PORTRAIT) && (dpX < 544))) { // space for four tabs and home, but not title actionBar.setDisplayShowHomeEnabled(true); actionBar.setDisplayShowTitleEnabled(false); } else { // ample space for home, title and all four tabs actionBar.setDisplayShowHomeEnabled(true); actionBar.setDisplayShowTitleEnabled(true); } setEmbeddedTabs(actionBar, true); providerLocations = new HashMap<String, Location>(); mAvailableProviderStyles = new ArrayList<String>(Arrays.asList(LOCATION_PROVIDER_STYLES)); providerStyles = new HashMap<String, String>(); providerAppliedStyles = new HashMap<String, String>(); providerInvalidationHandler = new Handler(); providerInvalidators = new HashMap<String, Runnable>(); // Create the adapter that will return a fragment for each of the three // primary sections of the app. mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); // Set up the ViewPager with the sections adapter. mViewPager = (ViewPager) findViewById(R.id.pager); mViewPager.setAdapter(mSectionsPagerAdapter); mViewPager.setOnPageChangeListener(this); // Add tabs, specifying the tab's text and TabListener for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) { actionBar.addTab(actionBar.newTab() //.setText(mSectionsPagerAdapter.getPageTitle(i)) .setIcon(mSectionsPagerAdapter.getPageIcon(i)).setTabListener(this)); } // This is needed by the mapsforge library. AndroidGraphicFactory.createInstance(this.getApplication()); // Get system services for event delivery mLocationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); mOrSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION); mAccSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); mGyroSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE); mMagSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD); mLightSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT); mProximitySensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY); mPressureSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PRESSURE); mHumiditySensor = mSensorManager.getDefaultSensor(Sensor.TYPE_RELATIVE_HUMIDITY); mTempSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE); mTelephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); mConnectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE); mAccSensorRes = getSensorDecimals(mAccSensor, mAccSensorRes); mGyroSensorRes = getSensorDecimals(mGyroSensor, mGyroSensorRes); mMagSensorRes = getSensorDecimals(mMagSensor, mMagSensorRes); mLightSensorRes = getSensorDecimals(mLightSensor, mLightSensorRes); mProximitySensorRes = getSensorDecimals(mProximitySensor, mProximitySensorRes); mPressureSensorRes = getSensorDecimals(mPressureSensor, mPressureSensorRes); mHumiditySensorRes = getSensorDecimals(mHumiditySensor, mHumiditySensorRes); mTempSensorRes = getSensorDecimals(mTempSensor, mTempSensorRes); networkTimehandler = new Handler(); networkTimeRunnable = new Runnable() { @Override public void run() { int newNetworkType = mTelephonyManager.getNetworkType(); if (getNetworkGeneration(newNetworkType) != mLastNetworkGen) onNetworkTypeChanged(newNetworkType); else networkTimehandler.postDelayed(this, NETWORK_REFRESH_DELAY); } }; wifiTimehandler = new Handler(); wifiTimeRunnable = new Runnable() { @Override public void run() { mWifiManager.startScan(); wifiTimehandler.postDelayed(this, WIFI_REFRESH_DELAY); } }; updateLocationProviderStyles(); }
From source file:com.vonglasow.michael.satstat.MainActivity.java
/** * Called when a sensor's reading changes. Updates sensor display. *//*from w ww. j ava 2 s . c o m*/ public void onSensorChanged(SensorEvent event) { //to enforce sensor rate boolean isRateElapsed = false; switch (event.sensor.getType()) { case Sensor.TYPE_ACCELEROMETER: isRateElapsed = (event.timestamp / 1000) - mAccLast >= iSensorRate; // if Z acceleration is greater than X/Y combined, lock rotation, else unlock if (Math.pow(event.values[2], 2) > Math.pow(event.values[0], 2) + Math.pow(event.values[1], 2)) { // workaround (SCREEN_ORIENTATION_LOCK is unsupported on API < 18) if (isWideScreen) setRequestedOrientation( OR_FROM_ROT_WIDE[this.getWindowManager().getDefaultDisplay().getRotation()]); else setRequestedOrientation( OR_FROM_ROT_TALL[this.getWindowManager().getDefaultDisplay().getRotation()]); } else { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); } break; case Sensor.TYPE_ORIENTATION: isRateElapsed = (event.timestamp / 1000) - mOrLast >= iSensorRate; break; case Sensor.TYPE_GYROSCOPE: isRateElapsed = (event.timestamp / 1000) - mGyroLast >= iSensorRate; break; case Sensor.TYPE_MAGNETIC_FIELD: isRateElapsed = (event.timestamp / 1000) - mMagLast >= iSensorRate; break; case Sensor.TYPE_LIGHT: isRateElapsed = (event.timestamp / 1000) - mLightLast >= iSensorRate; break; case Sensor.TYPE_PROXIMITY: isRateElapsed = (event.timestamp / 1000) - mProximityLast >= iSensorRate; break; case Sensor.TYPE_PRESSURE: isRateElapsed = (event.timestamp / 1000) - mPressureLast >= iSensorRate; break; case Sensor.TYPE_RELATIVE_HUMIDITY: isRateElapsed = (event.timestamp / 1000) - mHumidityLast >= iSensorRate; break; case Sensor.TYPE_AMBIENT_TEMPERATURE: isRateElapsed = (event.timestamp / 1000) - mTempLast >= iSensorRate; break; } if (isSensorViewReady && isRateElapsed) { switch (event.sensor.getType()) { case Sensor.TYPE_ACCELEROMETER: mAccLast = event.timestamp / 1000; accX.setText(String.format("%." + mAccSensorRes + "f", event.values[0])); accY.setText(String.format("%." + mAccSensorRes + "f", event.values[1])); accZ.setText(String.format("%." + mAccSensorRes + "f", event.values[2])); accTotal.setText(String.format("%." + mAccSensorRes + "f", Math.sqrt(Math.pow(event.values[0], 2) + Math.pow(event.values[1], 2) + Math.pow(event.values[2], 2)))); accStatus.setTextColor(getResources().getColor(accuracyToColor(event.accuracy))); break; case Sensor.TYPE_ORIENTATION: mOrLast = event.timestamp / 1000; orAzimuth.setText(String.format("%.0f%s", event.values[0], getString(R.string.unit_degree))); orAziText.setText(formatOrientation(event.values[0])); orPitch.setText(String.format("%.0f%s", event.values[1], getString(R.string.unit_degree))); orRoll.setText(String.format("%.0f%s", event.values[2], getString(R.string.unit_degree))); orStatus.setTextColor(getResources().getColor(accuracyToColor(event.accuracy))); break; case Sensor.TYPE_GYROSCOPE: mGyroLast = event.timestamp / 1000; rotX.setText(String.format("%." + mGyroSensorRes + "f", event.values[0])); rotY.setText(String.format("%." + mGyroSensorRes + "f", event.values[1])); rotZ.setText(String.format("%." + mGyroSensorRes + "f", event.values[2])); rotTotal.setText(String.format("%." + mGyroSensorRes + "f", Math.sqrt(Math.pow(event.values[0], 2) + Math.pow(event.values[1], 2) + Math.pow(event.values[2], 2)))); rotStatus.setTextColor(getResources().getColor(accuracyToColor(event.accuracy))); break; case Sensor.TYPE_MAGNETIC_FIELD: mMagLast = event.timestamp / 1000; magX.setText(String.format("%." + mMagSensorRes + "f", event.values[0])); magY.setText(String.format("%." + mMagSensorRes + "f", event.values[1])); magZ.setText(String.format("%." + mMagSensorRes + "f", event.values[2])); magTotal.setText(String.format("%." + mMagSensorRes + "f", Math.sqrt(Math.pow(event.values[0], 2) + Math.pow(event.values[1], 2) + Math.pow(event.values[2], 2)))); magStatus.setTextColor(getResources().getColor(accuracyToColor(event.accuracy))); break; case Sensor.TYPE_LIGHT: mLightLast = event.timestamp / 1000; light.setText(String.format("%." + mLightSensorRes + "f", event.values[0])); lightStatus.setTextColor(getResources().getColor(accuracyToColor(event.accuracy))); break; case Sensor.TYPE_PROXIMITY: mProximityLast = event.timestamp / 1000; proximity.setText(String.format("%." + mProximitySensorRes + "f", event.values[0])); proximityStatus.setTextColor(getResources().getColor(accuracyToColor(event.accuracy))); break; case Sensor.TYPE_PRESSURE: mPressureLast = event.timestamp / 1000; metPressure.setText(String.format("%." + mPressureSensorRes + "f", event.values[0])); pressureStatus.setTextColor(getResources().getColor(accuracyToColor(event.accuracy))); break; case Sensor.TYPE_RELATIVE_HUMIDITY: mHumidityLast = event.timestamp / 1000; metHumid.setText(String.format("%." + mHumiditySensorRes + "f", event.values[0])); humidStatus.setTextColor(getResources().getColor(accuracyToColor(event.accuracy))); break; case Sensor.TYPE_AMBIENT_TEMPERATURE: mTempLast = event.timestamp / 1000; metTemp.setText(String.format("%." + mTempSensorRes + "f", event.values[0])); tempStatus.setTextColor(getResources().getColor(accuracyToColor(event.accuracy))); break; } } if (isGpsViewReady && isRateElapsed) { switch (event.sensor.getType()) { case Sensor.TYPE_ORIENTATION: gpsStatusView.setYaw(event.values[0]); break; } } }