Home | History | Annotate | Download | only in instrumentation
      1 /*
      2  * Copyright (C) 2016 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.settings.core.instrumentation;
     18 
     19 import android.app.Activity;
     20 import android.content.Context;
     21 import android.content.Intent;
     22 
     23 import com.android.internal.logging.nano.MetricsProto;
     24 import com.android.settings.SettingsActivity;
     25 import com.android.settings.overlay.FeatureFactory;
     26 import com.android.settingslib.core.lifecycle.LifecycleObserver;
     27 import com.android.settingslib.core.lifecycle.events.OnAttach;
     28 import com.android.settingslib.core.lifecycle.events.OnPause;
     29 import com.android.settingslib.core.lifecycle.events.OnResume;
     30 
     31 import static com.android.settings.core.instrumentation.Instrumentable.METRICS_CATEGORY_UNKNOWN;
     32 
     33 /**
     34  * Logs visibility change of a fragment.
     35  */
     36 public class VisibilityLoggerMixin implements LifecycleObserver, OnResume, OnPause, OnAttach {
     37 
     38     private static final String TAG = "VisibilityLoggerMixin";
     39 
     40     private final int mMetricsCategory;
     41 
     42     private MetricsFeatureProvider mMetricsFeature;
     43     private int mSourceMetricsCategory = MetricsProto.MetricsEvent.VIEW_UNKNOWN;
     44 
     45     public VisibilityLoggerMixin(int metricsCategory) {
     46         // MetricsFeature will be set during onAttach.
     47         this(metricsCategory, null /* metricsFeature */);
     48     }
     49 
     50     public VisibilityLoggerMixin(int metricsCategory, MetricsFeatureProvider metricsFeature) {
     51         mMetricsCategory = metricsCategory;
     52         mMetricsFeature = metricsFeature;
     53     }
     54 
     55     @Override
     56     public void onAttach(Context context) {
     57         mMetricsFeature = FeatureFactory.getFactory(context).getMetricsFeatureProvider();
     58     }
     59 
     60     @Override
     61     public void onResume() {
     62         if (mMetricsFeature != null && mMetricsCategory != METRICS_CATEGORY_UNKNOWN) {
     63             mMetricsFeature.visible(null /* context */, mSourceMetricsCategory, mMetricsCategory);
     64         }
     65     }
     66 
     67     @Override
     68     public void onPause() {
     69         if (mMetricsFeature != null && mMetricsCategory != METRICS_CATEGORY_UNKNOWN) {
     70             mMetricsFeature.hidden(null /* context */, mMetricsCategory);
     71         }
     72     }
     73 
     74     /**
     75      * Sets source metrics category for this logger. Source is the caller that opened this UI.
     76      */
     77     public void setSourceMetricsCategory(Activity activity) {
     78         if (mSourceMetricsCategory != MetricsProto.MetricsEvent.VIEW_UNKNOWN || activity == null) {
     79             return;
     80         }
     81         final Intent intent = activity.getIntent();
     82         if (intent == null) {
     83             return;
     84         }
     85         mSourceMetricsCategory = intent.getIntExtra(SettingsActivity.EXTRA_SOURCE_METRICS_CATEGORY,
     86                 MetricsProto.MetricsEvent.VIEW_UNKNOWN);
     87     }
     88 }
     89