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

Commit 7a9e5935 authored by Tomasz Wasilczyk's avatar Tomasz Wasilczyk Committed by Android (Google) Code Review
Browse files

Merge "Implement client death monitoring."

parents 91208fa8 e8f4b34b
Loading
Loading
Loading
Loading
+0 −1
Original line number Diff line number Diff line
@@ -98,7 +98,6 @@ public class RadioService extends SystemService {
                throw new IllegalArgumentException("Callback must not be empty");
            }
            synchronized (mLock) {
                // TODO(b/36863239): add death monitoring for binder
                return nativeOpenTuner(mNativeContext, moduleId, bandConfig, withAudio, callback);
            }
        }
+14 −1
Original line number Diff line number Diff line
@@ -20,6 +20,8 @@ import android.annotation.NonNull;
import android.hardware.radio.ITuner;
import android.hardware.radio.ITunerCallback;
import android.hardware.radio.RadioManager;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Slog;

import java.util.List;
@@ -33,18 +35,28 @@ class Tuner extends ITuner.Stub {
     */
    private final long mNativeContext;

    @NonNull private final TunerCallback mTunerCallback;
    private final Object mLock = new Object();
    @NonNull private final TunerCallback mTunerCallback;
    @NonNull private final ITunerCallback mClientCallback;
    @NonNull private final IBinder.DeathRecipient mDeathRecipient;

    private boolean mIsClosed = false;
    private boolean mIsMuted = false;
    private int mRegion;  // TODO(b/62710330): find better solution to handle regions
    private final boolean mWithAudio;

    Tuner(@NonNull ITunerCallback clientCallback, int halRev, int region, boolean withAudio) {
        mClientCallback = clientCallback;
        mTunerCallback = new TunerCallback(this, clientCallback, halRev);
        mRegion = region;
        mWithAudio = withAudio;
        mNativeContext = nativeInit(halRev, withAudio);
        mDeathRecipient = this::close;
        try {
            mClientCallback.asBinder().linkToDeath(mDeathRecipient, 0);
        } catch (RemoteException ex) {
            close();
        }
    }

    @Override
@@ -81,6 +93,7 @@ class Tuner extends ITuner.Stub {
        synchronized (mLock) {
            if (mIsClosed) return;
            mTunerCallback.detach();
            mClientCallback.asBinder().unlinkToDeath(mDeathRecipient, 0);
            nativeClose(mNativeContext);
            mIsClosed = true;
        }
+14 −1
Original line number Diff line number Diff line
@@ -66,6 +66,7 @@ static const char* const kAudioDeviceName = "Radio tuner source";
struct TunerContext {
    TunerContext() {}

    bool mIsClosed = false;
    HalRevision mHalRev;
    bool mWithAudio;
    sp<V1_0::ITuner> mHalTuner;
@@ -127,6 +128,12 @@ void setHalTuner(JNIEnv *env, JavaRef<jobject> const &jTuner, sp<V1_0::ITuner> h
    AutoMutex _l(gContextMutex);
    auto& ctx = getNativeContext(env, jTuner);

    if (ctx.mIsClosed) {
        ALOGI("Tuner was closed during initialization");
        // dropping the last reference will close HAL tuner
        return;
    }

    ctx.mHalTuner = halTuner;
    ctx.mHalTuner11 = V1_1::ITuner::castFrom(halTuner).withDefault(nullptr);
    ALOGW_IF(ctx.mHalRev >= HalRevision::V1_1 && ctx.mHalTuner11 == nullptr,
@@ -159,7 +166,13 @@ Region getRegion(JNIEnv *env, jobject obj) {
static void nativeClose(JNIEnv *env, jobject obj, jlong nativeContext) {
    AutoMutex _l(gContextMutex);
    auto& ctx = getNativeContext(nativeContext);
    if (ctx.mHalTuner == nullptr) return;
    if (ctx.mIsClosed) return;
    ctx.mIsClosed = true;

    if (ctx.mHalTuner == nullptr) {
        ALOGI("Tuner closed during initialization");
        return;
    }

    ALOGI("Closing tuner %p", ctx.mHalTuner.get());
    notifyAudioService(ctx, false);