| 1 | // Copyright (c) 2012 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.content.browser; |
| 6 | |
| 7 | import android.content.Context; |
| 8 | import android.util.Log; |
| 9 | import android.view.MotionEvent; |
| 10 | import android.view.ScaleGestureDetector; |
| 11 | |
| 12 | /** |
| 13 | * ZoomManager is responsible for maintaining the ContentView's current zoom |
| 14 | * level state and process scaling-related gestures. |
| 15 | */ |
| 16 | class ZoomManager { |
| 17 | private static final String TAG = "ContentViewZoom"; |
| 18 | |
| 19 | private ContentViewCore mContentViewCore; |
| 20 | |
| 21 | private class ScaleGestureListener implements ScaleGestureDetector.OnScaleGestureListener { |
| 22 | // Completely silence scaling events. Used in WebView when zoom support |
| 23 | // is turned off. |
| 24 | private boolean mPermanentlyIgnoreDetectorEvents = false; |
| 25 | // Bypass events through the detector to maintain its state. Used when |
| 26 | // renderes already handles the touch event. |
| 27 | private boolean mTemporarilyIgnoreDetectorEvents = false; |
| 28 | |
| 29 | // Whether any pinch zoom event has been sent to native. |
| 30 | private boolean mPinchEventSent; |
| 31 | |
| 32 | boolean getPermanentlyIgnoreDetectorEvents() { |
| 33 | return mPermanentlyIgnoreDetectorEvents; |
| 34 | } |
| 35 | |
| 36 | void setPermanentlyIgnoreDetectorEvents(boolean value) { |
| 37 | // Note that returning false from onScaleBegin / onScale makes the |
| 38 | // gesture detector not to emit further scaling notifications |
| 39 | // related to this gesture. Thus, if detector events are enabled in |
| 40 | // the middle of the gesture, we don't need to do anything. |
| 41 | mPermanentlyIgnoreDetectorEvents = value; |
| 42 | } |
| 43 | |
| 44 | void setTemporarilyIgnoreDetectorEvents(boolean value) { |
| 45 | mTemporarilyIgnoreDetectorEvents = value; |
| 46 | } |
| 47 | |
| 48 | @Override |
| 49 | public boolean onScaleBegin(ScaleGestureDetector detector) { |
| 50 | if (ignoreDetectorEvents()) return false; |
| 51 | mPinchEventSent = false; |
| 52 | mContentViewCore.getContentViewGestureHandler().setIgnoreSingleTap(true); |
| 53 | return true; |
| 54 | } |
| 55 | |
| 56 | @Override |
| 57 | public void onScaleEnd(ScaleGestureDetector detector) { |
| 58 | if (!mPinchEventSent || !mContentViewCore.isAlive()) return; |
| 59 | mContentViewCore.getContentViewGestureHandler().pinchEnd(detector.getEventTime()); |
| 60 | mPinchEventSent = false; |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public boolean onScale(ScaleGestureDetector detector) { |
| 65 | if (ignoreDetectorEvents()) return false; |
| 66 | // It is possible that pinchBegin() was never called when we reach here. |
| 67 | // This happens when webkit handles the 2nd touch down event. That causes |
| 68 | // ContentView to ignore the onScaleBegin() call. And if webkit does not |
| 69 | // handle the touch move events afterwards, we will face a situation |
| 70 | // that pinchBy() is called without any pinchBegin(). |
| 71 | // To solve this problem, we call pinchBegin() here if it is never called. |
| 72 | if (!mPinchEventSent) { |
| 73 | mContentViewCore.getContentViewGestureHandler().pinchBegin(detector.getEventTime(), |
| 74 | (int) detector.getFocusX(), (int) detector.getFocusY()); |
| 75 | mPinchEventSent = true; |
| 76 | } |
| 77 | mContentViewCore.getContentViewGestureHandler().pinchBy( |
| 78 | detector.getEventTime(), (int) detector.getFocusX(), (int) detector.getFocusY(), |
| 79 | detector.getScaleFactor()); |
| 80 | return true; |
| 81 | } |
| 82 | |
| 83 | private boolean ignoreDetectorEvents() { |
| 84 | return mPermanentlyIgnoreDetectorEvents || |
| 85 | mTemporarilyIgnoreDetectorEvents || |
| 86 | !mContentViewCore.isAlive(); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | private ScaleGestureDetector mMultiTouchDetector; |
| 91 | private ScaleGestureListener mMultiTouchListener; |
| 92 | |
| 93 | ZoomManager(final Context context, ContentViewCore contentViewCore) { |
| 94 | mContentViewCore = contentViewCore; |
| 95 | mMultiTouchListener = new ScaleGestureListener(); |
| 96 | mMultiTouchDetector = new ScaleGestureDetector(context, mMultiTouchListener); |
| 97 | } |
| 98 | |
| 99 | boolean isScaleGestureDetectionInProgress() { |
| 100 | return !mMultiTouchListener.getPermanentlyIgnoreDetectorEvents() |
| 101 | && mMultiTouchDetector.isInProgress(); |
| 102 | } |
| 103 | |
| 104 | // Passes the touch event to ScaleGestureDetector so that its internal |
| 105 | // state won't go wrong, but instructs the listener to ignore the result |
| 106 | // of processing, if any. |
| 107 | void passTouchEventThrough(MotionEvent event) { |
| 108 | mMultiTouchListener.setTemporarilyIgnoreDetectorEvents(true); |
| 109 | try { |
| 110 | mMultiTouchDetector.onTouchEvent(event); |
| 111 | } catch (Exception e) { |
| 112 | Log.e(TAG, "ScaleGestureDetector got into a bad state!", e); |
| 113 | assert(false); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | // Passes the touch event to ScaleGestureDetector so that its internal state |
| 118 | // won't go wrong. ScaleGestureDetector needs two pointers in a MotionEvent |
| 119 | // to recognize a zoom gesture. |
| 120 | boolean processTouchEvent(MotionEvent event) { |
| 121 | // TODO: Need to deal with multi-touch transition |
| 122 | mMultiTouchListener.setTemporarilyIgnoreDetectorEvents(false); |
| 123 | try { |
| 124 | boolean inGesture = isScaleGestureDetectionInProgress(); |
| 125 | boolean retVal = mMultiTouchDetector.onTouchEvent(event); |
| 126 | if (event.getActionMasked() == MotionEvent.ACTION_UP && !inGesture) return false; |
| 127 | return retVal; |
| 128 | } catch (Exception e) { |
| 129 | Log.e(TAG, "ScaleGestureDetector got into a bad state!", e); |
| 130 | assert(false); |
| 131 | } |
| 132 | return false; |
| 133 | } |
| 134 | |
| 135 | void updateMultiTouchSupport(boolean supportsMultiTouchZoom) { |
| 136 | mMultiTouchListener.setPermanentlyIgnoreDetectorEvents(!supportsMultiTouchZoom); |
| 137 | } |
| 138 | } |