Implementing webview and handling callbacks

android

#1

Im trying to implement android webview in a nativescript app.
The webview is for handling prescriptions(refills/transfers) with the walgreens api.
The webview requires some logic to navigate it and handle callbacks
They have a sample project which works so im trying to apply the logic they use on my app.
The parts im having trouble on is getting the transfer form to go past the first screen. After I click on Next it just says error
Also on the refill screen its just blank. On the sample app it shows an alert saying ‘Unable to refill your prescription now. Please contact the Pharmacist.’ which is what im trying to get to show
Below im attaching the sample project from walgreens and my attempt at implementing the webview.
The code im trying to implement is in the folder: \WAG_Rx_Demo_Android_v1.3_20150106\WAG_Rx_Demo_Android\src\com\walgreens\rx\screens
which looks like this:

/*
 * Copyright Walgreen Co. All rights reserved *
 * Licensed under the Walgreens Developer Program and Portal Terms of Use and API License Agreement.
 * You may not use this file except in compliance with the License.
 * A copy of the API License Agreement can be found on https://developer.walgreens.com.
 *
 * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing  permissions and limitations under the License.
 */

package com.walgreens.rx.screens;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.location.Criteria;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import com.walgreens.rx.Configuration;
import com.walgreens.rx.R;
import com.walgreens.rx.httpConnection.RefillTask;
import com.walgreens.rx.refillInterface.AsyncResponse;

public class CustomWebView extends Activity implements AsyncResponse{

	public WebView webview;
	private ProgressDialog myProgressBar;
	final Activity customWebView = this;

	Configuration objConfig = Configuration.getInstance();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.customwebview);

		myProgressBar = ProgressDialog.show(CustomWebView.this, "Walgreens Rx",
				"Please wait...");


		Intent intent = getIntent();
		String actionType = intent.getStringExtra("Action");

		if (actionType.equalsIgnoreCase("refill")) {
			new RefillTask(MainActivity.refill, this).execute();
		} else if (actionType.equalsIgnoreCase("transfer")) {
			new RefillTask(MainActivity.transfer, this).execute();
		}

		webview = (WebView) findViewById(R.id.webPageLoader);
		webview.setWebViewClient(new WebViewController());
		webview.getSettings().setJavaScriptEnabled(true);

		webview.setWebChromeClient(new WebChromeClient() {
			@Override
			public void onGeolocationPermissionsShowPrompt(String origin,
					android.webkit.GeolocationPermissions.Callback callback) {
				callback.invoke(origin, true, false);
			}

			@Override
			public boolean onJsAlert(WebView view, String url, String message,
					JsResult result) {
				final JsResult finalRes = result;
				if (myProgressBar != null) {
					myProgressBar.dismiss();
				}
				new AlertDialog.Builder(view.getContext())
						.setMessage(message)
						.setPositiveButton(android.R.string.ok,
								new AlertDialog.OnClickListener() {
									@Override
									public void onClick(DialogInterface dialog,
											int which) {
										finalRes.confirm();
									}
								}).setCancelable(false).create().show();
				return true;
			}

			@Override
			public void onProgressChanged(WebView view, int progress) {
				customWebView.setTitle(R.string.loading_screen_title);
				customWebView.setProgress(progress * 100);
				if (progress == 100)
					customWebView.setTitle(R.string.app_name);
			}
		});

	}

	@Override
	public void onBackPressed() {
		if (webview.getVisibility() == View.VISIBLE) {
			if (webview.canGoBack()) {
				webview.loadUrl("javascript:dismissCalendar();");
				webview.loadUrl("javascript:nativeBackBtnClicked();");
			} else {
				Intent intent = new Intent(CustomWebView.this,
						MainActivity.class);
				CustomWebView.this.startActivity(intent);
				finish();
			}
		}
	}

	private void closeWB() {
		webview.loadUrl("javascript:document.open();document.close();");
		webview.clearCache(true);
		webview.setVisibility(View.GONE);
	}

	public class WebViewController extends WebViewClient {

		@Override
		public void onPageStarted(WebView view, String url, Bitmap favicon) {
			myProgressBar.setOnKeyListener(new DialogInterface.OnKeyListener() {
				@Override
				public boolean onKey(DialogInterface dialog, int keyCode,
						KeyEvent event) {
					if (keyCode == KeyEvent.KEYCODE_SEARCH
							&& event.getRepeatCount() == 0) {
						return true;
					}
					return false;
				}
			});
		}

		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {

			Uri url1 = Uri.parse(url);
			if (url1 != null
					&& url1.getScheme().equalsIgnoreCase("refillbyScan")) {
				String action = url1.getQueryParameter("callBackAction");
				if (action.equalsIgnoreCase("back")
						|| action.equalsIgnoreCase("fillAnother")
						|| action.equalsIgnoreCase("close")
						|| action.equalsIgnoreCase("cancel")
						|| action.equalsIgnoreCase("txCancel")
						|| action.equalsIgnoreCase("refillTryAgain")) {

					Intent intent1 = new Intent(CustomWebView.this,
							MainActivity.class);
					CustomWebView.this.startActivity(intent1);
					closeWB();
					return false;
				} else {
					view.loadUrl(url);
					return true;
				}
			} else {
				view.loadUrl(url);
				return true;
			}
		}

		@Override
		public void onPageFinished(WebView view, String url) {
			super.onPageFinished(view, url);
			if (myProgressBar.isShowing()) {
				myProgressBar.dismiss();
			}
		}
	}

	@Override
	public void processFinish(String loadURL, byte[] dataTobePosted) {
		// TODO Auto-generated method stub
		webview.setVisibility(View.VISIBLE);
		webview.postUrl(loadURL, dataTobePosted);

	}
}

In my attempt i already have the webview html string that im trying to make work for the transfer and the refill screens.

These are the sample projects
walgreens android sample project
attemp at implementing webview in ns app

This is the documentation ive been trying to follow as well
https://developer.walgreens.com/api/pharmacy-prescriptions/apimethod/pharmacy-prescription-api#HandleTransferCallbacks
https://developer.walgreens.com/api/pharmacy-prescriptions/apimethod/pharmacy-prescription-api#HandleRefillCallbacks

Any help on this is appreciated.

Thank you