Example usage for android.content Context SENSOR_SERVICE

List of usage examples for android.content Context SENSOR_SERVICE

Introduction

In this page you can find the example usage for android.content Context SENSOR_SERVICE.

Prototype

String SENSOR_SERVICE

To view the source code for android.content Context SENSOR_SERVICE.

Click Source Link

Document

Use with #getSystemService(String) to retrieve a android.hardware.SensorManager for accessing sensors.

Usage

From source file:net.line2soft.preambul.views.SlippyMapActivity.java

/**
 * Init map's overlays (compass, buttons, ...), attach listener.
 * You can launch map on normal or edit mode (to pick a position).
 * @param addFavorite If true, edit mode (to add favorite), if false normal mode
 *//*from   w  w  w  .j ava 2s  .c om*/
private void initMap(boolean addFavorite) {
    if (addFavorite == false) {
        //Launch map in normal mode
        findViewById(R.id.list_item).setVisibility(View.VISIBLE);
        findViewById(R.id.drawer).setVisibility(View.VISIBLE);

        //Add listener on excursions button
        ImageButton btExcursionsImg = (ImageButton) findViewById(R.id.imageButton1);
        btExcursionsImg.setOnClickListener(listener);
        btExcursionsImg.setVisibility(View.VISIBLE);
        findViewById(R.id.excursionButton).setVisibility(View.VISIBLE);
        ((ImageButton) findViewById(R.id.imageRight)).setOnClickListener(listener);
        ((ImageButton) findViewById(R.id.imageLeft)).setOnClickListener(listener);
        ((ImageButton) findViewById(R.id.favoritesButton)).setOnClickListener(listener);

        //Display POIs on map
        try {
            PointOfInterest[] pois = MapController.getInstance(this).getCurrentLocation().getPOIs(this);
            Drawable icon = null;
            OverlayItem item = null;
            boolean displayPoiType = true;
            for (PointOfInterest poi : pois) {
                displayPoiType = PreferenceManager.getDefaultSharedPreferences(this)
                        .getBoolean("type" + poi.getType().getId(), true);
                if (displayPoiType) {
                    icon = (poi.getType().getIcon() == null) ? poi.getType().getCategory().getIcon()
                            : poi.getType().getIcon();
                    item = new OverlayItem(poi.getPoint(), poi.getName(), poi.getComment(),
                            BalloonItemizedOverlay.boundLeftCenter(icon));
                    overlayPoiItemMarker.addItem(item, poi);
                }
            }
        } catch (Exception e) {
            displayInfo(getString(R.string.message_map_poi_load_error));
            e.printStackTrace();
        }

        //Display favorites on map
        try {
            HashMap<String, NamedPoint> favorites = MapController.getInstance(this).getCurrentLocation()
                    .getNamedFavorites(this);
            Drawable icon = getResources().getDrawable(R.drawable.marker_favorite);
            OverlayItem item = null;
            for (NamedPoint favPoint : favorites.values()) {
                item = new OverlayItem(favPoint.getPoint(), favPoint.getName(), favPoint.getComment(),
                        BalloonItemizedOverlay.boundLeftCenter(icon));
                overlayPoiItemMarker.addItem(item, favPoint);
            }
        } catch (IOException e) {
            displayInfo(getString(R.string.message_map_favorite_load_error));
            e.printStackTrace();
        }

        //Display excursions and instruction on map
        int pathToDisplay = MapController.getInstance(this).getPathToDisplay();
        displayExcursion(pathToDisplay);
        int instructionToDisplay = MapController.getInstance(this).getInstructionToDisplay() - 1;
        //Set instruction, only if > 0 because if instruction=0, was set when displaying excursion
        if (instructionToDisplay > 0) {
            setNavigationInstructionToDisplay(instructionToDisplay);
        }
        //Display of the bookmarksButton
        boolean displaybookmarksButton = PreferenceManager.getDefaultSharedPreferences(this)
                .getBoolean("displayBookmarksButton", true);
        ImageButton img = (ImageButton) findViewById(R.id.favoritesButton);
        if (displaybookmarksButton) {
            img.setVisibility(View.VISIBLE);
        } else {
            img.setVisibility(View.GONE);
        }
        //Display point
        GeoPoint pointToDisplay = MapController.getInstance(this).getPointToDisplay();
        if (pointToDisplay != null) {
            displayPosition(pointToDisplay.getLatitude(), pointToDisplay.getLongitude());
            MapController.getInstance(this).setPointToDisplay(null);
        }
        //Launch compass
        boolean displayCompass = PreferenceManager.getDefaultSharedPreferences(this)
                .getBoolean("displayCompass", true);
        CompassView cpv = (CompassView) findViewById(R.id.compass);
        CompassView cpvBig = (CompassView) findViewById(R.id.compassBig);
        if (displayCompass) {
            SensorManager mySensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
            Sensor mAccelerometer = mySensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
            Sensor mField = mySensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
            if (mField != null && mAccelerometer != null) {
                mySensorManager.registerListener(listener, mAccelerometer, SensorManager.SENSOR_DELAY_UI);
                mySensorManager.registerListener(listener, mField, SensorManager.SENSOR_DELAY_UI);
                cpv.setOnClickListener(listener);
                if (isCompassAccurate) {
                    cpv.setVisibility(View.VISIBLE);
                } else {
                    displayInfo(getString(R.string.message_compass_not_accurate));
                    cpv.setVisibility(View.GONE);
                }
                cpvBig.setVisibility(View.GONE);
                cpvBig.setOnClickListener(listener);
            } else {
                cpv.setVisibility(View.GONE);
                cpvBig.setVisibility(View.GONE);
            }
        } else {
            cpv.setVisibility(View.GONE);
            cpvBig.setVisibility(View.GONE);
        }

        //Hide validate button
        findViewById(R.id.validatePositionButton).setVisibility(View.GONE);
        findViewById(R.id.imageViewValidate).setVisibility(View.GONE);
        findViewById(R.id.mapPointer).setVisibility(View.GONE);
    } else {
        //Launch map in edit mode, to set coordinates of a favorite
        findViewById(R.id.list_item).setVisibility(View.GONE);
        findViewById(R.id.compass).setVisibility(View.GONE);
        findViewById(R.id.drawer).setVisibility(View.GONE);
        findViewById(R.id.favoritesButton).setVisibility(View.GONE);

        findViewById(R.id.mapPointer).setVisibility(View.VISIBLE);

        //Display validate button and add listener
        findViewById(R.id.validatePositionButton).setVisibility(View.VISIBLE);
        findViewById(R.id.imageViewValidate).setVisibility(View.VISIBLE);
        findViewById(R.id.imageViewValidate).setOnClickListener(listener);

        this.addFavorite = false;
    }
}

