Sample TIS with Tuner APIs. am: 1c8ef0a170 am: 2391dcef94

Original change: https://googleplex-android-review.googlesource.com/c/platform/packages/apps/TV/+/11721763

Change-Id: I4dfc3f9fe2280debc8a5cbc1b51200c54dead648
diff --git a/tuner/sampletunertvinput/Android.bp b/tuner/sampletunertvinput/Android.bp
new file mode 100644
index 0000000..9d737c8
--- /dev/null
+++ b/tuner/sampletunertvinput/Android.bp
@@ -0,0 +1,62 @@
+//
+// Copyright (C) 2019 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+android_app {
+    name: "sampletunertvinput",
+    srcs: ["src/**/*.java"],
+
+    optimize: {
+        enabled: false,
+    },
+
+    platform_apis: true,
+    system_ext_specific: true,
+
+
+    privileged: true,
+    certificate: "platform",
+    // product_specific: true,
+    // sdk_version: "system_current",
+    resource_dirs: ["res"],
+    static_libs: [
+        "android-support-annotations",
+        "android-support-compat",
+        "android-support-core-ui",
+        "android-support-v7-palette",
+        "android-support-v7-recyclerview",
+        "androidx.leanback_leanback",
+        "androidx.tvprovider_tvprovider",
+        "jsr330",
+        "live-tv-tuner-proto",
+        "tv-auto-value-jar",
+        "tv-auto-factory-jar",
+        "tv-common",
+        "tv-error-prone-annotations-jar",
+        "tv-guava-android-jar",
+        "tv-javax-annotations-jar",
+        "tv-lib-dagger",
+        "tv-lib-exoplayer",
+        "tv-lib-exoplayer-v2-core",
+        "tv-lib-dagger-android",
+        "tv-test-common",
+    ],
+    aaptflags: ["-0 .ts"],
+    plugins: [
+        "tv-auto-value",
+        "tv-auto-factory",
+    ],
+    // min_sdk_version: "29",
+}
diff --git a/tuner/sampletunertvinput/AndroidManifest.xml b/tuner/sampletunertvinput/AndroidManifest.xml
new file mode 100644
index 0000000..d282889
--- /dev/null
+++ b/tuner/sampletunertvinput/AndroidManifest.xml
@@ -0,0 +1,62 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+  ~ Copyright (C) 2019 The Android Open Source Project
+  ~
+  ~ Licensed under the Apache License, Version 2.0 (the "License");
+  ~ you may not use this file except in compliance with the License.
+  ~ You may obtain a copy of the License at
+  ~
+  ~      http://www.apache.org/licenses/LICENSE-2.0
+  ~
+  ~ Unless required by applicable law or agreed to in writing, software
+  ~ distributed under the License is distributed on an "AS IS" BASIS,
+  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  ~ See the License for the specific language governing permissions and
+  ~ limitations under the License.
+  -->
+
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:tools="http://schemas.android.com/tools"
+    package="com.android.tv.samples.sampletunertvinput"
+    android:sharedUserId="android.uid.system"
+    coreApp="true">
+
+  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
+  <uses-permission android:name="android.permission.INTERNET"/>
+  <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
+  <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
+  <uses-permission android:name="android.permission.ACCESS_TV_DESCRAMBLER" />
+  <uses-permission android:name="android.permission.ACCESS_TV_TUNER" />
+  <uses-permission android:name="android.permission.TUNER_RESOURCE_ACCESS" />
+  <uses-permission android:name="com.android.providers.tv.permission.READ_EPG_DATA" />
+  <uses-permission android:name="com.android.providers.tv.permission.WRITE_EPG_DATA" />
+  <uses-permission android:name="com.android.tv.permission.RECEIVE_INPUT_EVENT" />
+
+  <uses-feature android:name="android.software.leanback" android:required="true" />
+  <uses-feature android:name="android.software.live_tv" android:required="true" />
+
+  <uses-sdk android:targetSdkVersion="29" android:minSdkVersion="29"
+      tools:overrideLibrary="com.android.tv.testing"/>
+  <application android:label="@string/sample_tuner_tv_input"
+      tools:replace="android:label,icon,theme,appComponentFactory"
+      android:icon="@mipmap/ic_launcher"
+      android:theme="@android:style/Theme.Holo.Light.NoActionBar"
+      android:appComponentFactory="android.support.v4.app.CoreComponentFactory" >
+    <uses-library android:name="com.android.libraries.tv.tvsystem" android:required="false" />
+    <activity android:name=".SampleTunerTvInputSetupActivity" >
+      <intent-filter>
+        <action android:name="android.intent.action.MAIN" />
+      </intent-filter>
+    </activity>
+    <service android:name=".SampleTunerTvInputService"
+        android:permission="android.permission.BIND_TV_INPUT"
+        android:label="@string/sample_tuner_tv_input"
+        android:process="com.android.tv.samples.sampletunertvinput">
+      <intent-filter>
+        <action android:name="android.media.tv.TvInputService" />
+      </intent-filter>
+      <meta-data android:name="android.media.tv.input"
+          android:resource="@xml/sampletunertvinputservice" />
+    </service>
+  </application>
+</manifest>
diff --git a/tuner/sampletunertvinput/res/mipmap-hdpi/ic_launcher.png b/tuner/sampletunertvinput/res/mipmap-hdpi/ic_launcher.png
new file mode 100644
index 0000000..454c515
--- /dev/null
+++ b/tuner/sampletunertvinput/res/mipmap-hdpi/ic_launcher.png
Binary files differ
diff --git a/tuner/sampletunertvinput/res/mipmap-mdpi/ic_launcher.png b/tuner/sampletunertvinput/res/mipmap-mdpi/ic_launcher.png
new file mode 100644
index 0000000..5e53eb5
--- /dev/null
+++ b/tuner/sampletunertvinput/res/mipmap-mdpi/ic_launcher.png
Binary files differ
diff --git a/tuner/sampletunertvinput/res/mipmap-xhdpi/ic_launcher.png b/tuner/sampletunertvinput/res/mipmap-xhdpi/ic_launcher.png
new file mode 100644
index 0000000..898bac4
--- /dev/null
+++ b/tuner/sampletunertvinput/res/mipmap-xhdpi/ic_launcher.png
Binary files differ
diff --git a/tuner/sampletunertvinput/res/mipmap-xxhdpi/ic_launcher.png b/tuner/sampletunertvinput/res/mipmap-xxhdpi/ic_launcher.png
new file mode 100644
index 0000000..9da2990
--- /dev/null
+++ b/tuner/sampletunertvinput/res/mipmap-xxhdpi/ic_launcher.png
Binary files differ
diff --git a/tuner/sampletunertvinput/res/mipmap-xxxhdpi/ic_launcher.png b/tuner/sampletunertvinput/res/mipmap-xxxhdpi/ic_launcher.png
new file mode 100644
index 0000000..ff5c4b1
--- /dev/null
+++ b/tuner/sampletunertvinput/res/mipmap-xxxhdpi/ic_launcher.png
Binary files differ
diff --git a/tuner/sampletunertvinput/res/values/Strings.xml b/tuner/sampletunertvinput/res/values/Strings.xml
new file mode 100644
index 0000000..f76c8ee
--- /dev/null
+++ b/tuner/sampletunertvinput/res/values/Strings.xml
@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+  ~ Copyright (C) 2019 The Android Open Source Project
+  ~
+  ~ Licensed under the Apache License, Version 2.0 (the "License");
+  ~ you may not use this file except in compliance with the License.
+  ~ You may obtain a copy of the License at
+  ~
+  ~      http://www.apache.org/licenses/LICENSE-2.0
+  ~
+  ~ Unless required by applicable law or agreed to in writing, software
+  ~ distributed under the License is distributed on an "AS IS" BASIS,
+  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  ~ See the License for the specific language governing permissions and
+  ~ limitations under the License.
+  -->
+
+<resources>
+  <string name="sample_tuner_tv_input" translatable="false">Sample Tuner TV Input</string>
+</resources>
\ No newline at end of file
diff --git a/tuner/sampletunertvinput/res/xml/sampletunertvinputservice.xml b/tuner/sampletunertvinput/res/xml/sampletunertvinputservice.xml
new file mode 100644
index 0000000..ca39d95
--- /dev/null
+++ b/tuner/sampletunertvinput/res/xml/sampletunertvinputservice.xml
@@ -0,0 +1,19 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ~ Copyright (C) 2019 The Android Open Source Project
+  ~
+  ~ Licensed under the Apache License, Version 2.0 (the "License");
+  ~ you may not use this file except in compliance with the License.
+  ~ You may obtain a copy of the License at
+  ~
+  ~      http://www.apache.org/licenses/LICENSE-2.0
+  ~
+  ~ Unless required by applicable law or agreed to in writing, software
+  ~ distributed under the License is distributed on an "AS IS" BASIS,
+  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  ~ See the License for the specific language governing permissions and
+  ~ limitations under the License.
+  -->
+
+<tv-input xmlns:android="http://schemas.android.com/apk/res/android"
+    android:setupActivity="com.android.tv.samples.sampletunertvinput.SampleTunerTvInputSetupActivity" />
diff --git a/tuner/sampletunertvinput/src/com/android/tv/samples/sampletunertvinput/AndroidManifest.xml b/tuner/sampletunertvinput/src/com/android/tv/samples/sampletunertvinput/AndroidManifest.xml
new file mode 100644
index 0000000..8fc96b2
--- /dev/null
+++ b/tuner/sampletunertvinput/src/com/android/tv/samples/sampletunertvinput/AndroidManifest.xml
@@ -0,0 +1,58 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+  ~ Copyright (C) 2019 The Android Open Source Project
+  ~
+  ~ Licensed under the Apache License, Version 2.0 (the "License");
+  ~ you may not use this file except in compliance with the License.
+  ~ You may obtain a copy of the License at
+  ~
+  ~      http://www.apache.org/licenses/LICENSE-2.0
+  ~
+  ~ Unless required by applicable law or agreed to in writing, software
+  ~ distributed under the License is distributed on an "AS IS" BASIS,
+  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  ~ See the License for the specific language governing permissions and
+  ~ limitations under the License.
+  -->
+
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:tools="http://schemas.android.com/tools"
+    package="com.android.tv.samples.sampletunertvinput">
+
+  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
+  <uses-permission android:name="android.permission.INTERNET"/>
+  <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
+  <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
+  <uses-permission android:name="android.permission.ACCESS_TV_DESCRAMBLER" />
+  <uses-permission android:name="android.permission.ACCESS_TV_TUNER" />
+  <uses-permission android:name="android.permission.TUNER_RESOURCE_ACCESS" />
+  <uses-permission android:name="com.android.providers.tv.permission.READ_EPG_DATA" />
+  <uses-permission android:name="com.android.providers.tv.permission.WRITE_EPG_DATA" />
+  <uses-permission android:name="com.android.tv.permission.RECEIVE_INPUT_EVENT" />
+
+  <uses-feature android:name="android.software.leanback" android:required="true" />
+  <uses-feature android:name="android.software.live_tv" android:required="true" />
+
+  <uses-sdk android:targetSdkVersion="27" android:minSdkVersion="23"/>
+  <application android:label="@string/sample_tuner_tv_input"
+      tools:replace="android:label,icon,theme,appComponentFactory"
+      android:icon="@mipmap/ic_launcher"
+      android:theme="@android:style/Theme.Holo.Light.NoActionBar"
+      android:appComponentFactory="android.support.v4.app.CoreComponentFactory" >
+    <activity android:name=".SampleTunerTvInputSetupActivity" >
+      <intent-filter>
+        <action android:name="android.intent.action.MAIN" />
+      </intent-filter>
+    </activity>
+    <service android:name=".SampleTunerTvInputService"
+        android:permission="android.permission.BIND_TV_INPUT"
+        android:label="@string/sample_tuner_tv_input"
+        android:process="com.android.tv.samples.sampletunertvinput">
+      <intent-filter>
+        <action android:name="android.media.tv.TvInputService" />
+      </intent-filter>
+      <meta-data android:name="android.media.tv.input"
+          android:resource="@xml/sampletunertvinputservice" />
+    </service>
+  </application>
+</manifest>
diff --git a/tuner/sampletunertvinput/src/com/android/tv/samples/sampletunertvinput/SampleTunerTvInputService.java b/tuner/sampletunertvinput/src/com/android/tv/samples/sampletunertvinput/SampleTunerTvInputService.java
new file mode 100644
index 0000000..6ac9535
--- /dev/null
+++ b/tuner/sampletunertvinput/src/com/android/tv/samples/sampletunertvinput/SampleTunerTvInputService.java
@@ -0,0 +1,101 @@
+package com.android.tv.samples.sampletunertvinput;
+
+import android.content.Context;
+import android.media.tv.tuner.frontend.AtscFrontendSettings;
+import android.media.tv.tuner.frontend.FrontendSettings;
+import android.media.tv.tuner.Tuner;
+import android.media.tv.TvInputService;
+import android.net.Uri;
+import android.util.Log;
+import android.view.Surface;
+
+
+/** SampleTunerTvInputService */
+public class SampleTunerTvInputService extends TvInputService {
+    private static final String TAG = "SampleTunerTvInput";
+    private static final boolean DEBUG = true;
+
+    public static final String INPUT_ID =
+        "com.android.tv.samples.sampletunertvinput/.SampleTunerTvInputService";
+    private String mSessionId;
+
+    @Override
+    public TvInputSessionImpl onCreateSession(String inputId, String sessionId) {
+        TvInputSessionImpl session =  new TvInputSessionImpl(this);
+        if (DEBUG) {
+            Log.d(TAG, "onCreateSession(inputId=" + inputId + ", sessionId=" + sessionId + ")");
+        }
+        mSessionId = sessionId;
+        return session;
+    }
+
+    @Override
+    public TvInputSessionImpl onCreateSession(String inputId) {
+        return new TvInputSessionImpl(this);
+    }
+
+    class TvInputSessionImpl extends Session {
+
+        private Surface surface;
+        private final Context mContext;
+        Tuner tuner;
+
+
+        public TvInputSessionImpl(Context context) {
+            super(context);
+            mContext = context;
+        }
+
+        @Override
+        public void onRelease() {
+            if (DEBUG) {
+                Log.d(TAG, "onRelease");
+            }
+        }
+
+        @Override
+        public boolean onSetSurface(Surface surface) {
+            if (DEBUG) {
+                Log.d(TAG, "onSetSurface");
+            }
+            this.surface = surface;
+            return true;
+        }
+
+        @Override
+        public void onSetStreamVolume(float v) {
+            if (DEBUG) {
+                Log.d(TAG, "onSetStreamVolume " + v);
+            }
+        }
+
+        @Override
+        public boolean onTune(Uri uri) {
+            if (DEBUG) {
+                Log.d(TAG, "onTune " + uri);
+            }
+            tuner = new Tuner(mContext, mSessionId,
+                    TvInputService.PRIORITY_HINT_USE_CASE_TYPE_LIVE);
+
+            int feCount = tuner.getFrontendIds().size();
+            if (feCount <= 0) return false;
+
+            AtscFrontendSettings settings =
+                    AtscFrontendSettings
+                            .builder()
+                            .setFrequency(2000)
+                            .setModulation(AtscFrontendSettings.MODULATION_AUTO)
+                            .build();
+            tuner.tune(settings);
+
+            return true;
+        }
+
+        @Override
+        public void onSetCaptionEnabled(boolean b) {
+            if (DEBUG) {
+                Log.d(TAG, "onSetCaptionEnabled " + b);
+            }
+        }
+    }
+}
\ No newline at end of file
diff --git a/tuner/sampletunertvinput/src/com/android/tv/samples/sampletunertvinput/SampleTunerTvInputSetupActivity.java b/tuner/sampletunertvinput/src/com/android/tv/samples/sampletunertvinput/SampleTunerTvInputSetupActivity.java
new file mode 100644
index 0000000..b932b60
--- /dev/null
+++ b/tuner/sampletunertvinput/src/com/android/tv/samples/sampletunertvinput/SampleTunerTvInputSetupActivity.java
@@ -0,0 +1,50 @@
+package com.android.tv.samples.sampletunertvinput;
+
+import android.app.Activity;
+import android.content.Intent;
+import android.media.tv.TvInputInfo;
+import android.os.Bundle;
+import com.android.tv.testing.data.ChannelInfo;
+import com.android.tv.testing.data.ChannelUtils;
+import com.android.tv.testing.data.ProgramInfo;
+import java.util.Collections;
+
+/** Setup activity for SampleTunerTvInput */
+public class SampleTunerTvInputSetupActivity extends Activity {
+    @Override
+    public void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        ChannelInfo channel =
+            new ChannelInfo.Builder()
+                .setNumber("1-1")
+                .setName("Sample Channel")
+                .setLogoUrl(
+                    ChannelInfo.getUriStringForChannelLogo(this, 100))
+                .setOriginalNetworkId(1)
+                .setVideoWidth(640)
+                .setVideoHeight(480)
+                .setAudioChannel(2)
+                .setAudioLanguageCount(1)
+                .setHasClosedCaption(false)
+                .setProgram(
+                    new ProgramInfo(
+                        "Sample Program",
+                        "",
+                        0,
+                        0,
+                        ProgramInfo.GEN_POSTER,
+                        "Sample description",
+                        ProgramInfo.GEN_DURATION,
+                        null,
+                        ProgramInfo.GEN_GENRE,
+                        null))
+                .build();
+
+        Intent intent = getIntent();
+        String inputId = intent.getStringExtra(TvInputInfo.EXTRA_INPUT_ID);
+        ChannelUtils.updateChannels(this, inputId, Collections.singletonList(channel));
+        setResult(Activity.RESULT_OK);
+        finish();
+    }
+
+}