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.ichi2.anki.AbstractFlashcardViewer.java

protected void initTimer() {
    final TypedValue typedValue = new TypedValue();
    mShowTimer = mCurrentCard.showTimer();
    if (mShowTimer && mCardTimer.getVisibility() == View.INVISIBLE) {
        mCardTimer.setVisibility(View.VISIBLE);
    } else if (!mShowTimer && mCardTimer.getVisibility() != View.INVISIBLE) {
        mCardTimer.setVisibility(View.INVISIBLE);
    }/* www  . j  ava  2s .  c  om*/
    // Set normal timer color
    getTheme().resolveAttribute(android.R.attr.textColor, typedValue, true);
    mCardTimer.setTextColor(typedValue.data);

    mCardTimer.setBase(SystemClock.elapsedRealtime());
    mCardTimer.start();

    // Stop and highlight the timer if it reaches the time limit.
    getTheme().resolveAttribute(R.attr.maxTimerColor, typedValue, true);
    final int limit = mCurrentCard.timeLimit();
    mCardTimer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
        @Override
        public void onChronometerTick(Chronometer chronometer) {
            long elapsed = SystemClock.elapsedRealtime() - chronometer.getBase();
            if (elapsed >= limit) {
                chronometer.setTextColor(typedValue.data);
                chronometer.stop();
            }
        }
    });
}

From source file:eu.faircode.adblocker.ServiceSinkhole.java

@Override
public void onCreate() {
    Log.i(TAG, "Create version=" + Util.getSelfVersionName(this) + "/" + Util.getSelfVersionCode(this));

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

    // Native init
    jni_init();/* w w w .  j a v a  2 s.c  om*/
    boolean pcap = prefs.getBoolean("pcap", false);
    setPcap(pcap, this);

    prefs.registerOnSharedPreferenceChangeListener(this);

    Util.setTheme(this);
    super.onCreate();

    HandlerThread commandThread = new HandlerThread(getString(R.string.app_name) + " command");
    HandlerThread logThread = new HandlerThread(getString(R.string.app_name) + " log");
    HandlerThread statsThread = new HandlerThread(getString(R.string.app_name) + " stats");
    commandThread.start();
    logThread.start();
    statsThread.start();

    commandLooper = commandThread.getLooper();
    logLooper = logThread.getLooper();
    statsLooper = statsThread.getLooper();

    commandHandler = new CommandHandler(commandLooper);
    logHandler = new LogHandler(logLooper);
    statsHandler = new StatsHandler(statsLooper);

    // Listen for interactive state changes
    last_interactive = Util.isInteractive(this);
    IntentFilter ifInteractive = new IntentFilter();
    ifInteractive.addAction(Intent.ACTION_SCREEN_ON);
    ifInteractive.addAction(Intent.ACTION_SCREEN_OFF);
    ifInteractive.addAction(ACTION_SCREEN_OFF_DELAYED);
    registerReceiver(interactiveStateReceiver, ifInteractive);

    // Listen for power save mode
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && !Util.isPlayStoreInstall(this)) {
        PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
        powersaving = pm.isPowerSaveMode();
        IntentFilter ifPower = new IntentFilter();
        ifPower.addAction(PowerManager.ACTION_POWER_SAVE_MODE_CHANGED);
        registerReceiver(powerSaveReceiver, ifPower);
    }

    // Listen for user switches
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        IntentFilter ifUser = new IntentFilter();
        ifUser.addAction(Intent.ACTION_USER_BACKGROUND);
        ifUser.addAction(Intent.ACTION_USER_FOREGROUND);
        registerReceiver(userReceiver, ifUser);
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        // Listen for idle mode state changes
        IntentFilter ifIdle = new IntentFilter();
        ifIdle.addAction(PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED);
        registerReceiver(idleStateReceiver, ifIdle);
    }

    // Listen for connectivity updates
    IntentFilter ifConnectivity = new IntentFilter();
    ifConnectivity.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
    registerReceiver(connectivityChangedReceiver, ifConnectivity);

    // Listen for added applications
    IntentFilter ifPackage = new IntentFilter();
    ifPackage.addAction(Intent.ACTION_PACKAGE_ADDED);
    ifPackage.addDataScheme("package");
    registerReceiver(packageAddedReceiver, ifPackage);

    // Setup house holding
    Intent alarmIntent = new Intent(this, ServiceSinkhole.class);
    alarmIntent.setAction(ACTION_HOUSE_HOLDING);
    PendingIntent pi = PendingIntent.getService(this, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    am.setInexactRepeating(AlarmManager.RTC, SystemClock.elapsedRealtime() + 60 * 1000,
            AlarmManager.INTERVAL_HALF_DAY, pi);
}

