• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2014, 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.managedprovisioning.preprovisioning;
18 
19 import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.PROVISIONING_WEB_ACTIVITY_TIME_MS;
20 
21 import static java.net.HttpURLConnection.HTTP_FORBIDDEN;
22 
23 import android.app.Activity;
24 import android.content.Context;
25 import android.content.Intent;
26 import android.os.Bundle;
27 import android.webkit.URLUtil;
28 import android.webkit.WebResourceRequest;
29 import android.webkit.WebResourceResponse;
30 import android.webkit.WebSettings;
31 import android.webkit.WebView;
32 import android.webkit.WebViewClient;
33 import android.widget.Toast;
34 
35 import androidx.annotation.Nullable;
36 
37 import com.android.managedprovisioning.ManagedProvisioningBaseApplication;
38 import com.android.managedprovisioning.ManagedProvisioningScreens;
39 import com.android.managedprovisioning.R;
40 import com.android.managedprovisioning.common.ProvisionLogger;
41 import com.android.managedprovisioning.common.SettingsFacade;
42 import com.android.managedprovisioning.common.SetupLayoutActivity;
43 import com.android.managedprovisioning.common.TransitionHelper;
44 import com.android.managedprovisioning.preprovisioning.terms.TermsActivity;
45 
46 /**
47  * This activity shows a web view, which loads the url indicated in the starting intent. By default
48  * the user can click on links and load other urls. However, by passing the allowed url base, the
49  * web view can be limited to urls that start with this base.
50  *
51  * <p>This activity is considered for using by
52  * {@link TermsActivity} to display the support web pages
53  * about provisioning concepts.
54  */
55 public class WebActivity extends SetupLayoutActivity {
56     private static final String EXTRA_URL = "extra_url";
57 
58     private WebView mWebView;
59     private final SettingsFacade mSettingsFacade = new SettingsFacade();
60     private final TransitionHelper mTransitionHelper = new TransitionHelper();
61 
62     @Override
onCreate(Bundle savedInstanceState)63     public void onCreate(Bundle savedInstanceState) {
64         super.onCreate(savedInstanceState);
65         String extraUrl = getIntent().getStringExtra(EXTRA_URL);
66         if (extraUrl == null) {
67             Toast.makeText(this, R.string.url_error, Toast.LENGTH_SHORT).show();
68             ProvisionLogger.loge("No url provided to WebActivity.");
69             mTransitionHelper.finishActivity(this);
70         }
71 
72         mWebView = new WebView(this);
73         // We need a custom WebViewClient. Without this an external browser will load the URL.
74         mWebView.setWebViewClient(new WebViewClient() {
75             @Override
76             public WebResourceResponse shouldInterceptRequest(WebView view,
77                     WebResourceRequest request) {
78                 String url = request.getUrl().toString();
79                 if (!URLUtil.isHttpsUrl(url)) {
80                     ProvisionLogger.loge("Secure connection required, but insecure URL requested "
81                             + "explicitly, or as a part of the page.");
82                     return createNewSecurityErrorResponse();
83                 }
84                 return super.shouldInterceptRequest(view, request);
85             }
86         });
87         mWebView.loadUrl(extraUrl);
88         // Enable zoom gesture in web view.
89         WebSettings webSettings = mWebView.getSettings();
90         webSettings.setBuiltInZoomControls(true);
91         webSettings.setDisplayZoomControls(false);
92         webSettings.setJavaScriptEnabled(true);
93         if (!mSettingsFacade.isUserSetupCompleted(this)) {
94             // User should not be able to escape provisioning if user setup isn't complete.
95             mWebView.setOnLongClickListener(v -> true);
96         }
97         getThemeHelper()
98                 .applyWebSettingsDayNight(getApplicationContext(), webSettings, getIntent());
99         setContentView(mWebView);
100     }
101 
createNewSecurityErrorResponse()102     private WebResourceResponse createNewSecurityErrorResponse() {
103         WebResourceResponse response = new WebResourceResponse("text/plain", "UTF-8", null);
104         response.setStatusCodeAndReasonPhrase(HTTP_FORBIDDEN, "Secure connection required");
105         return response;
106     }
107 
getMetricsCategory()108     protected int getMetricsCategory() {
109         return PROVISIONING_WEB_ACTIVITY_TIME_MS;
110     }
111 
112     @Override
onBackPressed()113     public void onBackPressed() {
114         if (mWebView.canGoBack()) {
115             mWebView.goBack();
116         } else {
117             super.onBackPressed();
118         }
119     }
120 
121     /**
122      * Creates an intent to launch the {@link WebActivity}.
123      * @param url the url to be shown upon launching this activity
124      */
125     @Nullable
createIntent(Context context, String url)126     public static Intent createIntent(Context context, String url) {
127         if (URLUtil.isNetworkUrl(url)) {
128             return new Intent(context, getWebActivityClass(context))
129                     .putExtra(EXTRA_URL, url);
130         }
131         return null;
132     }
133 
getWebActivityClass(Context context)134     private static Class<? extends Activity> getWebActivityClass(Context context) {
135         return getBaseApplication(context)
136                 .getActivityClassForScreen(ManagedProvisioningScreens.WEB);
137     }
138 
getBaseApplication(Context context)139     private static ManagedProvisioningBaseApplication getBaseApplication(Context context) {
140         return (ManagedProvisioningBaseApplication) context.getApplicationContext();
141     }
142 }
143