Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 79b653f2 authored by Marvin W.'s avatar Marvin W. 🐿️
Browse files

Add snapshot feature to Map

parent e45f03b7
Loading
Loading
Loading
Loading
+66 −0
Original line number Diff line number Diff line
@@ -17,15 +17,22 @@
package org.microg.gms.maps;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;

import com.google.android.gms.maps.internal.ISnapshotReadyCallback;
import com.google.android.gms.maps.model.CameraPosition;

import org.microg.gms.maps.camera.CameraUpdate;
import org.microg.gms.maps.markup.DrawableMarkup;
import org.microg.gms.maps.markup.MarkerItemMarkup;
import org.microg.gms.maps.markup.Markup;
import org.oscim.backend.GL;
import org.oscim.backend.GLAdapter;
import org.oscim.core.MapPosition;
import org.oscim.core.Point;
import org.oscim.event.Event;
@@ -35,6 +42,7 @@ import org.oscim.layers.marker.MarkerItem;
import org.oscim.layers.vector.geometries.Drawable;
import org.oscim.map.Viewport;

import java.nio.IntBuffer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
@@ -42,6 +50,8 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.microedition.khronos.opengles.GL10;

public class BackendMap implements ItemizedLayer.OnItemGestureListener<MarkerItem>, org.oscim.map.Map.InputListener, org.oscim.map.Map.UpdateListener {
    private final static String TAG = "GmsMapBackend";

@@ -233,6 +243,62 @@ public class BackendMap implements ItemizedLayer.OnItemGestureListener<MarkerIte
        }
    }

    public void snapshot(final Bitmap bitmap, final ISnapshotReadyCallback callback) {
        mapView.queueEvent(new Runnable() {
            @Override
            public void run() {
                Bitmap surface = createBitmapFromGLSurface(0, 0, mapView.getWidth(), mapView.getHeight(), GLAdapter.gl);
                final Bitmap result;
                if (bitmap != null) {
                    Canvas c = new Canvas(bitmap);
                    c.drawBitmap(surface, 0, 0, new Paint());
                    result = bitmap;
                } else {
                    result = surface;
                }
                mapView.getHandler().post(new Runnable() {
                    @Override
                    public void run() {
                        Log.d(TAG, "snapshot result: " + result);
                        try {
                            callback.onBitmapReady(result);
                        } catch (RemoteException e) {
                            Log.w(TAG, e);
                        }
                    }
                });
            }
        });
    }

    private Bitmap createBitmapFromGLSurface(int x, int y, int w, int h, GL gl) {
        int bitmapBuffer[] = new int[w * h];
        int bitmapSource[] = new int[w * h];
        IntBuffer intBuffer = IntBuffer.wrap(bitmapBuffer);
        intBuffer.position(0);

        try {
            gl.readPixels(x, y, w, h, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, intBuffer);
            int offset1, offset2;
            for (int i = 0; i < h; i++) {
                offset1 = i * w;
                offset2 = (h - i - 1) * w;
                for (int j = 0; j < w; j++) {
                    int texturePixel = bitmapBuffer[offset1 + j];
                    int blue = (texturePixel >> 16) & 0xff;
                    int red = (texturePixel << 16) & 0x00ff0000;
                    int pixel = (texturePixel & 0xff00ff00) | red | blue;
                    bitmapSource[offset2 + j] = pixel;
                }
            }
        } catch (Exception e) {
            Log.e(TAG, "createBitmapFromGLSurface: " + e.getMessage(), e);
            return null;
        }

        return Bitmap.createBitmap(bitmapSource, w, h, Bitmap.Config.ARGB_8888);
    }

    public void setZoomGesturesEnabled(boolean enabled) {
        mapView.map().getEventLayer().enableZoom(enabled);
    }
+17 −3
Original line number Diff line number Diff line
@@ -18,11 +18,13 @@ package org.microg.gms.maps;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Parcel;
import android.os.RemoteException;
@@ -80,7 +82,7 @@ import static android.content.pm.PackageManager.PERMISSION_GRANTED;