From source file:com.ape.camera2raw.Camera2RawFragment.java

/**
 * Start the timer for the pre-capture sequence.
 * <p/>//w  w  w  .ja va 2 s. c om
 * Call this only with {@link #mCameraStateLock} held.
 */
private void startTimerLocked() {
    mCaptureTimer = SystemClock.elapsedRealtime();
}

From source file:com.ape.camera2raw.Camera2RawFragment.java

/**
 * Check if the timer for the pre-capture sequence has been hit.
 * <p/>/*from  w  w  w .j ava  2 s . c om*/
 * Call this only with {@link #mCameraStateLock} held.
 *
 * @return true if the timeout occurred.
 */
private boolean hitTimeoutLocked() {
    return (SystemClock.elapsedRealtime() - mCaptureTimer) > PRECAPTURE_TIMEOUT_MS;
}

From source file:com.anysoftkeyboard.keyboards.views.AnyKeyboardViewBase.java

@Override
public boolean onTouchEvent(@NonNull MotionEvent nativeMotionEvent) {
    if (mKeyboard == null)//I mean, if there isn't any keyboard I'm handling, what's the point?
        return false;

    final int action = MotionEventCompat.getActionMasked(nativeMotionEvent);
    final int pointerCount = MotionEventCompat.getPointerCount(nativeMotionEvent);
    if (pointerCount > 1)
        mLastTimeHadTwoFingers = SystemClock.elapsedRealtime();//marking the time. Read isAtTwoFingersState()

    if (mTouchesAreDisabledTillLastFingerIsUp) {
        if (!areTouchesDisabled(nativeMotionEvent)/*this means it was just reset*/) {
            mTouchesAreDisabledTillLastFingerIsUp = false;
            //continue with onTouchEvent flow.
            if (action != MotionEvent.ACTION_DOWN) {
                //swallowing the event.
                //in case this is a DOWN event, we do want to pass it
                return true;
            }//from w ww  .j  ava2 s.  c om
        } else {
            //swallowing touch event until we reset mTouchesAreDisabledTillLastFingerIsUp
            return true;
        }
    }

    final long eventTime = nativeMotionEvent.getEventTime();
    final int index = MotionEventCompat.getActionIndex(nativeMotionEvent);
    final int id = nativeMotionEvent.getPointerId(index);
    final int x = (int) nativeMotionEvent.getX(index);
    final int y = (int) nativeMotionEvent.getY(index);

    if (mKeyPressTimingHandler.isInKeyRepeat()) {
        // It will keep being in the key repeating mode while the key is
        // being pressed.
        if (action == MotionEvent.ACTION_MOVE) {
            return true;
        }
        final PointerTracker tracker = getPointerTracker(id);
        // Key repeating timer will be canceled if 2 or more keys are in
        // action, and current
        // event (UP or DOWN) is non-modifier key.
        if (pointerCount > 1 && !tracker.isModifier()) {
            mKeyPressTimingHandler.cancelKeyRepeatTimer();
        }
        // Up event will pass through.
    }

    if (action == MotionEvent.ACTION_MOVE) {
        for (int i = 0; i < pointerCount; i++) {
            PointerTracker tracker = getPointerTracker(nativeMotionEvent.getPointerId(i));
            tracker.onMoveEvent((int) nativeMotionEvent.getX(i), (int) nativeMotionEvent.getY(i));
        }
    } else {
        PointerTracker tracker = getPointerTracker(id);
        sendOnXEvent(action, eventTime, x, y, tracker);
    }

    return true;
}

From source file:com.lgallardo.qbittorrentclient.RefreshListener.java

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == SETTINGS_CODE) {

        //            Log.d("Debug", "Notification alarm set");

        alarmMgr = (AlarmManager) getApplication().getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(getApplication(), NotifierService.class);
        alarmIntent = PendingIntent.getBroadcast(getApplication(), 0, intent, 0);

        alarmMgr.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 5000,
                notification_period, alarmIntent);
    }/*w w w  .  j  av  a 2s  .  c o m*/

    if (requestCode == OPTION_CODE) {

        String json = "";

        // Get Options
        getOptions();

        /***************************************
         * Save qBittorrent's options remotely *
         ****************************************/

        // Maximum global number of simultaneous connections
        json += "\"max_connec\":" + global_max_num_connections;

        // Maximum number of simultaneous connections per torrent
        json += ",\"max_connec_per_torrent\":" + max_num_conn_per_torrent;

        // Global maximum number of upload slots:
        json += ",\"max_uploads\":" + max_uploads;

        // Maximum number of upload slots per torrent
        json += ",\"max_uploads_per_torrent\":" + max_num_upslots_per_torrent;

        // Global upload speed limit in KiB/s; -1 means no limit is applied
        json += ",\"up_limit\":" + global_upload;

        // Global download speed limit in KiB/s; -1 means no limit is
        // applied
        json += ",\"dl_limit\":" + global_download;

        // alternative global upload speed limit in KiB/s
        json += ",\"alt_up_limit\":" + alt_upload;

        // alternative global upload speed limit in KiB/s
        json += ",\"alt_dl_limit\":" + alt_download;

        // Is torrent queuing enabled ?
        json += ",\"queueing_enabled\":" + torrent_queueing;

        // Maximum number of active simultaneous downloads
        json += ",\"max_active_downloads\":" + max_act_downloads;

        // Maximum number of active simultaneous uploads
        json += ",\"max_active_uploads\":" + max_act_uploads;

        // Maximum number of active simultaneous downloads and uploads
        json += ",\"max_active_torrents\":" + max_act_torrents;

        // Schedule alternative rate limits
        json += ",\"scheduler_enabled\":" + schedule_alternative_rate_limits;

        // Scheduler starting hour
        json += ",\"schedule_from_hour\":" + alt_from_hour;

        // Scheduler starting min
        json += ",\"schedule_from_min\":" + alt_from_min;

        // Scheduler ending hour
        json += ",\"schedule_to_hour\":" + alt_to_hour;

        // Scheduler ending min
        json += ",\"schedule_to_min\":" + alt_to_min;

        // Scheduler scheduler days
        json += ",\"scheduler_days\":" + scheduler_days;

        //            Log.d("Debug", "max_ratio_enabled:" + max_ratio_enabled);

        // Share Ratio Limiting
        json += ",\"max_ratio_enabled\":" + max_ratio_enabled;

        if (max_ratio_enabled == false) {
            json += ",\"max_ratio\":-1";
        } else {
            json += ",\"max_ratio\":" + Float.parseFloat(max_ratio);
        }

        //            String max_ratio_string = "4) max_ratio: " + Float.parseFloat(max_ratio);
        //            Log.d("Debug", "3) max_ratio: " + Float.parseFloat(max_ratio));
        //            Log.d("Debug", max_ratio_string );

        json += ",\"max_ratio_act\":" + max_ratio_act;

        // Put everything in an json object
        json = "{" + json + "}";

        // Set preferences using this json object
        setQBittorrentPrefefrences(json);

        // Now it can be refreshed
        canrefresh = true;

    }

    if (requestCode == HELP_CODE && resultCode == RESULT_OK) {
        // Now it can be refreshed
        canrefresh = true;

        refreshSwipeLayout();
        refreshCurrent();
    }

    if (requestCode == SETTINGS_CODE && resultCode == RESULT_OK) {

        // Change current server (from settings or drawer menu)
        changeCurrentServer();

    }

    if (requestCode == ADDFILE_CODE && resultCode == RESULT_OK) {

        String file_path_value = "";

        // MaterialDesignPicker
        if (requestCode == ADDFILE_CODE && resultCode == RESULT_OK) {
            file_path_value = data.getStringExtra(FilePickerActivity.RESULT_FILE_PATH);
        }

        Log.d("Debug", "Torrent path: " + file_path_value);

        // Do something with the file path
        addTorrentFile(file_path_value);
    }

}

