Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Control DOM elements from android app

I'm trying to take control over the Play/Pause html DOM elements (in a browser open in a pc) from an android device.

In the html page (in Google Chrome browser) there's a <video> tag so I can control it like this:

//js code
document.querySelector("video").play();
document.querySelector("video").pause();

But I want that to run from an android device so I'm using GCM.

I read here and got some insight but I still have some questions.

  • First, since I'm writing in eclipse, and it sees no document variable, it produces an error. So how can eclipse recognize that element in the html page so I can compile and install the apk on the device?

  • Where do I specify the page url I want to communicate with? (send play/pause commands)

  • To run js inside java I'm using Rhino. I looked through the examples in the documentation but I'm still not sure if a @JSFunction annotation is enough to declare a js function.

Here's my code:

import com.alaa.chromote.util.SystemUiHider;
import com.google.android.gcm.GCMRegistrar;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;
import android.view.View.OnClickListener;

import org.mozilla.javascript.*;
import org.mozilla.javascript.annotations.*;

public class MainApplication extends Activity {
    private final static String GCM_SENDER_ID = "484514826047";
    private static final String LOG_TAG = "GetAClue::GCMIntentService";

    private Button playButton;
    private Button pauseButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main_application);

        playButton = (Button) findViewById(R.id.PlayButton);
        pauseButton = (Button) findViewById(R.id.PauseButton);

        playButton.setVisibility(View.INVISIBLE);
        pauseButton.setVisibility(View.VISIBLE);

        //connect to gcm
        GCMRegistrar.checkDevice( this );
        GCMRegistrar.checkManifest( this );
        final String regId = GCMRegistrar.getRegistrationId( this );
        if( regId.equals( "" ) ) {
            GCMRegistrar.register( this, GCM_SENDER_ID );
        }
        else {
            Log.v( LOG_TAG, "Already registered" );
        }

        Context.enter(); //start Rhino

        setupListeners();
    }

    @JSFunction
    public void play() { document.querySelector("video").play(); }

    @JSFunction
    public void pause() { document.querySelector("video").pause(); }

    private void setupListeners()
    {
        playButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                play();
            }
        });

        pauseButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                pause();
            }
        });
    }
    @Override
    protected void onStop() {
        Context.exit(); //stop Rhino
        super.onStop();
    }
}

How do I continue from here?

like image 877
Alaa M. Avatar asked Apr 23 '15 17:04

Alaa M.


People also ask

What are input controls in Android?

Next Page Input controls are the interactive components in your app's user interface. Android provides a wide variety of controls you can use in your UI, such as buttons, text fields, seek bars, check box, zoom buttons, toggle buttons, and many more.

What is DOM parser in Android?

In General, a DOM parser loads the XML file into the Android memory to parse the XML document. This results in more consumption of memory. The document is parsed through every possible node in the XML file. The XML file that contains the information to be extracted includes the following four main components:

What are the controls available in Android UI?

Android UI Controls Sr.No. UI Control & Description 1 TextView This control is used to display ... 2 EditText EditText is a predefined subcla ... 3 AutoCompleteTextView The AutoCompleteTex ... 4 Button A push-button that can be pressed ... 9 more rows ...

How to create a device control using the API?

The API provides builder methods to create the controls. To populate the builder, you need to determine the device you want to control and how the user should interact with it. In particular, you need to do the following: Pick the type of device the control represents. The DeviceTypes class is an enumeration of all currently-supported devices.


1 Answers

  • First, since I'm writing in eclipse, and it sees no document variable, it produces an error. So how can eclipse recognize that element in the html page so I can compile and install the apk on the device?

    answ: On your android device you just pass a message to the chrome browser. A.k. an action variable that is set to play or stop. You chrome app will then pick up the message and act accordingly. Also you can send the url as an variable in the message if you want to be able to play different urls.


  • Where do I specify the page url I want to communicate with? (send play/pause commands)?

    answ: Do you already created the chrome app you want and verified it works? It should check with a google cloud server for messages. That server keeps track of the url for you.


  • To run js inside java I'm using Rhino. I looked through the examples in the documentation but I'm still not sure if a @JSFunction annotation is enough to declare a js function.?

    answ: It seems you are misunderstanding the part what the android app does (sending the play action) and what the chrome browser does (actually playing the movie)


I hope my answer has helped a little, feedback is appreciated :)

like image 142
gabn88 Avatar answered Oct 18 '22 02:10

gabn88