Example usage for android.location Location equals

List of usage examples for android.location Location equals

Introduction

In this page you can find the example usage for android.location Location equals.

Prototype

public boolean equals(Object obj) 

Source Link

Document

Indicates whether some other object is "equal to" this one.

Usage

From source file:org.streetpacman.util.DMUtils.java

/**
 * Computes the distance on the two sphere between the point c0 and the line
 * segment c1 to c2./*from  w ww . j  a v a  2  s  .c  o m*/
 * 
 * @param c0 the first coordinate
 * @param c1 the beginning of the line segment
 * @param c2 the end of the lone segment
 * @return the distance in m (assuming spherical earth)
 */
public static double distance(final Location c0, final Location c1, final Location c2) {
    if (c1.equals(c2)) {
        return c2.distanceTo(c0);
    }

    final double s0lat = c0.getLatitude() * UnitConversions.TO_RADIANS;
    final double s0lng = c0.getLongitude() * UnitConversions.TO_RADIANS;
    final double s1lat = c1.getLatitude() * UnitConversions.TO_RADIANS;
    final double s1lng = c1.getLongitude() * UnitConversions.TO_RADIANS;
    final double s2lat = c2.getLatitude() * UnitConversions.TO_RADIANS;
    final double s2lng = c2.getLongitude() * UnitConversions.TO_RADIANS;

    double s2s1lat = s2lat - s1lat;
    double s2s1lng = s2lng - s1lng;
    final double u = ((s0lat - s1lat) * s2s1lat + (s0lng - s1lng) * s2s1lng)
            / (s2s1lat * s2s1lat + s2s1lng * s2s1lng);
    if (u <= 0) {
        return c0.distanceTo(c1);
    }
    if (u >= 1) {
        return c0.distanceTo(c2);
    }
    Location sa = new Location("");
    sa.setLatitude(c0.getLatitude() - c1.getLatitude());
    sa.setLongitude(c0.getLongitude() - c1.getLongitude());
    Location sb = new Location("");
    sb.setLatitude(u * (c2.getLatitude() - c1.getLatitude()));
    sb.setLongitude(u * (c2.getLongitude() - c1.getLongitude()));
    return sa.distanceTo(sb);
}

From source file:at.alladin.rmbt.android.test.RMBTTestFragment.java

