Java Code Examples for android.os.Looper#getThread()

The following examples show how to use android.os.Looper#getThread() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestUtilities.java    From google-authenticator-android with Apache License 2.0 6 votes vote down vote up
/**
 * Invokes the provided {@link Callable} on the main thread and blocks until the operation
 * completes or times out. If this method is invoked on the main thread, the {@code Callable} is
 * invoked immediately and no timeout is enforced.
 *
 * <p>Exceptions thrown by the {@code Callable} are rethrown by this method.
 *
 * @return result returned by the {@code Callable}.
 */
private static <V> V runOnMainSyncWithTimeoutAndWithCheckedExceptionsExpected(
    Callable<V> callable) throws Exception {
  Looper mainLooper = Looper.getMainLooper();
  if (mainLooper.getThread() == Thread.currentThread()) {
    // This method is being invoked on the main thread -- invoke the Callable inline to avoid
    // a deadlock.
    return callable.call();
  } else {
    FutureTask<V> task = new FutureTask<V>(callable);
    new Handler(mainLooper).post(task);
    try {
      return task.get(UI_ACTION_EFFECT_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
    } catch (ExecutionException e) {
      Throwable cause = e.getCause();
      if (cause instanceof Exception) {
        throw (Exception) cause;
      } else {
        throw new RuntimeException("Execution on main thread failed", e);
      }
    }
  }
}
 
Example 2
Source File: ByteBufferList.java    From MediaSDK with Apache License 2.0 5 votes vote down vote up
private static PriorityQueue<ByteBuffer> getReclaimed() {
    Looper mainLooper = Looper.getMainLooper();
    if (mainLooper != null) {
        if (Thread.currentThread() == mainLooper.getThread())
            return null;
    }
    return reclaimed;
}
 
Example 3
Source File: ByteBufferReader.java    From AsyncSocket with MIT License 5 votes vote down vote up
private static PriorityQueue<ByteBuffer> getReclaimed() {
    Looper mainLooper = Looper.getMainLooper();
    if (mainLooper != null) {
        if (Thread.currentThread() == mainLooper.getThread())
            return null;
    }
    return reclaimed;
}
 
Example 4
Source File: HandlerThreadHandler.java    From libcommon with Apache License 2.0 5 votes vote down vote up
/**
 * コンストラクタ
 * @param looper
 */
private HandlerThreadHandler(@NonNull final Looper looper, final boolean async) {
	super(looper);
	final Thread thread = looper.getThread();
	mId = thread != null ? thread.getId() : 0;
	mAsynchronous = async;
}
 
Example 5
Source File: HandlerThreadHandler.java    From libcommon with Apache License 2.0 5 votes vote down vote up
/**
 * コンストラクタ
 * @param looper
 * @param callback
 */
private HandlerThreadHandler(@NonNull final Looper looper,
	@Nullable final Callback callback, final boolean async) {

	super(looper, callback);
	final Thread thread = looper.getThread();
	mId = thread != null ? thread.getId() : 0;
	mAsynchronous = async;
}