Home | History | Annotate | Download | only in webkit
      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 WebViewClassic mWebView;
     29 
     30     // The controller is lazily initialized in getControls() for performance.
     31     private ZoomButtonsController mZoomButtonsController;
     32 
     33     public ZoomControlEmbedded(ZoomManager zoomManager, WebViewClassic webView) {
     34         mZoomManager = zoomManager;
     35         mWebView = webView;
     36     }
     37 
     38     public void show() {
     39         if (!getControls().isVisible() && !mZoomManager.isZoomScaleFixed()) {
     40 
     41             mZoomButtonsController.setVisible(true);
     42 
     43             if (mZoomManager.isDoubleTapEnabled()) {
     44                 WebSettingsClassic settings = mWebView.getSettings();
     45                 int count = settings.getDoubleTapToastCount();
     46                 if (mZoomManager.isInZoomOverview() && count > 0) {
     47                     settings.setDoubleTapToastCount(--count);
     48                     Toast.makeText(mWebView.getContext(),
     49                             com.android.internal.R.string.double_tap_toast,
     50                             Toast.LENGTH_LONG).show();
     51                 }
     52             }
     53         }
     54     }
     55 
     56     public void hide() {
     57         if (mZoomButtonsController != null) {
     58             mZoomButtonsController.setVisible(false);
     59         }
     60     }
     61 
     62     public boolean isVisible() {
     63         return mZoomButtonsController != null && mZoomButtonsController.isVisible();
     64     }
     65 
     66     public void update() {
     67         if (mZoomButtonsController == null) {
     68             return;
     69         }
     70 
     71         boolean canZoomIn = mZoomManager.canZoomIn();
     72         boolean canZoomOut = mZoomManager.canZoomOut() && !mZoomManager.isInZoomOverview();
     73         if (!canZoomIn && !canZoomOut) {
     74             // Hide the zoom in and out buttons if the page cannot zoom
     75             mZoomButtonsController.getZoomControls().setVisibility(View.GONE);
     76         } else {
     77             // Set each one individually, as a page may be able to zoom in or out
     78             mZoomButtonsController.setZoomInEnabled(canZoomIn);
     79             mZoomButtonsController.setZoomOutEnabled(canZoomOut);
     80         }
     81     }
     82 
     83     private ZoomButtonsController getControls() {
     84         if (mZoomButtonsController == null) {
     85             mZoomButtonsController = new ZoomButtonsController(mWebView.getWebView());
     86             mZoomButtonsController.setOnZoomListener(new ZoomListener());
     87             // ZoomButtonsController positions the buttons at the bottom, but in
     88             // the middle. Change their layout parameters so they appear on the
     89             // right.
     90             View controls = mZoomButtonsController.getZoomControls();
     91             ViewGroup.LayoutParams params = controls.getLayoutParams();
     92             if (params instanceof FrameLayout.LayoutParams) {
     93                 ((FrameLayout.LayoutParams) params).gravity = Gravity.RIGHT;
     94             }
     95         }
     96         return mZoomButtonsController;
     97     }
     98 
     99     private class ZoomListener implements ZoomButtonsController.OnZoomListener {
    100 
    101         public void onVisibilityChanged(boolean visible) {
    102             if (visible) {
    103                 mWebView.switchOutDrawHistory();
    104                 // Bring back the hidden zoom controls.
    105                 mZoomButtonsController.getZoomControls().setVisibility(View.VISIBLE);
    106                 update();
    107             }
    108         }
    109 
    110         public void onZoom(boolean zoomIn) {
    111             if (zoomIn) {
    112                 mWebView.zoomIn();
    113             } else {
    114                 mWebView.zoomOut();
    115             }
    116             update();
    117         }
    118     }
    119 }
    120