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

Commit fd794736 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Camera: Propagate camera usage metrics StatsLog"

parents 17d2f38b 6729f373
Loading
Loading
Loading
Loading
+27 −0
Original line number Diff line number Diff line
@@ -334,6 +334,7 @@ message Atom {
        BackGesture back_gesture_reported_reported = 224;
        UpdateEngineUpdateAttemptReported update_engine_update_attempt_reported = 225;
        UpdateEngineSuccessfulUpdateReported update_engine_successful_update_reported = 226;
        CameraActionEvent camera_action_event = 227;
    }

    // Pulled events will start at field 10000.
@@ -7164,3 +7165,29 @@ message FrameTimingHistogram {
    // It's required that len(time_millis) == len(frame_count)
    repeated int64 frame_counts = 2;
}


/**
 * Information about camera facing and API level usage.
 * Logged from:
 *   frameworks/base/services/core/java/com/android/server/camera/CameraServiceProxy.java
 */
message CameraActionEvent {
    // Camera session duration
    optional int64 duration = 1;

    // Camera API level used
    optional int32 api_level = 2;

    // Name of client package
    optional string package_name = 3;

    // Camera facing
    enum Facing {
        UNKNOWN = 0;
        BACK = 1;
        FRONT = 2;
        EXTERNAL = 3;
    }
    optional Facing facing = 4;
}
+59 −0
Original line number Diff line number Diff line
@@ -36,6 +36,7 @@ import android.os.UserManager;
import android.util.ArrayMap;
import android.util.ArraySet;
import android.util.Slog;
import android.util.StatsLog;

import com.android.internal.logging.MetricsLogger;
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
@@ -44,6 +45,9 @@ import com.android.server.ServiceThread;
import com.android.server.SystemService;
import com.android.server.wm.WindowManagerInternal;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
@@ -102,6 +106,9 @@ public class CameraServiceProxy extends SystemService

    private final boolean mNotifyNfc;

    private ScheduledThreadPoolExecutor mLogWriterService = new ScheduledThreadPoolExecutor(
            /*corePoolSize*/ 1);

    /**
     * Structure to track camera usage
     */
@@ -204,6 +211,9 @@ public class CameraServiceProxy extends SystemService

        mNotifyNfc = SystemProperties.getInt(NFC_NOTIFICATION_PROP, 0) > 0;
        if (DEBUG) Slog.v(TAG, "Notify NFC behavior is " + (mNotifyNfc ? "active" : "disabled"));
        // Don't keep any extra logging threads if not needed
        mLogWriterService.setKeepAliveTime(1, TimeUnit.SECONDS);
        mLogWriterService.allowCoreThreadTimeOut(true);
    }

    @Override
@@ -279,6 +289,51 @@ public class CameraServiceProxy extends SystemService
        }
    }

    private class EventWriterTask implements Runnable {
        private ArrayList<CameraUsageEvent> mEventList;
        private static final long WRITER_SLEEP_MS = 100;

        public EventWriterTask(ArrayList<CameraUsageEvent> eventList) {
            mEventList = eventList;
        }

        @Override
        public void run() {
            if (mEventList != null) {
                for (CameraUsageEvent event : mEventList) {
                    logCameraUsageEvent(event);
                    try {
                        Thread.sleep(WRITER_SLEEP_MS);
                    } catch (InterruptedException e) {}
                }
                mEventList.clear();
            }
        }

        /**
         * Write camera usage events to stats log.
         * Package-private
         */
        private void logCameraUsageEvent(CameraUsageEvent e) {
            int facing = StatsLog.CAMERA_ACTION_EVENT__FACING__UNKNOWN;
            switch(e.mCameraFacing) {
                case ICameraServiceProxy.CAMERA_FACING_BACK:
                    facing = StatsLog.CAMERA_ACTION_EVENT__FACING__BACK;
                    break;
                case ICameraServiceProxy.CAMERA_FACING_FRONT:
                    facing = StatsLog.CAMERA_ACTION_EVENT__FACING__FRONT;
                    break;
                case ICameraServiceProxy.CAMERA_FACING_EXTERNAL:
                    facing = StatsLog.CAMERA_ACTION_EVENT__FACING__EXTERNAL;
                    break;
                default:
                    Slog.w(TAG, "Unknown camera facing: " + e.mCameraFacing);
            }
            StatsLog.write(StatsLog.CAMERA_ACTION_EVENT, e.getDuration(), e.mAPILevel,
                    e.mClientName, facing);
        }
    }

    /**
     * Dump camera usage events to log.
     * Package-private
@@ -315,6 +370,10 @@ public class CameraServiceProxy extends SystemService
                        .setPackageName(e.mClientName);
                mLogger.write(l);
            }

            mLogWriterService.execute(new EventWriterTask(
                        new ArrayList<CameraUsageEvent>(mCameraUsageHistory)));

            mCameraUsageHistory.clear();
        }
        final long ident = Binder.clearCallingIdentity();