forked from
lass.pds.lassul.us/superconfig
This repository has no description
4.0 kB
124 lines
1package us.lassul.assist;
2
3import android.app.Activity;
4import android.app.KeyguardManager;
5import android.content.ComponentName;
6import android.content.Intent;
7import android.content.pm.PackageManager;
8import android.content.pm.ResolveInfo;
9import android.net.Uri;
10import android.os.Bundle;
11import androidx.browser.customtabs.CustomTabsClient;
12import androidx.browser.customtabs.CustomTabsIntent;
13import androidx.browser.customtabs.CustomTabsService;
14import androidx.browser.customtabs.CustomTabsServiceConnection;
15import androidx.browser.customtabs.CustomTabsSession;
16import androidx.browser.trusted.TrustedWebActivityIntentBuilder;
17
18/**
19 * Opens the voice page as a Trusted Web Activity, else as a Custom Tab or in the browser. The
20 * page starts listening at once (?listen).
21 */
22public class AssistActivity extends Activity {
23 private static final Uri URL = Uri.parse("https://assist.lassul.us/?listen");
24
25 private CustomTabsServiceConnection connection;
26 private boolean launched;
27
28 @Override
29 protected void onCreate(Bundle state) {
30 super.onCreate(state);
31 KeyguardManager keyguard = getSystemService(KeyguardManager.class);
32 if (!keyguard.isKeyguardLocked()) {
33 open();
34 return;
35 }
36 // Started over the lock screen (showWhenLocked): the browser cannot show there.
37 keyguard.requestDismissKeyguard(this, new KeyguardManager.KeyguardDismissCallback() {
38 @Override
39 public void onDismissSucceeded() {
40 open();
41 }
42
43 @Override
44 public void onDismissCancelled() {
45 finish();
46 }
47
48 @Override
49 public void onDismissError() {
50 finish();
51 }
52 });
53 }
54
55 private void open() {
56 String provider = twaProvider();
57 if (provider == null) {
58 openCustomTab();
59 return;
60 }
61 connection = new CustomTabsServiceConnection() {
62 @Override
63 public void onCustomTabsServiceConnected(ComponentName name, CustomTabsClient client) {
64 if (launched) {
65 return;
66 }
67 client.warmup(0);
68 CustomTabsSession session = client.newSession(null);
69 if (session == null) {
70 openCustomTab();
71 return;
72 }
73 launch(new TrustedWebActivityIntentBuilder(URL).build(session).getIntent());
74 }
75
76 @Override
77 public void onServiceDisconnected(ComponentName name) {}
78 };
79 if (!CustomTabsClient.bindCustomTabsService(this, provider, connection)) {
80 connection = null;
81 openCustomTab();
82 }
83 }
84
85 /** The first browser offering Trusted Web Activities, null if none does. */
86 private String twaProvider() {
87 Intent intent = new Intent(CustomTabsService.ACTION_CUSTOM_TABS_CONNECTION);
88 for (ResolveInfo info :
89 getPackageManager().queryIntentServices(intent, PackageManager.GET_RESOLVED_FILTER)) {
90 if (info.filter != null
91 && info.filter.hasCategory(CustomTabsService.TRUSTED_WEB_ACTIVITY_CATEGORY)) {
92 return info.serviceInfo.packageName;
93 }
94 }
95 return null;
96 }
97
98 /** A Custom Tab where supported; other browsers ignore its extras. */
99 private void openCustomTab() {
100 launch(new CustomTabsIntent.Builder().build().intent.setData(URL));
101 }
102
103 private void launch(Intent intent) {
104 launched = true;
105 startActivity(intent);
106 }
107
108 // Back from the page: this activity only sits below it in the task.
109 @Override
110 protected void onRestart() {
111 super.onRestart();
112 if (launched) {
113 finish();
114 }
115 }
116
117 @Override
118 protected void onDestroy() {
119 if (connection != null) {
120 unbindService(connection);
121 }
122 super.onDestroy();
123 }
124}