1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.sdksandboxclient;
18 
19 import android.app.Activity;
20 import android.app.sdksandbox.interfaces.IMediateeSdkApi;
21 import android.os.Bundle;
22 import android.os.RemoteException;
23 import android.webkit.JavascriptInterface;
24 import android.webkit.WebSettings;
25 import android.webkit.WebView;
26 
27 public class AppWebViewActivity extends Activity {
28 
29     private JsInterface mInterface;
30     private WebView mWebView;
31     private static final String SANDBOXED_SDK_BINDER = "com.android.sdksandboxclient.SANDBOXED_SDK";
32 
33     @Override
onCreate(Bundle bundle)34     public void onCreate(Bundle bundle) {
35         super.onCreate(bundle);
36         setContentView(R.layout.activity_app_webview);
37         mInterface =
38                 new JsInterface(
39                         IMediateeSdkApi.Stub.asInterface(
40                                 getIntent().getIBinderExtra(SANDBOXED_SDK_BINDER)));
41         startWebview();
42     }
43 
startWebview()44     private void startWebview() {
45         mWebView = findViewById(R.id.app_webview);
46         initializeSettings(mWebView.getSettings());
47         loadDataInWebview(
48                 "<body><h1 style=\"font-size:50px;\">"
49                         + "Touch the screen to check Javascript bridge functionality!"
50                         + "</h1></body>");
51         mWebView.addJavascriptInterface(mInterface, "interface");
52         mWebView.setOnTouchListener(
53                 (v, event) -> {
54                     loadDataInWebview(
55                             "<body onload=\"var res = window.interface.getString();"
56                                     + "document.getElementById('Result').innerHTML=res\">"
57                                     + "<h1 style=\"font-size:50px;\"id=\"Result\">Result here></h1>"
58                                     + "</body>");
59                     return true;
60                 });
61     }
62 
initializeSettings(WebSettings settings)63     private void initializeSettings(WebSettings settings) {
64         settings.setJavaScriptEnabled(true);
65     }
66 
67     /** Loads some basic HTML in the Webview. */
loadDataInWebview(String data)68     private void loadDataInWebview(String data) {
69         mWebView.loadData("<html><head></head>" + data + "</html>", "text/html", null);
70     }
71 
72     private static class JsInterface {
73         IMediateeSdkApi mApi;
74 
JsInterface(IMediateeSdkApi api)75         private JsInterface(IMediateeSdkApi api) {
76             mApi = api;
77         }
78 
79         @JavascriptInterface
80         @SuppressWarnings("UnusedMethod")
getString()81         public String getString() {
82             try {
83                 return mApi.getMessage();
84             } catch (RemoteException e) {
85                 return e.getMessage();
86             }
87         }
88     }
89 }
90