package com.example.reproductordelistasmultimedia;

import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class StreamingWebView extends Activity {

    //Nuestra vista necesaria para la web
    private WebView mWebView;
    private Handler mHandler;
    ProgressDialog dialogo;

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.visorweb);

        // Asociamos
        mWebView = (WebView) findViewById(R.id.webview);

        mWebView.getSettings().setSavePassword(false);
        mWebView.getSettings().setSaveFormData(false);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setBuiltInZoomControls(true);
        mWebView.setWebChromeClient(new MyWebChromeClient());
        mWebView.addJavascriptInterface(new DemoJavaScriptInterface(), "demo");

        // Cargo la url
        mWebView.loadUrl(ListaCanalesTv.setEnlaceTv());

        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return false;
            }
        });

        // Barra de progreso
        mWebView.setWebViewClient(new WebViewClient(){
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                dialogo = new ProgressDialog(StreamingWebView.this);
                dialogo.setMessage("Cargando...");
                dialogo.setCancelable(true);
                dialogo.show();
            }
            @Override
            public void onPageFinished(WebView view, String url) {
                dialogo.dismiss();
            }
        });
    }

    final class DemoJavaScriptInterface {

        DemoJavaScriptInterface() {
        }
        /**
         * No es llamada en la interfaz. Crea un runnable para invocar
         * loadUrl en la hebra de la interfaz.
         */
        public void clickOnAndroid() {
                mHandler.post(new Runnable() {
                    public void run() {
                        mWebView.loadUrl("javascript:wave()");
                    }
                });
        }
    }

    /**
     * "alerta" para javascript. Util para depurar javascript.
     */
    final class MyWebChromeClient extends WebChromeClient {
        @Override
        public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
            result.confirm();
            return true;
        }
    }
}

