TheeMachine / presentation-capacitor

4 stars 4 forks source link

openLink function only sending string? #1

Open schudav opened 1 year ago

schudav commented 1 year ago

Hello there I tried your plugin, and was attempting to open a page on a secondary display.

Stuff actually shows on the display, so it appears to be working, however it just prints the string instead of opening the URL.

import { CapacitorPresentation } from 'presentation-capacitor' 
async function showDisplay() {
    await CapacitorPresentation.openLink({url: '/testpage'});
}

This results in the display with a white screen showing "/testpage".

I'm using sveltekit and capacitor

TheeMachine commented 1 year ago

Hello, did you try on web or android? I'm still developing the package. But right now; Url works only Web. Html strings works with android ( nowadays I'll add html strings feature to web if PresentationRequest API has capability)

schudav commented 1 year ago

Oh I see, thanks for the response!

It is an android app.

Do you mean "web" as in desktop/normal computer?

TheeMachine commented 1 year ago

Yes. Sorry, I should have spoken more clearly. I use PresentationRequest Web API for web and this API works in most web browser and some of mobile browser. I'm attaching the link below for detail information;

PresentationRequest Web API

Browser Compatibility

schudav commented 1 year ago

Oh I understand now! I tested the code with Chrome on windows/desktop and it opens the URL.

I will wait for whenever they add support for android/webview, hopefully sooner than later! Thanks again for your efforts

metaroute-alex commented 1 year ago

maybe can test with the follow change:

  1. change

       if(!url.startsWith("https://")) {
            path = Uri.parse("file:///android_asset/public/index.html?route=" + url).toString();
        } else {
            path = url;
        }

    to:

    if(!url.startsWith("https://") && !url.startsWith("http://")) {
            path = Uri.parse("file:///android_asset/public/" + url).toString();
        } else {
            path = url;
        }
  2. add

      webView.setWebChromeClient(new WebChromeClient());
  3. change

     webView.loadDataWithBaseURL(null, path, "text/html", "UTF-8", null);

    to

    webView.loadUrl(path);

the full code like this:

    public class SecondaryDisplay extends Presentation {

    CapacitorPresentationPlugin capPlugin = new CapacitorPresentationPlugin();

    protected String url = "";

    public SecondaryDisplay(Context outerContext, Display display) {
        super(outerContext, display);
    }

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_secondary_display);

        WebView webView = findViewById(R.id.secondary_webview);
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        webSettings.setDomStorageEnabled(true);
        webSettings.setDatabaseEnabled(true);
        webSettings.setMediaPlaybackRequiresUserGesture(false);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setAppCacheEnabled(true);
        webSettings.setAllowContentAccess(true);
        webSettings.setAllowFileAccess(true);
        webSettings.setUseWideViewPort(true);
        webSettings.setSupportMultipleWindows(true);
        webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);

        String path = url;

        if(!url.startsWith("https://") && !url.startsWith("http://")) {
            path = Uri.parse("file:///android_asset/public/" + url).toString();
        } else {
            path = url;
        }

        webView.setWebChromeClient(new WebChromeClient());

        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String _url) {
                capPlugin.notifyToSuccess(webView, _url);
            }

            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    capPlugin.notifyToFail(webView, error.getErrorCode());
                } else {
                    capPlugin.notifyToFail(webView, 400);
                }
            }
        });
        webView.loadUrl(path);
    }

    public void loadUrl(String url) {
       this.url = url;
    }
}
TheeMachine commented 1 year ago

@metaroute-alex Thank you for sharing code. I'm not home right now because of Turkiye Earthquake. I'll test this code asap.

schudav commented 4 months ago

@TheeMachine It is nice to see you still working on this. Not sure what is different but it actually opens the html on capacitor (android).

However I had to make a change when running to device (npx cap run android):

Execution failed for task ':presentation-capacitor:compileDebugJavaWithJavac'
~\node_modules\presentation-capacitor\android\src\main\java\com\machine\thee\presentation\SecondaryDisplay.java:43:
        error: cannot find symbol
        webSettings.setAppCacheEnabled(true);
        ^
        symbol:   method setAppCacheEnabled(boolean)

If I comment out that line webSettings.setAppCacheEnabled(true) the project builds and runs.

I'm not really sure what setAppCacheEnabled does, is it an isssue?

Here is the full code that I needed to get secondary display to open html:

~\node_modules\presentation-capacitor\android\src\main\java\com\machine\thee\presentation\SecondaryDisplay.java

public class SecondaryDisplay extends Presentation {

    CapacitorPresentationPlugin capPlugin = new CapacitorPresentationPlugin();

    protected String url = "";

    public SecondaryDisplay(Context outerContext, Display display) {
        super(outerContext, display);
    }

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_secondary_display);

        WebView webView = findViewById(R.id.secondary_webview);
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        webSettings.setDomStorageEnabled(true);
        webSettings.setDatabaseEnabled(true);
        // webSettings.setAppCacheEnabled(true);
        webSettings.setAllowContentAccess(true);
        webSettings.setAllowFileAccess(true);
        webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        String path = url;
        /*
        if(!url.startsWith("https://")) {
            path = Uri.parse("file:///android_asset/public/index.html?route=" + url).toString();
        } else {
            path = url;
        }
         */
        if(!url.startsWith("https://") && !url.startsWith("http://")) {
            path = Uri.parse("file:///android_asset/public/" + url).toString();
        } else {
            path = url;
        }

        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String _url) {
                capPlugin.notifyToSuccess(webView, _url);
            }

            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    capPlugin.notifyToFail(webView, error.getErrorCode());
                } else {
                    capPlugin.notifyToFail(webView, 400);
                }
            }
        });
        // webView.loadDataWithBaseURL(null, path, "text/html", "UTF-8", null);
        webView.loadUrl(path);
    }

    public void loadUrl(String url) {
       this.url = url;

    }
}
TheeMachine commented 4 months ago

@schudav Thanks for your contributions. I will try this code and update npm and github asap.

DanijelPavlovic commented 3 months ago

@TheeMachine is there any eta on this?