1 /* 2 * Copyright (C) 2018 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 17 package android.app.slice; 18 19 import android.annotation.NonNull; 20 import android.content.Context; 21 import android.metrics.LogMaker; 22 import android.net.Uri; 23 24 import com.android.internal.logging.MetricsLogger; 25 import com.android.internal.logging.nano.MetricsProto.MetricsEvent; 26 27 /** 28 * Metrics interface for slices. 29 * 30 * This is called by SliceView, so Slice developers should 31 * not need to reference this class. 32 * 33 * @see androidx.slice.widget.SliceView 34 */ 35 public class SliceMetrics { 36 37 private static final String TAG = "SliceMetrics"; 38 private MetricsLogger mMetricsLogger; 39 private LogMaker mLogMaker; 40 41 /** 42 * An object to be used throughout the life of a slice to register events. 43 */ SliceMetrics(@onNull Context context, @NonNull Uri uri)44 public SliceMetrics(@NonNull Context context, @NonNull Uri uri) { 45 mMetricsLogger = new MetricsLogger(); 46 mLogMaker = new LogMaker(MetricsEvent.VIEW_UNKNOWN); 47 mLogMaker.addTaggedData(MetricsEvent.FIELD_SLICE_AUTHORITY, uri.getAuthority()); 48 mLogMaker.addTaggedData(MetricsEvent.FIELD_SLICE_PATH, uri.getPath()); 49 } 50 51 /** 52 * To be called whenever the slice becomes visible to the user. 53 */ logVisible()54 public void logVisible() { 55 synchronized (mLogMaker) { 56 mLogMaker.setCategory(MetricsEvent.SLICE) 57 .setType(MetricsEvent.TYPE_OPEN); 58 mMetricsLogger.write(mLogMaker); 59 } 60 } 61 62 /** 63 * To be called whenever the slice becomes invisible to the user. 64 */ logHidden()65 public void logHidden() { 66 synchronized (mLogMaker) { 67 mLogMaker.setCategory(MetricsEvent.SLICE) 68 .setType(MetricsEvent.TYPE_CLOSE); 69 mMetricsLogger.write(mLogMaker); 70 } 71 } 72 73 /** 74 * To be called whenever the user invokes a discrete action via a slice. 75 * 76 * <P> 77 * Use this for discrete events like a tap or the end of a drag, 78 * not for a continuous streams of events, such as the motion during a gesture. 79 * </P> 80 * 81 * @see androidx.slice.widget.EventInfo#actionType 82 * 83 * @param actionType The type of the event. 84 * @param subSlice The URI of the sub-slice that is the subject of the interaction. 85 */ logTouch(int actionType, @NonNull Uri subSlice)86 public void logTouch(int actionType, @NonNull Uri subSlice) { 87 synchronized (mLogMaker) { 88 mLogMaker.setCategory(MetricsEvent.SLICE) 89 .setType(MetricsEvent.TYPE_ACTION) 90 .addTaggedData(MetricsEvent.FIELD_SUBSLICE_AUTHORITY, subSlice.getAuthority()) 91 .addTaggedData(MetricsEvent.FIELD_SUBSLICE_PATH, subSlice.getPath()); 92 mMetricsLogger.write(mLogMaker); 93 } 94 } 95 } 96