blob: c29e72b7667ffedbc27c92718836bb9ae66ce896 [file] [log] [blame]
Derek Sollenberger90b6e482010-05-10 12:38:54 -04001/*
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.view.Gravity;
19import android.view.View;
20import android.view.ViewGroup;
21import android.widget.FrameLayout;
22import android.widget.Toast;
23import android.widget.ZoomButtonsController;
24
25class ZoomControlEmbedded implements ZoomControlBase {
26
27 private final ZoomManager mZoomManager;
28 private final WebView mWebView;
29
30 // The controller is lazily initialized in getControls() for performance.
31 private ZoomButtonsController mZoomButtonsController;
32
33 public ZoomControlEmbedded(ZoomManager zoomManager, WebView webView) {
34 mZoomManager = zoomManager;
35 mWebView = webView;
36 }
37
38 public void show() {
Derek Sollenberger369aca22010-06-09 14:11:59 -040039 if (!getControls().isVisible() && !mZoomManager.isZoomScaleFixed()) {
Derek Sollenberger90b6e482010-05-10 12:38:54 -040040
41 mZoomButtonsController.setVisible(true);
42
43 WebSettings settings = mWebView.getSettings();
44 int count = settings.getDoubleTapToastCount();
Derek Sollenberger15c5ddb2010-06-10 12:31:29 -040045 if (mZoomManager.isInZoomOverview() && count > 0) {
Derek Sollenberger90b6e482010-05-10 12:38:54 -040046 settings.setDoubleTapToastCount(--count);
47 Toast.makeText(mWebView.getContext(),
48 com.android.internal.R.string.double_tap_toast,
49 Toast.LENGTH_LONG).show();
50 }
51 }
52 }
53
54 public void hide() {
55 if (mZoomButtonsController != null) {
56 mZoomButtonsController.setVisible(false);
57 }
58 }
59
60 public boolean isVisible() {
61 return mZoomButtonsController != null && mZoomButtonsController.isVisible();
62 }
63
64 public void update() {
65 if (mZoomButtonsController == null) {
66 return;
67 }
68
Derek Sollenbergeraf39e4b2010-06-02 12:55:40 -040069 boolean canZoomIn = mZoomManager.canZoomIn();
Derek Sollenberger15c5ddb2010-06-10 12:31:29 -040070 boolean canZoomOut = mZoomManager.canZoomOut() && !mZoomManager.isInZoomOverview();
Derek Sollenberger90b6e482010-05-10 12:38:54 -040071 if (!canZoomIn && !canZoomOut) {
72 // Hide the zoom in and out buttons if the page cannot zoom
73 mZoomButtonsController.getZoomControls().setVisibility(View.GONE);
74 } else {
75 // Set each one individually, as a page may be able to zoom in or out
76 mZoomButtonsController.setZoomInEnabled(canZoomIn);
77 mZoomButtonsController.setZoomOutEnabled(canZoomOut);
78 }
79 }
80
81 private ZoomButtonsController getControls() {
82 if (mZoomButtonsController == null) {
83 mZoomButtonsController = new ZoomButtonsController(mWebView);
84 mZoomButtonsController.setOnZoomListener(new ZoomListener());
85 // ZoomButtonsController positions the buttons at the bottom, but in
86 // the middle. Change their layout parameters so they appear on the
87 // right.
88 View controls = mZoomButtonsController.getZoomControls();
89 ViewGroup.LayoutParams params = controls.getLayoutParams();
90 if (params instanceof FrameLayout.LayoutParams) {
91 ((FrameLayout.LayoutParams) params).gravity = Gravity.RIGHT;
92 }
93 }
94 return mZoomButtonsController;
95 }
96
97 private class ZoomListener implements ZoomButtonsController.OnZoomListener {
98
99 public void onVisibilityChanged(boolean visible) {
100 if (visible) {
101 mWebView.switchOutDrawHistory();
102 // Bring back the hidden zoom controls.
103 mZoomButtonsController.getZoomControls().setVisibility(View.VISIBLE);
104 update();
105 }
106 }
107
108 public void onZoom(boolean zoomIn) {
109 if (zoomIn) {
110 mWebView.zoomIn();
111 } else {
112 mWebView.zoomOut();
113 }
114 update();
115 }
116 }
117}