Replace legacy getStats with standard getStats in the Android example

Bug: webrtc:12688
Change-Id: I7e2e10ab1b1ce994bbfbcfad377a77b39239d3d0
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/221760
Reviewed-by: Xavier Lepaul‎ <xalep@webrtc.org>
Commit-Queue: Xavier Lepaul‎ <xalep@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#34870}
diff --git a/examples/androidapp/res/layout/fragment_hud.xml b/examples/androidapp/res/layout/fragment_hud.xml
index 20d2654..483e7ba 100644
--- a/examples/androidapp/res/layout/fragment_hud.xml
+++ b/examples/androidapp/res/layout/fragment_hud.xml
@@ -15,7 +15,7 @@
         android:layout_height="48dp"/>
 
     <TextView
-        android:id="@+id/encoder_stat_call"
+        android:id="@+id/hud_stat_call"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignParentEnd="true"
@@ -24,51 +24,4 @@
         android:textSize="12sp"
         android:layout_margin="8dp"/>
 
-    <TableLayout
-           android:id="@+id/hudview_container"
-           android:layout_width="match_parent"
-           android:layout_height="match_parent">
-
-           <TableRow>
-              <TextView
-                 android:id="@+id/hud_stat_bwe"
-                 android:layout_width="wrap_content"
-                 android:layout_height="wrap_content"
-                 android:alpha="0.4"
-                 android:padding="2dip"
-                 android:background="@android:color/white"
-                 android:textColor="@android:color/black" />
-
-              <TextView
-                 android:id="@+id/hud_stat_connection"
-                 android:layout_width="wrap_content"
-                 android:layout_height="wrap_content"
-                 android:alpha="0.4"
-                 android:padding="2dip"
-                 android:background="@android:color/white"
-                 android:textColor="@android:color/black" />
-
-           </TableRow>
-
-           <TableRow>
-              <TextView
-                 android:id="@+id/hud_stat_video_send"
-                 android:layout_width="wrap_content"
-                 android:layout_height="wrap_content"
-                 android:alpha="0.4"
-                 android:padding="2dip"
-                 android:background="@android:color/white"
-                 android:textColor="@android:color/black" />
-
-              <TextView
-                 android:id="@+id/hud_stat_video_recv"
-                 android:layout_width="wrap_content"
-                 android:layout_height="wrap_content"
-                 android:padding="2dip"
-                 android:alpha="0.4"
-                 android:background="@android:color/white"
-                 android:textColor="@android:color/black" />
-            </TableRow>
-    </TableLayout>
-
 </RelativeLayout>
diff --git a/examples/androidapp/src/org/appspot/apprtc/CallActivity.java b/examples/androidapp/src/org/appspot/apprtc/CallActivity.java
index 5d9d394..2da2073 100644
--- a/examples/androidapp/src/org/appspot/apprtc/CallActivity.java
+++ b/examples/androidapp/src/org/appspot/apprtc/CallActivity.java
@@ -51,10 +51,10 @@
 import org.webrtc.IceCandidate;
 import org.webrtc.Logging;
 import org.webrtc.PeerConnectionFactory;
+import org.webrtc.RTCStatsReport;
 import org.webrtc.RendererCommon.ScalingType;
 import org.webrtc.ScreenCapturerAndroid;
 import org.webrtc.SessionDescription;
-import org.webrtc.StatsReport;
 import org.webrtc.SurfaceViewRenderer;
 import org.webrtc.VideoCapturer;
 import org.webrtc.VideoFileRenderer;
@@ -951,12 +951,12 @@
   public void onPeerConnectionClosed() {}
 
   @Override
