Example usage for android.os SystemClock sleep

List of usage examples for android.os SystemClock sleep

Introduction

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

Prototype

public static void sleep(long ms) 

Source Link

Document

Waits a given number of milliseconds (of uptimeMillis) before returning.

Usage

From source file:com.louding.frame.http.download.RetryHandler.java

@Override
public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
    // ??//from w  w w  .  j a  va2 s.  c  om
    boolean retry = true;

    // ?
    Boolean b = (Boolean) context.getAttribute(ExecutionContext.HTTP_REQ_SENT);
    boolean sent = (b != null && b.booleanValue());

    if (executionCount > maxRetries) {
        // ????
        retry = false;
    } else if (isInList(exceptionBlacklist, exception)) {
        // ??????
        retry = false;
    } else if (isInList(exceptionWhitelist, exception)) {
        // ????
        retry = true;
    } else if (!sent) {
        // ?????
        retry = true;
    }

    if (retry) {
        // resend all idempotent requests
        HttpUriRequest currentReq = (HttpUriRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);
        String requestType = currentReq.getMethod();
        retry = !requestType.equals("POST");
    }

    if (retry) {
        SystemClock.sleep(RETRY_SLEEP_TIME_MILLIS);
    } else {
        exception.printStackTrace();
    }
    return retry;
}

From source file:com.ab.http.RetryHandler.java

/**
 * ??TODO//from ww  w  .  j a v a2s .  c  o  m
 * @see org.apache.http.client.HttpRequestRetryHandler#retryRequest(java.io.IOException, int, org.apache.http.protocol.HttpContext)
 * @author: zhaoqp
 * @date2013-10-22 ?4:23:15
 * @version v1.0
 */
@Override
public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
    boolean retry = true;

    Boolean b = (Boolean) context.getAttribute(ExecutionContext.HTTP_REQ_SENT);
    boolean sent = (b != null && b);

    if (executionCount > maxRetries) {
        // Do not retry if over max retry count
        retry = false;
    } else if (isInList(exceptionBlacklist, exception)) {
        // immediately cancel retry if the error is blacklisted
        retry = false;
    } else if (isInList(exceptionWhitelist, exception)) {
        // immediately retry if error is whitelisted
        retry = true;
    } else if (!sent) {
        // for most other errors, retry only if request hasn't been fully sent yet
        retry = true;
    }

    if (retry) {
        // resend all idempotent requests
        HttpUriRequest currentReq = (HttpUriRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);
        if (currentReq == null) {
            return false;
        }
        String requestType = currentReq.getMethod();
        retry = !requestType.equals("POST");
    }

    if (retry) {
        SystemClock.sleep(retrySleepTimeMS);
    } else {
        exception.printStackTrace();
    }

    return retry;
}

From source file:me.zhang.accountbook.ui.LicenseFragment.java

private void initPtr(View rootView) {
    mCanDoRefresh = true;/*from w w w.jav  a2  s. co m*/
    mPtrFrame = (PtrFrameLayout) rootView.findViewById(R.id.ptr_frame);

    StoreHouseHeader header = new StoreHouseHeader(getActivity());
    header.setTextColor(getResources().getColor(R.color.myPrimaryColor));
    header.setPadding(0, LocalDisplay.dp2px(20), 0, LocalDisplay.dp2px(20));
    header.initWithString("Loading...");

    mPtrFrame.setDurationToCloseHeader(1500);
    mPtrFrame.setHeaderView(header);
    mPtrFrame.addPtrUIHandler(header);

    mPtrFrame.setPtrHandler(new PtrDefaultHandler() {
        @Override
        public void onRefreshBegin(PtrFrameLayout frame) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    SystemClock.sleep(2 * 1000);
                    mPtrFrame.refreshComplete();
                    mHandler.sendEmptyMessage(0x10);
                }
            }).start();
        }

        @Override
        public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
            return PtrDefaultHandler.checkContentCanBePulledDown(frame, content, header) && mCanDoRefresh;
        }
    });
}

From source file:org.cook_e.cook_e.ui.HomeActivityTest.java

/**
 * Tests swiping to change tabs/*from   w w  w .j  a va  2s .  co m*/
 */
