view app/src/main/java/net/bgreco/gmapzoominvert/GMapZoomInvert.java @ 2:fbccc77ea0e6

Convert to Android Studio project
author Brad Greco <brad@bgreco.net>
date Wed, 05 Sep 2018 20:07:35 -0400
parents GMapZoomInvert/src/net/bgreco/gmapzoominvert/GMapZoomInvert.java@30f975176f8e
children c1204965159f
line wrap: on
line source

/* Copyright (c) 2014 Brad Greco <brad@bgreco.net>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

package net.bgreco.gmapzoominvert;

import static de.robv.android.xposed.XposedHelpers.findAndHookMethod;
import android.view.MotionEvent;
import de.robv.android.xposed.IXposedHookLoadPackage;
import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.callbacks.XC_LoadPackage.LoadPackageParam;

public class GMapZoomInvert implements IXposedHookLoadPackage {

	@Override
	public void handleLoadPackage(LoadPackageParam lpparam) throws Throwable {
		if(!lpparam.packageName.equals("com.google.android.apps.maps"))
			return;
		
		findAndHookMethod("com.google.android.apps.gmm.map.legacy.internal.vector.VectorMapViewImpl", lpparam.classLoader, "onTouchEvent", MotionEvent.class, new XC_MethodHook() {

			private float lastTapX;
			private float lastTapY;
			private long lastTapTime = 0;
			private boolean doubleTap = false;
			private boolean zooming = false;
			
			@Override
			protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
				MotionEvent e = (MotionEvent) param.args[0];
				int action = e.getAction();
				// Detect double taps
				if(action == MotionEvent.ACTION_DOWN) {
					// Do our best to mimic the Google Maps double tap detection logic
					if(e.getEventTime() < lastTapTime + 250) {
						if(e.getX() - lastTapX > -150 && e.getX() - lastTapX < 100 && e.getY() - lastTapY > -150 && e.getY() - lastTapY < 100) {
							doubleTap = true;
						}
					}
					lastTapTime = e.getEventTime();
					lastTapX = e.getX();
					lastTapY = e.getY();
				} else if(action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
					// Also Invert the Y axis on release, otherwise the zoom will snap back
					if(zooming) {
						e.setLocation(e.getX(), lastTapY - (e.getY() - lastTapY));
						param.args[0] = e;
					}
					doubleTap = false;
					zooming = false;
				} else if(action == MotionEvent.ACTION_MOVE) {
					// Invert the Y axis of the zoom gesture
					if(e.getPointerCount() == 1) {
						if(doubleTap) {
							//Log.d("gmapzoominvert", "y offset: " + (lastTapY - e.getY()));
							e.setLocation(e.getX(), lastTapY - (e.getY() - lastTapY));
							param.args[0] = e;
							zooming = true;
						}
					} else {
						// Another finger pressed cancels the zoom gesture
						doubleTap = false;
						zooming = false;
					}
				}
			}
			
		});
	}

}