1 /* 2 * Copyright (C) 2021 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.imsserviceentitlement; 18 19 import android.annotation.SuppressLint; 20 import android.app.Activity; 21 import android.graphics.Bitmap; 22 import android.os.Bundle; 23 import android.util.Log; 24 import android.view.LayoutInflater; 25 import android.view.View; 26 import android.view.ViewGroup; 27 import android.webkit.JavascriptInterface; 28 import android.webkit.WebSettings; 29 import android.webkit.WebView; 30 import android.webkit.WebViewClient; 31 import android.widget.ProgressBar; 32 33 import com.android.imsserviceentitlement.R; 34 35 import androidx.fragment.app.Fragment; 36 37 /** A fragment of WebView to render emergency address web portal */ 38 public class WfcQnsWebPortalFragment extends Fragment { 39 private static final String TAG = "IMSSE-WfcQnsWebPortalFragment"; 40 41 private static final String URL_KEY = "URL_KEY"; 42 43 /** Public static constructor */ newInstance(String url)44 public static WfcQnsWebPortalFragment newInstance(String url) { 45 WfcQnsWebPortalFragment frag = new WfcQnsWebPortalFragment(); 46 47 Bundle args = new Bundle(); 48 args.putString(URL_KEY, url); 49 frag.setArguments(args); 50 51 return frag; 52 } 53 54 @SuppressLint("SetJavaScriptEnabled") // only trusted URLs are loaded 55 @Override onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)56 public View onCreateView( 57 LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 58 View v = inflater.inflate(R.layout.fragment_webview, container, false); 59 60 Bundle arguments = getArguments(); 61 String url = arguments.getString(URL_KEY); 62 63 ProgressBar spinner = (ProgressBar) v.findViewById(R.id.loadingbar); 64 WebView webView = (WebView) v.findViewById(R.id.webview); 65 webView.setWebViewClient( 66 new WebViewClient() { 67 private boolean hideLoader = false; 68 69 @Override 70 public boolean shouldOverrideUrlLoading(WebView view, String url) { 71 Log.d(TAG, "shouldOverrideUrlLoading()"); 72 return false; // Let WebView handle redirected URL 73 } 74 75 @Override 76 public void onPageStarted(WebView webview, String url, Bitmap favicon) { 77 // Webview will be invisible for first time only 78 Log.d(TAG, "onPageStarted()"); 79 if (!hideLoader) { 80 webview.setVisibility(WebView.INVISIBLE); 81 Log.d(TAG, "onPageStarted() setVisibility(WebView.INVISIBLE)"); 82 } 83 } 84 85 @Override 86 public void onPageFinished(WebView view, String url) { 87 Log.d(TAG, "onPageFinished()"); 88 hideLoader = true; 89 spinner.setVisibility(View.GONE); 90 view.setVisibility(WebView.VISIBLE); 91 super.onPageFinished(view, url); 92 } 93 }); 94 95 webView.addJavascriptInterface(new JsInterface(), JsInterface.OBJECT_NAME); 96 97 WebSettings settings = webView.getSettings(); 98 settings.setDomStorageEnabled(true); 99 settings.setJavaScriptEnabled(true); 100 101 webView.loadUrl(url); 102 103 return v; 104 } 105 106 // JS interface required by Rogers web portal. 107 private class JsInterface { 108 /** 109 * Name of the JS controller object. 110 * 111 * <p>It's hard-coded in the JS; DO NOT change unless requested by Rogers. 112 */ 113 public static final String OBJECT_NAME = "WiFiCallingWebViewController"; 114 115 /** 116 * Finish the activity when the cancel button clicked. 117 * 118 * <p>The method name is hard-coded in the JS; DO NOT change unless requested by Rogers. 119 */ 120 @JavascriptInterface cancelButtonClicked()121 public void cancelButtonClicked() { 122 Log.d(TAG, "cancelButtonClicked()"); 123 final Activity activity = WfcQnsWebPortalFragment.this.getActivity(); 124 if (activity != null) { 125 activity.setResult(Activity.RESULT_CANCELED); 126 activity.finish(); 127 } 128 } 129 130 /** 131 * This method is invoked in JS but no implementation required. So define it to avoid JS 132 * failure. 133 * 134 * <p>The method name is hard-coded in the JS; DO NOT change unless requested by Rogers. 135 */ 136 @JavascriptInterface cancelButtonPressed()137 public void cancelButtonPressed() { 138 Log.d(TAG, "cancelButtonPressed()"); 139 } 140 141 /** 142 * This method is invoked in JS but no implementation required. So define it to avoid JS 143 * failure. 144 * 145 * <p>The method name is hard-coded in the JS; DO NOT change unless requested by Rogers. 146 */ 147 @JavascriptInterface phoneServicesAccountStatusChanged()148 public void phoneServicesAccountStatusChanged() { 149 Log.d(TAG, "phoneServicesAccountStatusChanged()"); 150 // No-op 151 } 152 153 /** 154 * Finish the activity when onCloseWebView() is called. 155 * 156 * <p>The method name is hard-coded in the JS; DO NOT change unless requested by Bell. 157 */ 158 @JavascriptInterface 159 @SuppressWarnings("checkstyle:MethodName") CloseWebView()160 public void CloseWebView() { 161 Log.d(TAG, "CloseWebView()"); 162 final Activity activity = WfcQnsWebPortalFragment.this.getActivity(); 163 if (activity != null) { 164 activity.setResult(Activity.RESULT_CANCELED); 165 activity.finish(); 166 } 167 } 168 } 169 } 170