/*
 * Copyright (C) 2024 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

syntax = "proto2";

package android.os.statsd.hardware.biometrics;

option java_package = "com.android.os.hardware.biometrics";
option java_multiple_files = true;

import "frameworks/proto_logging/stats/atoms.proto";
import "frameworks/proto_logging/stats/atom_field_options.proto";
import "frameworks/proto_logging/stats/enums/hardware/biometrics/enums.proto";

extend Atom {
  optional BiometricUnenrolled biometric_unenrolled = 944 [(module) = "framework"];
  optional BiometricEnumerated biometric_enumerated = 945 [(module) = "framework"];
  optional BiometricPromptStarted biometric_prompt_started = 1154 [(module) = "sysui"];
  optional BiometricPromptEvent biometric_prompt_event = 1155 [(module) = "sysui"];
  optional BiometricPromptEnded biometric_prompt_ended = 1156 [(module) = "sysui"];
}

/**
 * Logs when a biometric template is unenrolled.
 *
 * Logged from:
 *   frameworks/base/services/core/java/com/android/server/biometrics
 */
message BiometricUnenrolled {
    // Biometric modality for which a template was unenrolled.
    optional android.hardware.biometrics.ModalityEnum modality = 1;
    // The associated user. Eg: 0 for owners, 10+ for others. Defined in android/os/UserHandle.java
    optional int32 user = 2;
    // Reason why template was unenrolled.
    optional android.hardware.biometrics.UnenrollReasonEnum unenroll_reason = 3;
    // Numerical ID for unenrolled template. Ids increment with every new enrollment. Eg: 1, 2...
    optional int32 template_id = 4;
}

/**
 * Logs when templates are enumerated for a user.
 *
 * Logged from:
 *   frameworks/base/services/core/java/com/android/server/biometrics
 */
message BiometricEnumerated {
    // Biometric modality for which templates were enumerated.
    optional android.hardware.biometrics.ModalityEnum modality = 1;
    // The associated user. Eg: 0 for owners, 10+ for others. Defined in android/os/UserHandle.java
    optional int32 user = 2;
    // Result of enumeration. Eg: OK (templates match), mismatch from dangling template etc.
    optional android.hardware.biometrics.EnumerationResultEnum enumeration_result = 3;
    // Numerical IDs for templates reported by HAL. Ids increment with every new enrollment. Eg: 1, 2...
    repeated int32 template_ids_hal = 4;
    // Numerical IDs for templates reported by HAL. Ids increment with every new enrollment. Eg: 1, 2...
    repeated int32 template_ids_framework = 5;
}


/**
 * Logged when the BiometricPrompt starts, includes request information
 *
 * Logged from:
 *   frameworks/base/packages/SystemUI/src/com/android/systemui/biometrics/ui/biometricprompt/BiometricPromptLogger.kt
 *
 * Estimated logging rate: 10 times per day
 */
message BiometricPromptStarted {
    // Session Id of the current operation
    optional int32 session_id = 1;

    // Whether device credential is allowed by app
    optional bool allow_device_credential_fallback = 2;

    // Authenticators
    optional bool authenticator_device_credential = 3;
    optional bool authenticator_weak = 4;
    optional bool authenticator_strong = 5;
    optional bool authenticator_identity_check = 6;

    // Whether confirmation is required
    optional bool require_confirmation = 7;

    // Whether app has passed custom title
    optional bool has_custom_title = 8;

    // Whether app has passed custom subtitle
    optional bool has_custom_subtitle = 9;

    // Whether app has passed custom description
    optional bool has_custom_description = 10;

    // Whether app has passed custom content view
    optional bool has_custom_content = 11;

    // Whether app has passed custom negative button
    optional bool has_custom_negative_button_text = 12;

    // Number of fallback options added
    optional int32 fallback_count = 13;

    // Whether Identity Check is active
    optional bool is_identity_check_active = 14;
}

/**
 * Logged each time a non-cancelling event occurs within prompt
 *
 * Logged from:
 *   frameworks/base/packages/SystemUI/src/com/android/systemui/biometrics/ui/biometricprompt/BiometricPromptLogger.kt
 *
 * Estimated logging rate: 4 times per Biometric Prompt Session
 */
message BiometricPromptEvent {
    enum EventType {
        EVENT_TYPE_UNKNOWN = 0;
        EVENT_TYPE_BIOMETRIC_VIEW_SHOWN = 1;
        EVENT_TYPE_CREDENTIAL_VIEW_SHOWN = 2;
        EVENT_TYPE_FALLBACK_VIEW_SHOWN = 3;
        EVENT_TYPE_WATCH_RANGING_STARTED = 4;
        EVENT_TYPE_WATCH_RANGING_SUCCESS = 5;
        EVENT_TYPE_WATCH_RANGING_ENDED = 6;
    }

    // Session Id of the current operation
    optional int32 session_id = 1;

    // The event being logged
    optional EventType event = 2;
}

/**
 * Logged once when a prompt session ends.
 *
 * Logged from:
 *   frameworks/base/packages/SystemUI/src/com/android/systemui/biometrics/ui/biometricprompt/BiometricPromptLogger.kt
 *
 * Estimated logging rate: 10 times per day
 */
message BiometricPromptEnded {
    enum PromptEndedReason {
        PROMPT_ENDED_REASON_UNKNOWN = 0;
        PROMPT_ENDED_REASON_BIOMETRIC_CONFIRMED = 1;
        PROMPT_ENDED_REASON_NEGATIVE = 2;
        PROMPT_ENDED_REASON_USER_CANCEL = 3;
        PROMPT_ENDED_REASON_BIOMETRIC_CONFIRM_NOT_REQUIRED = 4;
        PROMPT_ENDED_REASON_ERROR = 5;
        PROMPT_ENDED_REASON_SERVER_REQUESTED = 6;
        PROMPT_ENDED_REASON_CREDENTIAL_CONFIRMED = 7;
        PROMPT_ENDED_REASON_CONTENT_VIEW_MORE_OPTIONS = 8;
        PROMPT_ENDED_REASON_ERROR_NO_WM = 9;
        PROMPT_ENDED_REASON_FALLBACK_OPTION = 10;
    }

    // Session Id of the current operation
    optional int32 session_id = 1;

    // The reason prompt ended
    optional PromptEndedReason reason = 2;
}