From source file:com.master.metehan.filtereagle.ServiceSinkhole.java

@Override
public void onCreate() {
    Log.i(TAG, "Create version=" + Util.getSelfVersionName(this) + "/" + Util.getSelfVersionCode(this));

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

    // Native init
    jni_init();//from  www  . j a  va 2s .c  o  m
    boolean pcap = prefs.getBoolean("pcap", false);
    setPcap(pcap, this);

    prefs.registerOnSharedPreferenceChangeListener(this);

    Util.setTheme(this);
    super.onCreate();

    HandlerThread commandThread = new HandlerThread(getString(R.string.app_name) + " command");
    HandlerThread logThread = new HandlerThread(getString(R.string.app_name) + " log");
    HandlerThread statsThread = new HandlerThread(getString(R.string.app_name) + " stats");
    commandThread.start();
    logThread.start();
    statsThread.start();

    commandLooper = commandThread.getLooper();
    logLooper = logThread.getLooper();
    statsLooper = statsThread.getLooper();

    commandHandler = new CommandHandler(commandLooper);
    logHandler = new LogHandler(logLooper);
    statsHandler = new StatsHandler(statsLooper);

    // Listen for interactive state changes
    last_interactive = Util.isInteractive(this);
    IntentFilter ifInteractive = new IntentFilter();
    ifInteractive.addAction(Intent.ACTION_SCREEN_ON);
    ifInteractive.addAction(Intent.ACTION_SCREEN_OFF);
    ifInteractive.addAction(ACTION_SCREEN_OFF_DELAYED);
    registerReceiver(interactiveStateReceiver, ifInteractive);
    registeredInteractiveState = true;

    // Listen for power save mode
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && !Util.isPlayStoreInstall(this)) {
        PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
        powersaving = pm.isPowerSaveMode();
        IntentFilter ifPower = new IntentFilter();
        ifPower.addAction(PowerManager.ACTION_POWER_SAVE_MODE_CHANGED);
        registerReceiver(powerSaveReceiver, ifPower);
        registeredPowerSave = true;
    }

    // Listen for user switches
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        IntentFilter ifUser = new IntentFilter();
        ifUser.addAction(Intent.ACTION_USER_BACKGROUND);
        ifUser.addAction(Intent.ACTION_USER_FOREGROUND);
        registerReceiver(userReceiver, ifUser);
        registeredUser = true;
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        // Listen for idle mode state changes
        IntentFilter ifIdle = new IntentFilter();
        ifIdle.addAction(PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED);
        registerReceiver(idleStateReceiver, ifIdle);
        registeredIdleState = true;
    }

    // Listen for connectivity updates
    IntentFilter ifConnectivity = new IntentFilter();
    ifConnectivity.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
    registerReceiver(connectivityChangedReceiver, ifConnectivity);
    registeredConnectivityChanged = true;

    // Listen for added applications
    IntentFilter ifPackage = new IntentFilter();
    ifPackage.addAction(Intent.ACTION_PACKAGE_ADDED);
    ifPackage.addDataScheme("package");
    registerReceiver(packageAddedReceiver, ifPackage);
    registeredPackageAdded = true;

    // Setup house holding
    Intent alarmIntent = new Intent(this, ServiceSinkhole.class);
    alarmIntent.setAction(ACTION_HOUSE_HOLDING);
    PendingIntent pi = PendingIntent.getService(this, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    am.setInexactRepeating(AlarmManager.RTC, SystemClock.elapsedRealtime() + 60 * 1000,
            AlarmManager.INTERVAL_HALF_DAY, pi);

    // Setup broadcast to send url map to server. Set inexact repeating for battery saving.
    Intent urlIntent = new Intent(this, URLBroadcastReceiver.class);
    PendingIntent urlPendingIntent = PendingIntent.getBroadcast(this, 0, urlIntent, 0);
    long frequency = 120 * 1000; // two minute frequency for testing
    //long frequency = AlarmManager.INTERVAL_DAY;
    am.setInexactRepeating(AlarmManager.RTC, SystemClock.elapsedRealtime() + 60 * 1000, frequency,
            urlPendingIntent);
}

