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

Commit beaca7ff authored by Tom Chan's avatar Tom Chan
Browse files

Add log to tell if a provided data stream is read-only or not.

Implementation adapted from https://source.corp.google.com/h/googleplex-android/platform/superproject/main/+/main:frameworks/base/services/core/java/com/android/server/ondeviceintelligence/BundleUtil.java;l=313;drc=6cdf7ce91a2643e02156d5ab8fb0f1c416f80c29

This helps with inspectability when we use the provideDataStream API to
send a read-only data stream into WearableSensingService.

Test: Tried on device. It correctly logs true for the read end of a PFD
pipe and false for a socket pair and the write end of a pipe.
Bug: 332871862

Change-Id: I171fcf7b70db56237ed82c19a6bfa07627f5bd99
parent 7dca5acb
Loading
Loading
Loading
Loading
+24 −1
Original line number Diff line number Diff line
@@ -17,6 +17,9 @@
package com.android.server.wearable;

import static android.service.wearable.WearableSensingService.HOTWORD_AUDIO_STREAM_BUNDLE_KEY;
import static android.system.OsConstants.F_GETFL;
import static android.system.OsConstants.O_ACCMODE;
import static android.system.OsConstants.O_RDONLY;

import android.Manifest;
import android.annotation.NonNull;
@@ -42,6 +45,8 @@ import android.os.SharedMemory;
import android.service.voice.HotwordAudioStream;
import android.service.voice.VoiceInteractionManagerInternal;
import android.service.voice.VoiceInteractionManagerInternal.WearableHotwordDetectionCallback;
import android.system.ErrnoException;
import android.system.Os;
import android.system.OsConstants;
import android.util.IndentingPrintWriter;
import android.util.Slog;
@@ -276,7 +281,10 @@ final class WearableSensingManagerPerUserService extends
            ParcelFileDescriptor parcelFileDescriptor,
            @Nullable IWearableSensingCallback wearableSensingCallback,
            RemoteCallback statusCallback) {
        Slog.i(TAG, "onProvideDataStream in per user service.");
        Slog.i(
                TAG,
                "onProvideDataStream in per user service. Is data stream read-only? "
                        + isReadOnly(parcelFileDescriptor));
        synchronized (mLock) {
            if (!setUpServiceIfNeeded()) {
                Slog.w(TAG, "Detection service is not available at this moment.");
@@ -511,4 +519,19 @@ final class WearableSensingManagerPerUserService extends
            }
        };
    }

    private static boolean isReadOnly(ParcelFileDescriptor parcelFileDescriptor) {
        try {
            int readMode =
                    Os.fcntlInt(parcelFileDescriptor.getFileDescriptor(), F_GETFL, 0) & O_ACCMODE;
            return readMode == O_RDONLY;
        } catch (ErrnoException ex) {
            Slog.w(
                    TAG,
                    "Error encountered when trying to determine if the parcelFileDescriptor is"
                        + " read-only. Treating it as not read-only",
                    ex);
        }
        return false;
    }
}