| 1 | // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | package org.chromium.android_webview; |
| 6 | |
| 7 | import android.view.Gravity; |
| 8 | import android.view.View; |
| 9 | import android.view.ViewGroup; |
| 10 | import android.widget.FrameLayout; |
| 11 | import android.widget.ZoomButtonsController; |
| 12 | import org.chromium.content.browser.ContentViewCore.ZoomControlsDelegate; |
| 13 | |
| 14 | class AwZoomControls implements ZoomControlsDelegate { |
| 15 | |
| 16 | private AwContents mAwContents; |
| 17 | // It is advised to use getZoomController() where possible. |
| 18 | private ZoomButtonsController mZoomButtonsController; |
| 19 | |
| 20 | AwZoomControls(AwContents awContents) { |
| 21 | mAwContents = awContents; |
| 22 | } |
| 23 | |
| 24 | @Override |
| 25 | public void invokeZoomPicker() { |
| 26 | ZoomButtonsController zoomController = getZoomController(); |
| 27 | if (zoomController != null) { |
| 28 | zoomController.setVisible(true); |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | @Override |
| 33 | public void dismissZoomPicker() { |
| 34 | ZoomButtonsController zoomController = getZoomController(); |
| 35 | if (zoomController != null) { |
| 36 | zoomController.setVisible(false); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | @Override |
| 41 | public void updateZoomControls() { |
| 42 | ZoomButtonsController zoomController = getZoomController(); |
| 43 | if (zoomController == null) { |
| 44 | return; |
| 45 | } |
| 46 | boolean canZoomIn = mAwContents.canZoomIn(); |
| 47 | boolean canZoomOut = mAwContents.canZoomOut(); |
| 48 | if (!canZoomIn && !canZoomOut) { |
| 49 | // Hide the zoom in and out buttons if the page cannot zoom |
| 50 | zoomController.getZoomControls().setVisibility(View.GONE); |
| 51 | } else { |
| 52 | // Set each one individually, as a page may be able to zoom in or out |
| 53 | zoomController.setZoomInEnabled(canZoomIn); |
| 54 | zoomController.setZoomOutEnabled(canZoomOut); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | // This method is used in tests. It doesn't modify the state of zoom controls. |
| 59 | View getZoomControlsViewForTest() { |
| 60 | return mZoomButtonsController != null ? mZoomButtonsController.getZoomControls() : null; |
| 61 | } |
| 62 | |
| 63 | private ZoomButtonsController getZoomController() { |
| 64 | if (mZoomButtonsController == null && |
| 65 | mAwContents.getSettings().shouldDisplayZoomControls()) { |
| 66 | mZoomButtonsController = new ZoomButtonsController( |
| 67 | mAwContents.getContentViewCore().getContainerView()); |
| 68 | mZoomButtonsController.setOnZoomListener(new ZoomListener()); |
| 69 | // ZoomButtonsController positions the buttons at the bottom, but in |
| 70 | // the middle. Change their layout parameters so they appear on the |
| 71 | // right. |
| 72 | View controls = mZoomButtonsController.getZoomControls(); |
| 73 | ViewGroup.LayoutParams params = controls.getLayoutParams(); |
| 74 | if (params instanceof FrameLayout.LayoutParams) { |
| 75 | ((FrameLayout.LayoutParams) params).gravity = Gravity.RIGHT; |
| 76 | } |
| 77 | } |
| 78 | return mZoomButtonsController; |
| 79 | } |
| 80 | |
| 81 | private class ZoomListener implements ZoomButtonsController.OnZoomListener { |
| 82 | @Override |
| 83 | public void onVisibilityChanged(boolean visible) { |
| 84 | if (visible) { |
| 85 | // Bring back the hidden zoom controls. |
| 86 | mZoomButtonsController.getZoomControls().setVisibility(View.VISIBLE); |
| 87 | updateZoomControls(); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public void onZoom(boolean zoomIn) { |
| 93 | if (zoomIn) { |
| 94 | mAwContents.zoomIn(); |
| 95 | } else { |
| 96 | mAwContents.zoomOut(); |
| 97 | } |
| 98 | // ContentView will call updateZoomControls after its current page scale |
| 99 | // is got updated from the native code. |
| 100 | } |
| 101 | } |
| 102 | } |