From source file:knayi.delevadriver.JobDetailActivity.java

private void startAlarmManager() {
    Log.d("TAG", "startAlarmManager");

    Context context = getBaseContext();
    alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    gpsTrackerIntent = new Intent(context, GpsTrackerAlarmReceiver.class);
    pendingIntent = PendingIntent.getBroadcast(context, 0, gpsTrackerIntent, 0);

    alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(),
            intervalInMinutes * 10000, // 60000 = 1 minute
            pendingIntent);/*  w  w w  .ja  v a 2 s.  c om*/

    //startService(new Intent(JobDetailActivity.this, GPSLocationService.class));

}

From source file:mp.teardrop.PlaybackService.java

@Override
public void onSensorChanged(SensorEvent se) {
    double x = se.values[0];
    double y = se.values[1];
    double z = se.values[2];

    double accel = Math.sqrt(x * x + y * y + z * z);
    double delta = accel - mAccelLast;
    mAccelLast = accel;//  w  ww.  j av a2s  .co  m

    double filtered = mAccelFiltered * 0.9f + delta;
    mAccelFiltered = filtered;

    if (filtered > mShakeThreshold) {
        long now = SystemClock.elapsedRealtime();
        if (now - mLastShakeTime > MIN_SHAKE_PERIOD) {
            mLastShakeTime = now;
            performAction(mShakeAction, null);
        }
    }
}

