• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.server.contentcapture;
17 
18 import android.annotation.NonNull;
19 import android.annotation.Nullable;
20 import android.content.ComponentName;
21 import android.content.ContentCaptureOptions;
22 import android.service.contentcapture.FlushMetrics;
23 
24 import com.android.internal.util.FrameworkStatsLog;
25 
26 import java.util.List;
27 
28 /** @hide */
29 public final class ContentCaptureMetricsLogger {
30     /**
31      * Class only contains static utility functions, and should not be instantiated
32      */
ContentCaptureMetricsLogger()33     private ContentCaptureMetricsLogger() {
34     }
35 
36     /** @hide */
writeServiceEvent(int eventType, @NonNull String serviceName, @Nullable String targetPackage)37     public static void writeServiceEvent(int eventType, @NonNull String serviceName,
38             @Nullable String targetPackage) {
39         FrameworkStatsLog.write(FrameworkStatsLog.CONTENT_CAPTURE_SERVICE_EVENTS, eventType,
40                 serviceName, targetPackage);
41     }
42 
43     /** @hide */
writeServiceEvent(int eventType, @NonNull ComponentName service, @Nullable ComponentName target)44     public static void writeServiceEvent(int eventType, @NonNull ComponentName service,
45             @Nullable ComponentName target) {
46         writeServiceEvent(eventType, ComponentName.flattenToShortString(service),
47                 ComponentName.flattenToShortString(target));
48     }
49 
50     /** @hide */
writeServiceEvent(int eventType, @NonNull ComponentName service, @Nullable String targetPackage)51     public static void writeServiceEvent(int eventType, @NonNull ComponentName service,
52             @Nullable String targetPackage) {
53         writeServiceEvent(eventType, ComponentName.flattenToShortString(service), targetPackage);
54     }
55 
56     /** @hide */
writeServiceEvent(int eventType, @NonNull ComponentName service)57     public static void writeServiceEvent(int eventType, @NonNull ComponentName service) {
58         writeServiceEvent(eventType, ComponentName.flattenToShortString(service), null);
59     }
60 
61     /** @hide */
writeSetWhitelistEvent(@ullable ComponentName service, @Nullable List<String> packages, @Nullable List<ComponentName> activities)62     public static void writeSetWhitelistEvent(@Nullable ComponentName service,
63             @Nullable List<String> packages, @Nullable List<ComponentName> activities) {
64         final String serviceName = ComponentName.flattenToShortString(service);
65         StringBuilder stringBuilder = new StringBuilder();
66         if (packages != null && packages.size() > 0) {
67             final int size = packages.size();
68             stringBuilder.append(packages.get(0));
69             for (int i = 1; i < size; i++) {
70                 stringBuilder.append(" ");
71                 stringBuilder.append(packages.get(i));
72             }
73         }
74         if (activities != null && activities.size() > 0) {
75             stringBuilder.append(" ");
76             stringBuilder.append(activities.get(0).flattenToShortString());
77             final int size = activities.size();
78             for (int i = 1; i < size; i++) {
79                 stringBuilder.append(" ");
80                 stringBuilder.append(activities.get(i).flattenToShortString());
81             }
82         }
83         FrameworkStatsLog.write(FrameworkStatsLog.CONTENT_CAPTURE_SERVICE_EVENTS,
84                 FrameworkStatsLog.CONTENT_CAPTURE_SERVICE_EVENTS__EVENT__SET_WHITELIST,
85                 serviceName, stringBuilder.toString());
86     }
87 
88     /** @hide */
writeSessionEvent(int sessionId, int event, int flags, @NonNull ComponentName service, @Nullable ComponentName app, boolean isChildSession)89     public static void writeSessionEvent(int sessionId, int event, int flags,
90             @NonNull ComponentName service, @Nullable ComponentName app, boolean isChildSession) {
91         FrameworkStatsLog.write(FrameworkStatsLog.CONTENT_CAPTURE_SESSION_EVENTS, sessionId, event,
92                 flags, ComponentName.flattenToShortString(service),
93                 ComponentName.flattenToShortString(app), isChildSession);
94     }
95 
96     /** @hide */
writeSessionFlush(int sessionId, @NonNull ComponentName service, @Nullable ComponentName app, @NonNull FlushMetrics fm, @NonNull ContentCaptureOptions options, int flushReason)97     public static void writeSessionFlush(int sessionId, @NonNull ComponentName service,
98             @Nullable ComponentName app, @NonNull FlushMetrics fm,
99             @NonNull ContentCaptureOptions options, int flushReason) {
100         FrameworkStatsLog.write(FrameworkStatsLog.CONTENT_CAPTURE_FLUSHED, sessionId,
101                 ComponentName.flattenToShortString(service),
102                 ComponentName.flattenToShortString(app), fm.sessionStarted, fm.sessionFinished,
103                 fm.viewAppearedCount, fm.viewDisappearedCount, fm.viewTextChangedCount,
104                 options.maxBufferSize, options.idleFlushingFrequencyMs,
105                 options.textChangeFlushingFrequencyMs, flushReason);
106     }
107 }
108