From source file:cl.ipp.katbag.fragment.Player.java

public void setSensing(String[] line, int lineNumber) {
    switch (Integer.parseInt(line[3])) {
    case 0:// w  w  w. j a  va2s  . c  om
        boolean ex = false;
        for (int z = 0; z < touchEventId.size(); z++) {
            if (line[0].contentEquals(String.valueOf(touchEventId.get(z)))) {
                ex = true;
                break;
            }
        }

        if (!ex) {
            int last = -1;
            for (int i = lineNumber + 1; i < develop.size(); i++) {
                String[] it = develop.get(i).split("&&");

                last = i;
                if (Integer.parseInt(line[8]) >= Integer.parseInt(it[8])) {
                    last--;
                    break;
                }
            }

            if ((lineNumber + 1) <= last) {
                touchEventId.add(Integer.valueOf(line[0]));
                touchEventDrawing.add(Integer.valueOf(line[4])); // drawing
                touchEventBgn.add(lineNumber + 1); // begin in
                touchEventLst.add(last); // ends

                final KatbagDrawing drawing = (KatbagDrawing) v.findViewById(Integer.valueOf(line[4]));
                if (drawing != null) {
                    drawing.setOnTouchListener(new OnTouchListener() {

                        @Override
                        public boolean onTouch(View v, MotionEvent event) {
                            switch (event.getAction()) {
                            case MotionEvent.ACTION_DOWN:
                                for (int i = 0; i < touchEventDrawing.size(); i++) {
                                    if (touchEventDrawing.get(i) == drawing.getId()) {
                                        setBranchTreeCondition(touchEventId.get(i), 1);
                                        if (checkTreeCondition(touchEventId.get(i))) {
                                            Log.d("ply", "touchEventDrawing - ini:" + touchEventBgn.get(i)
                                                    + ", last:" + touchEventLst.get(i));
                                            play(touchEventBgn.get(i), touchEventLst.get(i) + 1);
                                            break;
                                        }
                                    }
                                }

                                break;

                            case MotionEvent.ACTION_UP:
                                for (int i = 0; i < touchEventDrawing.size(); i++) {
                                    if (touchEventDrawing.get(i) == drawing.getId()) {
                                        setBranchTreeCondition(touchEventId.get(i), 0);
                                        break;
                                    }
                                }

                                break;
                            }

                            return true;
                        }
                    });

                    Log.d("ply",
                            "setSensing - touchEvent add! count:" + touchEventId.size() + ", drawing:"
                                    + touchEventDrawing.get(touchEventDrawing.size() - 1) + " ini:"
                                    + touchEventBgn.get(touchEventBgn.size() - 1) + " last:"
                                    + touchEventLst.get(touchEventLst.size() - 1));
                }
            }

            if (last > -1)
                if ((last + 1) < develop.size())
                    play((last + 1), develop.size());
        }

        break;

    case 1:
        boolean ex1 = false;
        for (int z = 0; z < shakeId.size(); z++) {
            if (line[0].contentEquals(String.valueOf(shakeId.get(z)))) {
                ex1 = true;
                break;
            }
        }

        if (!ex1) {
            int last1 = -1;
            for (int i = lineNumber + 1; i < develop.size(); i++) {
                String[] it = develop.get(i).split("&&");

                last1 = i;
                if (Integer.parseInt(line[8]) >= Integer.parseInt(it[8])) {
                    last1--;
                    break;
                }
            }

            if ((lineNumber + 1) <= last1) {
                shakeId.add(Integer.valueOf(line[0]));
                shakeBgn.add(lineNumber + 1); // begin in
                shakeLst.add(last1); // ends

                sensorManager = (SensorManager) mainActivity.context.getSystemService(Context.SENSOR_SERVICE);
                accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
                sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_FASTEST);
                useAccelerometer = true;

                Log.d("ply", "setSensing - shake add! count:" + shakeId.size() + ", ini:"
                        + shakeBgn.get(shakeBgn.size() - 1) + " last:" + shakeLst.get(shakeLst.size() - 1));
            }

            if (last1 > -1)
                if ((last1 + 1) < develop.size())
                    play((last1 + 1), develop.size());
        }

        break;
    }
}

