1/*
2 * Copyright 2009, The Android Open Source Project
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *  * Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 *  * Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25package android.webkit;
26
27import android.content.Context;
28import android.view.Gravity;
29import android.view.KeyEvent;
30import android.view.MotionEvent;
31import android.view.SurfaceView;
32import android.view.View;
33import android.view.ViewGroup;
34import android.widget.FrameLayout;
35
36class PluginFullScreenHolder {
37
38    private final WebViewClassic mWebView;
39    private final int mNpp;
40    private final int mOrientation;
41
42    // The container for the plugin view
43    private static CustomFrameLayout mLayout;
44
45    private View mContentView;
46
47    PluginFullScreenHolder(WebViewClassic webView, int orientation, int npp) {
48        mWebView = webView;
49        mNpp = npp;
50        mOrientation = orientation;
51    }
52
53    public void setContentView(View contentView) {
54
55        // Create a FrameLayout that will contain the plugin's view
56        mLayout = new CustomFrameLayout(mWebView.getContext());
57        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
58                            ViewGroup.LayoutParams.MATCH_PARENT,
59                            ViewGroup.LayoutParams.MATCH_PARENT,
60                            Gravity.CENTER);
61
62        mLayout.addView(contentView, layoutParams);
63        mLayout.setVisibility(View.VISIBLE);
64
65        // fixed size is only used either during pinch zoom or surface is too
66        // big. Make sure it is not fixed size before setting it to the full
67        // screen content view. The SurfaceView will be set to the correct mode
68        // by the ViewManager when it is re-attached to the WebView.
69        if (contentView instanceof SurfaceView) {
70            final SurfaceView sView = (SurfaceView) contentView;
71            if (sView.isFixedSize()) {
72                sView.getHolder().setSizeFromLayout();
73            }
74        }
75
76        mContentView = contentView;
77    }
78
79    public void show() {
80        // Other plugins may attempt to draw so hide them while we're active.
81        if (mWebView.getViewManager() != null)
82            mWebView.getViewManager().hideAll();
83
84        WebChromeClient client = mWebView.getWebChromeClient();
85        client.onShowCustomView(mLayout, mOrientation, mCallback);
86    }
87
88    public void hide() {
89        WebChromeClient client = mWebView.getWebChromeClient();
90        client.onHideCustomView();
91    }
92
93    private class CustomFrameLayout extends FrameLayout {
94
95        CustomFrameLayout(Context context) {
96            super(context);
97        }
98
99        @Override
100        public boolean onKeyDown(int keyCode, KeyEvent event) {
101            if (event.isSystem()) {
102                return super.onKeyDown(keyCode, event);
103            }
104            mWebView.onKeyDown(keyCode, event);
105            // always return true as we are the handler
106            return true;
107        }
108
109        @Override
110        public boolean onKeyUp(int keyCode, KeyEvent event) {
111            if (event.isSystem()) {
112                return super.onKeyUp(keyCode, event);
113            }
114            mWebView.onKeyUp(keyCode, event);
115            // always return true as we are the handler
116            return true;
117        }
118
119        @Override
120        public boolean onTouchEvent(MotionEvent event) {
121            // always return true as we don't want the event to propagate any further
122            return true;
123        }
124
125        @Override
126        public boolean onTrackballEvent(MotionEvent event) {
127            mWebView.onTrackballEvent(event);
128            // always return true as we are the handler
129            return true;
130        }
131    }
132
133    private final WebChromeClient.CustomViewCallback mCallback =
134        new WebChromeClient.CustomViewCallback() {
135            public void onCustomViewHidden() {
136
137                mWebView.mPrivateHandler.obtainMessage(WebViewClassic.HIDE_FULLSCREEN)
138                    .sendToTarget();
139
140                mWebView.getWebViewCore().sendMessage(
141                        WebViewCore.EventHub.HIDE_FULLSCREEN, mNpp, 0);
142
143                mLayout.removeView(mContentView);
144                mLayout = null;
145
146                // Re enable plugin views.
147                mWebView.getViewManager().showAll();
148            }
149        };
150}
151