// Copyright 2019 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.

package com.google.androidbrowserhelper.launchtwa;

import android.content.ComponentName;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.google.androidbrowserhelper.demo.R;
import com.google.androidbrowserhelper.trusted.TwaLauncher;
import com.google.androidbrowserhelper.trusted.TwaProviderPicker;

import java.util.Arrays;
import java.util.List;

import androidx.appcompat.app.AppCompatActivity;
import androidx.browser.customtabs.CustomTabsClient;
import androidx.browser.customtabs.CustomTabsServiceConnection;
import androidx.browser.customtabs.CustomTabsSession;
import androidx.browser.trusted.TrustedWebActivityIntentBuilder;

public class LaunchTwaActivity extends AppCompatActivity {
    private static final Uri LAUNCH_URI =
            Uri.parse("https://github.com/GoogleChrome/android-browser-helper");

    private final TrustedWebActivityIntentBuilder builder = new TrustedWebActivityIntentBuilder(
            LAUNCH_URI);

    private final CustomTabsServiceConnection customTabsServiceConnection = new CustomTabsServiceConnection() {
        CustomTabsSession mSession;
        private final static int SESSION_ID = 45;  // An arbitrary constant.

        @Override
        public void onCustomTabsServiceConnected(ComponentName name,
                                                 CustomTabsClient client) {
            mSession = client.newSession(null, SESSION_ID);

            if (mSession == null) {
                Toast.makeText(LaunchTwaActivity.this,
                        "Couldn't get session from provider.", Toast.LENGTH_LONG).show();
            }

            Intent intent = builder.build(mSession).getIntent();
            intent.putExtra(Intent.EXTRA_REFERRER,
                    Uri.parse("android-app://com.google.androidbrowserhelper?twa=true"));
            startActivity(intent);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            mSession = null;
        }
    };

    private boolean serviceBound = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_launch_twa);
    }

    /**
     * Launches a Trusted Web Activity without any customizations
     *
     * @param view the source of the event invoking this method.
     */
    public void launch(View view) {
        new TwaLauncher(this).launch(LAUNCH_URI);
    }

    /**
     * Launches a Trusted Web Activity where navigations to non-validate domains will open in a Custom
     * Tab where the toolbar color has been customized.
     *
     * @param view the source of the event invoking this method.
     */
    public void launchWithCustomColors(View view) {
        TrustedWebActivityIntentBuilder builder = new TrustedWebActivityIntentBuilder(LAUNCH_URI)
                .setNavigationBarColor(Color.RED)
                .setToolbarColor(Color.BLUE);

        new TwaLauncher(this).launch(builder, null, null);
    }

    /**
     * Opens a Trusted Web Activity where multiple domains are validated to open in full screen.
     *
     * @param view the source of the event invoking this method.
     */
    public void launcherWithMultipleOrigins(View view) {
        List<String> origins = Arrays.asList(
                "https://www.wikipedia.org/",
                "https://www.example.com/"
        );

        TrustedWebActivityIntentBuilder builder = new TrustedWebActivityIntentBuilder(LAUNCH_URI)
                .setAdditionalTrustedOrigins(origins);

        new TwaLauncher(this).launch(builder, null, null);
    }

    /**
     * Open a Trusted Web Activity where the loaded URL will receive a customized Referrer.
     *
     * @param view the source of the event invoking this method.
     */
    public void launchWithCustomReferrer(View view) {
        // The ergonomics will be improved here, since we're basically replicating the work of
        // TwaLauncher, see https://github.com/GoogleChrome/android-browser-helper/issues/13.

        TwaProviderPicker.Action action = TwaProviderPicker.pickProvider(getPackageManager());
        if (!serviceBound) {
            CustomTabsClient
                    .bindCustomTabsService(this, action.provider, customTabsServiceConnection);
            serviceBound = true;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (serviceBound) {
            unbindService(customTabsServiceConnection);
            serviceBound = false;
        }
    }
}

@@ -1035,16 +1035,44 @@
 icker;%0A%0A
+import java.util.ArrayList;%0A
 import j
@@ -1088,16 +1088,16 @@
 Arrays;%0A
-
 import j
@@ -1700,24 +1700,172 @@
 UNCH_URI);%0A%0A
+    /**%0A     * A bag to put all TwaLauncher in so we can dispose all at once.%0A     */%0A    private List%3CTwaLauncher%3E launchers = new ArrayList%3C%3E();%0A%0A
     private 
@@ -3287,32 +3287,55 @@
  view) %7B%0A       
+ TwaLauncher launcher =
  new TwaLauncher
@@ -3332,32 +3332,50 @@
 waLauncher(this)
+;%0A        launcher
 .launch(LAUNCH_U
@@ -3379,16 +3379,49 @@
 H_URI);%0A
+        launchers.add(launcher);%0A
     %7D%0A%0A 
@@ -3909,31 +3909,55 @@
 lor.BLUE);%0A%0A
+%0A
        
+ TwaLauncher launcher =
  new TwaLaun
@@ -3958,32 +3958,50 @@
 waLauncher(this)
+;%0A        launcher
 .launch(builder,
@@ -4006,32 +4006,65 @@
 r, null, null);%0A
+        launchers.add(launcher);%0A
     %7D%0A%0A    /**%0A 
@@ -4597,23 +4597,47 @@
 gins);%0A%0A
+%0A
        
+ TwaLauncher launcher =
  new Twa
@@ -4650,16 +4650,34 @@
 er(this)
+;%0A        launcher
 .launch(
@@ -4694,24 +4694,57 @@
 ull, null);%0A
+        launchers.add(launcher);%0A
     %7D%0A%0A    /
@@ -5504,24 +5504,24 @@
 Destroy() %7B%0A
-
         supe
@@ -5531,24 +5531,115 @@
 nDestroy();%0A
+        for (TwaLauncher launcher : launchers) %7B%0A            launcher.destroy();%0A        %7D%0A
         if (

fix leak in sample