Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.
This repository was archived by the owner on Aug 8, 2023. It is now read-only.

Android - Runtime styling Api #5610

Closed
Closed

Description

@ivovandongen

Now that the Runtime styling Api is taking shape in core, we can get started on the android side of things.

Major parts:

  • Add/remove/manipulate layers
    • Expose layers; Background/Fill/Line/Symbol/Raster/Circle
    • Control paint properties (incl visibility)
    • Control layout properties
    • Control filtering
  • Add/remove sources

It's been suggested to generate the Java bindings from the style specification. We can use the same approach as taken for the node bindings, using ejs templates or go for a more Java central approach by using a gradle plugin that transforms json into java classes, like jsonschema2pojo. Depending on integration in the main build or the Android sub-build (gradle).

Because the types (and manipulation of the types) need to go through jni, it's best to make the api flat (no 1-n relations). All manipulation/querying goes through the MapboxMap object. For some of the types we can use subclasses or type IntDef's if the types have little specialisation.

Possible api

Layer Types (generated):

  • BackgroundLayer
  • CircleLayer
  • FillLayer
  • LineLayer
  • RasterLayer
  • SymbolLayer

Related types:

  • LayoutProperty - Each layer supports a set of layout properties
  • PaintProperty - Each layer supports a set of paint properties
  • Filter - Each layer can have a filter

Layer methods on MapboxMap:

  • setFilter()
  • addLayer()
  • removeLayer()
  • setPaintProperty()

Source types:

  • GeoJsonSource
  • RasterSource
  • VectorSource

Source methods on MapboxMap

  • addSource()
  • removeSource()