private void updateUI() {
    String teststatus;//www.j av a2s . c o  m
    updateCounter++;

    if (rmbtService == null)
        return;

    intermediateResult = rmbtService.getIntermediateResult(intermediateResult);

    if (intermediateResult == null) {
        if (rmbtService.isConnectionError()) {
            if (progressDialog != null) {
                progressDialog.dismiss();
                progressDialog = null;
            }
            if (!rmbtService.isLoopMode()) {
                showErrorDialog(R.string.test_dialog_error_control_server_conn);
                return;
            }
        }

        if (!rmbtService.isTestRunning() && updateCounter > MAX_COUNTER_WITHOUT_RESULT
                && !(errorDialog != null && errorDialog.isShowing()))
            getActivity().getSupportFragmentManager().popBackStack(); // leave
        return;
    }

    if (intermediateResult.status == TestStatus.WAIT) {
        if (progressDialog != null) {
            long wait = (intermediateResult.remainingWait + 999) / 1000; // round
                                                                         // up
            if (wait < 0)
                wait = 0;
            if (wait != lastShownWaitTime) {
                lastShownWaitTime = wait;
                progressDialog.setMessage(MessageFormat.format(waitText, wait));
            }
        }
        return;
    }

    if (progressDialog != null) {
        progressDialog.dismiss();
        progressDialog = null;
    }

    boolean forceUpdate = false;

    if (rmbtService.getNdtStatus() == NdtStatus.RUNNING) {
        final String ndtStatus = String.format(Locale.US, "NDT (%d%%)",
                Math.round(rmbtService.getNDTProgress() * 100));
        if (lastStatusString == null || !ndtStatus.equals(lastStatusString)) {
            forceUpdate = true;
            lastStatusString = ndtStatus;
        }
    } else if (lastStatus != intermediateResult.status) {
        lastStatus = intermediateResult.status;
        lastStatusString = Helperfunctions.getTestStatusString(getResources(), intermediateResult.status);
        forceUpdate = true;
    }

    if (updateCounter % SLOW_UPDATE_COUNT == 0 || forceUpdate) {
        final int networkType = rmbtService.getNetworkType();
        if (lastNetworkType != networkType && networkType != 0) {
            lastNetworkType = networkType;
            lastNetworkTypeString = Helperfunctions.getNetworkTypeName(networkType);
        }

        final String operatorName = rmbtService.getOperatorName();
        if (operatorName != null)
            lastOperatorName = operatorName;
        testView.setHeaderString(lastOperatorName);
        testView.setSubHeaderString(lastNetworkTypeString);
        final String serverName = rmbtService.getServerName();
        if (serverName != null)
            lastServerName = serverName;
        if (lastServerName == null)
            lastServerName = "?";

        final Location location = rmbtService.getLocation();
        if (location != null && !location.equals(lastLocation))
            lastLocation = location;

        final String locationStr_line1;
        final String locationStr_line2;
        if (lastLocation != null) {
            locationStr_line1 = Helperfunctions.getLocationString(context, getResources(), lastLocation, 1);
            locationStr_line2 = Helperfunctions.getLocationString(context, getResources(), lastLocation, 2);
        } else {
            locationStr_line1 = "";
            locationStr_line2 = "";
        }

        final String ip = rmbtService.getIP();
        if (ip != null)
            lastIP = ip;
        if (lastIP == null)
            lastIP = "?";

        teststatus = lastStatusString;

        textView.setText(MessageFormat.format(bottomText, teststatus, lastServerName, lastIP, locationStr_line1,
                locationStr_line2));
    }

    Integer signal = rmbtService.getSignal();
    int signalType = rmbtService.getSignalType();

    if (signal == null || signal == 0)
        signalType = InformationCollector.SINGAL_TYPE_NO_SIGNAL;

    boolean signalTypeChanged = false;

    if (signalType != InformationCollector.SINGAL_TYPE_NO_SIGNAL) {
        signalTypeChanged = lastSignalType != signalType;
        lastSignal = signal;
        lastSignalType = signalType;
    }

    if (signalType == InformationCollector.SINGAL_TYPE_NO_SIGNAL
            && lastSignalType != InformationCollector.SINGAL_TYPE_NO_SIGNAL) {
        // keep old signal if we had one before
        signal = lastSignal;
        signalType = lastSignalType;
    }

    Double relativeSignal = null;
    MinMax<Integer> signalBounds = NetworkUtil.getSignalStrengthBounds(signalType);
    if (!(signalBounds.min == Integer.MIN_VALUE || signalBounds.max == Integer.MAX_VALUE))
        relativeSignal = (double) (signal - signalBounds.min) / (double) (signalBounds.max - signalBounds.min);

    if (signalTypeChanged && graphView != null) {
        if (signalGraph != null)
            signalGraph.clearGraphDontResetTime();
        if (relativeSignal != null)
            graphView.setSignalRange(signalBounds.min, signalBounds.max);
        else
            graphView.removeSignalRange();
        graphView.invalidate();
    }

    double speedValueRelative = 0d;
    int progressSegments = 0;
    switch (intermediateResult.status) {
    case WAIT:
        textView.setText("UDP progress: " + intermediateResult.progress);
        break;

    case INIT:
        progressSegments = Math.round(PROGRESS_SEGMENTS_INIT * intermediateResult.progress);
        if (graphStarted)
            resetGraph();
        break;

    case PING:
        progressSegments = PROGRESS_SEGMENTS_INIT
                + Math.round(PROGRESS_SEGMENTS_PING * intermediateResult.progress);
        break;

    case DOWN:
        progressSegments = PROGRESS_SEGMENTS_INIT + PROGRESS_SEGMENTS_PING
                + Math.round(PROGRESS_SEGMENTS_DOWN * intermediateResult.progress);
        speedValueRelative = intermediateResult.downBitPerSecLog;

        if (graphView != null) {
            if (uploadGraph)
                resetGraph();

            if (graphStarted || speedValueRelative != 0) {
                graphStarted = true;
                speedGraph.addValue(speedValueRelative, SmoothGraph.FLAG_USE_CURRENT_NODE_TIME);
                if (relativeSignal != null)
                    signalGraph.addValue(relativeSignal);
                graphView.invalidate();
            }
        }
        break;

    case INIT_UP:
        progressSegments = PROGRESS_SEGMENTS_INIT + PROGRESS_SEGMENTS_PING + PROGRESS_SEGMENTS_DOWN;
        speedValueRelative = intermediateResult.downBitPerSecLog;
        break;

    case UP:
        progressSegments = PROGRESS_SEGMENTS_INIT + PROGRESS_SEGMENTS_PING + PROGRESS_SEGMENTS_DOWN
                + Math.round(PROGRESS_SEGMENTS_UP * intermediateResult.progress);
        speedValueRelative = intermediateResult.upBitPerSecLog;

        if (graphView != null) {
            if (!uploadGraph) {
                resetGraph();
                uploadGraph = true;
            }
            if (graphStarted || speedValueRelative != 0) {
                graphStarted = true;
                speedGraph.addValue(speedValueRelative, SmoothGraph.FLAG_USE_CURRENT_NODE_TIME);
                if (relativeSignal != null)
                    signalGraph.addValue(relativeSignal);
                graphView.invalidate();
            }
        }
        break;

    case SPEEDTEST_END:
    case QOS_TEST_RUNNING:
        progressSegments = PROGRESS_SEGMENTS_INIT + PROGRESS_SEGMENTS_PING + PROGRESS_SEGMENTS_DOWN
                + PROGRESS_SEGMENTS_UP;
        speedValueRelative = intermediateResult.upBitPerSecLog;

        qosMode = true;

        break;

    case ERROR:
    case ABORTED:
        progressSegments = 0;
        resetGraph();

        if (!rmbtService.isLoopMode())
            showErrorDialog(R.string.test_dialog_error_text);
        return;
    }
    testView.setSpeedValue(speedValueRelative);

    testView.setSignalType(signalType);
    if (signal != null)
        testView.setSignalString(String.valueOf(signal));
    if (relativeSignal != null)
        testView.setSignalValue(relativeSignal);

    final double progressValue = (double) progressSegments / PROGRESS_SEGMENTS_TOTAL;
    final double correctProgressValue = testView.setProgressValue(progressValue);
    testView.setProgressString(percentFormat.format(correctProgressValue));

    final String pingStr;
    if (intermediateResult.pingNano < 0)
        pingStr = "";
    else
        pingStr = pingFormat.format(intermediateResult.pingNano / 1000000.0);
    testView.setResultPingString(pingStr);
    final String downStr;
    if (intermediateResult.downBitPerSec < 0)
        downStr = "";
    else
        downStr = speedFormat.format(intermediateResult.downBitPerSec / 1000000.0);
    testView.setResultDownString(downStr);
    final String upStr;
    if (intermediateResult.upBitPerSec < 0)
        upStr = "";
    else
        upStr = speedFormat.format(intermediateResult.upBitPerSec / 1000000.0);
    testView.setResultUpString(upStr);

    testView.setTestStatus(intermediateResult.status);

    testView.invalidate();
}