From source file:tv.piratemedia.flightcontroller.BluetoothComputerFragment.java

public void setupAccel() {
    SensorManager mSensorManager = (SensorManager) getActivity().getSystemService(Context.SENSOR_SERVICE);
    Sensor mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION);

    mSensorManager.registerListener(new SensorEventListener() {
        @Override/*ww w  .  j a  v  a2 s .  co m*/
        public void onSensorChanged(SensorEvent event) {
            if (!Moving && currentTrottle > 20) {
                float[] filtered = new float[3];
                lowPass(event.values, filtered);

                float x = getAverageX(filtered[0]);
                float y = getAverageY(filtered[1]);

                currentXOffset += x * multiValue;
                currentYOffset += y * multiValue;

                if (currentXOffset > maxOffset) {
                    currentXOffset = maxOffset;
                } else if (currentXOffset < -maxOffset) {
                    currentXOffset = -maxOffset;
                }

                if (currentYOffset > maxOffset) {
                    currentYOffset = maxOffset;
                } else if (currentYOffset < -maxOffset) {
                    currentYOffset = -maxOffset;
                }

                if (PitchCorrection != (int) currentYOffset && RollCorrection != (int) currentXOffset) {
                    PitchCorrection = (int) currentYOffset;
                    RollCorrection = -(int) currentXOffset;

                    JSONObject msg = new JSONObject();
                    try {
                        msg.put("action", "correction_update");
                        msg.put("pitch", PitchCorrection);
                        msg.put("roll", RollCorrection);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    sendTextMessage(msg.toString());
                    updateThrottle(currentTrottle);
                }
            } else if (!Moving && currentTrottle <= 20) {
                PitchCorrection = 0;
                RollCorrection = 0;

                JSONObject msg = new JSONObject();
                try {
                    msg.put("action", "correction_update");
                    msg.put("pitch", PitchCorrection);
                    msg.put("roll", RollCorrection);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                sendTextMessage(msg.toString());
                updateThrottle(currentTrottle);
            }
        }

        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {

        }
    }, mSensor, SensorManager.SENSOR_DELAY_FASTEST);
}

From source file:org.godotengine.godot.Godot.java

private void initializeGodot() {

    if (expansion_pack_path != null) {

        String[] new_cmdline;
        int cll = 0;
        if (command_line != null) {
            new_cmdline = new String[command_line.length + 2];
            cll = command_line.length;//from  w w w  .ja va2s  .c om
            for (int i = 0; i < command_line.length; i++) {
                new_cmdline[i] = command_line[i];
            }
        } else {
            new_cmdline = new String[2];
        }

        new_cmdline[cll] = "--main-pack";
        new_cmdline[cll + 1] = expansion_pack_path;
        command_line = new_cmdline;
    }

    io = new GodotIO(this);
    io.unique_id = Secure.getString(getContentResolver(), Secure.ANDROID_ID);
    GodotLib.io = io;
    mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_GAME);
    mGravity = mSensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY);
    mSensorManager.registerListener(this, mGravity, SensorManager.SENSOR_DELAY_GAME);
    mMagnetometer = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
    mSensorManager.registerListener(this, mMagnetometer, SensorManager.SENSOR_DELAY_GAME);
    mGyroscope = mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
    mSensorManager.registerListener(this, mGyroscope, SensorManager.SENSOR_DELAY_GAME);

    GodotLib.initialize(this, getAssets(), use_apk_expansion);

    result_callback = null;

    mPaymentsManager = PaymentsManager.createManager(this).initService();

    godot_initialized = true;
}

