MediaRouter - Display content to second screen

1.4k views Asked by At

I am a rookie to Android and doing my best to learn - don't throw rocks please.

Thanks to the awesome example of @commonsguy at: https://github.com/commonsguy/cw-omnibus/blob/master/Presentation/Simple/app/src/main/java/com/commonsware/android/preso/simple/MainActivity.java

I can display a website to the secondary display (my case a Miracast external monitor).

  1. Currently the website is displayed but I can't interact with it (scroll up, down, click links, enter username, password etc.). Is there a way to make this area as input ready? (have a hardware keyboard and mouse attached and would love to be able to use these to navigate the page)

Have added logic from Why is Android WebView refusing user input? but it did not help either.

/**
 Copyright (c) 2013 CommonsWare, LLC
 Licensed under the Apache License, Version 2.0 (the "License"); you may not
 use this file except in compliance with the License. You may obtain a copy
 of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
 by applicable law or agreed to in writing, software distributed under the
 License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
 OF ANY KIND, either express or implied. See the License for the specific
 language governing permissions and limitations under the License.

 Covered in detail in the book _The Busy Coder's Guide to Android Development_
 https://commonsware.com/Android
 */

package com.example.danielsz.simpledisplay;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Presentation;
import android.content.Context;
import android.media.MediaRouter;
import android.media.MediaRouter.RouteInfo;
import android.media.MediaRouter.SimpleCallback;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.WebView;

public class MainActivity extends Activity {
    MediaRouter router=null;
    Presentation preso=null;
    SimpleCallback cb=null;

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

        setContentView(R.layout.activity_main);
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    @Override
    protected void onStart() {
        super.onStart();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            if (cb==null) {
                cb=new RouteCallback();
                router=(MediaRouter)getSystemService(MEDIA_ROUTER_SERVICE);
            }

            handleRoute(router.getSelectedRoute(MediaRouter.ROUTE_TYPE_LIVE_VIDEO));
            router.addCallback(MediaRouter.ROUTE_TYPE_LIVE_VIDEO, cb);
        }
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    @Override
    protected void onStop() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            clearPreso();

            if (router != null) {
                router.removeCallback(cb);
            }
        }

        super.onStop();
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    private void handleRoute(RouteInfo route) {
        if (route == null) {
            clearPreso();
        }
        else {
            Display display=route.getPresentationDisplay();

            if (route.isEnabled() && display != null) {
                if (preso == null) {
                    showPreso(route);
                    Log.d(getClass().getSimpleName(), "enabled route");
                }
                else if (preso.getDisplay().getDisplayId() != display.getDisplayId()) {
                    clearPreso();
                    showPreso(route);
                    Log.d(getClass().getSimpleName(), "switched route");
                }
                else {
                    // no-op: should already be set
                }
            }
            else {
                clearPreso();
                Log.d(getClass().getSimpleName(), "disabled route");
            }
        }
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    private void clearPreso() {
        if (preso != null) {
            preso.dismiss();
            preso=null;
        }
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    private void showPreso(RouteInfo route) {
        preso=new SimplePresentation(this, route.getPresentationDisplay());
        preso.show();
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    private class RouteCallback extends SimpleCallback {
        @Override
        public void onRoutePresentationDisplayChanged(MediaRouter router,
                                                      RouteInfo route) {
            handleRoute(route);
        }
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    private class SimplePresentation extends Presentation {
        SimplePresentation(Context ctxt, Display display) {
            super(ctxt, display);
        }

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

            WebView wv=new WebView(getContext());


            wv.getSettings().setJavaScriptEnabled(true);
            wv.loadUrl("https://ctxtsprodeme.xendesktop.net/");

            setContentView(wv);
        }
    }
}

Many thanks. Daniel

1

There are 1 answers

2
CommonsWare On BEST ANSWER

Is there a way to make this area as input ready?

No, sorry. External displays are display-only in standard Android. You can collect input on the primary display (e.g., your phone or tablet) and use that to affect the contents on the external display (e.g., call pageDown() or pageUp() on the WebView), but that's it.