LiveTv TIAS Integration Start and stop TIAS

Currently, the LiveTv is able to receive AitInfoUpdated
notifications from the TIS. This CL has the LiveTv actually
start the TIAS once it receives these notifications, and
stop it once the LiveTv app is stopped.

Bug: 241110408
Test: Manually on Cuttlefish. Should see TIAS start.
Change-Id: I12aa5973427a2ae1aa817d14f9c5d6e38f52ab48
diff --git a/src/com/android/tv/MainActivity.java b/src/com/android/tv/MainActivity.java
index 35a5550..8bfa05b 100644
--- a/src/com/android/tv/MainActivity.java
+++ b/src/com/android/tv/MainActivity.java
@@ -738,7 +738,7 @@
         }
         initForTest();
         if (TvFeatures.HAS_TIAF.isEnabled(this)) {
-            mIAppManager = new IAppManager(this);
+            mIAppManager = new IAppManager(this, mTvView);
         }
         Debug.getTimer(Debug.TAG_START_UP_TIMER).log("MainActivity.onCreate end");
     }
@@ -1134,6 +1134,9 @@
     private void stopAll(boolean keepVisibleBehind) {
         mOverlayManager.hideOverlays(TvOverlayManager.FLAG_HIDE_OVERLAYS_WITHOUT_ANIMATION);
         stopTv("stopAll()", keepVisibleBehind);
+        if (mIAppManager != null) {
+            mIAppManager.stop();
+        }
     }
 
     public TvInputManagerHelper getTvInputManagerHelper() {
@@ -3001,7 +3004,9 @@
         @TargetApi(Build.VERSION_CODES.TIRAMISU)
         @Override
         public void onAitInfoUpdated(String inputId, AitInfo aitInfo) {
-            // TODO: Begin the Interactive App
+            if (mIAppManager != null) {
+                mIAppManager.onAitInfoUpdated(aitInfo);
+            }
         }
     }
 
diff --git a/src/com/android/tv/interactive/IAppManager.java b/src/com/android/tv/interactive/IAppManager.java
index b3061bc..7d8069c 100644
--- a/src/com/android/tv/interactive/IAppManager.java
+++ b/src/com/android/tv/interactive/IAppManager.java
@@ -19,32 +19,41 @@
 import android.annotation.TargetApi;
 import android.graphics.Rect;
 import android.media.tv.interactive.TvInteractiveAppManager;
+import android.media.tv.AitInfo;
+import android.media.tv.interactive.TvInteractiveAppServiceInfo;
 import android.media.tv.interactive.TvInteractiveAppView;
 import android.net.Uri;
 import android.os.Build;
 import android.os.Bundle;
 import android.util.Log;
+import android.view.View;
 
 import com.android.tv.MainActivity;
 import com.android.tv.R;
 import com.android.tv.common.SoftPreconditions;
 import com.android.tv.features.TvFeatures;
+import com.android.tv.ui.TunableTvView;
 
+import java.util.List;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 
 @TargetApi(Build.VERSION_CODES.TIRAMISU)
 public class IAppManager {
     private static final String TAG = "IAppManager";
+    private static final boolean DEBUG = false;
 
     private final MainActivity mMainActivity;
     private final TvInteractiveAppManager mTvIAppManager;
     private final TvInteractiveAppView mTvIAppView;
+    private final TunableTvView mTvView;
+    private AitInfo mCurrentAitInfo;
 
-    public IAppManager(MainActivity parentActivity) {
+    public IAppManager(MainActivity parentActivity, TunableTvView tvView) {
         SoftPreconditions.checkFeatureEnabled(parentActivity, TvFeatures.HAS_TIAF, TAG);
 
         mMainActivity = parentActivity;
+        mTvView = tvView;
         mTvIAppManager = mMainActivity.getSystemService(TvInteractiveAppManager.class);
         mTvIAppView = mMainActivity.findViewById(R.id.tv_app_view);
         if (mTvIAppManager == null || mTvIAppView == null) {
@@ -63,6 +72,65 @@
         );
     }
 
+    public void stop() {
+        mTvIAppView.stopInteractiveApp();
+        mTvIAppView.reset();
+        mCurrentAitInfo = null;
+    }
+
+    public void onAitInfoUpdated(AitInfo aitInfo) {
+        if (mTvIAppManager == null || aitInfo == null) {
+            return;
+        }
+        if (mCurrentAitInfo != null && mCurrentAitInfo.getType() == aitInfo.getType()) {
+            if (DEBUG) {
+                Log.d(TAG, "Ignoring AIT update: Same type as current");
+            }
+            return;
+        }
+
+        List<TvInteractiveAppServiceInfo> tvIAppInfoList =
+                mTvIAppManager.getTvInteractiveAppServiceList();
+        if (tvIAppInfoList.isEmpty()) {
+            if (DEBUG) {
+                Log.d(TAG, "Ignoring AIT update: No interactive app services registered");
+            }
+            return;
+        }
+
+        // App Type ID numbers allocated by DVB Services
+        int type = -1;
+        switch (aitInfo.getType()) {
+            case 0x0010: // HBBTV
+                type = TvInteractiveAppServiceInfo.INTERACTIVE_APP_TYPE_HBBTV;
+                break;
+            case 0x0006: // DCAP-J: DCAP Java applications
+            case 0x0007: // DCAP-X: DCAP XHTML applications
+                type = TvInteractiveAppServiceInfo.INTERACTIVE_APP_TYPE_ATSC;
+                break;
+            case 0x0001: // Ginga-J
+            case 0x0009: // Ginga-NCL
+            case 0x000b: // Ginga-HTML5
+                type = TvInteractiveAppServiceInfo.INTERACTIVE_APP_TYPE_GINGA;
+                break;
+            default:
+                Log.e(TAG, "AIT info contained unknown type: " + aitInfo.getType());
+                return;
+        }
+
+        // TODO: Only open interactive app if enabled through settings
+        for (TvInteractiveAppServiceInfo info : tvIAppInfoList) {
+            if ((info.getSupportedTypes() & type) > 0) {
+                mCurrentAitInfo = aitInfo;
+                if (mTvIAppView != null) {
+                    mTvIAppView.setVisibility(View.VISIBLE);
+                    mTvIAppView.prepareInteractiveApp(info.getId(), type);
+                }
+                break;
+            }
+        }
+    }
+
     private class MyInteractiveAppManagerCallback extends
             TvInteractiveAppManager.TvInteractiveAppCallback {
         @Override
@@ -76,7 +144,15 @@
 
         @Override
         public void onTvInteractiveAppServiceStateChanged(String iAppServiceId, int type, int state,
-                int err) {}
+                int err) {
+            if (state == TvInteractiveAppManager.SERVICE_STATE_READY && mTvIAppView != null) {
+                mTvIAppView.startInteractiveApp();
+                mTvIAppView.setTvView(mTvView.getTvView());
+                if (mTvView.getTvView() != null) {
+                    mTvView.getTvView().setInteractiveAppNotificationEnabled(true);
+                }
+            }
+        }
     }
 
     private class MyInteractiveAppViewCallback extends