diff --git a/src/android/cl/kunder/webview/WebViewActivity.java b/src/android/cl/kunder/webview/WebViewActivity.java index ff96ccd..1c145d2 100644 --- a/src/android/cl/kunder/webview/WebViewActivity.java +++ b/src/android/cl/kunder/webview/WebViewActivity.java @@ -1,15 +1,95 @@ package cl.kunder.webview; +import android.app.Activity; +import android.app.Dialog; +import android.content.DialogInterface; +import android.graphics.Color; import android.os.Bundle; +import android.view.KeyEvent; +import android.view.ViewGroup; +import android.widget.LinearLayout; +import android.widget.ProgressBar; +import android.widget.RelativeLayout; +import android.widget.TextView; import org.apache.cordova.CordovaActivity; public class WebViewActivity extends CordovaActivity { + static Dialog dialog; + static Activity activity2; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + //Aqui debo crear el loading + activity2 = this; Bundle b = getIntent().getExtras(); String url = b.getString("url"); + Boolean shouldShowLoading = false; + try{ + shouldShowLoading = b.getBoolean("shouldShowLoading"); + } + catch(Exception e){ + + } + if(shouldShowLoading){ + showLoading(); + } loadUrl((url.matches("^(.*://|javascript:)[\\s\\S]*$")?"":"file:///android_asset/www/")+url); } + + public static boolean showLoading() { + // Loading spinner + activity2.runOnUiThread(new Runnable() { + @Override + public void run() { + dialog = new Dialog(activity2,android.R.style.Theme_Translucent_NoTitleBar); + ProgressBar progressBar = new ProgressBar(activity2,null,android.R.attr.progressBarStyle); + + LinearLayout linearLayout = new LinearLayout(activity2); + linearLayout.setOrientation(LinearLayout.VERTICAL); + RelativeLayout layoutPrincipal = new RelativeLayout(activity2); + layoutPrincipal.setBackgroundColor(Color.parseColor("#d9000000")); + + RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); + params.addRule(RelativeLayout.CENTER_IN_PARENT); + + linearLayout.addView(progressBar); + + linearLayout.setLayoutParams(params); + + layoutPrincipal.addView(linearLayout); + + dialog.setContentView(layoutPrincipal); + dialog.setOnCancelListener(new DialogInterface.OnCancelListener() { + @Override + public void onCancel(DialogInterface dialogInterface) { + + } + }); + dialog.setOnKeyListener(new DialogInterface.OnKeyListener() { + @Override + public boolean onKey(DialogInterface dialogInterface, int i, KeyEvent keyEvent) { + if(keyEvent.getKeyCode() == KeyEvent.KEYCODE_BACK) + return true; + return false; + } + }); + + dialog.show(); + } + }); + + return true; + } + + public static boolean hideLoading() { + // Loading spinner + activity2.runOnUiThread(new Runnable() { + @Override + public void run() { + dialog.hide(); + } + }); + return true; + } } diff --git a/src/android/cl/kunder/webview/WebViewPlugin.java b/src/android/cl/kunder/webview/WebViewPlugin.java index 4f03a8b..63a2117 100644 --- a/src/android/cl/kunder/webview/WebViewPlugin.java +++ b/src/android/cl/kunder/webview/WebViewPlugin.java @@ -53,9 +53,15 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo if (action.equals("show") && args.length() > 0) { LOG.d(LOG_TAG, "Show Web View"); final String url = args.getString(0); + Boolean shouldShowLoading = false; + try{ + shouldShowLoading = args.getBoolean(1); + } + catch(Exception e){ + } if(!"".equals(url)) { - showWebView(url); + showWebView(url, shouldShowLoading); JSONObject r = new JSONObject(); r.put("responseCode", "ok"); callbackContext.success(r); @@ -73,6 +79,20 @@ else if(action.equals("hide")) { callbackContext.success(r); } + else if(action.equals("hideLoading")) { + LOG.d(LOG_TAG, "Hide Web View Loading"); + try{ + WebViewActivity.hideLoading(); + } + catch(Exception e){ + LOG.e(LOG_TAG, "Error in hideLoading"); + LOG.e(LOG_TAG, e.toString()); + } + JSONObject r = new JSONObject(); + r.put("responseCode", "ok"); + callbackContext.success(r); + } + else if(action.equals("subscribeCallback")){ LOG.d(LOG_TAG, "Subscribing Cordova CallbackContext"); subscribeCallbackContext = callbackContext; @@ -100,10 +120,11 @@ else if(action.equals("exitApp")){ return true; } - private void showWebView(final String url) { + private void showWebView(final String url, Boolean shouldShowLoading) { LOG.d(LOG_TAG, "Url: " + url); Intent i = new Intent(this.cordova.getActivity(), WebViewActivity.class); i.putExtra("url", url); + i.putExtra("shouldShowLoading", shouldShowLoading); i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); this.cordova.getActivity().getApplicationContext().startActivity(i); } diff --git a/www/webViewPlugin.js b/www/webViewPlugin.js index de3de57..15569fb 100644 --- a/www/webViewPlugin.js +++ b/www/webViewPlugin.js @@ -3,14 +3,23 @@ module.exports = (function() { - var _show = function(url, successCallback, errorCallback) { - cordova.exec(successCallback, errorCallback, 'WebViewPlugin', 'show', [url]); + var _show = function(url, successCallback, errorCallback, loading) { + if(loading){ + cordova.exec(successCallback, errorCallback, 'WebViewPlugin', 'show', [url, loading]); + } + else{ + cordova.exec(successCallback, errorCallback, 'WebViewPlugin', 'show', [url]); + } }; var _hide = function(successCallback, errorCallback) { cordova.exec(successCallback, errorCallback, 'WebViewPlugin', 'hide', []); }; + var _hideLoading = function(successCallback, errorCallback) { + cordova.exec(successCallback, errorCallback, 'WebViewPlugin', 'hideLoading', []); + }; + var _subscribeCallback = function(successCallback, errorCallback) { cordova.exec(successCallback, errorCallback, 'WebViewPlugin', 'subscribeCallback', []); }; @@ -30,7 +39,8 @@ module.exports = (function() { Close: _hide, SubscribeCallback: _subscribeCallback, SubscribeExitCallback: _subscribeExitCallback, - ExitApp: _exitApp + ExitApp: _exitApp, + HideLoading: _hideLoading }; })();