Home | History | Annotate | Download | only in logging
      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 
     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 
     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 
     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      */
     73     private static int getUid(Context context, ComponentName componentName, int userId) {
     74         int uid = -1;
     75         try {
     76             uid = context.getPackageManager().getApplicationInfoAsUser(
     77                     componentName.getPackageName(), 0, userId).uid;
     78         } catch (NameNotFoundException e) {
     79         }
     80         return uid;
     81     }
     82 
     83     public static void logPictureInPictureMenuVisible(Context context, boolean menuStateFull) {
     84         MetricsLogger.visibility(context, MetricsEvent.ACTION_PICTURE_IN_PICTURE_MENU,
     85                 menuStateFull);
     86     }
     87 
     88     public static void logPictureInPictureEnter(Context context,
     89             int uid, String shortComponentName, boolean supportsEnterPipOnTaskSwitch) {
     90         MetricsLogger.action(context, MetricsEvent.ACTION_PICTURE_IN_PICTURE_ENTERED,
     91                 supportsEnterPipOnTaskSwitch);
     92         StatsLog.write(StatsLog.PICTURE_IN_PICTURE_STATE_CHANGED, uid,
     93                 shortComponentName,
     94                 StatsLog.PICTURE_IN_PICTURE_STATE_CHANGED__STATE__ENTERED);
     95     }
     96 
     97     public static void logPictureInPictureFullScreen(Context context, int uid,
     98             String shortComponentName) {
     99         MetricsLogger.action(context,
    100                 MetricsEvent.ACTION_PICTURE_IN_PICTURE_EXPANDED_TO_FULLSCREEN);
    101         StatsLog.write(StatsLog.PICTURE_IN_PICTURE_STATE_CHANGED,
    102                 uid,
    103                 shortComponentName,
    104                 StatsLog.PICTURE_IN_PICTURE_STATE_CHANGED__STATE__EXPANDED_TO_FULL_SCREEN);
    105     }
    106 
    107     public static void logAppOverlayEnter(int uid, String packageName, boolean changed, int type, boolean usingAlertWindow) {
    108         if (changed) {
    109             if (type != LayoutParams.TYPE_APPLICATION_OVERLAY) {
    110                 StatsLog.write(StatsLog.OVERLAY_STATE_CHANGED, uid, packageName, true,
    111                         StatsLog.OVERLAY_STATE_CHANGED__STATE__ENTERED);
    112             } else if (!usingAlertWindow){
    113                 StatsLog.write(StatsLog.OVERLAY_STATE_CHANGED, uid, packageName, false,
    114                         StatsLog.OVERLAY_STATE_CHANGED__STATE__ENTERED);
    115             }
    116         }
    117     }
    118 
    119     public static void logAppOverlayExit(int uid, String packageName, boolean changed, int type, boolean usingAlertWindow) {
    120         if (changed) {
    121             if (type != LayoutParams.TYPE_APPLICATION_OVERLAY) {
    122                 StatsLog.write(StatsLog.OVERLAY_STATE_CHANGED, uid, packageName, true,
    123                         StatsLog.OVERLAY_STATE_CHANGED__STATE__EXITED);
    124             } else if (!usingAlertWindow){
    125                 StatsLog.write(StatsLog.OVERLAY_STATE_CHANGED, uid, packageName, false,
    126                         StatsLog.OVERLAY_STATE_CHANGED__STATE__EXITED);
    127             }
    128         }
    129     }
    130 }
    131