public void testTabSwiping() {
    final Matcher<View> mealsTab = withText(R.string.meals);
    final Matcher<View> recipesTab = withText(R.string.recipes);
    final Matcher<View> mealList = withTagKey(R.id.test_tag_meal_list, Is.<Object>is("Meal List"));
    final Matcher<View> recipeList = withTagKey(R.id.test_tag_recipe_list, Is.<Object>is("Recipe List"));
    final Matcher<View> pager = withClassName(IsEqual.equalTo(ViewPager.class.getName()));

    onView(pager).perform(swipeLeft());
    SystemClock.sleep(SWITCH_DELAY_MS);
    onView(recipesTab).check(matches(isSelected()));
    onView(pager).perform(swipeRight());
    SystemClock.sleep(SWITCH_DELAY_MS);
    onView(mealsTab).check(matches(isSelected()));
}

From source file:com.lurencun.cfuture09.androidkit.http.async.RetryHandler.java

@Override
public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
    // ??// w  w  w  .  j  ava 2s  .  c o m
    boolean retry = true;

    // ?
    Boolean b = (Boolean) context.getAttribute(ExecutionContext.HTTP_REQ_SENT);
    boolean sent = (b != null && b.booleanValue());

    if (executionCount > maxRetries) {
        // ????
        retry = false;
    } else if (isInList(exceptionBlacklist, exception)) {
        // ??????
        retry = false;
    } else if (isInList(exceptionWhitelist, exception)) {
        // ????
        retry = true;
    } else if (!sent) {
        // ?????
        retry = true;
    }

    if (retry) {
        // resend all idempotent requests
        HttpUriRequest currentReq = (HttpUriRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);
        String requestType = currentReq.getMethod();
        retry = !requestType.equals("POST");
    }

    if (retry) {
        SystemClock.sleep(RETRY_SLEEP_TIME_MILLIS);
    } else {
        exception.printStackTrace();
        log.w(exception);
    }
    return retry;
}

From source file:org.robam.xutils.http.client.RetryHandler.java

/**
 * @param exception/*from www. j a  va 2 s  .c o m*/
 * @param retriedTimes
 * @param context      HTTP,??,ActivityContext
 * @return
 */
@Override
public boolean retryRequest(IOException exception, int retriedTimes, HttpContext context) {
    boolean retry = true;

    if (exception == null || context == null) {
        return false;
    }

    // ?????,?,True
    Object isReqSent = context.getAttribute(ExecutionContext.HTTP_REQ_SENT);
    boolean sent = isReqSent == null ? false : (Boolean) isReqSent;

    // ???
    if (retriedTimes > maxRetries) {
        // ??,
        retry = false;
    } else if (exceptionBlackList.contains(exception.getClass())) {
        // ?BlackList???.
        retry = false;
    } else if (exceptionWhiteList.contains(exception.getClass())) {
        // ?????.
        retry = true;
    } else if (!sent) {
        // ??
        retry = true;
    }

    if (retry) {
        try {
            Object currRequest = context.getAttribute(ExecutionContext.HTTP_REQUEST);
            if (currRequest != null) {
                if (currRequest instanceof HttpRequestBase) {
                    // ???GET?retry,?GET,??
                    HttpRequestBase requestBase = (HttpRequestBase) currRequest;
                    retry = "GET".equals(requestBase.getMethod());
                } else if (currRequest instanceof RequestWrapper) {
                    RequestWrapper requestWrapper = (RequestWrapper) currRequest;
                    retry = "GET".equals(requestWrapper.getMethod());
                }
            } else {
                retry = false;
                LogUtils.e("retry error, curr request is null");
            }
        } catch (Throwable e) {
            retry = false;
            LogUtils.e("retry error", e);
        }
    }

    if (retry) {
        // sleep a while and retry http request again.
        SystemClock.sleep(RETRY_SLEEP_INTERVAL);
    }

    return retry;
}