From source file:com.zhengde163.netguard.ServiceSinkhole.java

@Override
public void onCreate() {
    Log.i(TAG, "Create version=" + Util.getSelfVersionName(this) + "/" + Util.getSelfVersionCode(this));

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

    // Native init
    jni_init();/*from w  ww.jav a 2s .  c  o  m*/
    boolean pcap = prefs.getBoolean("pcap", false);
    setPcap(pcap, this);

    prefs.registerOnSharedPreferenceChangeListener(this);

    //        Util.setTheme(this);
    setTheme(R.style.AppThemeBlue);
    super.onCreate();

    HandlerThread commandThread = new HandlerThread(getString(R.string.app_name) + " command");
    HandlerThread logThread = new HandlerThread(getString(R.string.app_name) + " log");
    HandlerThread statsThread = new HandlerThread(getString(R.string.app_name) + " stats");
    commandThread.start();
    logThread.start();
    statsThread.start();

    commandLooper = commandThread.getLooper();
    logLooper = logThread.getLooper();
    statsLooper = statsThread.getLooper();

    commandHandler = new CommandHandler(commandLooper);
    logHandler = new LogHandler(logLooper);
    statsHandler = new StatsHandler(statsLooper);

    // Listen for interactive state changes
    last_interactive = Util.isInteractive(this);
    IntentFilter ifInteractive = new IntentFilter();
    ifInteractive.addAction(Intent.ACTION_SCREEN_ON);
    ifInteractive.addAction(Intent.ACTION_SCREEN_OFF);
    ifInteractive.addAction(ACTION_SCREEN_OFF_DELAYED);
    registerReceiver(interactiveStateReceiver, ifInteractive);
    registeredInteractiveState = true;

    // Listen for power save mode
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && !Util.isPlayStoreInstall(this)) {
        PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
        powersaving = pm.isPowerSaveMode();
        IntentFilter ifPower = new IntentFilter();
        ifPower.addAction(PowerManager.ACTION_POWER_SAVE_MODE_CHANGED);
        registerReceiver(powerSaveReceiver, ifPower);
        registeredPowerSave = true;
    }

    // Listen for user switches
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        IntentFilter ifUser = new IntentFilter();
        ifUser.addAction(Intent.ACTION_USER_BACKGROUND);
        ifUser.addAction(Intent.ACTION_USER_FOREGROUND);
        registerReceiver(userReceiver, ifUser);
        registeredUser = true;
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        // Listen for idle mode state changes
        IntentFilter ifIdle = new IntentFilter();
        ifIdle.addAction(PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED);
        registerReceiver(idleStateReceiver, ifIdle);
        registeredIdleState = true;
    }

    // Listen for connectivity updates
    IntentFilter ifConnectivity = new IntentFilter();
    ifConnectivity.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
    registerReceiver(connectivityChangedReceiver, ifConnectivity);
    registeredConnectivityChanged = true;

    // Listen for added applications
    IntentFilter ifPackage = new IntentFilter();
    ifPackage.addAction(Intent.ACTION_PACKAGE_ADDED);
    ifPackage.addDataScheme("package");
    registerReceiver(packageAddedReceiver, ifPackage);
    registeredPackageAdded = true;

    // Setup house holding
    Intent alarmIntent = new Intent(this, ServiceSinkhole.class);
    alarmIntent.setAction(ACTION_HOUSE_HOLDING);
    PendingIntent pi = PendingIntent.getService(this, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    am.setInexactRepeating(AlarmManager.RTC, SystemClock.elapsedRealtime() + 60 * 1000,
            AlarmManager.INTERVAL_HALF_DAY, pi);
}