-  public void onPeerConnectionStatsReady(final StatsReport[] reports) {
+  public void onPeerConnectionStatsReady(final RTCStatsReport report) {
     runOnUiThread(new Runnable() {
       @Override
       public void run() {
         if (!isError && connected) {
-          hudFragment.updateEncoderStatistics(reports);
+          hudFragment.updateEncoderStatistics(report);
         }
       }
     });
diff --git a/examples/androidapp/src/org/appspot/apprtc/HudFragment.java b/examples/androidapp/src/org/appspot/apprtc/HudFragment.java
index ad6b37d..94ca055 100644
--- a/examples/androidapp/src/org/appspot/apprtc/HudFragment.java
+++ b/examples/androidapp/src/org/appspot/apprtc/HudFragment.java
@@ -12,29 +12,20 @@
 
 import android.app.Fragment;
 import android.os.Bundle;
-import android.util.TypedValue;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.ImageButton;
 import android.widget.TextView;
-
-import org.webrtc.StatsReport;
-
-import java.util.HashMap;
-import java.util.Map;
+import org.webrtc.RTCStats;
+import org.webrtc.RTCStatsReport;
 
 /**
  * Fragment for HUD statistics display.
  */
 public class HudFragment extends Fragment {
-  private TextView encoderStatView;
-  private TextView hudViewBwe;
-  private TextView hudViewConnection;
-  private TextView hudViewVideoSend;
-  private TextView hudViewVideoRecv;
+  private TextView statView;
   private ImageButton toggleDebugButton;
-  private boolean videoCallEnabled;
   private boolean displayHud;
   private volatile boolean isRunning;
   private CpuMonitor cpuMonitor;
@@ -45,20 +36,15 @@
     View controlView = inflater.inflate(R.layout.fragment_hud, container, false);
 
     // Create UI controls.
-    encoderStatView = controlView.findViewById(R.id.encoder_stat_call);
-    hudViewBwe = controlView.findViewById(R.id.hud_stat_bwe);
-    hudViewConnection = controlView.findViewById(R.id.hud_stat_connection);
-    hudViewVideoSend = controlView.findViewById(R.id.hud_stat_video_send);
-    hudViewVideoRecv = controlView.findViewById(R.id.hud_stat_video_recv);
+    statView = controlView.findViewById(R.id.hud_stat_call);
     toggleDebugButton = controlView.findViewById(R.id.button_toggle_debug);
 
     toggleDebugButton.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View view) {
         if (displayHud) {
-          int visibility =
-              (hudViewBwe.getVisibility() == View.VISIBLE) ? View.INVISIBLE : View.VISIBLE;
-          hudViewsSetProperties(visibility);
+          statView.setVisibility(
+              statView.getVisibility() == View.VISIBLE ? View.INVISIBLE : View.VISIBLE);
         }
       }
     });
@@ -72,13 +58,11 @@
 
     Bundle args = getArguments();
     if (args != null) {
-      videoCallEnabled = args.getBoolean(CallActivity.EXTRA_VIDEO_CALL, true);
       displayHud = args.getBoolean(CallActivity.EXTRA_DISPLAY_HUD, false);
     }
     int visibility = displayHud ? View.VISIBLE : View.INVISIBLE;
-    encoderStatView.setVisibility(visibility);
+    statView.setVisibility(View.INVISIBLE);
     toggleDebugButton.setVisibility(visibility);
-    hudViewsSetProperties(View.INVISIBLE);
     isRunning = true;
   }
 
@@ -92,113 +76,27 @@
     this.cpuMonitor = cpuMonitor;
   }
 
