111438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba/*
211438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba * Copyright 2009, The Android Open Source Project
311438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba *
411438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba * Redistribution and use in source and binary forms, with or without
511438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba * modification, are permitted provided that the following conditions
611438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba * are met:
711438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba *  * Redistributions of source code must retain the above copyright
811438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba *    notice, this list of conditions and the following disclaimer.
911438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba *  * Redistributions in binary form must reproduce the above copyright
1011438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba *    notice, this list of conditions and the following disclaimer in the
1111438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba *    documentation and/or other materials provided with the distribution.
1211438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba *
1311438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
1411438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1511438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1611438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
1711438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
1811438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
1911438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
2011438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
2111438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2211438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2311438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2411438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba */
2511438c371e5757bde2a5c08eb81aedb794805fc1Grace Klobapackage android.webkit;
2611438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba
2711438c371e5757bde2a5c08eb81aedb794805fc1Grace Klobaimport android.app.Dialog;
2811438c371e5757bde2a5c08eb81aedb794805fc1Grace Klobaimport android.view.KeyEvent;
2911438c371e5757bde2a5c08eb81aedb794805fc1Grace Klobaimport android.view.MotionEvent;
306edb3792d83e6e299a9d330db938075634e76d14Grace Klobaimport android.view.SurfaceView;
3111438c371e5757bde2a5c08eb81aedb794805fc1Grace Klobaimport android.view.View;
327c5bf4666cb8f2f79a61f46f14dd2272b0ba41dbDerek Sollenbergerimport android.view.ViewGroup;
3311438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba
3411438c371e5757bde2a5c08eb81aedb794805fc1Grace Klobaclass PluginFullScreenHolder extends Dialog {
3511438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba
3611438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba    private final WebView mWebView;
3711438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba    private final int mNpp;
387c5bf4666cb8f2f79a61f46f14dd2272b0ba41dbDerek Sollenberger    private View mContentView;
3911438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba
4011438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba    PluginFullScreenHolder(WebView webView, int npp) {
4111438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba        super(webView.getContext(), android.R.style.Theme_NoTitleBar_Fullscreen);
4211438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba        mWebView = webView;
4311438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba        mNpp = npp;
4411438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba    }
4511438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba
4611438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba    @Override
477c5bf4666cb8f2f79a61f46f14dd2272b0ba41dbDerek Sollenberger    public void setContentView(View contentView) {
48c28ff44b1e48c7232065b0a81f64a7accb9d2ebfDerek Sollenberger        // as we are sharing the View between full screen and
49c28ff44b1e48c7232065b0a81f64a7accb9d2ebfDerek Sollenberger        // embedded mode, we have to remove the
50c28ff44b1e48c7232065b0a81f64a7accb9d2ebfDerek Sollenberger        // AbsoluteLayout.LayoutParams set by embedded mode to
51c28ff44b1e48c7232065b0a81f64a7accb9d2ebfDerek Sollenberger        // ViewGroup.LayoutParams before adding it to the dialog
52c28ff44b1e48c7232065b0a81f64a7accb9d2ebfDerek Sollenberger        contentView.setLayoutParams(new ViewGroup.LayoutParams(
53c28ff44b1e48c7232065b0a81f64a7accb9d2ebfDerek Sollenberger                ViewGroup.LayoutParams.MATCH_PARENT,
54c28ff44b1e48c7232065b0a81f64a7accb9d2ebfDerek Sollenberger                ViewGroup.LayoutParams.MATCH_PARENT));
556edb3792d83e6e299a9d330db938075634e76d14Grace Kloba        // fixed size is only used either during pinch zoom or surface is too
566edb3792d83e6e299a9d330db938075634e76d14Grace Kloba        // big. Make sure it is not fixed size before setting it to the full
57f706ef857bd14501d2c91b52e12e6bd387f63dadGrace Kloba        // screen content view. The SurfaceView will be set to the correct mode
58f706ef857bd14501d2c91b52e12e6bd387f63dadGrace Kloba        // by the ViewManager when it is re-attached to the WebView.
596edb3792d83e6e299a9d330db938075634e76d14Grace Kloba        if (contentView instanceof SurfaceView) {
606edb3792d83e6e299a9d330db938075634e76d14Grace Kloba            final SurfaceView sView = (SurfaceView) contentView;
616edb3792d83e6e299a9d330db938075634e76d14Grace Kloba            if (sView.isFixedSize()) {
626edb3792d83e6e299a9d330db938075634e76d14Grace Kloba                sView.getHolder().setSizeFromLayout();
636edb3792d83e6e299a9d330db938075634e76d14Grace Kloba            }
646edb3792d83e6e299a9d330db938075634e76d14Grace Kloba        }
657c5bf4666cb8f2f79a61f46f14dd2272b0ba41dbDerek Sollenberger        super.setContentView(contentView);
667c5bf4666cb8f2f79a61f46f14dd2272b0ba41dbDerek Sollenberger        mContentView = contentView;
677c5bf4666cb8f2f79a61f46f14dd2272b0ba41dbDerek Sollenberger    }
687c5bf4666cb8f2f79a61f46f14dd2272b0ba41dbDerek Sollenberger
697c5bf4666cb8f2f79a61f46f14dd2272b0ba41dbDerek Sollenberger    @Override
7011438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba    public void onBackPressed() {
7111438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba        mWebView.mPrivateHandler.obtainMessage(WebView.HIDE_FULLSCREEN)
7211438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba                .sendToTarget();
7311438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba    }
7411438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba
7511438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba    @Override
7611438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba    public boolean onKeyDown(int keyCode, KeyEvent event) {
7711438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba        if (event.isSystem()) {
7811438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba            return super.onKeyDown(keyCode, event);
7911438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba        }
8011438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba        mWebView.onKeyDown(keyCode, event);
8111438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba        // always return true as we are the handler
8211438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba        return true;
8311438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba    }
8411438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba
8511438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba    @Override
8611438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba    public boolean onKeyUp(int keyCode, KeyEvent event) {
8711438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba        if (event.isSystem()) {
8811438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba            return super.onKeyUp(keyCode, event);
8911438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba        }
9011438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba        mWebView.onKeyUp(keyCode, event);
9111438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba        // always return true as we are the handler
9211438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba        return true;
9311438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba    }
9411438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba
9511438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba    @Override
9611438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba    public boolean onTouchEvent(MotionEvent event) {
97c28ff44b1e48c7232065b0a81f64a7accb9d2ebfDerek Sollenberger        // always return true as we don't want the event to propagate any further
9811438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba        return true;
9911438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba    }
10011438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba
10111438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba    @Override
10211438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba    public boolean onTrackballEvent(MotionEvent event) {
10311438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba        mWebView.onTrackballEvent(event);
10411438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba        // always return true as we are the handler
10511438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba        return true;
10611438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba    }
10711438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba
10811438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba    @Override
10911438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba    protected void onStop() {
11011438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba        super.onStop();
1117c5bf4666cb8f2f79a61f46f14dd2272b0ba41dbDerek Sollenberger        // manually remove the contentView's parent since the dialog does not
1127c5bf4666cb8f2f79a61f46f14dd2272b0ba41dbDerek Sollenberger        if (mContentView != null && mContentView.getParent() != null) {
1137c5bf4666cb8f2f79a61f46f14dd2272b0ba41dbDerek Sollenberger            ViewGroup vg = (ViewGroup) mContentView.getParent();
1147c5bf4666cb8f2f79a61f46f14dd2272b0ba41dbDerek Sollenberger            vg.removeView(mContentView);
1157c5bf4666cb8f2f79a61f46f14dd2272b0ba41dbDerek Sollenberger        }
11611438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba        mWebView.getWebViewCore().sendMessage(
11711438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba                WebViewCore.EventHub.HIDE_FULLSCREEN, mNpp, 0);
11811438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba    }
11911438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba
12011438c371e5757bde2a5c08eb81aedb794805fc1Grace Kloba}
121