public class GoogleMapImpl extends IGoogleMapDelegate.Stub
        implements UiSettingsImpl.UiSettingsListener, Markup.MarkupListener, BackendMap.CameraUpdateListener {
    private static final String TAG = "GoogleMapImpl";
    private static final String TAG = "GmsMapImpl";

    private final GoogleMapOptions options;
    private final Context context;
@@ -525,8 +527,18 @@ public class GoogleMapImpl extends IGoogleMapDelegate.Stub
    }

    @Override
    public void setOnMapLoadedCallback(IOnMapLoadedCallback callback) throws RemoteException {
    public void setOnMapLoadedCallback(final IOnMapLoadedCallback callback) throws RemoteException {
        Log.d(TAG, "not yet usable: setOnMapLoadedCallback");
        new Handler(context.getMainLooper()).postDelayed(new Runnable() {
            @Override
            public void run() {
                try {
                    callback.onMapLoaded();
                } catch (RemoteException e) {
                    Log.w(TAG, e);
                }
            }
        }, 5000);
    }
    
    /*
@@ -541,7 +553,9 @@ public class GoogleMapImpl extends IGoogleMapDelegate.Stub
    @Override
    public void snapshot(ISnapshotReadyCallback callback, IObjectWrapper bitmap)
            throws RemoteException {

        Bitmap b = (Bitmap) ObjectWrapper.unwrap(bitmap);
        Log.d(TAG, "snapshot!: " + b);
        backendMap.snapshot(b, callback);
    }

    @Override
+26 −5
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package org.microg.gms.maps;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Parcel;
import android.os.RemoteException;
import android.util.Log;
@@ -37,6 +38,7 @@ public class MapViewImpl extends IMapViewDelegate.Stub {
    private GoogleMapOptions options;
    private Context context;
    private IOnMapReadyCallback readyCallback;
    private boolean isReady = false;

    public MapViewImpl(Context context, GoogleMapOptions options) {
        this.context = context;
@@ -59,13 +61,17 @@ public class MapViewImpl extends IMapViewDelegate.Stub {
    @Override
    public void onCreate(Bundle savedInstanceState) throws RemoteException {
        //myMap().onCreate(savedInstanceState);
        Log.d("MapViewImpl", "onCreate");
        Log.d(TAG, "onCreate");
    }

    @Override
    public void onResume() throws RemoteException {
        myMap().onResume();
        Log.d(TAG, "onResume");
        synchronized (this) {
            isReady = true;
        }

        myMap().onResume();
        if (readyCallback != null) {
            try {
                readyCallback.onMapReady(map);
@@ -74,12 +80,14 @@ public class MapViewImpl extends IMapViewDelegate.Stub {
                Log.w(TAG, e);
            }
        }

    }

    @Override
    public void onPause() throws RemoteException {
        myMap().onPause();
        synchronized (this) {
            isReady = false;
        }
    }

    @Override
@@ -103,8 +111,21 @@ public class MapViewImpl extends IMapViewDelegate.Stub {
    }

    @Override
    public void addOnMapReadyCallback(IOnMapReadyCallback callback) throws RemoteException {
    public synchronized void addOnMapReadyCallback(final IOnMapReadyCallback callback) throws RemoteException {
        if (!isReady) {
            this.readyCallback = callback;
        } else {
            new Handler(context.getMainLooper()).post(new Runnable() {
                @Override
                public void run() {
                    try {
                        callback.onMapReady(map);
                    } catch (RemoteException e) {
                        Log.w(TAG, e);
                    }
                }
            });
        }
    }

    @Override
+1 −0
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@ public class UiSettingsImpl extends IUiSettingsDelegate.Stub {
    private boolean zoomGesturesEnabled = true;
    private boolean tiltGesturesEnabled = true;
    private boolean rotateGesturesEnabled = true;
    private boolean allGesturesEnabled = true;
    
    private UiSettingsListener listener;