Bug 1301468 - Add activity stream session for new tab panel. r=ahunt
authorSebastian Kaspari <s.kaspari@gmail.com>
Wed, 23 Nov 2016 17:24:31 +0100
changeset 324543 bf33178d80fae1c9b09534c342f0cb37485a5c26
parent 324542 3a2e2cbe04c02d350eacf972a769607c9c09a6af
child 324544 2a80ee5b4cb1eaedeb9afe02dc84b001540150ae
push id24
push usermaklebus@msu.edu
push dateTue, 20 Dec 2016 03:11:33 +0000
reviewersahunt
bugs1301468
milestone53.0a1
Bug 1301468 - Add activity stream session for new tab panel. r=ahunt MozReview-Commit-ID: 45yMkb3bJfS
mobile/android/base/java/org/mozilla/gecko/TelemetryContract.java
mobile/android/base/java/org/mozilla/gecko/home/activitystream/ActivityStreamHomeFragment.java
--- a/mobile/android/base/java/org/mozilla/gecko/TelemetryContract.java
+++ b/mobile/android/base/java/org/mozilla/gecko/TelemetryContract.java
@@ -226,16 +226,20 @@ public interface TelemetryContract {
 
     /**
      * Holds session names. Intended for use with
      * Telemetry.startUISession() as the "sessionName" parameter.
      *
      * Please keep this list sorted.
      */
     public enum Session {
+        // Started whenever the activity stream panel is visible. Stopped as soon as the panel is
+        // not visible anymore.
+        ACTIVITY_STREAM("activitystream.1"),
+
         // Awesomescreen (including frecency search) is active.
         AWESOMESCREEN("awesomescreen.1"),
 
         // Used to tag experiments being run.
         EXPERIMENT("experiment.1"),
 
         // Started the very first time we believe the application has been launched.
         FIRSTRUN("firstrun.1"),
--- a/mobile/android/base/java/org/mozilla/gecko/home/activitystream/ActivityStreamHomeFragment.java
+++ b/mobile/android/base/java/org/mozilla/gecko/home/activitystream/ActivityStreamHomeFragment.java
@@ -6,34 +6,85 @@ package org.mozilla.gecko.home.activitys
 
 import android.os.Bundle;
 import android.support.annotation.Nullable;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 
 import org.mozilla.gecko.R;
+import org.mozilla.gecko.Telemetry;
+import org.mozilla.gecko.TelemetryContract;
 import org.mozilla.gecko.home.HomeFragment;
 
 /**
  * Simple wrapper around the ActivityStream view that allows embedding as a HomePager panel.
  */
 public class ActivityStreamHomeFragment
         extends HomeFragment {
     private ActivityStream activityStream;
 
+    private boolean isSessionActive;
+
     @Override
     protected void load() {
         activityStream.load(getLoaderManager());
     }
 
     @Nullable
     @Override
     public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                              @Nullable Bundle savedInstanceState) {
         if (activityStream == null) {
             activityStream = (ActivityStream) inflater.inflate(R.layout.activity_stream, container, false);
             activityStream.setOnUrlOpenListeners(mUrlOpenListener, mUrlOpenInBackgroundListener);
         }
 
         return activityStream;
     }
+
+    @Override
+    public void setUserVisibleHint(boolean isVisibleToUser) {
+        super.setUserVisibleHint(isVisibleToUser);
+
+        if (isVisibleToUser) {
+            sendPanelShownTelemetry();
+        } else {
+            sendPanelHiddenTelemetry();
+        }
+    }
+
+    @Override
+    public void onResume() {
+        super.onResume();
+
+        if (!isSessionActive && getUserVisibleHint()) {
+            // The activity is being resumed and we are showing the panel again: Start session.
+            sendPanelShownTelemetry();
+        }
+    }
+
+    @Override
+    public void onPause() {
+        super.onPause();
+
+        // User is navigating away from activity (to other app or settings). Stop the session.
+        sendPanelHiddenTelemetry();
+    }
+
+    private void sendPanelHiddenTelemetry() {
+        if (isSessionActive) {
+            Telemetry.sendUIEvent(TelemetryContract.Event.CANCEL, TelemetryContract.Method.PANEL, "as_newtab");
+
+            Telemetry.stopUISession(TelemetryContract.Session.ACTIVITY_STREAM, "newtab");
+
+            isSessionActive = false;
+        }
+    }
+
+    private void sendPanelShownTelemetry() {
+        Telemetry.startUISession(TelemetryContract.Session.ACTIVITY_STREAM, "newtab");
+
+        Telemetry.sendUIEvent(TelemetryContract.Event.SHOW, TelemetryContract.Method.PANEL, "as_newtab");
+
+        isSessionActive = true;
+    }
 }