Android Open Source - getback_gps Tools






From Project

Back to project page getback_gps.

License

The source code is released under:

GNU General Public License

If you think the Android project getback_gps listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/**
 * Collection of useful methods.//from w  w  w. j  ava 2 s  .  co m
 *
 * Copyright (C) 2014-2015 Dieter Adriaenssens
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @package com.github.ruleant.getback_gps.lib
 * @author  Dieter Adriaenssens <ruleant@users.sourceforge.net>
 */
package com.github.ruleant.getback_gps.lib;

import android.annotation.TargetApi;
import android.os.Build;
import android.os.SystemClock;

/**
 * Collection of useful methods.
 *
 * @author  Dieter Adriaenssens <ruleant@users.sourceforge.net>
 */
public class Tools {
    /**
     * Millisecond to nanosecond conversion rate.
     */
    public static final long MILLI_IN_NANO = 1000000;

    /**
     * Microsecond to nanosecond conversion rate.
     */
    public static final long MICRO_IN_NANO = 1000;

    /**
     * Seconds to milliseconds conversion rate.
     */
    public static final long SECOND_IN_MILLIS = 1000;

    /**
     * Hidden constructor, to prevent instantiating.
     */
    protected Tools() {
        // prevents calls from subclass
        throw new UnsupportedOperationException();
    }

    /**
     * Return biggest number.
     *
     * @param value1 one value
     * @param value2 another value
     * @return biggest number
     */
    public static long getMax(final long value1, final long value2) {
        if (value1 > value2) {
            return value1;
        } else {
            return value2;
        }
    }

    /**
     * Returns current timestamp.
     *
     * @return realtime timestamp in nanoseconds
     */
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    public static long getTimestampNano() {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
            // use elapsedRealtime when using API 16 or lower
            return SystemClock.elapsedRealtime() * MILLI_IN_NANO;
        } else {
            // use elapsedRealtimeNanos when using API 17 or higher
            return SystemClock.elapsedRealtimeNanos();
        }
    }

    /**
     * Checks if timestamp (in milliseconds) is recent.
     *
     * @param timestamp timestamp in milliseconds
     * @param validity timestamp validity in milliseconds
     * @return true if timestamp is recent.
     */
    public static boolean isTimestampRecent(final long timestamp,
                                      final long validity) {
        // TODO test with instrumentation test
        return isTimestampRecent(SystemClock.elapsedRealtime(),
                timestamp, validity);
    }

    /**
     * Checks if timestamp (in nanoseconds) is recent.
     *
     * @param timestamp timestamp in nanoseconds
     * @param validity timestamp validity in nanoseconds
     * @return true if timestamp is recent.
     */
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    public static boolean isTimestampNanoRecent(final long timestamp,
                                          final long validity) {
        // TODO test with instrumentation test
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
            return isTimestampRecent(timestamp / MILLI_IN_NANO,
                    validity / MILLI_IN_NANO);
        } else {
            // use elapsedRealtimeNanos when using API 17 or higher
            return isTimestampRecent(SystemClock.elapsedRealtimeNanos(),
                    timestamp, validity);
        }
    }

    /**
     * Checks if timestamp is recent.
     *
     * @param currentTimestamp current timestamp
     * @param previousTimestamp previous timestamp
     * @param validity timestamp validity (maximum difference)
     * @return true if timestamp is recent.
     */
    public static boolean isTimestampRecent(final long currentTimestamp,
                                           final long previousTimestamp,
                                            final long validity) {
        // check parameter ranges
        if (currentTimestamp < 0) {
            throw new IllegalArgumentException(
                "currentTimestamp can't be a negative value");
        }

        if (previousTimestamp < 0) {
            throw new IllegalArgumentException(
                "previousTimestamp can't be a negative value");
        }

        if (validity <= 0) {
            throw new IllegalArgumentException(
                "validity should be a non-zero positive value");
        }

        // is currentTimestamp more recent than previousTimestamp
        // and within a valid range
        return currentTimestamp >= previousTimestamp
                && (currentTimestamp - previousTimestamp) <= validity;
    }
}




Java Source Code List

com.github.ruleant.getback_gps.AboutActivity.java
com.github.ruleant.getback_gps.AbstractGetBackGpsActivity.java
com.github.ruleant.getback_gps.DetailsActivity.java
com.github.ruleant.getback_gps.LocationService.java
com.github.ruleant.getback_gps.MainActivity.java
com.github.ruleant.getback_gps.NavigationView.java
com.github.ruleant.getback_gps.SettingsActivity.java
com.github.ruleant.getback_gps.lib.AbstractGeoCoordinate.java
com.github.ruleant.getback_gps.lib.AriadneLocation.java
com.github.ruleant.getback_gps.lib.CardinalDirection.java
com.github.ruleant.getback_gps.lib.CircularAverage.java
com.github.ruleant.getback_gps.lib.CoordinateConverterInterface.java
com.github.ruleant.getback_gps.lib.CoordinateRotation.java
com.github.ruleant.getback_gps.lib.Coordinate.java
com.github.ruleant.getback_gps.lib.Coordinates.java
com.github.ruleant.getback_gps.lib.DebugLevel.java
com.github.ruleant.getback_gps.lib.FormatUtils.java
com.github.ruleant.getback_gps.lib.Latitude.java
com.github.ruleant.getback_gps.lib.Longitude.java
com.github.ruleant.getback_gps.lib.LowPassFilter.java
com.github.ruleant.getback_gps.lib.Navigator.java
com.github.ruleant.getback_gps.lib.SensorOrientation.java
com.github.ruleant.getback_gps.lib.StoredDestination.java
com.github.ruleant.getback_gps.lib.StoredLocation.java
com.github.ruleant.getback_gps.lib.Tools.java
com.github.ruleant.getback_gps.lib.package-info.java
com.github.ruleant.getback_gps.package-info.java
com.github.ruleant.unitconversion.UnitConversionInterface.java
com.github.ruleant.unitconversion.package-info.java