-
Notifications
You must be signed in to change notification settings - Fork 300
/
Copy pathLaunchTwaActivity.java
161 lines (135 loc) · 5.62 KB
/
LaunchTwaActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// 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.app.Activity;
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.ArrayList;
import java.util.Arrays;
import java.util.List;
import androidx.browser.customtabs.CustomTabsClient;
import androidx.browser.customtabs.CustomTabsServiceConnection;
import androidx.browser.customtabs.CustomTabsSession;
import androidx.browser.trusted.TrustedWebActivityIntentBuilder;
public class LaunchTwaActivity extends Activity {
private static final Uri LAUNCH_URI =
Uri.parse("https://github.com/GoogleChrome/android-browser-helper");
private final TrustedWebActivityIntentBuilder builder = new TrustedWebActivityIntentBuilder(
LAUNCH_URI);
/**
* A bag to put all TwaLauncher in so we can dispose all at once.
*/
private List<TwaLauncher> launchers = new ArrayList<>();
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) {
TwaLauncher launcher = new TwaLauncher(this);
launcher.launch(LAUNCH_URI);
launchers.add(launcher);
}
/**
* 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);
TwaLauncher launcher = new TwaLauncher(this);
launcher.launch(builder, null, null, null);
launchers.add(launcher);
}
/**
* 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);
TwaLauncher launcher = new TwaLauncher(this);
launcher.launch(builder, null, null , null);
launchers.add(launcher);
}
/**
* 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();
for (TwaLauncher launcher : launchers) {
launcher.destroy();
}
if (serviceBound) {
unbindService(customTabsServiceConnection);
serviceBound = false;
}
}
}