/*
 * Copyright (C) 2022 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.permissioncontroller;

import "frameworks/proto_logging/stats/atoms.proto";
import "frameworks/proto_logging/stats/atom_field_options.proto";

// This file contains extension atoms for permission controller.
option java_package = "com.android.os.permissioncontroller";
option java_multiple_files = true;

extend Atom {
    optional PermissionRationaleDialogViewed permission_rationale_dialog_viewed =
        645 [(module) = "permissioncontroller"];
    optional PermissionRationaleDialogActionReported permission_rationale_dialog_action_reported =
        646 [(module) = "permissioncontroller"];
    optional AppDataSharingUpdatesNotificationInteraction app_data_sharing_updates_notification_interaction =
        647 [(module) = "permissioncontroller"];
    optional AppDataSharingUpdatesFragmentViewed app_data_sharing_updates_fragment_viewed =
        648 [(module) = "permissioncontroller"];
    optional AppDataSharingUpdatesFragmentActionReported app_data_sharing_updates_fragment_action_reported =
        649 [(module) = "permissioncontroller"];
    optional EnhancedConfirmationDialogResultReported enhanced_confirmation_dialog_result_reported =
        827 [(module) = "permissioncontroller"];
    optional EnhancedConfirmationRestrictionCleared enhanced_confirmation_restriction_cleared =
        828 [(module) = "permissioncontroller"];
    optional RoleSettingsFragmentActionReported role_settings_fragment_action_reported =
        1020 [(module) = "permissioncontroller"];
    optional EcmRestrictionQueryInCallReported ecm_restriction_query_in_call_reported =
        1034 [(module) = "permissioncontroller"];
    optional CallWithEcmInteractionReported call_with_ecm_interaction_reported =
        1035 [(module) = "permissioncontroller"];
    optional PermissionManagerPageInteraction permission_manager_page_interaction =
        1049 [(module) = "permissioncontroller"];
}

/**
* Information about a Permission Rationale dialog viewed by user.
* Logged from ui/model/v34/PermissionRationaleViewModel.java
*/
message PermissionRationaleDialogViewed {
    // id which identifies single session of user interacting with permission controller
    optional int64 session_id = 1;

    // UID of package for which permissions are viewed
    optional int32 uid = 2 [(is_uid) = true];

    // Permission group viewed
    optional string permission_group_name = 3;

    // Data usage purposes shown to user for this permission group in the dialog - bit flags,
    // bit numbers are in accordance with PURPOSE_ constants in DataPurposeConstants.java
    optional int32 purposes_presented = 4;
}

/**
 * Information about a button clicks made by user inside PermissionRationaleActivity
 */
message PermissionRationaleDialogActionReported {
    // id which identifies single session of user interacting with permission controller
    optional int64 session_id = 1;

    // UID of package the permission belongs to
    optional int32 uid = 2 [(is_uid) = true];

    // The permission group currently presented
    optional string permission_group_name = 3;

    enum Button {
        UNDEFINED = 0;
        // Dialog canceled or back clicked
        CANCEL = 1;
        // Link to install source (app store)
        INSTALL_SOURCE = 2;
        // Link to help center article
        HELP_CENTER = 3;
        // Link to permission settings
        PERMISSION_SETTINGS = 4;
    }

    // Button pressed in the dialog
    optional Button button_pressed = 4;
}

/**
 * Information about AppDataSharingUpdates notification and interaction
 */
message AppDataSharingUpdatesNotificationInteraction {
    // Session Id to link the notification with the issue card.
    optional int64 session_id = 1; // to map the notification and issue card interaction

    enum Action {
        UNKNOWN = 0;
        NOTIFICATION_SHOWN = 1;
        NOTIFICATION_CLICKED = 2;
        DISMISSED = 3;
    }

    // Action taken on the notification.
    optional Action action = 2;

    // Number app data sharing updates represented by the notification
    optional int32 number_of_app_updates = 3;
}

/**
* Information about AppDataSharingUpdatesFragment viewed by user.
* Logged from ui/model/v34/AppDataSharingUpdatesViewModel.java
*/
message AppDataSharingUpdatesFragmentViewed {
    // id which identifies single session of user interacting with permission controller
    optional int64 session_id = 1;

    // Number of app data sharing updates displayed when viewed
    optional int32 number_of_app_updates = 2;
}

/**
 * Information about a button clicks made by user inside AppDataSharingUpdatesFragment
 */
message AppDataSharingUpdatesFragmentActionReported {
    // id which identifies single session of user interacting with permission controller
    optional int64 session_id = 1;

    // UID of package the interacted data sharing update belongs to
    optional int32 uid = 2 [(is_uid) = true];

    enum DataSharingChange {
        UNKNOWN = 0;
        ADDS_ADVERTISING_PURPOSE = 1;
        ADDS_SHARING_WITHOUT_ADVERTISING_PURPOSE = 2;
        ADDS_SHARING_WITH_ADVERTISING_PURPOSE = 3;
    }

    // Data sharing change associated with this data sharing update
    optional DataSharingChange data_sharing_change = 3;
}