Activity

  1. added
    Android
    on Jul 7, 2016
  2. added this to the android-v4.2.0 milestone on Jul 7, 2016
  3. bleege commented on Jul 7, 2016

    @bleege
    Contributor

    /cc @mapbox/mobile

  4. ivovandongen commented on Jul 8, 2016

    @ivovandongen
    ContributorAuthor

    Ok, so we have a decision to make as to the level of typing we want in this api. The core api is beautifully typed, which is great to work with. For the android side of things there are some considerations though:

    • A completely typed api adds quite a bit of api surface
    • Passing rich types over JNI is not what we want, performance wise. So it would be translated to simpler types before crossing the boundary, where it would again be converted to core types.

    The other end of the spectrum is a non-typed api (or very basic at least). Passing mostly Strings and values around. This is how the QT api is shaping up, and it's nice and small (see for example setLayoutProperty()). As the type checking is done on the core side anyways, there is not much lost here, except for code completion / compile time checking of correct property types.

    A nice compromise might be, to offer some factory methods to construct properties (as we still want to avoid Enums). This would aid in compiler assistance of type checking per property. This can easily be generated. For example:

    // This file is generated. Do not edit.
    package com.mapbox.mapboxsdk.style.properties;
    
    public class LayoutPropertyFactory {
    
    
        public static LayoutProperty<Float> lineMiterLimit(Float value) {
            return new LayoutProperty<>("line-miter-limit", value);
        }
        public static LayoutProperty<Float> lineRoundLimit(Float value) {
            return new LayoutProperty<>("line-round-limit", value);
        }
    
     ...
    
        public static LayoutProperty<Boolean> textOptional(Boolean value) {
            return new LayoutProperty<>("text-optional", value);
        }
    
        public static final class LayoutProperty<T> {
            public final String name;
            public final T value;
    
            private LayoutProperty(String name, T value) {
                this.name = name;
                this.value = value;
            }
        }
    
    }
    

    cc @tobrun @zugaldia @bleege

  5. zugaldia commented on Jul 8, 2016

    @zugaldia
    Member

    Passing rich types over JNI is not what we want, performance wise.

    Right. Do we foresee that the style API is something that devs would call so often that we'd notice the performance impact?

    The other end of the spectrum is a non-typed api (or very basic at least). Passing mostly Strings and values around. This is how the QT api is shaping up

    I personally like this approach, not only for consistency with Qt and because, as you say, checks are gonna happen anyway at the core level, but also because 1) it's easier to implement (and explain) and 2) it leaves the door open to a higher level API built on top of this one (e.g. factory methods) in the future should customers require it.

  6. tmpsantos commented on Jul 8, 2016

    @tmpsantos
    Contributor

    The other end of the spectrum is a non-typed api (or very basic at least). Passing mostly Strings and values around. This is how the QT api is shaping up

    Another plus of this approach is as the spec changes, you get new properties, types, etc for free.

  7. added a commit that references this issue on Jul 8, 2016
  8. ivovandongen commented on Jul 8, 2016

    @ivovandongen
    ContributorAuthor

    I've added an example of the api we could use if we type the properties (Only layout properties done for this example). See the commit for details.

    So, the main entry point for the user would look like:

    public void setLayoutProperty(String layerId, LayoutProperty<?> property)

    Where the LayoutProperty can be retrieved from LayoutPropertyFactory. Both are generated with ejs templates.

    Advantages:

    • Type safe api for the user. Although not as strong as the core as it doesn't restrict the set of properties per Layer type compile time.
    • Easy lookup of properties / value types

    Disadvantages:

    • Quite a few methods added to the api - this is only the layout properties atm
  9. ivovandongen commented on Jul 8, 2016

    @ivovandongen
    ContributorAuthor

    Right. Do we foresee that the style API is something that devs would call so often that we'd notice the performance impact?

    Could animating properties from Android animators be a valid use case? If so, then yes.

    @zugaldia

  10. jfirebaugh commented on Jul 8, 2016

    @jfirebaugh
    Contributor

    Quick comments:

    • I don't think we can say performance is an advantage either way unless we implement both strong and weakly typed APIs and benchmark them. Whatever JNI overhead there may be could easily be negated by the need to manually typecheck arguments.
    • I think we're likely to eliminate an external distinction between paint and layout properties sometime in the future. To the runtime style API, the main implication of the distinction is that you can set a paint property for a specific paint class. But since the Android SDK is not exposing paint classes otherwise, it shouldn't provide this option for paint properties. So I see little reason to distinguish paint and layout properties in the runtime styling API.
    • I strongly encourage you to implement layer and source types, and not have the entire API live on Map. I.e., assuming a weakly typed API, map.getLayer(layerId) and layer.setProperty(property). (This is likely what gl-js will switch to.)
  11. ivovandongen commented on Jul 8, 2016

    @ivovandongen
    ContributorAuthor

    @jfirebaugh Thanks for the comments. I'll work that in.

    About the JNI part; we've been limiting the amount of callbacks into the runtime lately (see #5103). But since we don't expose that part of the api, we can go either way depending on any performance issues.

  12. bleege commented on Jul 11, 2016

    @bleege
    Contributor

    For continuity, the iOS / mac OS Styling API is simultaneously being worked on in #5626.

  13. added 3 commits that reference this issue on Jul 11, 2016
  14. 116 remaining items

  15. ivovandongen commented on Aug 2, 2016

    @ivovandongen
    ContributorAuthor

    First issue was resolved by rebasing master.

    Second issue still remains. To be more specific, the crash happens when adding a Layer in the onMapReady callback. I've added a test case here to reproduce the issue (thanks @cammace)

    08-02 10:14:44.321 4607-4607/? D/mbgl: {pboxsdk.testapp}[JNI]: nativeAddLayer
    08-02 10:14:44.321 4607-4607/? D/mbgl: {pboxsdk.testapp}[Android]: NativeMapView::activate
    08-02 10:14:44.321 4607-4607/? D/mbgl: {pboxsdk.testapp}[Android]: NativeMapView::deactivate
    08-02 10:14:44.322 4607-4607/? D/mbgl: {pboxsdk.testapp}[JNI]: nativeLatLngForPixel
    08-02 10:14:44.322 4607-4607/? D/mbgl: {pboxsdk.testapp}[JNI]: nativeLatLngForPixel
    08-02 10:14:44.322 4607-4607/? D/mbgl: {pboxsdk.testapp}[JNI]: nativeLatLngForPixel
    08-02 10:14:44.322 4607-4607/? D/mbgl: {pboxsdk.testapp}[JNI]: nativeLatLngForPixel
    08-02 10:14:44.322 4607-4607/? D/mbgl: {pboxsdk.testapp}[JNI]: nativeGetAnnotationsInBounds
    08-02 10:14:44.323 4607-4607/? D/mbgl: {pboxsdk.testapp}[Android]: NativeMapView::notifyMapChange()
    08-02 10:14:44.323 4607-4607/? E/libEGL: call to OpenGL ES API with no current context (logged once per thread)
    08-02 10:14:44.323 4607-4607/? E/mbgl: {pboxsdk.testapp}[Shader]: Vertex shader fill failed to compile: precision highp float;
                                           #ifdef GL_ES
                                           precision highp float;
                                           #else
                                           #define lowp
                                           #define mediump
                                           #define highp
                                           #endif
    
                                           attribute vec2 a_pos;
    
                                           uniform mat4 u_matrix;
    
                                           uniform lowp vec4 u_color;
                                           uniform lowp float u_opacity;
    
                                           void main() {
                                               lowp vec4 color = u_color;
                                               lowp float opacity = u_opacity;
    
                                               gl_Position = u_matrix * vec4(a_pos, 0, 1);
                                           }
    08-02 10:14:44.323 4607-4607/? D/AndroidRuntime: Shutting down VM
    
  16. ivovandongen commented on Aug 2, 2016

    @ivovandongen
    ContributorAuthor

    Merged into master

  17. incanus commented on Aug 15, 2016

    @incanus
    Contributor

    @ivovandongen Assuming the bug mentioned in #6014 is real (in which case we'll need to ticket it separately), otherwise this feature is fully built out, right? Can we close this ticket?

  18. ivovandongen commented on Aug 16, 2016

    @ivovandongen
    ContributorAuthor

    Thought it was closed already. #5852 was the last missing piece.

  19. 1ec5 commented on Sep 28, 2016

    @1ec5
    Contributor

    For posterity, the PRs that implemented the initial buildout were #5734 and #5830.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Android

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions