1// Copyright 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5package org.chromium.android_webview;
6
7import android.content.Context;
8import android.view.KeyEvent;
9import android.view.View;
10import android.webkit.URLUtil;
11import android.webkit.WebChromeClient;
12import android.widget.FrameLayout;
13
14import org.chromium.content.browser.ContentVideoView;
15import org.chromium.content.browser.ContentVideoViewClient;
16import org.chromium.content.browser.ContentViewClient;
17
18/**
19 * ContentViewClient implementation for WebView
20 */
21public class AwContentViewClient extends ContentViewClient {
22
23    private class AwContentVideoViewClient implements ContentVideoViewClient {
24        @Override
25        public boolean onShowCustomView(View view) {
26            final FrameLayout viewGroup = new FrameLayout(mContext);
27            viewGroup.addView(view);
28            viewGroup.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
29                @Override
30                public void onViewDetachedFromWindow(View v) {
31                    // Intentional no-op (see onDestroyContentVideoView).
32                }
33
34                @Override
35                public void onViewAttachedToWindow(View v) {
36                    if (mAwContents.isFullScreen()) {
37                        return;
38                    }
39                    View fullscreenView = mAwContents.enterFullScreen();
40                    if (fullscreenView != null) {
41                        fullscreenView.setOnKeyListener(new View.OnKeyListener() {
42                            @Override
43                            public boolean onKey(View v, int keyCode, KeyEvent event) {
44                                if (event.getKeyCode() == KeyEvent.KEYCODE_BACK
45                                        && event.getAction() == KeyEvent.ACTION_UP
46                                        && mAwContents.isFullScreen()) {
47                                    exitFullscreen();
48                                    return true;
49                                }
50                                return false;
51                            }
52                        });
53                        fullscreenView.setFocusable(true);
54                        fullscreenView.setFocusableInTouchMode(true);
55                        fullscreenView.requestFocus();
56                        viewGroup.addView(fullscreenView);
57                    }
58                }
59            });
60            WebChromeClient.CustomViewCallback cb = new WebChromeClient.CustomViewCallback() {
61                @Override
62                public void onCustomViewHidden() {
63                    exitFullscreen();
64                }
65            };
66            mAwContentsClient.onShowCustomView(viewGroup, cb);
67            return true;
68        }
69
70        private void exitFullscreen() {
71            ContentVideoView contentVideoView = ContentVideoView.getContentVideoView();
72            if (contentVideoView != null)
73                contentVideoView.exitFullscreen(false);
74        }
75
76        @Override
77        public void onDestroyContentVideoView() {
78            mAwContents.exitFullScreen();
79            mAwContentsClient.onHideCustomView();
80        }
81
82        @Override
83        public View getVideoLoadingProgressView() {
84            return mAwContentsClient.getVideoLoadingProgressView();
85        }
86    }
87
88    private AwContentsClient mAwContentsClient;
89    private AwSettings mAwSettings;
90    private AwContents mAwContents;
91    private Context mContext;
92
93    public AwContentViewClient(
94            AwContentsClient awContentsClient, AwSettings awSettings, AwContents awContents,
95            Context context) {
96        mAwContentsClient = awContentsClient;
97        mAwSettings = awSettings;
98        mAwContents = awContents;
99        mContext = context;
100    }
101
102    @Override
103    public void onBackgroundColorChanged(int color) {
104        mAwContentsClient.onBackgroundColorChanged(color);
105    }
106
107    @Override
108    public void onStartContentIntent(Context context, String contentUrl) {
109        //  Callback when detecting a click on a content link.
110        mAwContentsClient.shouldOverrideUrlLoading(contentUrl);
111    }
112
113    @Override
114    public void onUpdateTitle(String title) {
115        mAwContentsClient.onReceivedTitle(title);
116    }
117
118    @Override
119    public boolean shouldOverrideKeyEvent(KeyEvent event) {
120        return mAwContentsClient.shouldOverrideKeyEvent(event);
121    }
122
123    @Override
124    public final ContentVideoViewClient getContentVideoViewClient() {
125        return new AwContentVideoViewClient();
126    }
127
128    @Override
129    public boolean shouldBlockMediaRequest(String url) {
130        return mAwSettings != null ?
131                mAwSettings.getBlockNetworkLoads() && URLUtil.isNetworkUrl(url) : true;
132    }
133}
134