Java Examples for android.net.SntpClient

The following java examples will help you to understand the usage of android.net.SntpClient. These source code samples are taken from different open source projects.

Example 1
Project: cnAndroidDocs-master  File: NtpTrustedTime.java View source code
@Override
public boolean forceRefresh() {
    if (mServer == null) {
        // missing server, so no trusted time available
        return false;
    }
    if (LOGD)
        Log.d(TAG, "forceRefresh() from cache miss");
    final SntpClient client = new SntpClient();
    if (client.requestTime(mServer, (int) mTimeout)) {
        mHasCache = true;
        mCachedNtpTime = client.getNtpTime();
        mCachedNtpElapsedRealtime = client.getNtpTimeReference();
        mCachedNtpCertainty = client.getRoundTripTime() / 2;
        return true;
    } else {
        return false;
    }
}
Example 2
Project: MobileSensing-master  File: TimeProvider.java View source code
/**
   * Method to synchronize time with a NTP server
   * 
   * @return true if successful, false otherwise
   */
private final boolean syncTime(Context context) {
    // hack
    return false;
//    android.net.SntpClient sntpClient = new SntpClient();
//    
//    for ( String provider : getProviders() )
//    {
//      // after a wake up we do often fail with a route to host, even if network
//      // is connected
//      // thus we do force a route to host request here
//      if ( !ConnectivityWrapperImpl.getInstance().testHostReachability(
//          context, provider ) )
//        Logger.getInstance().debug( this, "No route to host: " + provider );
//      
//      if ( sntpClient.requestTime( provider, 5000 ) )
//      {
//        long phoneTime = System.currentTimeMillis();
//        long now =
//            sntpClient.getNtpTime() + SystemClock.elapsedRealtime()
//                - sntpClient.getNtpTimeReference();
//        setOffset( phoneTime - now );
//        lastUpdateTs.set( getTimeStamp() );
//        synced.compareAndSet( false, true );
//        break;
//      }
//    }
//    
//    return synced.get();
}
Example 3
Project: folio100_frameworks_base-master  File: GpsLocationProvider.java View source code
private void handleInjectNtpTime() {
    if (!mNetworkAvailable) {
        // try again when network is up
        mInjectNtpTimePending = true;
        return;
    }
    mInjectNtpTimePending = false;
    SntpClient client = new SntpClient();
    long delay;
    if (client.requestTime(mNtpServer, 10000)) {
        long time = client.getNtpTime();
        long timeReference = client.getNtpTimeReference();
        int certainty = (int) (client.getRoundTripTime() / 2);
        long now = System.currentTimeMillis();
        long systemTimeOffset = time - now;
        Log.d(TAG, "NTP server returned: " + time + " (" + new Date(time) + ") reference: " + timeReference + " certainty: " + certainty + " system time offset: " + systemTimeOffset);
        // sanity check NTP time and do not use if it is too far from system time
        if (systemTimeOffset < 0) {
            systemTimeOffset = -systemTimeOffset;
        }
        if (systemTimeOffset < MAX_NTP_SYSTEM_TIME_OFFSET) {
            native_inject_time(time, timeReference, certainty);
        } else {
            Log.e(TAG, "NTP time differs from system time by " + systemTimeOffset + "ms.  Ignoring.");
        }
        delay = NTP_INTERVAL;
    } else {
        if (DEBUG)
            Log.d(TAG, "requestTime failed");
        delay = RETRY_INTERVAL;
    }
    // send delayed message for next NTP injection
    mHandler.removeMessages(INJECT_NTP_TIME);
    mHandler.sendMessageDelayed(Message.obtain(mHandler, INJECT_NTP_TIME), delay);
}
Example 4
Project: WS171-frameworks-base-master  File: GpsLocationProvider.java View source code
public void runLocked() {
    if (Config.LOGD)
        Log.d(TAG, "NetworkThread starting");
    SntpClient client = new SntpClient();
    GpsXtraDownloader xtraDownloader = null;
    if (native_supports_xtra()) {
        xtraDownloader = new GpsXtraDownloader(mContext, mProperties);
    }
    // thread exits after disable() is called
    while (!mDone) {
        long waitTime = getWaitTime();
        do {
            synchronized (this) {
                try {
                    if (!mNetworkAvailable) {
                        if (Config.LOGD)
                            Log.d(TAG, "NetworkThread wait for network");
                        wait();
                    } else if (waitTime > 0) {
                        if (Config.LOGD) {
                            Log.d(TAG, "NetworkThread wait for " + waitTime + "ms");
                        }
                        wait(waitTime);
                    }
                } catch (InterruptedException e) {
                    if (Config.LOGD) {
                        Log.d(TAG, "InterruptedException in GpsNetworkThread");
                    }
                }
            }
            waitTime = getWaitTime();
        } while (!mDone && ((!mXtraDownloadRequested && !mSetSuplServer && waitTime > 0) || !mNetworkAvailable));
        if (Config.LOGD)
            Log.d(TAG, "NetworkThread out of wake loop");
        if (!mDone) {
            if (mNtpServer != null && mNextNtpTime <= System.currentTimeMillis()) {
                if (Config.LOGD) {
                    Log.d(TAG, "Requesting time from NTP server " + mNtpServer);
                }
                if (client.requestTime(mNtpServer, 10000)) {
                    long time = client.getNtpTime();
                    long timeReference = client.getNtpTimeReference();
                    int certainty = (int) (client.getRoundTripTime() / 2);
                    if (Config.LOGD)
                        Log.d(TAG, "calling native_inject_time: " + time + " reference: " + timeReference + " certainty: " + certainty);
                    native_inject_time(time, timeReference, certainty);
                    mNextNtpTime = System.currentTimeMillis() + NTP_INTERVAL;
                } else {
                    if (Config.LOGD)
                        Log.d(TAG, "requestTime failed");
                    mNextNtpTime = System.currentTimeMillis() + RETRY_INTERVAL;
                }
            }
            // Set the SUPL server address if we have not yet
            if (mSetSuplServer) {
                try {
                    InetAddress inetAddress = InetAddress.getByName(mSuplHost);
                    if (inetAddress != null) {
                        byte[] addrBytes = inetAddress.getAddress();
                        long addr = 0;
                        for (int i = 0; i < addrBytes.length; i++) {
                            int temp = addrBytes[i];
                            // signed -> unsigned
                            if (temp < 0)
                                temp = 256 + temp;
                            addr = addr * 256 + temp;
                        }
                        // use MS-Based position mode if SUPL support is enabled
                        mPositionMode = GPS_POSITION_MODE_MS_BASED;
                        native_set_supl_server((int) addr, mSuplPort);
                        mSetSuplServer = false;
                    }
                } catch (UnknownHostException e) {
                    Log.e(TAG, "unknown host for SUPL server " + mSuplHost);
                }
            }
            if ((mXtraDownloadRequested || (mNextXtraTime > 0 && mNextXtraTime <= System.currentTimeMillis())) && xtraDownloader != null) {
                byte[] data = xtraDownloader.downloadXtraData();
                if (data != null) {
                    if (Config.LOGD) {
                        Log.d(TAG, "calling native_inject_xtra_data");
                    }
                    native_inject_xtra_data(data, data.length);
                    mNextXtraTime = 0;
                    mXtraDownloadRequested = false;
                } else {
                    mNextXtraTime = System.currentTimeMillis() + RETRY_INTERVAL;
                }
            }
        }
    }
    if (Config.LOGD)
        Log.d(TAG, "NetworkThread exiting");
}
Example 5
Project: XobotOS-master  File: NtpTrustedTime.java View source code
/** {@inheritDoc} */
public boolean forceRefresh() {
    if (mServer == null) {
        // missing server, so no trusted time available
        return false;
    }
    if (LOGD)
        Log.d(TAG, "forceRefresh() from cache miss");
    final SntpClient client = new SntpClient();
    if (client.requestTime(mServer, (int) mTimeout)) {
        mHasCache = true;
        mCachedNtpTime = client.getNtpTime();
        mCachedNtpElapsedRealtime = client.getNtpTimeReference();
        mCachedNtpCertainty = client.getRoundTripTime() / 2;
        return true;
    } else {
        return false;
    }
}
Example 6
Project: property-db-master  File: NtpTrustedTime.java View source code
@Override
public boolean forceRefresh() {
    if (mServer == null) {
        // missing server, so no trusted time available
        return false;
    }
    if (LOGD)
        Log.d(TAG, "forceRefresh() from cache miss");
    final SntpClient client = new SntpClient();
    if (client.requestTime(mServer, (int) mTimeout)) {
        mHasCache = true;
        mCachedNtpTime = client.getNtpTime();
        mCachedNtpElapsedRealtime = client.getNtpTimeReference();
        mCachedNtpCertainty = client.getRoundTripTime() / 2;
        return true;
    } else {
        return false;
    }
}
Example 7
Project: frameworks_base_disabled-master  File: NtpTrustedTime.java View source code
/** {@inheritDoc} */
public boolean forceRefresh() {
    if (mServer == null) {
        // missing server, so no trusted time available
        return false;
    }
    if (LOGD)
        Log.d(TAG, "forceRefresh() from cache miss");
    final SntpClient client = new SntpClient();
    if (client.requestTime(mServer, (int) mTimeout)) {
        mHasCache = true;
        mCachedNtpTime = client.getNtpTime();
        mCachedNtpElapsedRealtime = client.getNtpTimeReference();
        mCachedNtpCertainty = client.getRoundTripTime() / 2;
        return true;
    } else {
        return false;
    }
}
Example 8
Project: android-15-master  File: NtpTrustedTime.java View source code
/** {@inheritDoc} */
public boolean forceRefresh() {
    if (mServer == null) {
        // missing server, so no trusted time available
        return false;
    }
    if (LOGD)
        Log.d(TAG, "forceRefresh() from cache miss");
    final SntpClient client = new SntpClient();
    if (client.requestTime(mServer, (int) mTimeout)) {
        mHasCache = true;
        mCachedNtpTime = client.getNtpTime();
        mCachedNtpElapsedRealtime = client.getNtpTimeReference();
        mCachedNtpCertainty = client.getRoundTripTime() / 2;
        return true;
    } else {
        return false;
    }
}
Example 9
Project: platform_frameworks_base-master  File: NtpTrustedTime.java View source code
@Override
public boolean forceRefresh() {
    if (TextUtils.isEmpty(mServer)) {
        // missing server, so no trusted time available
        return false;
    }
    // We can't do this at initialization time: ConnectivityService might not be running yet.
    synchronized (this) {
        if (mCM == null) {
            mCM = (ConnectivityManager) sContext.getSystemService(Context.CONNECTIVITY_SERVICE);
        }
    }
    final NetworkInfo ni = mCM == null ? null : mCM.getActiveNetworkInfo();
    if (ni == null || !ni.isConnected()) {
        if (LOGD)
            Log.d(TAG, "forceRefresh: no connectivity");
        return false;
    }
    if (LOGD)
        Log.d(TAG, "forceRefresh() from cache miss");
    final SntpClient client = new SntpClient();
    if (client.requestTime(mServer, (int) mTimeout)) {
        mHasCache = true;
        mCachedNtpTime = client.getNtpTime();
        mCachedNtpElapsedRealtime = client.getNtpTimeReference();
        mCachedNtpCertainty = client.getRoundTripTime() / 2;
        return true;
    } else {
        return false;
    }
}
Example 10
Project: android_frameworks_base-master  File: NtpTrustedTime.java View source code
@Override
public boolean forceRefresh() {
    if (TextUtils.isEmpty(mServer)) {
        // missing server, so no trusted time available
        return false;
    }
    // We can't do this at initialization time: ConnectivityService might not be running yet.
    synchronized (this) {
        if (mCM == null) {
            mCM = (ConnectivityManager) sContext.getSystemService(Context.CONNECTIVITY_SERVICE);
        }
    }
    final NetworkInfo ni = mCM == null ? null : mCM.getActiveNetworkInfo();
    if (ni == null || !ni.isConnected()) {
        if (LOGD)
            Log.d(TAG, "forceRefresh: no connectivity");
        return false;
    }
    if (LOGD)
        Log.d(TAG, "forceRefresh() from cache miss");
    final SntpClient client = new SntpClient();
    if (client.requestTime(mServer, (int) mTimeout)) {
        mHasCache = true;
        mCachedNtpTime = client.getNtpTime();
        mCachedNtpElapsedRealtime = client.getNtpTimeReference();
        mCachedNtpCertainty = client.getRoundTripTime() / 2;
        return true;
    } else {
        return false;
    }
}
Example 11
Project: android-sdk-sources-for-api-level-23-master  File: NtpTrustedTime.java View source code
@Override
public boolean forceRefresh() {
    if (TextUtils.isEmpty(mServer)) {
        // missing server, so no trusted time available
        return false;
    }
    // We can't do this at initialization time: ConnectivityService might not be running yet.
    synchronized (this) {
        if (mCM == null) {
            mCM = (ConnectivityManager) sContext.getSystemService(Context.CONNECTIVITY_SERVICE);
        }
    }
    final NetworkInfo ni = mCM == null ? null : mCM.getActiveNetworkInfo();
    if (ni == null || !ni.isConnected()) {
        if (LOGD)
            Log.d(TAG, "forceRefresh: no connectivity");
        return false;
    }
    if (LOGD)
        Log.d(TAG, "forceRefresh() from cache miss");
    final SntpClient client = new SntpClient();
    if (client.requestTime(mServer, (int) mTimeout)) {
        mHasCache = true;
        mCachedNtpTime = client.getNtpTime();
        mCachedNtpElapsedRealtime = client.getNtpTimeReference();
        mCachedNtpCertainty = client.getRoundTripTime() / 2;
        return true;
    } else {
        return false;
    }
}