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 com.android.internal.os.logging; 18 19 import android.content.ComponentName; 20 import android.content.Context; 21 import android.content.pm.PackageManager.NameNotFoundException; 22 import android.util.Pair; 23 import android.util.StatsLog; 24 import android.view.WindowManager.LayoutParams; 25 26 import com.android.internal.logging.MetricsLogger; 27 import com.android.internal.logging.nano.MetricsProto.MetricsEvent; 28 29 /** 30 * Used to wrap different logging calls in one, so that client side code base is clean and more 31 * readable. 32 */ 33 public class MetricsLoggerWrapper { 34 35 private static final int METRIC_VALUE_DISMISSED_BY_TAP = 0; 36 private static final int METRIC_VALUE_DISMISSED_BY_DRAG = 1; 37 logPictureInPictureDismissByTap(Context context, Pair<ComponentName, Integer> topActivityInfo)38 public static void logPictureInPictureDismissByTap(Context context, 39 Pair<ComponentName, Integer> topActivityInfo) { 40 MetricsLogger.action(context, MetricsEvent.ACTION_PICTURE_IN_PICTURE_DISMISSED, 41 METRIC_VALUE_DISMISSED_BY_TAP); 42 StatsLog.write(StatsLog.PICTURE_IN_PICTURE_STATE_CHANGED, 43 getUid(context, topActivityInfo.first, topActivityInfo.second), 44 topActivityInfo.first.flattenToString(), 45 StatsLog.PICTURE_IN_PICTURE_STATE_CHANGED__STATE__DISMISSED); 46 } 47 logPictureInPictureDismissByDrag(Context context, Pair<ComponentName, Integer> topActivityInfo)48 public static void logPictureInPictureDismissByDrag(Context context, 49 Pair<ComponentName, Integer> topActivityInfo) { 50 MetricsLogger.action(context, 51 MetricsEvent.ACTION_PICTURE_IN_PICTURE_DISMISSED, 52 METRIC_VALUE_DISMISSED_BY_DRAG); 53 StatsLog.write(StatsLog.PICTURE_IN_PICTURE_STATE_CHANGED, 54 getUid(context, topActivityInfo.first, topActivityInfo.second), 55 topActivityInfo.first.flattenToString(), 56 StatsLog.PICTURE_IN_PICTURE_STATE_CHANGED__STATE__DISMISSED); 57 } 58 logPictureInPictureMinimize(Context context, boolean isMinimized, Pair<ComponentName, Integer> topActivityInfo)59 public static void logPictureInPictureMinimize(Context context, boolean isMinimized, 60 Pair<ComponentName, Integer> topActivityInfo) { 61 MetricsLogger.action(context, MetricsEvent.ACTION_PICTURE_IN_PICTURE_MINIMIZED, 62 isMinimized); 63 StatsLog.write(StatsLog.PICTURE_IN_PICTURE_STATE_CHANGED, 64 getUid(context, topActivityInfo.first, topActivityInfo.second), 65 topActivityInfo.first.flattenToString(), 66 StatsLog.PICTURE_IN_PICTURE_STATE_CHANGED__STATE__MINIMIZED); 67 } 68 69 /** 70 * Get uid from component name and user Id 71 * @return uid. -1 if not found. 72 */ getUid(Context context, ComponentName componentName, int userId)73 private static int getUid(Context context, ComponentName componentName, int userId) { 74 int uid = -1; 75 if (componentName == null) { 76 return uid; 77 } 78 try { 79 uid = context.getPackageManager().getApplicationInfoAsUser( 80 componentName.getPackageName(), 0, userId).uid; 81 } catch (NameNotFoundException e) { 82 } 83 return uid; 84 } 85 logPictureInPictureMenuVisible(Context context, boolean menuStateFull)86 public static void logPictureInPictureMenuVisible(Context context, boolean menuStateFull) { 87 MetricsLogger.visibility(context, MetricsEvent.ACTION_PICTURE_IN_PICTURE_MENU, 88 menuStateFull); 89 } 90 logPictureInPictureEnter(Context context, int uid, String shortComponentName, boolean supportsEnterPipOnTaskSwitch)91 public static void logPictureInPictureEnter(Context context, 92 int uid, String shortComponentName, boolean supportsEnterPipOnTaskSwitch) { 93 MetricsLogger.action(context, MetricsEvent.ACTION_PICTURE_IN_PICTURE_ENTERED, 94 supportsEnterPipOnTaskSwitch); 95 StatsLog.write(StatsLog.PICTURE_IN_PICTURE_STATE_CHANGED, uid, 96 shortComponentName, 97 StatsLog.PICTURE_IN_PICTURE_STATE_CHANGED__STATE__ENTERED); 98 } 99 logPictureInPictureFullScreen(Context context, int uid, String shortComponentName)100 public static void logPictureInPictureFullScreen(Context context, int uid, 101 String shortComponentName) { 102 MetricsLogger.action(context, 103 MetricsEvent.ACTION_PICTURE_IN_PICTURE_EXPANDED_TO_FULLSCREEN); 104 StatsLog.write(StatsLog.PICTURE_IN_PICTURE_STATE_CHANGED, 105 uid, 106 shortComponentName, 107 StatsLog.PICTURE_IN_PICTURE_STATE_CHANGED__STATE__EXPANDED_TO_FULL_SCREEN); 108 } 109 logAppOverlayEnter(int uid, String packageName, boolean changed, int type, boolean usingAlertWindow)110 public static void logAppOverlayEnter(int uid, String packageName, boolean changed, int type, boolean usingAlertWindow) { 111 if (changed) { 112 if (type != LayoutParams.TYPE_APPLICATION_OVERLAY) { 113 StatsLog.write(StatsLog.OVERLAY_STATE_CHANGED, uid, packageName, true, 114 StatsLog.OVERLAY_STATE_CHANGED__STATE__ENTERED); 115 } else if (!usingAlertWindow){ 116 StatsLog.write(StatsLog.OVERLAY_STATE_CHANGED, uid, packageName, false, 117 StatsLog.OVERLAY_STATE_CHANGED__STATE__ENTERED); 118 } 119 } 120 } 121 logAppOverlayExit(int uid, String packageName, boolean changed, int type, boolean usingAlertWindow)122 public static void logAppOverlayExit(int uid, String packageName, boolean changed, int type, boolean usingAlertWindow) { 123 if (changed) { 124 if (type != LayoutParams.TYPE_APPLICATION_OVERLAY) { 125 StatsLog.write(StatsLog.OVERLAY_STATE_CHANGED, uid, packageName, true, 126 StatsLog.OVERLAY_STATE_CHANGED__STATE__EXITED); 127 } else if (!usingAlertWindow){ 128 StatsLog.write(StatsLog.OVERLAY_STATE_CHANGED, uid, packageName, false, 129 StatsLog.OVERLAY_STATE_CHANGED__STATE__EXITED); 130 } 131 } 132 } 133 } 134