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

Commit b2d09de1 authored by Yohei Yukawa's avatar Yohei Yukawa
Browse files

Use AndroidFuture instead

This CL replaces our inhouse utility classes in favor of an existing
alternative AndroidFuture, which allows us to remove 8 files in total.

Several downsides are:

  * We lose compile-time type checking in the remote end, because AIDL
    does not support generics.
  * AndroidFuture<T> does not support any raw Binder interfaces.  For
    instance, AndroidFuture<IInputContentUriToken> would crash due to
    java.lang.ClassCastException at run time. You must use IBinder
    instead and manually create a stub object in the receiver side.
  * Data transfer would be a bit slower for primitive types and much
    more slower for Parcelable types, because AndroidFuture internally
    uses Parcel.{read,write}Value(), which is known to be around 4x
    slower than Parcel.{read,write}TypedObject() for Parcelable
    objects [1][2].
  * When you find an AndroidFuture<T> on a code, you have to figure
    out whether the code is running on the source side or remote side.
    If you are on the source side, what you see on that object is an
    aggregation of the computations on both the source and remote
    ends, which is probably easy to understand.  If you are on the
    remote side, however, you will never observe any computation
    result in the source side.  What you can observe in the remote
    side remains to be your local data that is specific to your remote
    process.

Other than above, there should be no observable semantic change in
this CL.

  [1]: https://developer.android.com/reference/android/os/Parcel
  [2]: I29548ce6c2e5886f0e90a5dc70d8e9ecc0fb25a8
       b293d933

Bug: 192412909
Bug: 195699814
Test: presubmit
Change-Id: I74657826a99b11ca1f86932f8f41cca6e449cc8a
parent ba8cdf5d
Loading
Loading
Loading
Loading
+0 −140
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.internal.inputmethod;

import android.annotation.NonNull;
import android.os.RemoteException;

import java.util.function.BooleanSupplier;
import java.util.function.IntSupplier;
import java.util.function.Supplier;

/**
 * Defines a set of helper methods to callback corresponding results in {@link ResultCallbacks}.
 */
public final class CallbackUtils {

    /**
     * Not intended to be instantiated.
     */
    private CallbackUtils() {
    }

    /**
     * A utility method using given {@link IBooleanResultCallback} to callback the result.
     *
     * @param callback {@link IBooleanResultCallback} to be called back.
     * @param resultSupplier the supplier from which the result is provided.
     */
    public static void onResult(@NonNull IBooleanResultCallback callback,
            @NonNull BooleanSupplier resultSupplier) {
        boolean result = false;
        Throwable exception = null;

        try {
            result = resultSupplier.getAsBoolean();
        } catch (Throwable throwable) {
            exception = throwable;
        }

        try {
            if (exception != null) {
                callback.onError(ThrowableHolder.of(exception));
                return;
            }
            callback.onResult(result);
        } catch (RemoteException ignored) { }
    }

    /**
     * A utility method using given {@link IIntResultCallback} to callback the result.
     *
     * @param callback {@link IIntResultCallback} to be called back.
     * @param resultSupplier the supplier from which the result is provided.
     */
    public static void onResult(@NonNull IIntResultCallback callback,
            @NonNull IntSupplier resultSupplier) {
        int result = 0;
        Throwable exception = null;

        try {
            result = resultSupplier.getAsInt();
        } catch (Throwable throwable) {
            exception = throwable;
        }

        try {
            if (exception != null) {
                callback.onError(ThrowableHolder.of(exception));
                return;
            }
            callback.onResult(result);
        } catch (RemoteException ignored) { }
    }

    /**
     * A utility method using given {@link IVoidResultCallback} to callback the result.
     *
     * @param callback {@link IVoidResultCallback} to be called back.
     * @param runnable to execute the given method
     */
    public static void onResult(@NonNull IVoidResultCallback callback,
            @NonNull Runnable runnable) {
        Throwable exception = null;

        try {
            runnable.run();
        } catch (Throwable throwable) {
            exception = throwable;
        }

        try {
            if (exception != null) {
                callback.onError(ThrowableHolder.of(exception));
                return;
            }
            callback.onResult();
        } catch (RemoteException ignored) { }
    }

    /**
     * A utility method using given {@link IInputContentUriTokenResultCallback} to callback the
     * result.
     *
     * @param callback {@link IInputContentUriTokenResultCallback} to be called back.
     * @param resultSupplier the supplier from which the result is provided.
     */
    public static void onResult(@NonNull IInputContentUriTokenResultCallback callback,
            @NonNull Supplier<IInputContentUriToken> resultSupplier) {
        IInputContentUriToken result = null;
        Throwable exception = null;

        try {
            result = resultSupplier.get();
        } catch (Throwable throwable) {
            exception = throwable;
        }

        try {
            if (exception != null) {
                callback.onError(ThrowableHolder.of(exception));
                return;
            }
            callback.onResult(result);
        } catch (RemoteException ignored) { }
    }
}
+0 −24
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.internal.inputmethod;

import com.android.internal.inputmethod.ThrowableHolder;

oneway interface IBooleanResultCallback {
    void onResult(boolean result);
    void onError(in ThrowableHolder exception);
}
 No newline at end of file
+0 −21
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.internal.inputmethod;

oneway interface ICharSequenceResultCallback {
    void onResult(in CharSequence result);
}
+0 −23
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.internal.inputmethod;

import android.view.inputmethod.ExtractedText;

oneway interface IExtractedTextResultCallback {
    void onResult(in ExtractedText result);
}
+0 −25
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.internal.inputmethod;

import com.android.internal.inputmethod.IInputContentUriToken;
import com.android.internal.inputmethod.ThrowableHolder;

oneway interface IInputContentUriTokenResultCallback {
    void onResult(in IInputContentUriToken result);
    void onError(in ThrowableHolder exception);
}
 No newline at end of file
Loading