From source file:com.nogago.android.tracks.services.TrackRecordingService.java

private void onLocationChangedAsync(Location location) {
    Log.d(TAG, "TrackRecordingService.onLocationChanged");

    try {//from  www  .j a v a  2s. c  om
        // Don't record if the service has been asked to pause recording:
        if (!isRecording) {
            Log.w(TAG, "Not recording because recording has been paused.");
            return;
        }

        // This should never happen, but just in case (we really don't want the
        // service to crash):
        if (location == null) {
            Log.w(TAG, "Location changed, but location is null.");
            return;
        }

        // Don't record if the accuracy is too bad:
        if (location.getAccuracy() > minRequiredAccuracy) {
            Log.d(TAG, "Not recording. Bad accuracy.");
            return;
        }

        // At least one track must be available for appending points:
        recordingTrack = getRecordingTrack();
        if (recordingTrack == null) {
            Log.d(TAG, "Not recording. No track to append to available.");
            return;
        }

        // Update the idle time if needed.
        locationListenerPolicy.updateIdleTime(statsBuilder.getIdleTime());
        addLocationToStats(location);
        if (currentRecordingInterval != locationListenerPolicy.getDesiredPollingInterval()) {
            registerLocationListener();
        }

        Location lastRecordedLocation = providerUtils.getLastLocation();
        double distanceToLastRecorded = Double.POSITIVE_INFINITY;
        if (lastRecordedLocation != null) {
            distanceToLastRecorded = location.distanceTo(lastRecordedLocation);
        }
        double distanceToLast = Double.POSITIVE_INFINITY;
        if (lastLocation != null) {
            distanceToLast = location.distanceTo(lastLocation);
        }
        boolean hasSensorData = sensorManager != null && sensorManager.isEnabled()
                && sensorManager.getSensorDataSet() != null && sensorManager.isSensorDataSetValid();

        // If the user has been stationary for two recording just record the first
        // two and ignore the rest. This code will only have an effect if the
        // maxRecordingDistance = 0
        if (distanceToLast == 0 && !hasSensorData) {
            if (isMoving) {
                Log.d(TAG, "Found two identical locations.");
                isMoving = false;
                if (lastLocation != null && lastRecordedLocation != null
                        && !lastRecordedLocation.equals(lastLocation)) {
                    // Need to write the last location. This will happen when
                    // lastRecordedLocation.distance(lastLocation) <
                    // minRecordingDistance
                    if (!insertLocation(lastLocation, lastRecordedLocation, recordingTrackId)) {
                        return;
                    }
                }
            } else {
                Log.d(TAG, "Not recording. More than two identical locations.");
            }
        } else if (distanceToLastRecorded > minRecordingDistance || hasSensorData) {
            if (lastLocation != null && !isMoving) {
                // Last location was the last stationary location. Need to go back and
                // add it.
                if (!insertLocation(lastLocation, lastRecordedLocation, recordingTrackId)) {
                    return;
                }
                isMoving = true;
            }

            // If separation from last recorded point is too large insert a
            // separator to indicate end of a segment:
            boolean startNewSegment = lastRecordedLocation != null && lastRecordedLocation.getLatitude() < 90
                    && distanceToLastRecorded > maxRecordingDistance && recordingTrack.getStartId() >= 0;
            if (startNewSegment) {
                // Insert a separator point to indicate start of new track:
                Log.d(TAG, "Inserting a separator.");
                Location separator = new Location(LocationManager.GPS_PROVIDER);
                separator.setLongitude(0);
                separator.setLatitude(100);
                separator.setTime(lastRecordedLocation.getTime());
                providerUtils.insertTrackPoint(separator, recordingTrackId);
            }

            if (!insertLocation(location, lastRecordedLocation, recordingTrackId)) {
                return;
            }
        } else {
            Log.d(TAG,
                    String.format(Locale.US,
                            "Not recording. Distance to last recorded point (%f m) is less than %d m.",
                            distanceToLastRecorded, minRecordingDistance));
            // Return here so that the location is NOT recorded as the last location.
            return;
        }
    } catch (Error e) {
        // Probably important enough to rethrow.
        Log.e(TAG, "Error in onLocationChanged", e);
        throw e;
    } catch (RuntimeException e) {
        // Safe usually to trap exceptions.
        Log.e(TAG, "Trapping exception in onLocationChanged", e);
        throw e;
    }
    lastLocation = location;
}