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

Commit d2df7036 authored by Nick Chalko's avatar Nick Chalko
Browse files

Include the TV Input Service uid in metrics logging.

Change-Id: I0a8801248e120e60df368273b91b9d87d00e2496
Bug: 165372105
Test: ./out/host/linux-x86/bin/statsd_testdrive
parent 1fd8b567
Loading
Loading
Loading
Loading
+72 −16
Original line number Diff line number Diff line
@@ -338,6 +338,7 @@ public final class TvInputManagerService extends SystemService {
                inputState = new TvInputState();
            }
            inputState.info = info;
            inputState.uid = getInputUid(info);
            inputMap.put(info.getId(), inputState);
        }

@@ -372,6 +373,16 @@ public final class TvInputManagerService extends SystemService {
        userState.inputMap = inputMap;
    }

    private int getInputUid(TvInputInfo info) {
        try {
            return getContext().getPackageManager().getApplicationInfo(
                    info.getServiceInfo().packageName, 0).uid;
        } catch (NameNotFoundException e) {
            Slog.w(TAG, "Unable to get UID for  " + info, e);
            return Process.INVALID_UID;
        }
    }

    @GuardedBy("mLock")
    private void buildTvContentRatingSystemListLocked(int userId) {
        UserState userState = getOrCreateUserStateLocked(userId);
@@ -406,7 +417,7 @@ public final class TvInputManagerService extends SystemService {
                return;
            }
            if (mUserStates.contains(mCurrentUserId)) {
                UserState userState = mUserStates.get(mCurrentUserId);
                UserState userState = getUserStateLocked(mCurrentUserId);
                List<SessionState> sessionStatesToRelease = new ArrayList<>();
                for (SessionState sessionState : userState.sessionStateMap.values()) {
                    if (sessionState.session != null && !sessionState.isRecordingSession) {
@@ -475,7 +486,7 @@ public final class TvInputManagerService extends SystemService {

    private void removeUser(int userId) {
        synchronized (mLock) {
            UserState userState = mUserStates.get(userId);
            UserState userState = getUserStateLocked(userId);
            if (userState == null) {
                return;
            }
@@ -536,7 +547,7 @@ public final class TvInputManagerService extends SystemService {

    @GuardedBy("mLock")
    private UserState getOrCreateUserStateLocked(int userId) {
        UserState userState = mUserStates.get(userId);
        UserState userState = getUserStateLocked(userId);
        if (userState == null) {
            userState = new UserState(mContext, userId);
            mUserStates.put(userId, userState);
@@ -911,6 +922,7 @@ public final class TvInputManagerService extends SystemService {
            return;
        }
        inputState.info = inputInfo;
        inputState.uid = getInputUid(inputInfo);

        int n = userState.mCallbacks.beginBroadcast();
        for (int i = 0; i < n; ++i) {
@@ -1287,6 +1299,8 @@ public final class TvInputManagerService extends SystemService {
                    TvInputInfo info = inputState.info;
                    ServiceState serviceState = userState.serviceStateMap.get(info.getComponent());
                    if (serviceState == null) {
                        int tisUid = PackageManager.getApplicationInfoAsUserCached(
                                info.getComponent().getPackageName(), 0, resolvedUserId).uid;
                        serviceState = new ServiceState(info.getComponent(), resolvedUserId);
                        userState.serviceStateMap.put(info.getComponent(), serviceState);
                    }
@@ -1320,7 +1334,7 @@ public final class TvInputManagerService extends SystemService {
                        updateServiceConnectionLocked(info.getComponent(), resolvedUserId);
                    }
                    logTuneStateChanged(FrameworkStatsLog.TIF_TUNE_STATE_CHANGED__STATE__CREATED,
                            sessionState);
                            sessionState, inputState);
                }
            } finally {
                Binder.restoreCallingIdentity(identity);
@@ -1338,12 +1352,16 @@ public final class TvInputManagerService extends SystemService {
            final long identity = Binder.clearCallingIdentity();
            try {
                SessionState sessionState = null;
                UserState userState = null;
                synchronized (mLock) {
                    sessionState = releaseSessionLocked(sessionToken, callingUid, resolvedUserId);
                    userState = getUserStateLocked(userId);
                }
                if (sessionState != null) {
                    TvInputState tvInputState = TvInputManagerService.getTvInputState(sessionState,
                            userState);
                    logTuneStateChanged(FrameworkStatsLog.TIF_TUNE_STATE_CHANGED__STATE__RELEASED,
                            sessionState);
                            sessionState, tvInputState);
                }
            } finally {
                Binder.restoreCallingIdentity(identity);
@@ -1398,9 +1416,11 @@ public final class TvInputManagerService extends SystemService {
                    userId, "setSurface");
            final long identity = Binder.clearCallingIdentity();
            SessionState sessionState = null;
            UserState userState = null;
            try {
                synchronized (mLock) {
                    try {
                        userState = getUserStateLocked(userId);
                        sessionState = getSessionStateLocked(sessionToken, callingUid,
                                resolvedUserId);
                        if (sessionState.hardwareSessionToken == null) {
@@ -1423,7 +1443,8 @@ public final class TvInputManagerService extends SystemService {
                            ?
                            FrameworkStatsLog.TIF_TUNE_STATE_CHANGED__STATE__SURFACE_ATTACHED
                            : FrameworkStatsLog.TIF_TUNE_STATE_CHANGED__STATE__SURFACE_DETACHED;
                    logTuneStateChanged(state, sessionState);
                    logTuneStateChanged(state, sessionState,
                            TvInputManagerService.getTvInputState(sessionState, userState));
                }
                Binder.restoreCallingIdentity(identity);
            }
@@ -1514,7 +1535,8 @@ public final class TvInputManagerService extends SystemService {

                        logTuneStateChanged(
                                FrameworkStatsLog.TIF_TUNE_STATE_CHANGED__STATE__TUNE_STARTED,
                                sessionState);
                                sessionState,
                                TvInputManagerService.getTvInputState(sessionState, userState));
                        // Log the start of watch.
                        SomeArgs args = SomeArgs.obtain();
                        args.arg1 = sessionState.componentName.getPackageName();
@@ -2338,6 +2360,16 @@ public final class TvInputManagerService extends SystemService {
        }
    }

    @Nullable
    private static TvInputState getTvInputState(
            SessionState sessionState,
            @Nullable UserState userState) {
        if (userState != null) {
            return userState.inputMap.get(sessionState.inputId);
        }
        return null;
    }

    @GuardedBy("mLock")
    private List<TunedInfo> getCurrentTunedInfosInternalLocked(
            UserState userState, int callingPid, int callingUid) {
@@ -2416,12 +2448,13 @@ public final class TvInputManagerService extends SystemService {
     *   <li>{@link SessionState#sessionId}
     * </ul>
     */
    private void logTuneStateChanged(int state, SessionState sessionState) {
        // TODO(b/165372105): log the tis uid.
    private void logTuneStateChanged(int state, SessionState sessionState,
            @Nullable TvInputState inputState) {
        // TODO(b/173536904): log input type and id
        FrameworkStatsLog.write(FrameworkStatsLog.TIF_TUNE_CHANGED,
                new int[]{sessionState.callingUid},
                new String[]{"tif_player"}, state, sessionState.sessionId);
                new int[]{sessionState.callingUid,
                        inputState == null ? Process.INVALID_UID : inputState.uid},
                new String[]{"tif_player", "tv_input_service"}, state, sessionState.sessionId);
    }

    private static final class UserState {
@@ -2521,10 +2554,25 @@ public final class TvInputManagerService extends SystemService {
    }

    private static final class TvInputState {
        // A TvInputInfo object which represents the TV input.

        /** A TvInputInfo object which represents the TV input. */
        private TvInputInfo info;

        // The state of TV input. Connected by default.
        /**
         * The kernel user-ID that has been assigned to the application the TvInput is a part of.
         *
         * <p>
         * Currently this is not a unique ID (multiple applications can have
         * the same uid).
         */
        private int uid;

        /**
         * The state of TV input.
         *
         * <p>
         * Connected by default
         */
        private int state = INPUT_STATE_CONNECTED;

        @Override
@@ -2619,7 +2667,7 @@ public final class TvInputManagerService extends SystemService {
                Slog.d(TAG, "onServiceConnected(component=" + component + ")");
            }
            synchronized (mLock) {
                UserState userState = mUserStates.get(mUserId);
                UserState userState = getUserStateLocked(mUserId);
                if (userState == null) {
                    // The user was removed while connecting.
                    mContext.unbindService(this);
@@ -2889,11 +2937,13 @@ public final class TvInputManagerService extends SystemService {
                if (mSessionState.session == null || mSessionState.client == null) {
                    return;
                }
                TvInputState tvInputState = getTvInputState(mSessionState,
                        getUserStateLocked(mCurrentUserId));
                try {
                    mSessionState.client.onVideoAvailable(mSessionState.seq);
                    logTuneStateChanged(
                            FrameworkStatsLog.TIF_TUNE_STATE_CHANGED__STATE__VIDEO_AVAILABLE,
                            mSessionState);
                            mSessionState, tvInputState);
                } catch (RemoteException e) {
                    Slog.e(TAG, "error in onVideoAvailable", e);
                }
@@ -2909,6 +2959,8 @@ public final class TvInputManagerService extends SystemService {
                if (mSessionState.session == null || mSessionState.client == null) {
                    return;
                }
                TvInputState tvInputState = getTvInputState(mSessionState,
                        getUserStateLocked(mCurrentUserId));
                try {
                    mSessionState.client.onVideoUnavailable(reason, mSessionState.seq);
                    int loggedReason = reason + FrameworkStatsLog
@@ -2920,7 +2972,7 @@ public final class TvInputManagerService extends SystemService {
                        loggedReason = FrameworkStatsLog
                                .TIF_TUNE_STATE_CHANGED__STATE__VIDEO_UNAVAILABLE_REASON_UNKNOWN;
                    }
                    logTuneStateChanged(loggedReason, mSessionState);
                    logTuneStateChanged(loggedReason, mSessionState, tvInputState);
                } catch (RemoteException e) {
                    Slog.e(TAG, "error in onVideoUnavailable", e);
                }
@@ -3106,6 +3158,10 @@ public final class TvInputManagerService extends SystemService {
        }
    }

    private UserState getUserStateLocked(int userId) {
        return mUserStates.get(userId);
    }

    private static final class WatchLogHandler extends Handler {
        // There are only two kinds of watch events that can happen on the system:
        // 1. The current TV input session is tuned to a new channel.