@Override public void onSensorChanged(SensorEvent event) { Sensor mySensor = event.sensor; if (mySensor.getType() == SensorManager.SENSOR_ACCELEROMETER) { long curTime = System.currentTimeMillis(); if ((curTime - lastForce) > SHAKE_TIMEOUT) { shakeCount = 0; } // Only allow one update every 100ms. if ((curTime - lastUpdate) > TIME_THRESHOLD) { long diffTime = (curTime - lastUpdate); lastUpdate = curTime; float x = event.values[SensorManager.DATA_X]; float y = event.values[SensorManager.DATA_Y]; float z = event.values[SensorManager.DATA_Z]; float speed = Math.abs(x + y + z - last_x - last_y - last_z) / diffTime * 10000; if (speed > FORCE_THRESHOLD) { if ((++shakeCount >= SHAKE_COUNT) && (curTime - lastShake > SHAKE_DURATION)) { lastShake = curTime; shakeCount = 0; if (sensorMgr != null) { for (ShakeEventListener listener : listeners) { listener.onShakeEvent(); } } } lastForce = curTime; } lastForce = curTime; last_x = x; last_y = y; last_z = z; } } }
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Normally one shouldn't instantiate all these objects in the onCreate method, // as onCreate is called every time a configuration change occurs (orientation, // keyboard hidden, screen size, etc). But we are handling configuration changes // ourselves. // hide the status bar this.getWindow() .setFlags( WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); // get local Bluetooth adapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (bluetoothAdapter == null) { Toast.makeText(this, R.string.bluetooth_unavailable, Toast.LENGTH_LONG).show(); finish(); return; } communicator = new AndroidCommunicator(this, bluetoothAdapter); communicator.addListener(this); remoteViewCommunicator = new SocketAndroidCommunicator(this); DatasetModelState datasetsState = new DatasetModelState(communicator, this); LayerModelState layersState = new LayerModelState(communicator, this); PlaceModelState placesState = new PlaceModelState(communicator, this); ItemModelState[] states = new ItemModelState[] {datasetsState, layersState, placesState}; for (ItemModelState state : states) { itemModelStates.put(state.getModel().getId(), state); ItemModelFragmentMenuProvider menuProvider = new EmptyMenuProvider(); if (state == placesState) { menuProvider = new PlacesMenuProvider(communicator); } menuProviders.put(state.getModel().getId(), menuProvider); } controlFragment = ControlFragment.newInstance(remoteViewCommunicator); datasetsFragment = ItemModelFragment.newInstance(datasetsState.getModel().getId(), false); layersFragment = ItemModelFragment.newInstance(layersState.getModel().getId(), false); flatLayersFragment = ItemModelFragment.newInstance(layersState.getModel().getId(), true); placesFragment = ItemModelFragment.newInstance(placesState.getModel().getId(), false); tabFragments = new Fragment[] { controlFragment, datasetsFragment, layersFragment, flatLayersFragment, placesFragment }; // create the tabs getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); int[] tabIds = new int[] { R.string.controls_tab, R.string.datasets_tab, R.string.layers_tab, R.string.flat_layers_tab, R.string.places_tab }; for (int i = 0; i < tabIds.length; i++) { ActionBar.Tab tab = getSupportActionBar().newTab(); tab.setTag(tabIds[i]); tab.setText(tabIds[i]); tab.setTabListener(this); getSupportActionBar().addTab(tab); } getSupportActionBar().setDisplayShowTitleEnabled(false); getSupportActionBar().setHomeButtonEnabled(true); // setup the shake sensor sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); sensorListener.setOnShakeListener( new ShakeEventListener.OnShakeListener() { @Override public void onShake() { communicator.sendMessage(new ShakeMessage()); } }); // Acquire a reference to the system Location Manager LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); // Define a listener that responds to location updates LocationListener locationListener = new LocationListener() { public void onLocationChanged(Location location) { if (isSendLocation()) { communicator.sendMessage( new LocationMessage( location.getLatitude(), location.getLongitude(), location.getAltitude(), location.getAccuracy(), location.getBearing())); } } public void onStatusChanged(String provider, int status, Bundle extras) {} public void onProviderEnabled(String provider) {} public void onProviderDisabled(String provider) {} }; locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener); // locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, // locationListener); }