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 WebView mWebView;
41    private String mTrackerLabel;
42
43    /**
44     * Create a new WebDialogFragment to show a particular web page.
45     *
46     * @param url   The URL of the content to show.
47     * @param title Optional title for the dialog.
48     */
49    public static WebDialogFragment newInstance(String url, @Nullable String title,
50            String trackerLabel) {
51        WebDialogFragment f = new WebDialogFragment();
52        Bundle args = new Bundle();
53        args.putString(URL, url);
54        args.putString(TITLE, title);
55        args.putString(TRACKER_LABEL, trackerLabel);
56        f.setArguments(args);
57        return f;
58    }
59
60    @Override
61    public void onCreate(Bundle savedInstanceState) {
62        super.onCreate(savedInstanceState);
63        String title = getArguments().getString(TITLE);
64        mTrackerLabel = getArguments().getString(TRACKER_LABEL);
65        int style = TextUtils.isEmpty(title) ? DialogFragment.STYLE_NO_TITLE
66                : DialogFragment.STYLE_NORMAL;
67        setStyle(style, 0);
68    }
69
70    @Nullable
71    @Override
72    public View onCreateView(LayoutInflater inflater, ViewGroup container,
73            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
87    public void onDestroyView() {
88        super.onDestroyView();
89        if (mWebView != null) {
90            mWebView.destroy();
91        }
92    }
93
94    @Override
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
102    public String getTrackerLabel() {
103        return mTrackerLabel;
104    }
105}
106