1/*
2 * Copyright (C) 2010 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 */
16package android.webkit;
17
18import android.content.Context;
19import android.os.Handler;
20import android.view.LayoutInflater;
21import android.view.View;
22import android.view.ViewConfiguration;
23import android.view.View.OnClickListener;
24import android.view.animation.AlphaAnimation;
25import android.widget.FrameLayout;
26
27@Deprecated
28class ZoomControlExternal implements ZoomControlBase {
29
30    // The time that the external controls are visible before fading away
31    private static final long ZOOM_CONTROLS_TIMEOUT =
32            ViewConfiguration.getZoomControlsTimeout();
33    // The view containing the external zoom controls
34    private ExtendedZoomControls mZoomControls;
35    private Runnable mZoomControlRunnable;
36    private final Handler mPrivateHandler = new Handler();
37
38    private final WebView mWebView;
39
40    public ZoomControlExternal(WebView webView) {
41        mWebView = webView;
42    }
43
44    public void show() {
45        if(mZoomControlRunnable != null) {
46            mPrivateHandler.removeCallbacks(mZoomControlRunnable);
47        }
48        getControls().show(true);
49        mPrivateHandler.postDelayed(mZoomControlRunnable, ZOOM_CONTROLS_TIMEOUT);
50    }
51
52    public void hide() {
53        if (mZoomControlRunnable != null) {
54            mPrivateHandler.removeCallbacks(mZoomControlRunnable);
55        }
56        if (mZoomControls != null) {
57            mZoomControls.hide();
58        }
59    }
60
61    public boolean isVisible() {
62        return mZoomControls != null && mZoomControls.isShown();
63    }
64
65    public void update() { }
66
67    public ExtendedZoomControls getControls() {
68        if (mZoomControls == null) {
69            mZoomControls = createZoomControls();
70
71            /*
72             * need to be set to VISIBLE first so that getMeasuredHeight() in
73             * {@link #onSizeChanged()} can return the measured value for proper
74             * layout.
75             */
76            mZoomControls.setVisibility(View.VISIBLE);
77            mZoomControlRunnable = new Runnable() {
78                public void run() {
79                    /* Don't dismiss the controls if the user has
80                     * focus on them. Wait and check again later.
81                     */
82                    if (!mZoomControls.hasFocus()) {
83                        mZoomControls.hide();
84                    } else {
85                        mPrivateHandler.removeCallbacks(mZoomControlRunnable);
86                        mPrivateHandler.postDelayed(mZoomControlRunnable,
87                                ZOOM_CONTROLS_TIMEOUT);
88                    }
89                }
90            };
91        }
92        return mZoomControls;
93    }
94
95    private ExtendedZoomControls createZoomControls() {
96        ExtendedZoomControls zoomControls = new ExtendedZoomControls(mWebView.getContext());
97        zoomControls.setOnZoomInClickListener(new OnClickListener() {
98            public void onClick(View v) {
99                // reset time out
100                mPrivateHandler.removeCallbacks(mZoomControlRunnable);
101                mPrivateHandler.postDelayed(mZoomControlRunnable, ZOOM_CONTROLS_TIMEOUT);
102                mWebView.zoomIn();
103            }
104        });
105        zoomControls.setOnZoomOutClickListener(new OnClickListener() {
106            public void onClick(View v) {
107                // reset time out
108                mPrivateHandler.removeCallbacks(mZoomControlRunnable);
109                mPrivateHandler.postDelayed(mZoomControlRunnable, ZOOM_CONTROLS_TIMEOUT);
110                mWebView.zoomOut();
111            }
112        });
113        return zoomControls;
114    }
115
116    private static class ExtendedZoomControls extends FrameLayout {
117
118        private android.widget.ZoomControls mPlusMinusZoomControls;
119
120        public ExtendedZoomControls(Context context) {
121            super(context, null);
122            LayoutInflater inflater = (LayoutInflater)
123                    context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
124            inflater.inflate(com.android.internal.R.layout.zoom_magnify, this, true);
125            mPlusMinusZoomControls = (android.widget.ZoomControls) findViewById(
126                    com.android.internal.R.id.zoomControls);
127            findViewById(com.android.internal.R.id.zoomMagnify).setVisibility(
128                    View.GONE);
129        }
130
131        public void show(boolean showZoom) {
132            mPlusMinusZoomControls.setVisibility(showZoom ? View.VISIBLE : View.GONE);
133            fade(View.VISIBLE, 0.0f, 1.0f);
134        }
135
136        public void hide() {
137            fade(View.GONE, 1.0f, 0.0f);
138        }
139
140        private void fade(int visibility, float startAlpha, float endAlpha) {
141            AlphaAnimation anim = new AlphaAnimation(startAlpha, endAlpha);
142            anim.setDuration(500);
143            startAnimation(anim);
144            setVisibility(visibility);
145        }
146
147        public boolean hasFocus() {
148            return mPlusMinusZoomControls.hasFocus();
149        }
150
151        public void setOnZoomInClickListener(OnClickListener listener) {
152            mPlusMinusZoomControls.setOnZoomInClickListener(listener);
153        }
154
155        public void setOnZoomOutClickListener(OnClickListener listener) {
156            mPlusMinusZoomControls.setOnZoomOutClickListener(listener);
157        }
158    }
159}
160