Java Question onGenericMotionEvent on a Google Glass Wrapper Library (GDK.JAR)

Discussion in 'Libraries developers questions' started by Alberto Iglesias, May 7, 2014.

  1. Alberto Iglesias

    Alberto Iglesias Well-Known Member Licensed User

    Dears Users, can you help me with this event onGenericMotionEvent on my new Google Glass Library....

    The HelloWorldActivity.java develop in ADT and work 100%, it´s intercept all types of touch in Google Glass Touchpad.

    The FirstLib.java is a wrapper of gdk.jar and I´m compiling with SIMPLE LIBRARY COMPILER and compile OK and works OK in B4A but the touchs is not intercepting, what I have to do wrong? The code is basicly the same.

    I think the onGenericMotionEvent is not called, but where tu put them?

    Please, only left this to this library works!
     

    Attached Files:

  2. Erel

    Erel Administrator Staff Member Licensed User

    You can handle the onGenericMotionEvent by following these steps:
    1. Add the following class to your library:
    Code:
    package anywheresoftware.b4a.objects;

    import android.app.Activity;
    import android.view.MotionEvent;
    import anywheresoftware.b4a.BA;

    public class ActivityEx extends Activity {

       @Override
       
    public boolean onGenericMotionEvent(MotionEvent event) {
         
    try {
           BA ba = (BA) this.getClass().getField(
    "processBA").get(null);
           ba.raiseEvent(
    null"activity_ongenericmotionevent"event);
           
    return true;
         
    } catch (Exception e) {
           throw new RuntimeException(e);
         }
       }
       @Override
       public void onUserInteraction() {
         super.onUserInteraction();
         try {
           BA ba = (BA) this.getClass().getField("processBA").get(null);
           ba.raiseEvent2(null,true,  "activity_onuserinteraction", false);
         } catch (Exception e) {
           throw new RuntimeException(e);
         }
       }
    }
    Add the following line to your activity:
    Code:
    #Extends: anywheresoftware.b4a.objects.ActivityEx
    And handle the events in your code:
    Code:
    Sub Activity_OnUserInteraction
       
    Log("OnUserInteraction")
    End Sub

    Sub Activity_OnGenericMotionEvent(event As Object)
       
    Log(event)
    End Sub
    The OnUserInteraction is just an example. You can remove it.
     
  3. Alberto Iglesias

    Alberto Iglesias Well-Known Member Licensed User

    I added this class in my library, but when compile with Simple Compile Library, got this error:

    Starting step: Compiling Java code.
    javac 1.7.0_21
    H:\Android\Library\GoogleGlassLibrary\GlassLib\src\anywharesoftware\b4a\sample\FirstLib.java:38: error: class ActivityEx is public, should be declared in a file named ActivityEx.java
    public class ActivityEx extends Activity {
    ^
    1 error

    Error.
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    Create a new file named ActivityEx.java and add it to your project. It should be under: anywheresoftware\b4a\objects.
     
  5. XverhelstX

    XverhelstX Well-Known Member Licensed User

    Hi Erel

    Is there an #Extends annotation available for a B4A Service?

    Tomas
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    No. However there are libraries that extend all kinds of custom services and then bridge between one of the "developer" service and the embedded service.
     
  7. XverhelstX

    XverhelstX Well-Known Member Licensed User

    Any examples of such library?
     
  8. Erel

    Erel Administrator Staff Member Licensed User

    DreamService:

    Code:
    package anywheresoftware.b4a.objects;

    import java.lang.ref.WeakReference;

    import android.content.Context;
    import android.content.Intent;
    import android.service.dreams.DreamService;
    import android.view.ViewGroup;
    import android.view.View.MeasureSpec;
    import android.view.ViewGroup.LayoutParams;
    import anywheresoftware.b4a.BA;
    import anywheresoftware.b4a.BALayout;
    import anywheresoftware.b4a.BA.Events;
    import anywheresoftware.b4a.BA.Hide;
    import anywheresoftware.b4a.BA.ShortName;
    import anywheresoftware.b4a.BA.Version;
    import anywheresoftware.b4a.keywords.Common;
    import anywheresoftware.b4a.objects.drawable.CanvasWrapper;

    @Hide
    public class DreamServiceWrapper extends DreamService{
       
    public static DreamServiceWrapper instance;
       
    public PanelWrapper panel;
       
    public CanvasWrapper canvas = new CanvasWrapper();
       
    public ViewGroup main;
       
    public Daydream dd;
       @Override
       
    public void onCreate() {
         super.onCreate();
         
    try {
           
    Intent i = new Intent(this, Class.forName(getPackageName() + ".dreamservice"));
           
    startService(i);
           instance = this;
           float deviceScale = getApplicationContext().getResources().getDisplayMetrics().density;
        BALayout.setDeviceScale(deviceScale);
         
    } catch (ClassNotFoundException e) {
           throw new RuntimeException("DreamService not found.");
         }
       }

       @Override
       public void onAttachedToWindow () {
         super.onAttachedToWindow();
         main = new MyBALayout(this);
         setContentView(main);
         panel = new PanelWrapper();
       }
       private class MyBALayout extends BALayout {
          public MyBALayout(Context context) {
           super(context);
         }

         @Override
          protected void onSizeChanged (int w, int h, int oldw, int oldh) {
             super.onSizeChanged(w, h, oldw, oldh);
             main.removeAllViews();
             BALayout layout = new BALayout(DreamServiceWrapper.this);
             main.addView(layout, new BALayout.LayoutParams(0, 0, w, h));
             panel.setObject(layout);
             if (dd != null)
               dd.ba.raiseEventFromUI(null, dd.eventName + "_sizechanged");
          }
       }
       @Override
       public void onDreamingStarted () {
         dd.ba.raiseEvent(null, dd.eventName + "_dreamstarted");
       }
       @Override
       public void onDreamingStopped() {
         dd.ba.raiseEvent(null, dd.eventName + "_dreamstopped");
       }
       @Override
      public void onDetachedFromWindow() {
         Common.Log("onDetachedFromWindow");
      }
       /**
        * Daydream is a new "screen saver" feature introduced in Android 4.2.
        *See the Daydream tutorial for more information.
        */
       @Events(values={"DreamStarted", "SizeChanged", "DreamStopped"})
       @ShortName("Daydream")
       @Version(1.0f)
       public static class Daydream {
         @Hide
         public BA ba;
         @Hide
         public String eventName;
         /**
          * Initializes the object and sets the subs that will handle the events.
          */
         public void Initialize(BA ba, String EventName) {
           instance.dd = this;
           this.ba = ba;
           this.eventName = EventName.toLowerCase(BA.cul);
         }
         /**
          * Manually finishes the dream.
          */
         public void Finish() {
           if (instance != null)
             instance.finish();
         }
         /**
          * Returns the main panel.
          */
         public PanelWrapper getPanel() {
           return instance.panel;
         }
         /**
          * A placeholder for Canvas.
          */
         public CanvasWrapper getCanvas() {
           return instance.canvas;
         }
         /**
          * Gets or sets whether user interacts will be handled instead of finishing the dream.
          */
         public void setInteractive(boolean v) {
           instance.setInteractive(v);
         }
         public boolean getInteractive() {
           return instance.isInteractive();
         }
         /**
          * Gets or sets whether the system bar appears.
          */
         public boolean getFullScreen() {
           return instance.isFullscreen();
         }
         public void setFullScreen(boolean v) {
           instance.setFullscreen(v);
         }
         /**
          * Gets or sets whether the screen should stay bright.
          */
         public void setScreenBright(boolean v) {
           instance.setScreenBright(v);
         }
         public boolean getScreenBright() {
           return instance.isScreenBright();
         }
       }

    }
     
    XverhelstX likes this.
  9. g7jjf

    g7jjf Member Licensed User

    I am also trying to compile this code using the Simple Library Compiler but get the following compile error :
    Sorry, but I don't know how to correct this error. I am trying to write a library to interface to the moga controller and this code looks like it could do the job.
     
  10. Erel

    Erel Administrator Staff Member Licensed User

    Which version of Android.jar are you referencing? Try to reference the latest one.
     
  11. g7jjf

    g7jjf Member Licensed User

    I am using your Simple Library Compiler V 1.01. How do I reference Android.jar ?

    I just copied your code in post #2 above to a file called C:\Users\jon\Documents\B4ALibs\moga\src\anywheresoftware\b4a\objects\ActivityEx.java. Ran your SLC, specified the project folder as C:\Users\jon\Documents\B4ALibs\moga, clicked compile and got the error mentioned above.
     
  12. Erel

    Erel Administrator Staff Member Licensed User

    SLC uses the same android.jar that the IDE is configured to use (Tools - Configure Paths).
     
  13. g7jjf

    g7jjf Member Licensed User

    Thanks, got it to compile OK now by choosing the API Level 12 version of android.jar.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice