WebDialogFragment.java revision 721bd0da688cd552737fbb753a00597f95103b95
1/* 2 * Copyright (C) 2015 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 17package com.android.tv.dialog; 18 19import android.app.DialogFragment; 20import android.os.Bundle; 21import android.support.annotation.Nullable; 22import android.text.TextUtils; 23import android.util.Log; 24import android.view.LayoutInflater; 25import android.view.View; 26import android.view.ViewGroup; 27import android.view.ViewGroup.LayoutParams; 28import android.webkit.WebView; 29import android.webkit.WebViewClient; 30 31/** 32 * A DialogFragment that shows a web view. 33 */ 34public class WebDialogFragment extends SafeDismissDialogFragment { 35 private static final String TAG = "WebDialogFragment"; 36 private static final String URL = "URL"; 37 private static final String TITLE = "TITLE"; 38 private static final String TRACKER_LABEL = "TRACKER_LABEL"; 39 40 private String mTrackerLabel; 41 42 /** 43 * Create a new WebDialogFragment to show a particular web page. 44 * 45 * @param url The URL of the content to show. 46 * @param title Optional title for the dialog. 47 */ 48 public static WebDialogFragment newInstance(String url, @Nullable String title, 49 String trackerLabel) { 50 WebDialogFragment f = new WebDialogFragment(); 51 Bundle args = new Bundle(); 52 args.putString(URL, url); 53 args.putString(TITLE, title); 54 args.putString(TRACKER_LABEL, trackerLabel); 55 f.setArguments(args); 56 return f; 57 } 58 59 @Override 60 public void onCreate(Bundle savedInstanceState) { 61 super.onCreate(savedInstanceState); 62 String title = getArguments().getString(TITLE); 63 mTrackerLabel = getArguments().getString(TRACKER_LABEL); 64 int style = TextUtils.isEmpty(title) ? DialogFragment.STYLE_NO_TITLE 65 : DialogFragment.STYLE_NORMAL; 66 setStyle(style, 0); 67 } 68 69 @Nullable 70 @Override 71 public View onCreateView(LayoutInflater inflater, ViewGroup container, 72 Bundle savedInstanceState) { 73 String title = getArguments().getString(TITLE); 74 getDialog().setTitle(title); 75 76 WebView webView = new WebView(getActivity()); 77 webView.setWebViewClient(new WebViewClient()); 78 String url = getArguments().getString(URL); 79 webView.loadUrl(url); 80 Log.d(TAG, "Loading web content from " + url); 81 82 return webView; 83 } 84 85 @Override 86 public void onStart() { 87 super.onStart(); 88 // Ensure the dialog is fullscreen, even if the webview doesn't have its content yet. 89 getDialog().getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); 90 } 91 92 @Override 93 public String getTrackerLabel() { 94 return mTrackerLabel; 95 } 96} 97