Example usage for android.os SystemClock elapsedRealtime

List of usage examples for android.os SystemClock elapsedRealtime

Introduction

In this page you can find the example usage for android.os SystemClock elapsedRealtime.

Prototype

@CriticalNative
native public static long elapsedRealtime();

Source Link

Document

Returns milliseconds since boot, including time spent in sleep.

Usage

From source file:com.horizondigital.delta.UpdateService.java

private Thread getThreadedProgress(String filename, String display, long start, long currentOut,
        long totalOut) {
    final File _file = new File(filename);
    final String _display = display;
    final long _currentOut = currentOut;
    final long _totalOut = totalOut;
    final long _start = start;

    return new Thread(new Runnable() {
        @Override/*from ww  w.j  a v  a 2  s  .co  m*/
        public void run() {
            while (true) {
                try {
                    long current = _currentOut + _file.length();
                    updateState(STATE_ACTION_APPLYING_PATCH, ((float) current / (float) _totalOut) * 100f,
                            current, _totalOut, _display, SystemClock.elapsedRealtime() - _start);

                    Thread.sleep(16);
                } catch (InterruptedException e) {
                    // We're being told to quit
                    break;
                }
            }
        }
    });
}

From source file:com.android.tv.settings.accessories.AddAccessoryActivity.java

@Override
public void statusChanged() {
    synchronized (mLock) {
        if (mBtPairer == null)
            return;

        int numDevices = mBtPairer.getAvailableDevices().size();
        int status = mBtPairer.getStatus();
        int oldStatus = mPreviousStatus;
        mPreviousStatus = status;//w  w w.  ja v a  2  s.  c  o  m

        String address = mBtPairer.getTargetDevice() == null ? ADDRESS_NONE
                : mBtPairer.getTargetDevice().getAddress();

        if (DEBUG) {
            String state = "?";
            switch (status) {
            case InputPairer.STATUS_NONE:
                state = "InputPairer.STATUS_NONE";
                break;
            case InputPairer.STATUS_SCANNING:
                state = "InputPairer.STATUS_SCANNING";
                break;
            case InputPairer.STATUS_WAITING_TO_PAIR:
                state = "InputPairer.STATUS_WAITING_TO_PAIR";
                break;
            case InputPairer.STATUS_PAIRING:
                state = "InputPairer.STATUS_PAIRING";
                break;
            case InputPairer.STATUS_CONNECTING:
                state = "InputPairer.STATUS_CONNECTING";
                break;
            case InputPairer.STATUS_ERROR:
                state = "InputPairer.STATUS_ERROR";
                break;
            }
            long time = mBtPairer.getNextStageTime() - SystemClock.elapsedRealtime();
            Log.d(TAG, "Update received, number of devices:" + numDevices + " state: " + state
                    + " target device: " + address + " time to next event: " + time);
        }

        mBtDevices.clear();
        for (BluetoothDevice device : mBtPairer.getAvailableDevices()) {
            mBtDevices.add(device);
        }

        cancelTimeout();

        switch (status) {
        case InputPairer.STATUS_NONE:
            // if we just connected to something or just tried to connect
            // to something, restart scanning just in case the user wants
            // to pair another device.
            if (oldStatus == InputPairer.STATUS_CONNECTING) {
                if (mPairingSuccess) {
                    // Pairing complete
                    mCurrentTargetStatus = getString(R.string.accessory_state_paired);
                    mMsgHandler.sendEmptyMessage(MSG_UPDATE_VIEW);
                    mMsgHandler.sendEmptyMessageDelayed(MSG_PAIRING_COMPLETE, DONE_MESSAGE_TIMEOUT);
                    mDone = true;
                    if (mAnimation != null) {
                        mAnimation.setOneShot(true);
                    }

                    // Done, return here and just wait for the message
                    // to close the activity
                    return;
                }
                if (DEBUG) {
                    Log.d(TAG, "Invalidating and restarting.");
                }

                mBtPairer.invalidateDevice(mBtPairer.getTargetDevice());
                mBtPairer.start();
                mBtPairer.cancelPairing();
                setPairingBluetooth(false);

                // if this looks like a successful connection run, reflect
                // this in the UI, otherwise use the default message
                if (!mPairingSuccess && InputPairer.hasValidInputDevice(this)) {
                    mPairingSuccess = true;
                }
            }
            break;
        case InputPairer.STATUS_SCANNING:
            mPairingSuccess = false;
            break;
        case InputPairer.STATUS_WAITING_TO_PAIR:
            break;
        case InputPairer.STATUS_PAIRING:
            // reset the pairing success value since this is now a new
            // pairing run
            mPairingSuccess = true;
            setTimeout(PAIR_OPERATION_TIMEOUT);
            break;
        case InputPairer.STATUS_CONNECTING:
            setTimeout(CONNECT_OPERATION_TIMEOUT);
            break;
        case InputPairer.STATUS_ERROR:
            mPairingSuccess = false;
            setPairingBluetooth(false);
            if (mNoInputMode) {
                clearDeviceList();
            }
            break;
        }

        mCurrentTargetAddress = address;
        mCurrentTargetStatus = getMessageForStatus(status);
        mMsgHandler.sendEmptyMessage(MSG_UPDATE_VIEW);
    }
}