From source file:com.zainsoft.ramzantimetable.QiblaActivity.java

private void unregisterListeners() {
    ((LocationManager) getSystemService(Context.LOCATION_SERVICE)).removeUpdates(qiblaManager);

    ((LocationManager) getSystemService(Context.LOCATION_SERVICE)).removeUpdates(qiblaManager);
    SensorManager mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    Sensor gsensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    Sensor msensor = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
    mSensorManager.unregisterListener(qiblaManager, gsensor);
    mSensorManager.unregisterListener(qiblaManager, msensor);
    cancelSchedule();//  ww  w .  j  a va 2 s . co m

}

From source file:com.coincide.alphafitness.ui.Fragment_Main.java

@Override
public void onResume() {
    super.onResume();
    //        getActivity().getActionBar().setDisplayHomeAsUpEnabled(false);

    Database db = Database.getInstance(getActivity());

    if (BuildConfig.DEBUG)
        db.logState();/*from   ww w.  j  a  v  a 2 s . com*/
    // read todays offset
    todayOffset = db.getSteps(Util.getToday());

    SharedPreferences prefs = getActivity().getSharedPreferences("pedometer", Context.MODE_PRIVATE);

    goal = prefs.getInt("goal", Fragment_Settings.DEFAULT_GOAL);
    since_boot = db.getCurrentSteps(); // do not use the value from the sharedPreferences
    int pauseDifference = since_boot - prefs.getInt("pauseCount", since_boot);

    // register a sensorlistener to live update the UI if a step is taken
    if (!prefs.contains("pauseCount")) {
        SensorManager sm = (SensorManager) getActivity().getSystemService(Context.SENSOR_SERVICE);
        Sensor sensor = sm.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
        if (sensor == null) {
            new AlertDialog.Builder(getActivity()).setTitle(R.string.no_sensor)
                    .setMessage(R.string.no_sensor_explain)
                    .setOnDismissListener(new DialogInterface.OnDismissListener() {
                        @Override
                        public void onDismiss(final DialogInterface dialogInterface) {
                            getActivity().finish();
                        }
                    }).setNeutralButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(final DialogInterface dialogInterface, int i) {
                            dialogInterface.dismiss();
                        }
                    }).create().show();
        } else {
            sm.registerListener(this, sensor, SensorManager.SENSOR_DELAY_UI, 0);
        }
    }

    since_boot -= pauseDifference;

    total_start = db.getTotalWithoutToday();
    total_days = db.getDays();

    db.close();

    SharedPreferences sharedpreferences = getActivity().getSharedPreferences(MyPREFERENCES,
            Context.MODE_PRIVATE);
    mBodyWeight = Float.parseFloat(sharedpreferences.getString(sp_weight, "0"));

    stepsDistanceChanged();

}

From source file:system.info.reader.java

public void refreshOnce() {//something not change, then put here
    Properties.sdkversion = android.os.Build.VERSION.SDK;

    cameraSizes = Properties.camera();

    Properties.dr();/*ww w.  ja v  a2  s  .c om*/

    processors = Properties.processor();

    Properties.memtotal();

    sensorMgr = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    Properties.sensors = sensorMgr.getSensorList(Sensor.TYPE_ALL).size() + "";

    TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    teles = Properties.telephonies(tm);

    try {
        serverWeb.loadUrl(getString(R.string.url));
    } catch (Exception e) {
    }
}

