package com.github.timnew.smartremotecontrol;

import android.annotation.SuppressLint;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.webkit.ConsoleMessage;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import com.googlecode.androidannotations.annotations.AfterViews;
import com.googlecode.androidannotations.annotations.Bean;
import com.googlecode.androidannotations.annotations.EFragment;
import com.googlecode.androidannotations.annotations.FragmentArg;
import com.googlecode.androidannotations.annotations.ViewById;

@EFragment(R.layout.control_panel_fragment)
public class ControlPanelFragment extends Fragment {

    public static final String PANEL_PATH_TEMPLATE = "file:///android_asset/panels/%s/index.html";
    public static final String PANEL = "Panel";

    @FragmentArg
    protected String layoutUrl;

    @ViewById(R.id.panel)
    protected WebView panel;

    @Bean
    protected InfraredEmitter emitter;

    @Bean
    protected AssetsPathProvider pathProvider;

    @SuppressLint("SetJavaScriptEnabled")
    @AfterViews
    protected void afterView() {
        panel.getSettings().setJavaScriptEnabled(true);
        panel.addJavascriptInterface(emitter, "ir");
        panel.addJavascriptInterface(pathProvider, "path");
        panel.setWebViewClient(new WebViewClient() {
        });

        panel.setWebChromeClient(new WebChromeClient() {
            @Override
            public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
                String messageText = String.format("[%s](#%d):%s", consoleMessage.sourceId(), consoleMessage.lineNumber(), consoleMessage.message());
                Toast.makeText(getActivity(), consoleMessage.message(), Toast.LENGTH_LONG).show();

                switch (consoleMessage.messageLevel()) {
                    case DEBUG:
                        Log.d(PANEL, messageText);
                        break;
                    case ERROR:
                        Log.e(PANEL, messageText);
                        break;
                    case LOG:
                        Log.v(PANEL, messageText);
                        break;
                    case TIP:
                        Log.i(PANEL, messageText);
                        break;
                    case WARNING:
                        Log.w(PANEL, messageText);
                        break;
                }

                return true;
            }
        });

        loadPanel();
    }

    protected void loadPanel() {
        panel.loadUrl(layoutUrl);
    }
}
@@ -76,24 +76,86 @@
 ppressLint;%0A
+import android.annotation.TargetApi;%0Aimport android.os.Build;%0A
 import andro
@@ -276,32 +276,67 @@
 ebChromeClient;%0A
+import android.webkit.WebSettings;%0A
 import android.w
@@ -932,16 +932,71 @@
 .html%22;%0A
+    public static final String PANELS_PATH = %22panels%22;%0A
     publ
@@ -1201,60 +1201,49 @@
    @
-Bean%0A    protected AssetsPathProvider pathProvider;%0A
+TargetApi(Build.VERSION_CODES.JELLY_BEAN)
 %0A   
@@ -1333,24 +1333,47 @@
 () %7B%0A       
+ WebSettings settings =
  panel.getSe
@@ -1380,16 +1380,34 @@
 ttings()
+;%0A        settings
 .setJava
@@ -1439,53 +1439,162 @@
     
-panel.addJavascriptInterface(emitter, %22ir%22
+settings.setAllowContentAccess(true);%0A        settings.setAllowFileAccessFromFileURLs(true);%0A        settings.setAllowUniversalAccessFromFileURLs(true
 );%0A
+%0A
     
@@ -1630,27 +1630,20 @@
 ace(
-pathProvid
+emitt
 er, %22
-path
+ir
 %22);%0A
@@ -1687,32 +1687,33 @@
 bViewClient() %7B%0A
+%0A
         %7D);%0A%0A   

Fix ajax across domain issue