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.uiflows; 18 19 import android.app.Activity; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.os.Bundle; 23 import android.view.View; 24 import android.view.View.OnLongClickListener; 25 import android.webkit.WebResourceRequest; 26 import android.webkit.WebView; 27 import android.webkit.WebViewClient; 28 29 import com.android.managedprovisioning.ProvisionLogger; 30 import com.android.managedprovisioning.common.Utils; 31 32 /** 33 * This activity shows a web view, which loads the url indicated in the starting intent. By default 34 * the user can click on links and load other urls. However, by passing the allowed url base, the 35 * web view can be limited to urls that start with this base. 36 * 37 * <p>This activity is currently used by the 38 * {@link com.android.managedprovisioning.UserConsentDialog} to display the google support web page 39 * about the provisioning concepts. 40 */ 41 public class WebActivity extends Activity { 42 private static final String EXTRA_URL = "extra_url"; 43 44 // Users can only browse urls starting with the base specified by the following extra. 45 // If this extra is not used, there are no restrictions on browsable urls. 46 private static final String EXTRA_ALLOWED_URL_BASE = "extra_allowed_url_base"; 47 48 private WebView mWebView; 49 private final Utils mUtils = new Utils(); 50 51 @Override 52 public void onCreate(Bundle savedInstanceState) { 53 super.onCreate(savedInstanceState); 54 55 mWebView = new WebView(this); 56 final String extraUrl = getIntent().getStringExtra(EXTRA_URL); 57 final String extraAllowedUrlBase = getIntent().getStringExtra(EXTRA_ALLOWED_URL_BASE); 58 if (extraUrl == null) { 59 ProvisionLogger.loge("No url provided to WebActivity."); 60 finish(); 61 return; 62 } 63 mWebView.loadUrl(extraUrl); 64 mWebView.setWebViewClient(new WebViewClient() { 65 @Override 66 public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) { 67 String url = request.getUrl().toString(); 68 if (extraAllowedUrlBase != null && url.startsWith(extraAllowedUrlBase)) { 69 view.loadUrl(url); 70 } 71 return true; 72 } 73 }); 74 if (!mUtils.isUserSetupCompleted(this)) { 75 // User should not be able to escape provisioning if user setup isn't complete. 76 mWebView.setOnLongClickListener(new OnLongClickListener() { 77 @Override 78 public boolean onLongClick(View v) { 79 return true; 80 } 81 }); 82 } 83 this.setContentView(mWebView); 84 } 85 86 /** 87 * Creates an intent to launch the webactivity. 88 * 89 * @param url the url to be shown upon launching this activity 90 * @param allowedUrlBase the limit to all urls allowed to be seen in this webview 91 */ 92 public static Intent createIntent(Context context, String url, String allowedUrlBase) { 93 Intent intent = new Intent(context, WebActivity.class); 94 intent.putExtra(WebActivity.EXTRA_URL, url); 95 intent.putExtra(WebActivity.EXTRA_ALLOWED_URL_BASE, allowedUrlBase); 96 return intent; 97 } 98 }