From source file:org.zywx.wbpalmstar.plugin.uexaudio.EUExAudio.java

public void setProximityState(String[] params) {
    if (params == null || params.length < 1)
        return;// w  w  w. j  ava  2s.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:org.mozilla.gecko.GeckoAppShell.java

public static void enableDeviceMotion(boolean enable) {
    LayerView v = GeckoApp.mAppContext.getLayerController().getView();
    SensorManager sm = (SensorManager) v.getContext().getSystemService(Context.SENSOR_SERVICE);

    if (gAccelerometerSensor == null || gOrientationSensor == null) {
        gAccelerometerSensor = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        gOrientationSensor = sm.getDefaultSensor(Sensor.TYPE_ORIENTATION);
    }//from ww  w  .  j  a v  a2s. c  o  m

    if (enable) {
        if (gAccelerometerSensor != null)
            sm.registerListener(GeckoApp.mAppContext, gAccelerometerSensor, SensorManager.SENSOR_DELAY_GAME);
        if (gOrientationSensor != null)
            sm.registerListener(GeckoApp.mAppContext, gOrientationSensor, SensorManager.SENSOR_DELAY_GAME);
    } else {
        if (gAccelerometerSensor != null)
            sm.unregisterListener(GeckoApp.mAppContext, gAccelerometerSensor);
        if (gOrientationSensor != null)
            sm.unregisterListener(GeckoApp.mAppContext, gOrientationSensor);
    }
}

From source file:tv.piratemedia.flightcontroller.BluetoothComputerFragment.java

public void setupGyro() {
    final Handler TimeoutHandler = new Handler();
    SensorManager mSensorManager;/*w  w  w . j  a  v a 2  s . c  o m*/
    Sensor mSensor;

    mSensorManager = (SensorManager) getActivity().getSystemService(Context.SENSOR_SERVICE);
    mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY);

    SensorEventListener sensorListener = new SensorEventListener() {
        @Override
        public void onSensorChanged(SensorEvent event) {
            int X = (int) (event.values[0] / 2.0f);
            int Y = (int) (event.values[1] / 2.0f);
            int Z = (int) (event.values[2] / 2.0f);
            Y = -Y;

            //Log.d("gyro", "X: " + X + " Y: " + Y + " Z: " + Z);
            if (currentPitch != Y || currentRoll != X || currentYaw != Z) {
                if (!Moving && currentTrottle > 10) {
                    if ((PitchCorrection < MaxCorrectionValue || Y < 0)
                            && (PitchCorrection > -MaxCorrectionValue || Y > 0)) {
                        PitchCorrection += Y;
                        if (PitchCorrection > MaxCorrectionValue) {
                            PitchCorrection = MaxCorrectionValue;
                        } else if (PitchCorrection < -MaxCorrectionValue) {
                            PitchCorrection = -MaxCorrectionValue;
                        }
                    }
                    if ((RollCorrection < MaxCorrectionValue || X < 0)
                            && (RollCorrection > -MaxCorrectionValue || X > 0)) {
                        RollCorrection += X;
                        if (RollCorrection > MaxCorrectionValue) {
                            RollCorrection = MaxCorrectionValue;
                        } else if (RollCorrection < -MaxCorrectionValue) {
                            RollCorrection = -MaxCorrectionValue;
                        }
                    }
                    updateThrottle(currentTrottle);
                } else if (currentTrottle <= 10) {
                    PitchCorrection = 0;
                    RollCorrection = 0;
                }
                currentPitch = Y;
                currentRoll = X;
                currentYaw = Z;

                JSONObject msg = new JSONObject();
                try {
                    msg.put("action", "gyro_update");
                    msg.put("x", currentRoll);
                    msg.put("y", currentPitch);
                    msg.put("z", currentYaw);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                sendTextMessage(msg.toString());
            }
        }

        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {

        }
    };

    if (mSensor != null) {
        mSensorManager.registerListener(sensorListener, mSensor, SensorManager.SENSOR_DELAY_FASTEST);
    } else {
        Toast.makeText(getActivity().getApplicationContext(), "No Gravity Sensor on device", Toast.LENGTH_LONG)
                .show();
    }
}