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