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

Commit 004e73c3 authored by Emilian Peev's avatar Emilian Peev
Browse files

Camera: SessionConfiguration should use Executors

Handlers from clients should not be used any more.
Executors are the preferred method for invoking any
registered callbacks. Replace handlers as much as
possible with executors.

Bug: 73953366
Test: Camera CTS
Change-Id: I96aee1bc46e83dfb76a4c40c7f8ebbe18610788b
parent 9bc56108
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -16425,8 +16425,8 @@ package android.hardware.camera2.params {
  }
  public final class SessionConfiguration {
    ctor public SessionConfiguration(int, java.util.List<android.hardware.camera2.params.OutputConfiguration>, android.hardware.camera2.CameraCaptureSession.StateCallback, android.os.Handler);
    method public android.os.Handler getHandler();
    ctor public SessionConfiguration(int, java.util.List<android.hardware.camera2.params.OutputConfiguration>, java.util.concurrent.Executor, android.hardware.camera2.CameraCaptureSession.StateCallback);
    method public java.util.concurrent.Executor getExecutor();
    method public android.hardware.camera2.params.InputConfiguration getInputConfiguration();
    method public java.util.List<android.hardware.camera2.params.OutputConfiguration> getOutputConfigurations();
    method public android.hardware.camera2.CaptureRequest getSessionParameters();
+2 −1
Original line number Diff line number Diff line
@@ -819,7 +819,8 @@ public abstract class CameraDevice implements AutoCloseable {
     * @param config A session configuration (see {@link SessionConfiguration}).
     *
     * @throws IllegalArgumentException In case the session configuration is invalid; or the output
     *                                  configurations are empty.
     *                                  configurations are empty; or the session configuration
     *                                  executor is invalid.
     * @throws CameraAccessException In case the camera device is no longer connected or has
     *                               encountered a fatal error.
     * @see #createCaptureSession(List, CameraCaptureSession.StateCallback, Handler)
+0 −85
Original line number Diff line number Diff line
/*
 * Copyright (C) 2014 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 android.hardware.camera2.dispatch;

import java.lang.reflect.Method;

import static com.android.internal.util.Preconditions.*;

/**
 * A dispatcher that replaces one argument with another; replaces any argument at an index
 * with another argument.
 *
 * <p>For example, we can override an {@code void onSomething(int x)} calls to have {@code x} always
 * equal to 1. Or, if using this with a duck typing dispatcher, we could even overwrite {@code x} to
 * be something
 * that's not an {@code int}.</p>
 *
 * @param <T>
 *          source dispatch type, whose methods with {@link #dispatch} will be called
 * @param <TArg>
 *          argument replacement type, args in {@link #dispatch} matching {@code argumentIndex}
 *          will be overriden to objects of this type
 */
public class ArgumentReplacingDispatcher<T, TArg> implements Dispatchable<T> {

    private final Dispatchable<T> mTarget;
    private final int mArgumentIndex;
    private final TArg mReplaceWith;

    /**
     * Create a new argument replacing dispatcher; dispatches are forwarded to {@code target}
     * after the argument is replaced.
     *
     * <p>For example, if a method {@code onAction(T1 a, Integer b, T2 c)} is invoked, and we wanted
     * to replace all occurrences of {@code b} with {@code 0xDEADBEEF}, we would set
     * {@code argumentIndex = 1} and {@code replaceWith = 0xDEADBEEF}.</p>
     *
     * <p>If a method dispatched has less arguments than {@code argumentIndex}, it is
     * passed through with the arguments unchanged.</p>
     *
     * @param target destination dispatch type, methods will be redirected to this dispatcher
     * @param argumentIndex the numeric index of the argument {@code >= 0}
     * @param replaceWith arguments matching {@code argumentIndex} will be replaced with this object
     */
    public ArgumentReplacingDispatcher(Dispatchable<T> target, int argumentIndex,
            TArg replaceWith) {
        mTarget = checkNotNull(target, "target must not be null");
        mArgumentIndex = checkArgumentNonnegative(argumentIndex,
                "argumentIndex must not be negative");
        mReplaceWith = checkNotNull(replaceWith, "replaceWith must not be null");
    }

    @Override
    public Object dispatch(Method method, Object[] args) throws Throwable {

        if (args.length > mArgumentIndex) {
            args = arrayCopy(args); // don't change in-place since it can affect upstream dispatches
            args[mArgumentIndex] = mReplaceWith;
        }

        return mTarget.dispatch(method, args);
    }

    private static Object[] arrayCopy(Object[] array) {
        int length = array.length;
        Object[] newArray = new Object[length];
        for (int i = 0; i < length; ++i) {
            newArray[i] = array[i];
        }
        return newArray;
    }
}
+0 −64
Original line number Diff line number Diff line
/*
 * Copyright (C) 2014 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 android.hardware.camera2.dispatch;


import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;

import static com.android.internal.util.Preconditions.*;

/**
 * Broadcast a single dispatch into multiple other dispatchables.
 *
 * <p>Every time {@link #dispatch} is invoked, all the broadcast targets will
 * see the same dispatch as well. The first target's return value is returned.</p>
 *
 * <p>This enables a single listener to be converted into a multi-listener.</p>
 */
public class BroadcastDispatcher<T> implements Dispatchable<T> {

    private final List<Dispatchable<T>> mDispatchTargets;

    /**
     * Create a broadcast dispatcher from the supplied dispatch targets.
     *
     * @param dispatchTargets one or more targets to dispatch to
     */
    @SafeVarargs
    public BroadcastDispatcher(Dispatchable<T>... dispatchTargets) {
        mDispatchTargets = Arrays.asList(
                checkNotNull(dispatchTargets, "dispatchTargets must not be null"));
    }

    @Override
    public Object dispatch(Method method, Object[] args) throws Throwable {
        Object result = null;
        boolean gotResult = false;

        for (Dispatchable<T> dispatchTarget : mDispatchTargets) {
            Object localResult = dispatchTarget.dispatch(method, args);

            if (!gotResult) {
                gotResult = true;
                result = localResult;
            }
        }

        return result;
    }
}
+0 −35
Original line number Diff line number Diff line
/*
 * Copyright (C) 2014 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 android.hardware.camera2.dispatch;

import java.lang.reflect.Method;

/**
 * Dynamically dispatch a method and its argument to some object.
 *
 * <p>This can be used to intercept method calls and do work around them, redirect work,
 * or block calls entirely.</p>
 */
public interface Dispatchable<T> {
    /**
     * Dispatch the method and arguments to this object.
     * @param method a method defined in class {@code T}
     * @param args arguments corresponding to said {@code method}
     * @return the object returned when invoking {@code method}
     * @throws Throwable any exception that might have been raised while invoking the method
     */
    public Object dispatch(Method method, Object[] args) throws Throwable;
}
Loading