Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 151c83a0 authored by Wilson Wu's avatar Wilson Wu Committed by Android (Google) Code Review
Browse files

Merge "Make IInputMethodManager to oneway (2/N)"

parents dfb43e60 16b795f3
Loading
Loading
Loading
Loading
+50 −0
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package com.android.internal.inputmethod;
import android.annotation.AnyThread;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.util.Log;

import com.android.internal.annotations.GuardedBy;

@@ -275,4 +276,53 @@ public final class Completable {
     */
    public static final class InputBindResult
            extends Values<com.android.internal.view.InputBindResult> { }

    /**
     * Await the result by the {@link Completable.Int}, and log it if there is no result after
     * given timeout.
     *
     * @return the result once {@link ValueBase#onComplete()}
     */
    @AnyThread
    public static int getResultOrZero(@NonNull Completable.Int value, String tag,
            @NonNull String methodName, @Nullable CancellationGroup cancellationGroup,
            int maxWaitTime) {
        final boolean timedOut = value.await(maxWaitTime, TimeUnit.MILLISECONDS, cancellationGroup);
        if (value.hasValue()) {
            return value.getValue();
        }
        logInternal(tag, methodName, timedOut, maxWaitTime, 0);
        return 0;
    }

    /**
     * Await the result by the {@link Completable.Values}, and log it if there is no result after
     * given timeout.
     *
     * @return the result once {@link ValueBase#onComplete()}
     */
    @AnyThread
    @Nullable
    public static <T> T getResultOrNull(@NonNull Completable.Values<T> value, String tag,
            @NonNull String methodName, @Nullable CancellationGroup cancellationGroup,
            int maxWaitTime) {
        final boolean timedOut = value.await(maxWaitTime, TimeUnit.MILLISECONDS, cancellationGroup);
        if (value.hasValue()) {
            return value.getValue();
        }
        logInternal(tag, methodName, timedOut, maxWaitTime, null);
        return null;
    }

    @AnyThread
    private static void logInternal(String tag, @Nullable String methodName, boolean timedOut,
            int maxWaitTime, @Nullable Object defaultValue) {
        if (timedOut) {
            Log.w(tag, methodName + " didn't respond in " + maxWaitTime + " msec."
                    + " Returning default: " + defaultValue);
        } else {
            Log.w(tag, methodName + " was canceled before complete. Returning default: "
                    + defaultValue);
        }
    }
}
+16 −47
Original line number Diff line number Diff line
@@ -24,7 +24,6 @@ import android.inputmethodservice.AbstractInputMethodService;
import android.os.Bundle;
import android.os.Handler;
import android.os.RemoteException;
import android.util.Log;
import android.view.KeyEvent;
import android.view.inputmethod.CompletionInfo;
import android.view.inputmethod.CorrectionInfo;
@@ -41,7 +40,6 @@ import com.android.internal.inputmethod.Completable;
import com.android.internal.inputmethod.ResultCallbacks;

import java.lang.ref.WeakReference;
import java.util.concurrent.TimeUnit;

public class InputConnectionWrapper implements InputConnection {
    private static final String TAG = "InputConnectionWrapper";
@@ -73,43 +71,6 @@ public class InputConnectionWrapper implements InputConnection {
        mCancellationGroup = cancellationGroup;
    }

    @AnyThread
    private static void logInternal(@Nullable String methodName, boolean timedOut,
            @Nullable Object defaultValue) {
        if (timedOut) {
            Log.w(TAG, methodName + " didn't respond in " + MAX_WAIT_TIME_MILLIS + " msec."
                    + " Returning default: " + defaultValue);
        } else {
            Log.w(TAG, methodName + " was canceled before complete. Returning default: "
                    + defaultValue);
        }
    }

    @AnyThread
    private static int getResultOrZero(@NonNull Completable.Int value, @NonNull String methodName,
            @Nullable CancellationGroup cancellationGroup) {
        final boolean timedOut =
                value.await(MAX_WAIT_TIME_MILLIS,  TimeUnit.MILLISECONDS, cancellationGroup);
        if (value.hasValue()) {
            return value.getValue();
        }
        logInternal(methodName, timedOut, 0);
        return 0;
    }

