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 import android.util.StatsLog;
24 
25 import java.util.List;
26 
27 /** @hide */
28 public final class ContentCaptureMetricsLogger {
29     /**
30      * Class only contains static utility functions, and should not be instantiated
31      */
ContentCaptureMetricsLogger()32     private ContentCaptureMetricsLogger() {
33     }
34 
35     /** @hide */
writeServiceEvent(int eventType, @NonNull String serviceName, @Nullable String targetPackage)36     public static void writeServiceEvent(int eventType, @NonNull String serviceName,
37             @Nullable String targetPackage) {
38         StatsLog.write(StatsLog.CONTENT_CAPTURE_SERVICE_EVENTS, eventType, serviceName,
39                 targetPackage);
40     }
41 
42     /** @hide */
writeServiceEvent(int eventType, @NonNull ComponentName service, @Nullable ComponentName target)43     public static void writeServiceEvent(int eventType, @NonNull ComponentName service,
44             @Nullable ComponentName target) {
45         writeServiceEvent(eventType, ComponentName.flattenToShortString(service),
46                 ComponentName.flattenToShortString(target));
47     }
48 
49     /** @hide */
writeServiceEvent(int eventType, @NonNull ComponentName service, @Nullable String targetPackage)50     public static void writeServiceEvent(int eventType, @NonNull ComponentName service,
51             @Nullable String targetPackage) {
52         writeServiceEvent(eventType, ComponentName.flattenToShortString(service), targetPackage);
53     }
54 
55     /** @hide */
writeServiceEvent(int eventType, @NonNull ComponentName service)56     public static void writeServiceEvent(int eventType, @NonNull ComponentName service) {
57         writeServiceEvent(eventType, ComponentName.flattenToShortString(service), null);
58     }
59 
60     /** @hide */
writeSetWhitelistEvent(@ullable ComponentName service, @Nullable List<String> packages, @Nullable List<ComponentName> activities)61     public static void writeSetWhitelistEvent(@Nullable ComponentName service,
62             @Nullable List<String> packages, @Nullable List<ComponentName> activities) {
63         final String serviceName = ComponentName.flattenToShortString(service);
64         StringBuilder stringBuilder = new StringBuilder();
65         if (packages != null && packages.size() > 0) {
66             final int size = packages.size();
67             stringBuilder.append(packages.get(0));
68             for (int i = 1; i < size; i++) {
69                 stringBuilder.append(" ");
70                 stringBuilder.append(packages.get(i));
71             }
72         }
73         if (activities != null && activities.size() > 0) {
74             stringBuilder.append(" ");
75             stringBuilder.append(activities.get(0).flattenToShortString());
76             final int size = activities.size();
77             for (int i = 1; i < size; i++) {
78                 stringBuilder.append(" ");
79                 stringBuilder.append(activities.get(i).flattenToShortString());
80             }
81         }
82         StatsLog.write(StatsLog.CONTENT_CAPTURE_SERVICE_EVENTS,
83                 StatsLog.CONTENT_CAPTURE_SERVICE_EVENTS__EVENT__SET_WHITELIST,
84                 serviceName, stringBuilder.toString());
85     }
86 
87     /** @hide */
writeSessionEvent(int sessionId, int event, int flags, @NonNull ComponentName service, @Nullable ComponentName app, boolean isChildSession)88     public static void writeSessionEvent(int sessionId, int event, int flags,
89             @NonNull ComponentName service, @Nullable ComponentName app, boolean isChildSession) {
90         StatsLog.write(StatsLog.CONTENT_CAPTURE_SESSION_EVENTS, sessionId, event, flags,
91                 ComponentName.flattenToShortString(service),
92                 ComponentName.flattenToShortString(app), isChildSession);
93     }
94 
95     /** @hide */
writeSessionFlush(int sessionId, @NonNull ComponentName service, @Nullable ComponentName app, @NonNull FlushMetrics fm, @NonNull ContentCaptureOptions options, int flushReason)96     public static void writeSessionFlush(int sessionId, @NonNull ComponentName service,
97             @Nullable ComponentName app, @NonNull FlushMetrics fm,
98             @NonNull ContentCaptureOptions options, int flushReason) {
99         StatsLog.write(StatsLog.CONTENT_CAPTURE_FLUSHED, sessionId,
100                 ComponentName.flattenToShortString(service),
101                 ComponentName.flattenToShortString(app), fm.sessionStarted, fm.sessionFinished,
102                 fm.viewAppearedCount, fm.viewDisappearedCount, fm.viewTextChangedCount,
103                 options.maxBufferSize, options.idleFlushingFrequencyMs,
104                 options.textChangeFlushingFrequencyMs, flushReason);
105     }
106 }
107