List of usage examples for android.hardware Sensor TYPE_PROXIMITY
int TYPE_PROXIMITY
To view the source code for android.hardware Sensor TYPE_PROXIMITY.
Click Source Link
From source file:org.zywx.wbpalmstar.plugin.uexaudio.EUExAudio.java
public void setProximityState(String[] params) { if (params == null || params.length < 1) return;/* w ww . ja v a2 s. c o m*/ boolean state = "1".equals(params[0]); SensorManager mSensorManager = (SensorManager) mContext.getApplicationContext() .getSystemService(Context.SENSOR_SERVICE); if (!state) { if (sensorEventListener != null) mSensorManager.unregisterListener(sensorEventListener); return; } Sensor mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY); if (sensorEventListener == null) { if (m_pfMusicPlayer != null) { sensorEventListener = m_pfMusicPlayer.getSensorEventListener(); } else { errorCallback(0, EUExCallback.F_E_AUDIO_MUSIC_STOP_NO_OPEN_ERROR_CODE, /* "" */finder.getString(mContext, "plugin_audio_no_open_error")); return; } } mSensorManager.registerListener(sensorEventListener, mSensor, SensorManager.SENSOR_DELAY_NORMAL); }
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. j ava 2 s .co m 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:it.unime.mobility4ckan.MainActivity.java
void sendTask(final boolean shouldUpdateCountdown) { if (!isDeviceCurrentSensorsRegistered && !isRegistering) { new RegisterDevice(this).execute(datasetName); isRegistering = true;/*from w ww .j a v a 2s . c o m*/ 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:org.mozilla.gecko.GeckoAppShell.java
public static void enableSensor(int aSensortype) { SensorManager sm = (SensorManager) GeckoApp.mAppContext.getSystemService(Context.SENSOR_SERVICE); switch (aSensortype) { case SENSOR_PROXIMITY: if (gProximitySensor == null) gProximitySensor = sm.getDefaultSensor(Sensor.TYPE_PROXIMITY); sm.registerListener(GeckoApp.mAppContext, gProximitySensor, SensorManager.SENSOR_DELAY_GAME); break;/*from ww w .j a va 2s.co m*/ } }
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. com */ 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 w w w . j av a 2s . com*/ 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.ferdi2005.secondgram.voip.VoIPService.java
@SuppressLint("NewApi") @Override// w ww . j av a 2s .co m public void onSensorChanged(SensorEvent event) { if (event.sensor.getType() == Sensor.TYPE_PROXIMITY) { AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE); if (isHeadsetPlugged || am.isSpeakerphoneOn() || (isBluetoothHeadsetConnected() && am.isBluetoothScoOn())) { return; } boolean newIsNear = event.values[0] < Math.min(event.sensor.getMaximumRange(), 3); if (newIsNear != isProximityNear) { FileLog.d("proximity " + newIsNear); isProximityNear = newIsNear; try { if (isProximityNear) { proximityWakelock.acquire(); } else { proximityWakelock.release(1); // this is non-public API before L } } catch (Exception x) { FileLog.e(x); } } } }
From source file:com.vonglasow.michael.satstat.MainActivity.java
/** * Called when a sensor's reading changes. Updates sensor display. *///from w w w .ja v a2s . co 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; } } }
From source file:com.ferdi2005.secondgram.voip.VoIPService.java
private void configureDeviceForCall() { needPlayEndSound = true;/*ww w . ja v a 2s . c o m*/ AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE); am.setMode(AudioManager.MODE_IN_COMMUNICATION); am.setSpeakerphoneOn(false); am.requestAudioFocus(this, AudioManager.STREAM_VOICE_CALL, AudioManager.AUDIOFOCUS_GAIN); SensorManager sm = (SensorManager) getSystemService(SENSOR_SERVICE); Sensor proximity = sm.getDefaultSensor(Sensor.TYPE_PROXIMITY); try { if (proximity != null) { proximityWakelock = ((PowerManager) getSystemService(Context.POWER_SERVICE)) .newWakeLock(PROXIMITY_SCREEN_OFF_WAKE_LOCK, "telegram-voip-prx"); sm.registerListener(this, proximity, SensorManager.SENSOR_DELAY_NORMAL); } } catch (Exception x) { FileLog.e("Error initializing proximity sensor", x); } }
From source file:com.aware.Aware_Preferences.java
/** * Proximity module settings UI// w ww. java2 s. c o m */ private void proximity() { final PreferenceScreen proxi_pref = (PreferenceScreen) findPreference("proximity"); Sensor temp = mSensorMgr.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION); if (temp != null) { proxi_pref.setSummary( proxi_pref.getSummary().toString().replace("*", " - Power: " + temp.getPower() + " mA")); } else { proxi_pref.setSummary(proxi_pref.getSummary().toString().replace("*", "")); } final CheckBoxPreference proximity = (CheckBoxPreference) findPreference( Aware_Preferences.STATUS_PROXIMITY); proximity.setChecked( Aware.getSetting(getApplicationContext(), Aware_Preferences.STATUS_PROXIMITY).equals("true")); proximity.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { @Override public boolean onPreferenceClick(Preference preference) { if (mSensorMgr.getDefaultSensor(Sensor.TYPE_PROXIMITY) == null) { showDialog(DIALOG_ERROR_MISSING_SENSOR); proximity.setChecked(false); Aware.setSetting(getApplicationContext(), Aware_Preferences.STATUS_PROXIMITY, false); return false; } Aware.setSetting(getApplicationContext(), Aware_Preferences.STATUS_PROXIMITY, proximity.isChecked()); if (proximity.isChecked()) { framework.startProximity(); } else { framework.stopProximity(); } return true; } }); final EditTextPreference frequency_proximity = (EditTextPreference) findPreference( Aware_Preferences.FREQUENCY_PROXIMITY); if (Aware.getSetting(getApplicationContext(), Aware_Preferences.FREQUENCY_PROXIMITY).length() > 0) { frequency_proximity .setSummary(Aware.getSetting(getApplicationContext(), Aware_Preferences.FREQUENCY_PROXIMITY) + " microseconds"); } frequency_proximity .setText(Aware.getSetting(getApplicationContext(), Aware_Preferences.FREQUENCY_PROXIMITY)); frequency_proximity.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { @Override public boolean onPreferenceChange(Preference preference, Object newValue) { Aware.setSetting(getApplicationContext(), Aware_Preferences.FREQUENCY_PROXIMITY, (String) newValue); frequency_proximity.setSummary((String) newValue + " microseconds"); framework.startProximity(); return true; } }); }