Derek Sollenberger | 90b6e48 | 2010-05-10 12:38:54 -0400 | [diff] [blame] | 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 | */ |
| 16 | package android.webkit; |
| 17 | |
| 18 | import android.view.Gravity; |
| 19 | import android.view.View; |
| 20 | import android.view.ViewGroup; |
| 21 | import android.widget.FrameLayout; |
| 22 | import android.widget.Toast; |
| 23 | import android.widget.ZoomButtonsController; |
| 24 | |
| 25 | class 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 Sollenberger | 369aca2 | 2010-06-09 14:11:59 -0400 | [diff] [blame] | 39 | if (!getControls().isVisible() && !mZoomManager.isZoomScaleFixed()) { |
Derek Sollenberger | 90b6e48 | 2010-05-10 12:38:54 -0400 | [diff] [blame] | 40 | |
| 41 | mZoomButtonsController.setVisible(true); |
| 42 | |
| 43 | WebSettings settings = mWebView.getSettings(); |
| 44 | int count = settings.getDoubleTapToastCount(); |
Derek Sollenberger | 15c5ddb | 2010-06-10 12:31:29 -0400 | [diff] [blame] | 45 | if (mZoomManager.isInZoomOverview() && count > 0) { |
Derek Sollenberger | 90b6e48 | 2010-05-10 12:38:54 -0400 | [diff] [blame] | 46 | 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 Sollenberger | af39e4b | 2010-06-02 12:55:40 -0400 | [diff] [blame] | 69 | boolean canZoomIn = mZoomManager.canZoomIn(); |
Derek Sollenberger | 15c5ddb | 2010-06-10 12:31:29 -0400 | [diff] [blame] | 70 | boolean canZoomOut = mZoomManager.canZoomOut() && !mZoomManager.isInZoomOverview(); |
Derek Sollenberger | 90b6e48 | 2010-05-10 12:38:54 -0400 | [diff] [blame] | 71 | 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 | } |