-  private void hudViewsSetProperties(int visibility) {
-    hudViewBwe.setVisibility(visibility);
-    hudViewConnection.setVisibility(visibility);
-    hudViewVideoSend.setVisibility(visibility);
-    hudViewVideoRecv.setVisibility(visibility);
-    hudViewBwe.setTextSize(TypedValue.COMPLEX_UNIT_PT, 5);
-    hudViewConnection.setTextSize(TypedValue.COMPLEX_UNIT_PT, 5);
-    hudViewVideoSend.setTextSize(TypedValue.COMPLEX_UNIT_PT, 5);
-    hudViewVideoRecv.setTextSize(TypedValue.COMPLEX_UNIT_PT, 5);
-  }
-
-  private Map<String, String> getReportMap(StatsReport report) {
-    Map<String, String> reportMap = new HashMap<>();
-    for (StatsReport.Value value : report.values) {
-      reportMap.put(value.name, value.value);
-    }
-    return reportMap;
-  }
-
-  public void updateEncoderStatistics(final StatsReport[] reports) {
+  public void updateEncoderStatistics(final RTCStatsReport report) {
     if (!isRunning || !displayHud) {
       return;
     }
-    StringBuilder encoderStat = new StringBuilder(128);
-    StringBuilder bweStat = new StringBuilder();
-    StringBuilder connectionStat = new StringBuilder();
-    StringBuilder videoSendStat = new StringBuilder();
-    StringBuilder videoRecvStat = new StringBuilder();
-    String fps = null;
-    String targetBitrate = null;
-    String actualBitrate = null;
 
-    for (StatsReport report : reports) {
-      if (report.type.equals("ssrc") && report.id.contains("ssrc") && report.id.contains("send")) {
-        // Send video statistics.
-        Map<String, String> reportMap = getReportMap(report);
-        String trackId = reportMap.get("googTrackId");
-        if (trackId != null && trackId.contains(PeerConnectionClient.VIDEO_TRACK_ID)) {
-          fps = reportMap.get("googFrameRateSent");
-          videoSendStat.append(report.id).append("\n");
-          for (StatsReport.Value value : report.values) {
-            String name = value.name.replace("goog", "");
-            videoSendStat.append(name).append("=").append(value.value).append("\n");
-          }
-        }
-      } else if (report.type.equals("ssrc") && report.id.contains("ssrc")
-          && report.id.contains("recv")) {
-        // Receive video statistics.
-        Map<String, String> reportMap = getReportMap(report);
-        // Check if this stat is for video track.
-        String frameWidth = reportMap.get("googFrameWidthReceived");
-        if (frameWidth != null) {
-          videoRecvStat.append(report.id).append("\n");
-          for (StatsReport.Value value : report.values) {
-            String name = value.name.replace("goog", "");
-            videoRecvStat.append(name).append("=").append(value.value).append("\n");
-          }
-        }
-      } else if (report.id.equals("bweforvideo")) {
-        // BWE statistics.
-        Map<String, String> reportMap = getReportMap(report);
-        targetBitrate = reportMap.get("googTargetEncBitrate");
-        actualBitrate = reportMap.get("googActualEncBitrate");
-
-        bweStat.append(report.id).append("\n");
-        for (StatsReport.Value value : report.values) {
-          String name = value.name.replace("goog", "").replace("Available", "");
-          bweStat.append(name).append("=").append(value.value).append("\n");
-        }
-      } else if (report.type.equals("googCandidatePair")) {
-        // Connection statistics.
-        Map<String, String> reportMap = getReportMap(report);
-        String activeConnection = reportMap.get("googActiveConnection");
-        if (activeConnection != null && activeConnection.equals("true")) {
-          connectionStat.append(report.id).append("\n");
-          for (StatsReport.Value value : report.values) {
-            String name = value.name.replace("goog", "");
-            connectionStat.append(name).append("=").append(value.value).append("\n");
-          }
-        }
-      }
-    }
-    hudViewBwe.setText(bweStat.toString());
-    hudViewConnection.setText(connectionStat.toString());
-    hudViewVideoSend.setText(videoSendStat.toString());
-    hudViewVideoRecv.setText(videoRecvStat.toString());
-
-    if (videoCallEnabled) {
-      if (fps != null) {
-        encoderStat.append("Fps:  ").append(fps).append("\n");
-      }
-      if (targetBitrate != null) {
-        encoderStat.append("Target BR: ").append(targetBitrate).append("\n");
-      }
-      if (actualBitrate != null) {
-        encoderStat.append("Actual BR: ").append(actualBitrate).append("\n");
-      }
-    }
+    StringBuilder sb = new StringBuilder();
 
     if (cpuMonitor != null) {
-      encoderStat.append("CPU%: ")
+      sb.append("CPU%: ")
           .append(cpuMonitor.getCpuUsageCurrent())
           .append("/")
           .append(cpuMonitor.getCpuUsageAverage())
           .append(". Freq: ")
-          .append(cpuMonitor.getFrequencyScaleAverage());
+          .append(cpuMonitor.getFrequencyScaleAverage())
+          .append("\n");
     }
-    encoderStatView.setText(encoderStat.toString());
+
+    for (RTCStats stat : report.getStatsMap().values()) {
+      sb.append(stat.toString()).append("\n");
+    }
+
+    statView.setText(sb.toString());
   }
 }
