1 /*
2  * Copyright (C) 2023 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 
17 package com.android.DeviceAsWebcam;
18 
19 import android.content.Context;
20 import android.util.Range;
21 import android.view.MotionEvent;
22 import android.view.ScaleGestureDetector;
23 
24 /**
25  * A class helps to convert the motion events to the zoom ratio.
26  *
27  * <p>Callers can register {@link android.view.View.OnTouchListener} to the target view and then
28  * pass the received motion events to the {@link #onTouchEvent(MotionEvent)} function in this
29  * class. This class uses {@link ScaleGestureDetector} to calculate the zoom ratio and then invoke
30  * {@link ZoomRatioUpdatedListener#onZoomRatioUpdated(float)} with the resulting zoom ratio.
31  * Callers can use the updated zoom ratio to submit capture request for the zoom function.
32  */
33 public class MotionEventToZoomRatioConverter {
34     private Range<Float> mZoomRatioRange;
35     private float mCurrentZoomRatio = 1.0F;
36     private final ScaleGestureDetector mScaleGestureDetector;
37     private final ZoomRatioUpdatedListener mZoomRatioUpdatedListener;
38     private ScaleGestureDetector.SimpleOnScaleGestureListener mScaleGestureListener =
39             new ScaleGestureDetector.SimpleOnScaleGestureListener() {
40                 @Override
41                 public boolean onScale(ScaleGestureDetector detector) {
42                     mZoomRatioUpdatedListener.onZoomRatioUpdated(
43                             getAndUpdateScaledZoomRatio(detector.getScaleFactor()));
44                     return true;
45                 }
46             };
47 
48     /**
49      * Creates a MotionEventToZoomRatioConverter instance.
50      *
51      * @param applicationContext       used to create a ScaleGestureDetector to convert the motion
52      *                                 events into scale factors.
53      * @param zoomRatioRange           the available zoom ratio range.
54      * @param currentZoomRatio         current zoom ratio setting.
55      * @param zoomRatioUpdatedListener the listener to receive the updated zoom ratio events.
56      */
MotionEventToZoomRatioConverter(Context applicationContext, Range<Float> zoomRatioRange, float currentZoomRatio, ZoomRatioUpdatedListener zoomRatioUpdatedListener)57     public MotionEventToZoomRatioConverter(Context applicationContext,
58             Range<Float> zoomRatioRange,
59             float currentZoomRatio,
60             ZoomRatioUpdatedListener zoomRatioUpdatedListener) {
61         mZoomRatioRange = zoomRatioRange;
62         mCurrentZoomRatio = currentZoomRatio;
63         mScaleGestureDetector = new ScaleGestureDetector(applicationContext, mScaleGestureListener);
64         mZoomRatioUpdatedListener = zoomRatioUpdatedListener;
65     }
66 
67     /**
68      * The function to receive the motion events passed from a
69      * {@link android.view.View.OnTouchListener} of the target view.
70      */
onTouchEvent(MotionEvent motionEvent)71     public boolean onTouchEvent(MotionEvent motionEvent) {
72         return mScaleGestureDetector.onTouchEvent(motionEvent);
73     }
74 
75     /**
76      * Sets the zoom ratio value.
77      */
setZoomRatio(float zoomRatio)78     public void setZoomRatio(float zoomRatio) {
79         mCurrentZoomRatio = zoomRatio;
80     }
81 
82     /**
83      * Resets the converter with the new zoom ratio range setting.
84      */
reset(float currZoomRatio, Range<Float> zoomRatioRange)85     public void reset(float currZoomRatio, Range<Float> zoomRatioRange) {
86         mCurrentZoomRatio = currZoomRatio;
87         mZoomRatioRange = zoomRatioRange;
88     }
89 
getAndUpdateScaledZoomRatio(float scaleFactor)90     private float getAndUpdateScaledZoomRatio(float scaleFactor) {
91         mCurrentZoomRatio = Math.max(mZoomRatioRange.getLower(),
92                 Math.min(mZoomRatioRange.getUpper(), mCurrentZoomRatio * scaleFactor));
93         return mCurrentZoomRatio;
94     }
95 
96     /**
97      * An interface to receive the updated zoom ratio.
98      */
99     interface ZoomRatioUpdatedListener {
100         /**
101          * The callback function which will be invoked after converting the received motion
102          * events to the updated zoom ratio.
103          *
104          * @param updatedZoomRatio the updated zoom ratio.
105          */
onZoomRatioUpdated(float updatedZoomRatio)106         void onZoomRatioUpdated(float updatedZoomRatio);
107     }
108 }
109