1/*
2 * Copyright (C) 2017 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
17package com.android.systemui.statusbar.phone;
18
19import android.metrics.LogMaker;
20import android.util.ArrayMap;
21
22import com.android.internal.annotations.VisibleForTesting;
23import com.android.internal.logging.MetricsLogger;
24import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
25import com.android.systemui.Dependency;
26import com.android.systemui.EventLogConstants;
27import com.android.systemui.EventLogTags;
28
29/**
30 * Wrapper that emits both new- and old-style gesture logs.
31 * TODO: delete this once the old logs are no longer needed.
32 */
33public class LockscreenGestureLogger {
34    private ArrayMap<Integer, Integer> mLegacyMap;
35    private LogMaker mLogMaker = new LogMaker(MetricsEvent.VIEW_UNKNOWN)
36            .setType(MetricsEvent.TYPE_ACTION);
37    private final MetricsLogger mMetricsLogger = Dependency.get(MetricsLogger.class);
38
39    public LockscreenGestureLogger() {
40        mLegacyMap = new ArrayMap<>(EventLogConstants.METRICS_GESTURE_TYPE_MAP.length);
41        for (int i = 0; i < EventLogConstants.METRICS_GESTURE_TYPE_MAP.length ; i++) {
42            mLegacyMap.put(EventLogConstants.METRICS_GESTURE_TYPE_MAP[i], i);
43        }
44    }
45
46    public void write(int gesture, int length, int velocity) {
47        mMetricsLogger.write(mLogMaker.setCategory(gesture)
48                .setType(MetricsEvent.TYPE_ACTION)
49                .addTaggedData(MetricsEvent.FIELD_GESTURE_LENGTH, length)
50                .addTaggedData(MetricsEvent.FIELD_GESTURE_VELOCITY, velocity));
51        // also write old-style logs for backward-0compatibility
52        EventLogTags.writeSysuiLockscreenGesture(safeLookup(gesture), length, velocity);
53    }
54
55    /**
56     * Record the location of a swipe gesture, expressed as percentages of the whole screen
57     * @param category the action
58     * @param xPercent x-location / width * 100
59     * @param yPercent y-location / height * 100
60     */
61    public void writeAtFractionalPosition(
62            int category, int xPercent, int yPercent, int rotation) {
63        mMetricsLogger.write(mLogMaker.setCategory(category)
64                .setType(MetricsEvent.TYPE_ACTION)
65                .addTaggedData(MetricsEvent.FIELD_GESTURE_X_PERCENT, xPercent)
66                .addTaggedData(MetricsEvent.FIELD_GESTURE_Y_PERCENT, yPercent)
67                .addTaggedData(MetricsEvent.FIELD_DEVICE_ROTATION, rotation));
68    }
69
70    private int safeLookup(int gesture) {
71        Integer value = mLegacyMap.get(gesture);
72        if (value == null) {
73            return MetricsEvent.VIEW_UNKNOWN;
74        }
75        return value;
76    }
77}
78