diff --git a/examples/androidapp/src/org/appspot/apprtc/PeerConnectionClient.java b/examples/androidapp/src/org/appspot/apprtc/PeerConnectionClient.java
index d027f97..c5e9059 100644
--- a/examples/androidapp/src/org/appspot/apprtc/PeerConnectionClient.java
+++ b/examples/androidapp/src/org/appspot/apprtc/PeerConnectionClient.java
@@ -54,6 +54,8 @@
 import org.webrtc.PeerConnection.IceConnectionState;
 import org.webrtc.PeerConnection.PeerConnectionState;
 import org.webrtc.PeerConnectionFactory;
+import org.webrtc.RTCStatsCollectorCallback;
+import org.webrtc.RTCStatsReport;
 import org.webrtc.RtpParameters;
 import org.webrtc.RtpReceiver;
 import org.webrtc.RtpSender;
@@ -62,8 +64,6 @@
 import org.webrtc.SessionDescription;
 import org.webrtc.SoftwareVideoDecoderFactory;
 import org.webrtc.SoftwareVideoEncoderFactory;
-import org.webrtc.StatsObserver;
-import org.webrtc.StatsReport;
 import org.webrtc.SurfaceTextureHelper;
 import org.webrtc.VideoCapturer;
 import org.webrtc.VideoDecoderFactory;
@@ -312,7 +312,7 @@
     /**
      * Callback fired once peer connection statistics is ready.
      */
-    void onPeerConnectionStatsReady(final StatsReport[] reports);
+    void onPeerConnectionStatsReady(final RTCStatsReport report);
 
     /**
      * Callback fired once peer connection error happened.
@@ -747,20 +747,16 @@
     return isVideoCallEnabled() && videoWidth * videoHeight >= 1280 * 720;
   }
 
-  @SuppressWarnings("deprecation") // TODO(sakal): getStats is deprecated.
   private void getStats() {
     if (peerConnection == null || isError) {
       return;
     }
-    boolean success = peerConnection.getStats(new StatsObserver() {
+    peerConnection.getStats(new RTCStatsCollectorCallback() {
       @Override
-      public void onComplete(final StatsReport[] reports) {
-        events.onPeerConnectionStatsReady(reports);
+      public void onStatsDelivered(RTCStatsReport report) {
+        events.onPeerConnectionStatsReady(report);
       }
-    }, null);
-    if (!success) {
-      Log.e(TAG, "getStats() returns false!");
-    }
+    });
   }
 
   public void enableStatsEvents(boolean enable, int periodMs) {
diff --git a/examples/androidtests/src/org/appspot/apprtc/test/PeerConnectionClientTest.java b/examples/androidtests/src/org/appspot/apprtc/test/PeerConnectionClientTest.java
index 63cea59..ac3fb23 100644
--- a/examples/androidtests/src/org/appspot/apprtc/test/PeerConnectionClientTest.java
+++ b/examples/androidtests/src/org/appspot/apprtc/test/PeerConnectionClientTest.java
@@ -39,8 +39,8 @@
 import org.webrtc.IceCandidate;
 import org.webrtc.PeerConnection;
 import org.webrtc.PeerConnectionFactory;
+import org.webrtc.RTCStatsReport;
 import org.webrtc.SessionDescription;
-import org.webrtc.StatsReport;
 import org.webrtc.VideoCapturer;
 import org.webrtc.VideoFrame;
 import org.webrtc.VideoSink;
@@ -208,7 +208,7 @@
   }
 
   @Override
-  public void onPeerConnectionStatsReady(StatsReport[] reports) {}
+  public void onPeerConnectionStatsReady(final RTCStatsReport report) {}
 
   // Helper wait functions.
   private boolean waitForLocalDescription(int timeoutMs) throws InterruptedException {