package be.manger_veggie.mangerveggie;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.ConsoleMessage;
import android.webkit.GeolocationPermissions;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import vegout.desmottes.be.vegout.R;


public class MainActivity extends Activity {

    private WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Remove title bar
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);

        //Remove notification bar
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_main);
        webView = (WebView) findViewById(R.id.activity_main_webView);
        // Enable Javascript
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setGeolocationEnabled(true);
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setUseWideViewPort(true);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);

        webView.setWebChromeClient(new WebChromeClient() {
            public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
                callback.invoke(origin, true, true);
            }

            public boolean onConsoleMessage(ConsoleMessage msg) {
                Log.d("mangerveggie", msg.sourceId() + "[" + msg.lineNumber() + "] " + msg.message());
                return true;
            }
        });
        webView.setWebViewClient(new CustomWebViewClient());
        webView.loadUrl("http://manger-veggie.be");

    }

    private class CustomWebViewClient extends WebViewClient {
        public boolean shouldOverrideUrlLoading(WebView view, String url)
        {
            //do whatever you want with the url that is clicked inside the webview.
            //for example tell the webview to load that url.
            boolean shouldHandle = url.contains("manger-veggie.be");
            if(shouldHandle)
                view.loadUrl(url);
            //return true if this method handled the link event
            //or false otherwise
            return shouldHandle;
        }


    }

    @Override
    public void onBackPressed() {
        if (webView.canGoBack()) {
            webView.goBack();
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

@@ -62,16 +62,41 @@
 tivity;%0A
+import android.os.Build;%0A
 import a
@@ -1426,16 +1426,347 @@
 (true);%0A
+        webSettings.setAppCacheEnabled(false);%0A%0A        webSettings.setDomStorageEnabled(true);%0A        webSettings.setDatabaseEnabled(true);%0A        if (Build.VERSION.SDK_INT %3C Build.VERSION_CODES.KITKAT) %7B%0A            webSettings.setDatabasePath(%22/data/data/%22 + webView.getContext().getPackageName() + %22/databases/%22);%0A        %7D%0A%0A
 %0A       
@@ -2089,17 +2089,17 @@
     Log.
-d
+w
 (%22manger
@@ -2211,24 +2211,279 @@
           %7D%0A
+            public void onConsoleMessage(String message, int lineNumber, String sourceID) %7B%0A                Log.w(%22MyApplication%22, message + %22 -- From line %22%0A                        + lineNumber + %22 of %22%0A                        + sourceID);%0A            %7D%0A
         %7D);%0A
@@ -2543,32 +2543,34 @@
 ent());%0A        
+//
 webView.loadUrl(
@@ -2596,16 +2596,71 @@
 ie.be%22);
+%0A        webView.loadUrl(%22http://172.17.229.127:8000%22);
 %0A%0A    %7D%0A

fix android not properly running javascript in webview