From source file:com.saulcintero.moveon.services.MoveOnService.java

public static void stopPractice() {
    isPracticeRunning = false;//w  w  w .j av  a2  s.  c o m
    isPausedForSavePractice = false;

    TextToSpeechUtils.say(mContext, mContext.getString(R.string.tts_stop_practice));

    if (!pause)
        locListener.populatesRoute(
                ((SystemClock.elapsedRealtime() / 1000) - (elapsedRealtime / 1000)) + accumulatedTime);
    else
        locListener.populatesRoute((accumulatedTime + (lengthChrono / 1000)) - (elapsedRealtime / 1000));

    pause = true;
    autoPause = false;

    if (prefs.getInt("practice_display5",
            TypesOfPractices.BASIC_PRACTICE.getTypes()) == TypesOfPractices.HIIT_PRACTICE.getTypes())
        releaseCountDownTimer();

    releaseMediaPlayer();
}

From source file:com.ubergeek42.WeechatAndroid.service.RelayServiceBackbone.java

@Override
public void onConnect() {
    if (DEBUG)//from w  w w .j a v  a2  s . c  om
        logger.debug("onConnect()");
    connection_status = CONNECTED;

    if (prefs.getBoolean(PREF_PING_ENABLED, true)) {
        long triggerAt = SystemClock.elapsedRealtime() + pingTimeout();
        schedulePing(triggerAt);
    }

    connection.addHandler("*", lastMessageHandler);

    for (RelayConnectionHandler rch : connectionHandlers)
        rch.onConnect();
}

From source file:com.scooter1556.sms.android.fragment.tv.TvAudioPlayerFragment.java

private void updateProgress() {
    if (lastPlaybackState == null || playbackControlsRow == null) {
        return;/* w w w  .  j a  va2  s.c  om*/
    }

    long currentPosition = lastPlaybackState.getPosition();

    if (lastPlaybackState.getState() != PlaybackStateCompat.STATE_PAUSED) {
        // Calculate the elapsed time between the last position update and now and unless
        // paused, we can assume (delta * speed) + current position is approximately the
        // latest position. This ensures that we do not repeatedly call the getPlaybackState()
        // on MediaControllerCompat.
        long timeDelta = SystemClock.elapsedRealtime() - lastPlaybackState.getLastPositionUpdateTime();
        currentPosition += (int) timeDelta * lastPlaybackState.getPlaybackSpeed();
    }

    playbackControlsRow.setCurrentTime((int) (currentPosition));
}

From source file:com.koma.music.service.MusicService.java

private void scheduleDelayedShutdown() {
    LogUtils.d(TAG, "Scheduling shutdown in " + MusicServiceConstants.IDLE_DELAY + " ms");
    if (!mReadGranted) {
        return;/*w ww  . j a va  2 s  .  com*/
    }
    mAlarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
            SystemClock.elapsedRealtime() + MusicServiceConstants.IDLE_DELAY, mShutdownIntent);
    mShutdownScheduled = true;
}

From source file:com.androzic.location.LocationService.java

