view app/src/main/java/net/bgreco/gmapzoominvert/GMapZoomInvert.java @ 3:c1204965159f

Update for new Maps version
author Brad Greco <brad@bgreco.net>
date Wed, 05 Sep 2018 21:23:32 -0400
parents fbccc77ea0e6
children
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.app.AndroidAppHelper;
import android.view.MotionEvent;
import android.widget.Toast;

import de.robv.android.xposed.IXposedHookLoadPackage;
import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.callbacks.XC_LoadPackage.LoadPackageParam;

public class GMapZoomInvert implements IXposedHookLoadPackage {

    @Override
    public void handleLoadPackage(LoadPackageParam lpparam) {
        if(!lpparam.packageName.equals("com.google.android.apps.maps"))
            return;

        try {
            findAndHookMethod("com.google.android.apps.gmm.base.views.map.MapViewContainer", lpparam.classLoader, "onInterceptTouchEvent", MotionEvent.class, motionEventHook);
        } catch (Throwable e) {
            XposedBridge.log(e);
            XposedBridge.log("Could not hook MapViewContainer. Trying VectorMapViewImpl.");
            try {
                findAndHookMethod("com.google.android.apps.gmm.map.legacy.internal.vector.VectorMapViewImpl", lpparam.classLoader, "onTouchEvent", MotionEvent.class, motionEventHook);
            } catch (Throwable e2) {
                XposedBridge.log(e2);
                XposedBridge.log("Could not hook VectorMapViewImpl.");
            }
        }
    }

    private XC_MethodHook motionEventHook = 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) {
            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;
                }
            }
        }
    };
}