/**
 * Logged when the ECM (Enhanced Confirmation Mode) dialog is closed.
 *
 * Logs from: com.android.permissioncontroller
 *            .permission.ui.GrantPermissionsActivity
 *
 */
message EnhancedConfirmationDialogResultReported {
  // UID of the restricted app
  optional int32 uid = 1 [(is_uid) = true];

  // Identifier of the restricted setting
  optional string setting_identifier = 2;

  // True if this is the first time showing this dialog for this app
  optional bool first_show_for_app = 3;

  enum SettingType {
    SETTING_TYPE_UNSPECIFIED = 0;
    SETTING_TYPE_APPOP = 1;
    SETTING_TYPE_PERMISSION = 2;
    SETTING_TYPE_ROLE = 3;
    SETTING_TYPE_OTHER = 4;
  }

  // Which type of resource setting_identifier refers to
  optional SettingType setting_type = 4;

  enum Result {
    RESULT_UNSPECIFIED = 0;
    RESULT_CANCELLED = 1;
    RESULT_LEARN_MORE = 2;
    RESULT_OK = 3;
    RESULT_SUPPRESSED = 4;
  }

  // Which button or action the user invoked to close the dialog
  optional Result result = 5;
}

/**
 * Logged when an app's ECM (Enhanced Confirmation Mode) restriction
 * status is cleared by the user.
 *
 * Logs from: com.android.ecm.EnhancedConfirmationService
 *
 */
message EnhancedConfirmationRestrictionCleared {
  // UID of the restricted app
  optional int32 uid = 1 [(is_uid) = true];
}

/**
 * Reports that User has changed a default app for a role in settings.
 * Logged from:
 *   packages/modules/Permission/PermissionController/src/com/android/permissioncontroller/role/ui/DefaultAppChildFragment.java
 * Estimated Logging Rate:
 *   Peak: few times per device per week | Avg: Between 0 and 1 times per device per day
 */
message RoleSettingsFragmentActionReported {
  // UID of application assigned the role
  optional int32 uid = 1 [(is_uid) = true];

  // Package name of application assigned the role
  optional string package_name = 2;

  optional string role_name = 3;
}

/*
 * Reports that the Enhanced Confirmation Service has received a query to the restriction state of
 * a setting that is restricted while in an untrusted call.
 * Logs from: com.android.ecm.EnhancedConfirmationService
 */
message EcmRestrictionQueryInCallReported {
  // UID of the possibly restricted app
  optional int32 uid = 1 [(is_uid) = true];

  // The setting being checked
  optional string setting_identifier = 2;

  // Whether or not the setting was allowed (vs. restricted)
  optional bool allowed = 3;

  // Whether a call was in progress at the time of this restriction check
  optional bool call_in_progress = 4;

  // Whether the call was incoming. False, if there was no call
  optional bool call_incoming = 5;

  // Whether the call was trusted. True, if there was no call
  optional bool call_trusted = 6;

  // Whether a block had happened in the past hour for the given call. False if there is no call.
  optional bool call_back_after_block = 7;
}

/*
 * Reports that a call happened, during which the EnhancedConfirmation Service was queried for the
 * restriction state of a setting that is restricted while in an untrusted call.
 * Logs from: com.android.ecm.EnhancedConfirmationService
 */
message CallWithEcmInteractionReported {
    // Whether any setting was blocked during the call
    optional bool any_setting_blocked = 1;
    // The call duration, in seconds
    optional int32 duration_secs = 2;
}

/**
* Capture user interactions with the permission manager page.
* Logged from ManageStandardPermissionsFragment.java, and
* ManageCustomPermissionsFragment.java
*/
message PermissionManagerPageInteraction {
    // id which identifies single session of user interacting with
    // permission manager page
    optional int64 session_id = 1;

    enum Action {
        UNKNOWN = 0;
        PERMISSION_MANAGER_OPENED = 1;
        UNUSED_APPS_LEARN_MORE_CLICKED = 2;
        STANDARD_PERMISSION_GROUP_CLICKED = 3;
        ADDITIONAL_PERMISSIONS_CLICKED = 4;
        ADDITIONAL_PERMISSION_GROUP_CLICKED = 5;
    }

    // The action the user took to interact with the fragment
    optional Action action = 2;
    // The permission group the user interacted with, only applies to
    // STANDARD_PERMISSION_GROUP_CLICKED and ADDITIONAL_PERMISSION_GROUP_CLICKED
    optional string permission_group_name = 3;
}
