1 // Copyright 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.annotation.SuppressLint;
8 import android.view.Gravity;
9 import android.view.View;
10 import android.view.ViewGroup;
11 import android.widget.FrameLayout;
12 
13 import androidx.annotation.VisibleForTesting;
14 
15 // This class is visible purely for tests.
16 public class AwZoomControls {
17     private AwContents mAwContents;
18     // It is advised to use getZoomController() where possible.
19     @SuppressWarnings("deprecation")
20     private android.widget.ZoomButtonsController mZoomButtonsController;
21     private boolean mCanZoomIn;
22     private boolean mCanZoomOut;
23 
AwZoomControls(AwContents awContents)24     AwZoomControls(AwContents awContents) {
25         mAwContents = awContents;
26     }
27 
28     @VisibleForTesting
canZoomIn()29     public boolean canZoomIn() {
30         return mCanZoomIn;
31     }
32 
33     @VisibleForTesting
canZoomOut()34     public boolean canZoomOut() {
35         return mCanZoomOut;
36     }
37 
38     @SuppressWarnings("deprecation")
invokeZoomPicker()39     public void invokeZoomPicker() {
40         android.widget.ZoomButtonsController zoomController = getZoomController();
41         if (zoomController != null) {
42             zoomController.setVisible(true);
43         }
44     }
45 
46     @SuppressWarnings("deprecation")
dismissZoomPicker()47     public void dismissZoomPicker() {
48         android.widget.ZoomButtonsController zoomController = getZoomController();
49         if (zoomController != null) {
50             zoomController.setVisible(false);
51         }
52     }
53 
54     @SuppressWarnings("deprecation")
updateZoomControls()55     public void updateZoomControls() {
56         android.widget.ZoomButtonsController zoomController = getZoomController();
57         if (zoomController == null) {
58             return;
59         }
60         mCanZoomIn = mAwContents.canZoomIn();
61         mCanZoomOut = mAwContents.canZoomOut();
62         if (!mCanZoomIn && !mCanZoomOut) {
63             // Hide the zoom in and out buttons if the page cannot zoom
64             zoomController.getZoomControls().setVisibility(View.GONE);
65         } else {
66             // Set each one individually, as a page may be able to zoom in or out
67             zoomController.setZoomInEnabled(mCanZoomIn);
68             zoomController.setZoomOutEnabled(mCanZoomOut);
69         }
70     }
71 
72     // This method is used in tests. It doesn't modify the state of zoom controls.
getZoomControlsViewForTest()73     View getZoomControlsViewForTest() {
74         return mZoomButtonsController != null ? mZoomButtonsController.getZoomControls() : null;
75     }
76 
77     @SuppressLint("RtlHardcoded")
78     @SuppressWarnings("deprecation")
getZoomController()79     private android.widget.ZoomButtonsController getZoomController() {
80         if (mZoomButtonsController == null
81                 && mAwContents.getSettings().shouldDisplayZoomControls()) {
82             mZoomButtonsController =
83                     new android.widget.ZoomButtonsController(mAwContents.getContainerView());
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     @SuppressWarnings("deprecation")
98     private class ZoomListener implements android.widget.ZoomButtonsController.OnZoomListener {
99         @Override
onVisibilityChanged(boolean visible)100         public void onVisibilityChanged(boolean visible) {
101             if (visible) {
102                 // Bring back the hidden zoom controls.
103                 mZoomButtonsController.getZoomControls().setVisibility(View.VISIBLE);
104                 updateZoomControls();
105             }
106         }
107 
108         @Override
onZoom(boolean zoomIn)109         public void onZoom(boolean zoomIn) {
110             if (zoomIn) {
111                 mAwContents.zoomIn();
112             } else {
113                 mAwContents.zoomOut();
114             }
115             // ContentView will call updateZoomControls after its current page scale
116             // is got updated from the native code.
117         }
118     }
119 }
120