@Override
public void onLocationChanged(final Location location) {
    tics++;//from   w  ww .  j  a  v  a  2  s. co m

    boolean fromGps = false;
    boolean sendUpdate = false;

    long time = SystemClock.elapsedRealtime();

    // Log.i(TAG, "Location arrived: "+location.toString());

    if (LocationManager.NETWORK_PROVIDER.equals(location.getProvider())) {
        if (useNetwork && (gpsStatus == GPS_OFF
                || (gpsStatus == GPS_SEARCHING && time > lastLocationMillis + gpsLocationTimeout))) {
            Log.d(TAG, "New location");
            lastKnownLocation = location;
            lastLocationMillis = time;
            isContinous = false;
            sendUpdate = true;
        } else {
            return;
        }
    } else {
        fromGps = true;

        Log.d(TAG, "Fix arrived");

        long prevLocationMillis = lastLocationMillis;
        float prevSpeed = lastKnownLocation.getSpeed();

        lastKnownLocation = location;
        lastLocationMillis = time;
        sendUpdate = true;

        if (!Float.isNaN(nmeaGeoidHeight)) {
            location.setAltitude(location.getAltitude() + nmeaGeoidHeight);
        }

        if (justStarted) {
            justStarted = prevSpeed == 0;
        } else if (lastKnownLocation.getSpeed() > 0) {
            // filter speed outrages
            double a = 2 * 9.8 * (lastLocationMillis - prevLocationMillis) / 1000;
            if (Math.abs(lastKnownLocation.getSpeed() - prevSpeed) > a)
                lastKnownLocation.setSpeed(prevSpeed);
        }

        // smooth speed
        float smoothspeed = 0;
        float curspeed = lastKnownLocation.getSpeed();
        for (int i = speed.length - 1; i > 1; i--) {
            smoothspeed += speed[i];
            speed[i] = speed[i - 1];
        }
        smoothspeed += speed[1];
        if (speed[1] < speed[0] && speed[0] > curspeed) {
            speed[0] = (speed[1] + curspeed) / 2;
        }
        smoothspeed += speed[0];
        speed[1] = speed[0];
        lastKnownLocation.setSpeed(speed[1]);
        speed[0] = curspeed;
        if (speed[0] == 0 && speed[1] == 0)
            smoothspeed = 0;
        else
            smoothspeed = smoothspeed / speed.length;

        // average speed
        float avspeed = 0;
        for (int i = speedav.length - 1; i >= 0; i--) {
            avspeed += speedav[i];
        }
        avspeed = avspeed / speedav.length;
        if (tics % pause == 0) {
            if (avspeed > 0) {
                float diff = curspeed / avspeed;
                if (0.95 < diff && diff < 1.05) {
                    for (int i = speedav.length - 1; i > 0; i--) {
                        speedav[i] = speedav[i - 1];
                    }
                    speedav[0] = curspeed;
                }
            }
            float fluct = 0;
            for (int i = speedavex.length - 1; i > 0; i--) {
                fluct += speedavex[i] / curspeed;
                speedavex[i] = speedavex[i - 1];
            }
            fluct += speedavex[0] / curspeed;
            speedavex[0] = curspeed;
            fluct = fluct / speedavex.length;
            if (0.95 < fluct && fluct < 1.05) {
                for (int i = speedav.length - 1; i >= 0; i--) {
                    speedav[i] = speedavex[i];
                }
                if (pause < 5)
                    pause++;
            }
        }

        smoothSpeed = smoothspeed;
        avgSpeed = avspeed;
    }

    /*
     * lastKnownLocation.setSpeed(20); lastKnownLocation.setBearing(55);
     * lastKnownLocation.setAltitude(169);
     * lastKnownLocation.setLatitude(55.852527);
     * lastKnownLocation.setLongitude(29.451150);
     */

    if (sendUpdate)
        updateLocation();

    isContinous = fromGps;
}

From source file:com.umaps.gpslogger.GpsLoggingService.java

private void SetAlarmForNextPoint() {
    Log.d(TAG, "Set alarm for " + AppSettings.getMinimumSeconds() + " seconds");

    Intent i = new Intent(this, GpsLoggingService.class);
    i.putExtra(IntentConstants.GET_NEXT_POINT, true);
    PendingIntent pi = PendingIntent.getService(this, 0, i, 0);
    nextPointAlarmManager.cancel(pi);/*from   ww  w  . j  ava 2 s .  c  o m*/

    nextPointAlarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
            SystemClock.elapsedRealtime() + AppSettings.getMinimumSeconds() * 1000, pi);
}

From source file:com.android.mail.utils.NotificationActionUtils.java

/**
 * Registers a timeout for the undo notification such that when it expires, the undo bar will
 * disappear, and the action will be performed.
 *///from  ww  w  .j a v a 2 s . c  o  m
public static void registerUndoTimeout(final Context context, final NotificationAction notificationAction) {
    LogUtils.i(LOG_TAG, "registerUndoTimeout for %s", notificationAction.getNotificationActionType());

    if (sUndoTimeoutMillis == -1) {
        sUndoTimeoutMillis = context.getResources().getInteger(R.integer.undo_notification_timeout);
    }

    final AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

    final long triggerAtMills = SystemClock.elapsedRealtime() + sUndoTimeoutMillis;

    final PendingIntent pendingIntent = createUndoTimeoutPendingIntent(context, notificationAction);

    alarmManager.set(AlarmManager.ELAPSED_REALTIME, triggerAtMills, pendingIntent);
}

From source file:com.gelakinetic.mtgfam.activities.MainActivity.java

public void displayTimeLeft() {
    long timeLeftMillis = endTime - SystemClock.elapsedRealtime();
    String timeLeftStr = "";

    if (timeLeftMillis <= 0) {
        timeLeftStr = "00:00:00";
    } else {/*from w  w w  . ja v a2 s  .c  om*/
        long timeLeftInSecs = (timeLeftMillis / 1000);

        // This is a slight hack to handle the fact that it always rounds down. It
        // makes the clock look much nicer this way.
        timeLeftInSecs++;

        String hours = String.valueOf(timeLeftInSecs / (3600));
        String minutes = String.valueOf((timeLeftInSecs % 3600) / 60);
        String seconds = String.valueOf(timeLeftInSecs % 60);

        if (hours.length() == 1) {
            timeLeftStr += "0";
        }
        timeLeftStr += hours + ":";

        if (minutes.length() == 1) {
            timeLeftStr += "0";
        }
        timeLeftStr += minutes + ":";

        if (seconds.length() == 1) {
            timeLeftStr += "0";
        }
        timeLeftStr += seconds;
    }

    if (timeShowing) {
        getSupportActionBar().setTitle(timeLeftStr);
    }
}