[CCT] Add an option to launch Partial Height CCT in the test app

It also has slightly different behavior than launching the normal CCT

- The close button is the normal "X" button.
- The animation is from bottom to up when launching and from up to
  bottom when exiting.
- Refactored out the session handling logic so it can be reused.

Bug: 1210827
Test: Launch the app and click the newly added button to see if it
Test: launches the Partial Height CCT.
Change-Id: If044d2c0fe4b22baa8bc288384ddafd90623a491
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3384959
Reviewed-by: Peter Conn <peconn@chromium.org>
Commit-Queue: Shimi Zhang <ctzsm@chromium.org>
Cr-Commit-Position: refs/heads/main@{#958654}
diff --git a/chrome/browser/android/examples/custom_tabs_client/BUILD.gn b/chrome/browser/android/examples/custom_tabs_client/BUILD.gn
index 44f344e..009a5a3 100644
--- a/chrome/browser/android/examples/custom_tabs_client/BUILD.gn
+++ b/chrome/browser/android/examples/custom_tabs_client/BUILD.gn
@@ -6,10 +6,14 @@
 
 android_resources("chrome_tabs_client_example_apk_resources") {
   sources = [
+    "src/res/anim/slide_in_bottom.xml",
     "src/res/anim/slide_in_left.xml",
     "src/res/anim/slide_in_right.xml",
+    "src/res/anim/slide_in_up.xml",
+    "src/res/anim/slide_out_bottom.xml",
     "src/res/anim/slide_out_left.xml",
     "src/res/anim/slide_out_right.xml",
+    "src/res/anim/slide_out_up.xml",
     "src/res/drawable-hdpi/cover.jpg",
     "src/res/drawable-hdpi/ic_arrow_back.png",
     "src/res/drawable-hdpi/ic_launcher.png",
diff --git a/chrome/browser/android/examples/custom_tabs_client/src/java/org/chromium/customtabsclient/MainActivity.java b/chrome/browser/android/examples/custom_tabs_client/src/java/org/chromium/customtabsclient/MainActivity.java
index 61e4bc4..14e9a8fd 100644
--- a/chrome/browser/android/examples/custom_tabs_client/src/java/org/chromium/customtabsclient/MainActivity.java
+++ b/chrome/browser/android/examples/custom_tabs_client/src/java/org/chromium/customtabsclient/MainActivity.java
@@ -64,6 +64,7 @@
     private Button mMayLaunchButton;
     private Button mLaunchButton;
     private Button mLaunchIncognitoButton;
+    private Button mLaunchPartialHeightCctButton;
     private MediaPlayer mMediaPlayer;
 
     /**
@@ -110,6 +111,7 @@
         mMayLaunchButton = (Button) findViewById(R.id.may_launch_button);
         mLaunchButton = (Button) findViewById(R.id.launch_button);
         mLaunchIncognitoButton = findViewById(R.id.launch_incognito_button);
+        mLaunchPartialHeightCctButton = findViewById(R.id.launch_pcct_button);
         Spinner spinner = (Spinner) findViewById(R.id.spinner);
         mEditText.requestFocus();
         mConnectButton.setOnClickListener(this);
@@ -117,6 +119,7 @@
         mMayLaunchButton.setOnClickListener(this);
         mLaunchButton.setOnClickListener(this);
         mLaunchIncognitoButton.setOnClickListener(this);
+        mLaunchPartialHeightCctButton.setOnClickListener(this);
         mMediaPlayer = MediaPlayer.create(this, R.raw.amazing_grace);
         findViewById(R.id.register_twa_service).setOnClickListener(this);
 
@@ -246,13 +249,20 @@
             customTabsIntent.intent.putExtra(
                     "com.google.android.apps.chrome.EXTRA_OPEN_NEW_INCOGNITO_TAB",
                     viewId == R.id.launch_incognito_button);
-            if (session != null) {
-                CustomTabsHelper.addKeepAliveExtra(this, customTabsIntent.intent);
-            } else {
-                if (!TextUtils.isEmpty(mPackageNameToBind)) {
-                    customTabsIntent.intent.setPackage(mPackageNameToBind);
-                }
-            }
+            configSessionConnection(session, customTabsIntent);
+            customTabsIntent.launchUrl(this, Uri.parse(url));
+        } else if (viewId == R.id.launch_pcct_button) {
+            CustomTabsSession session = getSession();
+            CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder(session);
+            builder.setToolbarColor(Color.parseColor(TOOLBAR_COLOR)).setShowTitle(true);
+            prepareMenuItems(builder);
+            prepareActionButton(builder);
+            builder.setStartAnimations(this, R.anim.slide_in_up, R.anim.slide_out_bottom);
+            builder.setExitAnimations(this, R.anim.slide_in_bottom, R.anim.slide_out_up);
+            CustomTabsIntent customTabsIntent = builder.build();
+            configSessionConnection(session, customTabsIntent);
+            customTabsIntent.intent.putExtra(
+                    "androidx.browser.customtabs.extra.INITIAL_ACTIVITY_HEIGHT_IN_PIXEL", 500);
             customTabsIntent.launchUrl(this, Uri.parse(url));
         }
     }
@@ -285,6 +295,17 @@
                 BottomBarManager.getClickableIDs(), BottomBarManager.getOnClickPendingIntent(this));
     }
 
+    private void configSessionConnection(
+            CustomTabsSession session, CustomTabsIntent customTabsIntent) {
+        if (session != null) {
+            CustomTabsHelper.addKeepAliveExtra(this, customTabsIntent.intent);
+        } else {
+            if (!TextUtils.isEmpty(mPackageNameToBind)) {
+                customTabsIntent.intent.setPackage(mPackageNameToBind);
+            }
+        }
+    }
+
     @Override
     public void onServiceConnected(CustomTabsClient client) {
         mClient = client;
diff --git a/chrome/browser/android/examples/custom_tabs_client/src/res/anim/slide_in_bottom.xml b/chrome/browser/android/examples/custom_tabs_client/src/res/anim/slide_in_bottom.xml
new file mode 100644
index 0000000..c9e4d195
--- /dev/null
+++ b/chrome/browser/android/examples/custom_tabs_client/src/res/anim/slide_in_bottom.xml
@@ -0,0 +1,19 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright 2022 Google Inc. All Rights Reserved.
+
+     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.
+-->
+<set xmlns:android="http://schemas.android.com/apk/res/android">
+    <translate android:fromYDelta="-100%p" android:toYDelta="0"
+        android:duration="@android:integer/config_mediumAnimTime"/>
+</set>
diff --git a/chrome/browser/android/examples/custom_tabs_client/src/res/anim/slide_in_up.xml b/chrome/browser/android/examples/custom_tabs_client/src/res/anim/slide_in_up.xml
new file mode 100644
index 0000000..138990a2
--- /dev/null
+++ b/chrome/browser/android/examples/custom_tabs_client/src/res/anim/slide_in_up.xml
@@ -0,0 +1,19 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright 2022 Google Inc. All Rights Reserved.
+
+     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.
+-->
+<set xmlns:android="http://schemas.android.com/apk/res/android">
+    <translate android:fromYDelta="100%p" android:toYDelta="0"
+        android:duration="@android:integer/config_mediumAnimTime"/>
+</set>
diff --git a/chrome/browser/android/examples/custom_tabs_client/src/res/anim/slide_out_bottom.xml b/chrome/browser/android/examples/custom_tabs_client/src/res/anim/slide_out_bottom.xml
new file mode 100644
index 0000000..01a0fd02
--- /dev/null
+++ b/chrome/browser/android/examples/custom_tabs_client/src/res/anim/slide_out_bottom.xml
@@ -0,0 +1,19 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright 2022 Google Inc. All Rights Reserved.
+
+     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.
+-->
+<set xmlns:android="http://schemas.android.com/apk/res/android">
+    <translate android:fromYDelta="0" android:toYDelta="-100%p"
+        android:duration="@android:integer/config_mediumAnimTime"/>
+</set>
diff --git a/chrome/browser/android/examples/custom_tabs_client/src/res/anim/slide_out_up.xml b/chrome/browser/android/examples/custom_tabs_client/src/res/anim/slide_out_up.xml
new file mode 100644
index 0000000..513d32e
--- /dev/null
+++ b/chrome/browser/android/examples/custom_tabs_client/src/res/anim/slide_out_up.xml
@@ -0,0 +1,19 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright 2022 Google Inc. All Rights Reserved.
+
+     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.
+-->
+<set xmlns:android="http://schemas.android.com/apk/res/android">
+    <translate android:fromYDelta="0" android:toYDelta="100%p"
+        android:duration="@android:integer/config_mediumAnimTime"/>
+</set>
diff --git a/chrome/browser/android/examples/custom_tabs_client/src/res/layout/main.xml b/chrome/browser/android/examples/custom_tabs_client/src/res/layout/main.xml
index 4cb3694..2dbad91 100644
--- a/chrome/browser/android/examples/custom_tabs_client/src/res/layout/main.xml
+++ b/chrome/browser/android/examples/custom_tabs_client/src/res/layout/main.xml
@@ -120,6 +120,14 @@
             android:textAllCaps="false"
             android:text="@string/register_twa_service" />
 
+        <Button
+            android:id="@+id/launch_pcct_button"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:layout_margin="3dp"
+            android:textAllCaps="false"
+            android:text="@string/launch_pcct_text" />
+
     </LinearLayout>
 
 </ScrollView>
diff --git a/chrome/browser/android/examples/custom_tabs_client/src/res/values/strings.xml b/chrome/browser/android/examples/custom_tabs_client/src/res/values/strings.xml
index 26362e0..c61b2467e 100644
--- a/chrome/browser/android/examples/custom_tabs_client/src/res/values/strings.xml
+++ b/chrome/browser/android/examples/custom_tabs_client/src/res/values/strings.xml
@@ -23,6 +23,7 @@
     <string name="connect_button_text">Connect to the service</string>
     <string name="launch_browser_actions_button_text">Launch URL in Browser Actions Context Menu</string>
     <string name="register_twa_service">Register TrustedWebActivityService</string>
+    <string name="launch_pcct_text">Launch URL in a Partial Height Custom Tab</string>
     <string name="default_url">https://www.google.com</string>
     <string name="package_label">Package:</string>
     <string name="amazing_grace">Amazing Grace 2011</string>