From source file:com.android.cts.uiautomator.Test3DetailFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedState) {
    View rootView = inflater.inflate(R.layout.test3_detail_fragment, container, false);
    mTextClock = (TextView) rootView.findViewById(R.id.test3ClockTextView);
    mSubmitButton = (Button) rootView.findViewById(R.id.test3SubmitButton);
    mEditText = (EditText) rootView.findViewById(R.id.test3TextField);
    mSubmitButton.setOnClickListener(new Button.OnClickListener() {
        @Override//from  w w  w .  j  av a 2  s  . c o  m
        public void onClick(View v) {
            // close soft keyboard
            InputMethodManager imm = (InputMethodManager) getActivity()
                    .getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0);

            // display the submitted text
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            builder.setTitle(R.string.test3_dialog_title);
            builder.setPositiveButton(R.string.OK, null);
            CharSequence inputText = mEditText.getText();
            if (inputText != null && !inputText.toString().isEmpty()) {
                long inputTime = Long.parseLong(inputText.toString());
                builder.setMessage("" + (mCurrentTime - inputTime));
            } else {
                builder.setMessage("<NO DATA>");
            }
            AlertDialog diag = builder.create();
            diag.show();
            mEditText.setText("");
            mRunCounter = false;
        }
    });

    new Thread(new Runnable() {
        @Override
        public void run() {
            while (mRunCounter) {
                synchronized (sync) {
                    mCurrentTime = SystemClock.elapsedRealtime();
                }
                mHandler.post(mClockRunnable);
                SystemClock.sleep(100);
            }
        }
    }).start();

    return rootView;
}

From source file:com.frostwire.android.gui.httpserver.DesktopUploadRequestHandler.java

private boolean waitForAccept(String token) {
    int count = MAX_SECONDS_WAIT_AUTHORIZATION;

    while (count >= 0 && sessionManager.getDURStatus(token) != DesktopUploadRequestStatus.ACCEPTED) {
        SystemClock.sleep(1000);
        count--;/*from  w  ww  .  j av  a  2 s . c o  m*/

        if (sessionManager.getDURStatus(token) == DesktopUploadRequestStatus.REJECTED) {
            break;
        }

        sessionManager.refreshDUR(token);
    }

    return sessionManager.getDURStatus(token) == DesktopUploadRequestStatus.ACCEPTED;
}

From source file:org.pixmob.fm2.ui.AccountsFragment.java

@Override
public void onLoadFinished(Loader<List<Account>> loader, final List<Account> data) {
    if (DEBUG) {/*from   www .  j a  va2  s  . co m*/
        Log.d(TAG, "AccountsFragment.onLoadFinished");
    }

    getActivity().runOnUiThread(new Runnable() {
        @Override
        public void run() {
            // The list should now be shown.
            if (isResumed()) {
                setListShown(true);
            } else {
                setListShownNoAnimation(true);
            }

            // Set the new data in the adapter.
            accountAdapter.setData(data);

            if (dualPane) {
                getListView().setItemChecked(selectedAccountIndex, false);
            }
        }
    });

    if (dualPane) {
        // FIXME Remove this hack
        new Thread() {
            @Override
            public void run() {
                SystemClock.sleep(1000);

                final AccountDetailsFragment details = (AccountDetailsFragment) getSupportFragmentManager()
                        .findFragmentById(R.id.account_details);
                if (details != null) {
                    final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                    ft.remove(details);
                    ft.commit();
                }
            }
        }.start();
    }
}

From source file:io.realm.examples.threads.ThreadFragment.java

@Override
public void onResume() {
    super.onResume();

    // Enable UI refresh while the fragment is active.
    realm.addChangeListener(realmListener);

    // Create background thread that add a new dot every 0.5 second.
    backgroundThread = new Thread() {

        @Override/*from w  w w  .j  ava  2s  .c o m*/
        public void run() {
            // Realm instances cannot be shared between threads, so we need to create a new
            // instance on the background thread.
            int redColor = getResources().getColor(R.color.realm_red);
            Realm backgroundThreadRealm = Realm.getInstance(getActivity());
            while (!backgroundThread.isInterrupted()) {
                backgroundThreadRealm.beginTransaction();

                // Add red dot from the background thread
                Dot dot = backgroundThreadRealm.createObject(Dot.class);
                dot.setX(random.nextInt(100));
                dot.setY(random.nextInt(100));
                dot.setColor(redColor);
                backgroundThreadRealm.commitTransaction();

                // Wait 0.5 sec. before adding the next dot.
                SystemClock.sleep(500);
            }

            // Also close Realm instances used in background threads.
            backgroundThreadRealm.close();
        }
    };
    backgroundThread.start();
}