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

Commit a14d983c authored by Avichal Rakesh's avatar Avichal Rakesh
Browse files

Print monitored tag information from new connected clients

Current implementation of `watch` command only prints live information of
clients that are connected at the time of calling print. Information
from clients connected after the command invocation is not printed.

This CL updates the print function to print to print tag monitoring
information from clients that connect while information is being printed
live.

Bug: 199746421
Test: Manually Tested
Change-Id: I234ec6a31344c6ca1f696204d2d6f05de6426cb8
parent 66394b8c
Loading
Loading
Loading
Loading
+18 −17
Original line number Diff line number Diff line
@@ -4894,41 +4894,42 @@ status_t CameraService::printWatchedTags(const Vector<String16> &args, int outFd
}

status_t CameraService::printWatchedTagsUntilInterrupt(useconds_t refreshMicros, int outFd) {
    std::unordered_map<std::string, std::string> cameraToLastEvent;
    auto cameraClients = mActiveClientManager.getAll();

    if (cameraClients.empty()) {
        dprintf(outFd, "No clients connected.\n");
        return OK;
    }

    dprintf(outFd, "Press Ctrl + C to exit...\n\n");
    std::map<String16, std::string> packageNameToLastEvent;

    while (true) {
        bool serviceLock = tryLock(mServiceLock);
        auto cameraClients = mActiveClientManager.getAll();
        if (serviceLock) { mServiceLock.unlock(); }

        for (const auto& clientDescriptor : cameraClients) {
            Mutex::Autolock lock(mLogLock);
            if (clientDescriptor == nullptr) { continue; }
            const char* cameraId = clientDescriptor->getKey().string();

            // This also initializes the map entries with an empty string
            const std::string& lastPrintedEvent = cameraToLastEvent[cameraId];

            sp<BasicClient> client = clientDescriptor->getValue();
            if (client.get() == nullptr) { continue; }
            if (!isClientWatchedLocked(client.get())) { continue; }

            const String16 &packageName = client->getPackageName();
            // This also initializes the map entries with an empty string
            const std::string& lastPrintedEvent = packageNameToLastEvent[packageName];

            std::vector<std::string> latestEvents;
            client->dumpWatchedEventsToVector(latestEvents);

            if (!latestEvents.empty()) {
                String8 cameraId = clientDescriptor->getKey();
                const char *printableCameraId = cameraId.lockBuffer(cameraId.size());
                printNewWatchedEvents(outFd,
                                      cameraId,
                                      client->getPackageName(),
                                      printableCameraId,
                                      packageName,
                                      latestEvents,
                                      lastPrintedEvent);
                cameraToLastEvent[cameraId] = latestEvents[0];
                packageNameToLastEvent[packageName] = latestEvents[0];
                cameraId.unlockBuffer();
            }
        }
        usleep(refreshMicros);  // convert ms to us
        usleep(refreshMicros);
    }
    return OK;
}