    @AnyThread
    @Nullable
    private static <T> T getResultOrNull(@NonNull Completable.Values<T> value,
            @NonNull String methodName, @Nullable CancellationGroup cancellationGroup) {
        final boolean timedOut =
                value.await(MAX_WAIT_TIME_MILLIS,  TimeUnit.MILLISECONDS, cancellationGroup);
        if (value.hasValue()) {
            return value.getValue();
        }
        logInternal(methodName, timedOut, null);
        return null;
    }

    /**
     * See {@link InputConnection#getTextAfterCursor(int, int)}.
     */
@@ -126,7 +87,8 @@ public class InputConnectionWrapper implements InputConnection {
        } catch (RemoteException e) {
            return null;
        }
        return getResultOrNull(value, "getTextAfterCursor()", mCancellationGroup);
        return Completable.getResultOrNull(
                value, TAG, "getTextAfterCursor()", mCancellationGroup, MAX_WAIT_TIME_MILLIS);
    }

    /**
@@ -145,7 +107,8 @@ public class InputConnectionWrapper implements InputConnection {
        } catch (RemoteException e) {
            return null;
        }
        return getResultOrNull(value, "getTextBeforeCursor()", mCancellationGroup);
        return Completable.getResultOrNull(
                value, TAG, "getTextBeforeCursor()", mCancellationGroup, MAX_WAIT_TIME_MILLIS);
    }

    @AnyThread
@@ -164,7 +127,8 @@ public class InputConnectionWrapper implements InputConnection {
        } catch (RemoteException e) {
            return null;
        }
        return getResultOrNull(value, "getSelectedText()", mCancellationGroup);
        return Completable.getResultOrNull(
                value, TAG, "getSelectedText()", mCancellationGroup, MAX_WAIT_TIME_MILLIS);
    }

    /**
@@ -197,7 +161,8 @@ public class InputConnectionWrapper implements InputConnection {
        } catch (RemoteException e) {
            return null;
        }
        return getResultOrNull(value, "getSurroundingText()", mCancellationGroup);
        return Completable.getResultOrNull(
                value, TAG, "getSurroundingText()", mCancellationGroup, MAX_WAIT_TIME_MILLIS);
    }

    @AnyThread
@@ -212,7 +177,8 @@ public class InputConnectionWrapper implements InputConnection {
        } catch (RemoteException e) {
            return 0;
        }
        return getResultOrZero(value, "getCursorCapsMode()", mCancellationGroup);
        return Completable.getResultOrZero(
                value, TAG, "getCursorCapsMode()", mCancellationGroup, MAX_WAIT_TIME_MILLIS);
    }

    @AnyThread
@@ -227,7 +193,8 @@ public class InputConnectionWrapper implements InputConnection {
        } catch (RemoteException e) {
            return null;
        }
        return getResultOrNull(value, "getExtractedText()", mCancellationGroup);
        return Completable.getResultOrNull(
                value, TAG, "getExtractedText()", mCancellationGroup, MAX_WAIT_TIME_MILLIS);
    }

    @AnyThread
@@ -438,7 +405,8 @@ public class InputConnectionWrapper implements InputConnection {
        } catch (RemoteException e) {
            return false;
        }
        return getResultOrZero(value, "requestUpdateCursorAnchorInfo()", mCancellationGroup) != 0;
        return Completable.getResultOrZero(value, TAG, "requestUpdateCursorAnchorInfo()",
                mCancellationGroup, MAX_WAIT_TIME_MILLIS) != 0;
    }

    @AnyThread
@@ -478,7 +446,8 @@ public class InputConnectionWrapper implements InputConnection {
        } catch (RemoteException e) {
            return false;
        }
        return getResultOrZero(value, "commitContent()", mCancellationGroup) != 0;
        return Completable.getResultOrZero(
                value, TAG, "commitContent()", mCancellationGroup, MAX_WAIT_TIME_MILLIS) != 0;
    }

    @AnyThread