Code example for WebView: Displaying web pages

Methods: getSettingsloadUrlsetWebViewClient, requestFocus, setOnTouchListener

0
		} 
 
		final LoginUIOperationCallback externalCallback = callback;
		final AlertDialog.Builder builder = new AlertDialog.Builder(context);
		// Create the Web View to show the login page 
		final WebView wv = new WebView(context);
		builder.setTitle("Connecting to a service");
		builder.setCancelable(true);
		builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
 
			@Override 
			public void onCancel(DialogInterface dialog) {
				if (externalCallback != null) {
					externalCallback.onCompleted(null,
							new MobileServiceException("User Canceled")); 
				} 
			} 
		}); 
 
		// Set cancel button's action 
		builder.setNegativeButton("Cancel",
				new DialogInterface.OnClickListener() {
 
					@Override 
					public void onClick(DialogInterface dialog, int which) {
						if (externalCallback != null) {
							externalCallback
									.onCompleted(null, 
											new MobileServiceException( 
													"User Canceled")); 
						} 
						wv.destroy();
					} 
				}); 
 
		wv.getSettings().setJavaScriptEnabled(true);
 
		wv.requestFocus(View.FOCUS_DOWN);
		wv.setOnTouchListener(new View.OnTouchListener() {
 
			@Override 
			public boolean onTouch(View view, MotionEvent event) {
				int action = event.getAction();
				if (action == MotionEvent.ACTION_DOWN
						|| action == MotionEvent.ACTION_UP) {
					if (!view.hasFocus()) {
						view.requestFocus();
					} 
				} 
 
				return false; 
			} 
		}); 
 
		// Create a LinearLayout and add the WebView to the Layout 
		LinearLayout layout = new LinearLayout(context);
		layout.setOrientation(LinearLayout.VERTICAL);
		layout.addView(wv);
 
		// Add a dummy EditText to the layout as a workaround for a bug 
		// that prevents showing the keyboard for the WebView on some devices 
		EditText dummyEditText = new EditText(context);
		dummyEditText.setVisibility(View.GONE);
		layout.addView(dummyEditText);
 
		// Add the layout to the dialog 
		builder.setView(layout);
 
		final AlertDialog dialog = builder.create();
 
		wv.setWebViewClient(new WebViewClient() {
 
			@Override 
			public void onPageStarted(WebView view, String url, Bitmap favicon) {
				// If the URL of the started page matches with the final URL 
				// format, the login process finished 
				if (isFinalUrl(url)) {
					if (externalCallback != null) {
						externalCallback.onCompleted(url, null);
					} 
 
					dialog.dismiss();
				} 
 
				super.onPageStarted(view, url, favicon);
			} 
 
			// Checks if the given URL matches with the final URL's format 
			private boolean isFinalUrl(String url) {
				if (url == null) {
					return false; 
				} 
 
				return url.startsWith(endUrl);
			} 
 
			// Checks if the given URL matches with the start URL's format 
			private boolean isStartUrl(String url) {
				if (url == null) {
					return false; 
				} 
 
				return url.startsWith(startUrl);
			} 
 
			@Override 
			public void onPageFinished(WebView view, String url) {
				if (isStartUrl(url)) {
					if (externalCallback != null) {
						externalCallback
								.onCompleted( 
										null, 
										new MobileServiceException( 
												"Logging in with the selected authentication provider is not enabled")); 
					} 
 
					dialog.dismiss();
				} 
			} 
		}); 
 
		wv.loadUrl(startUrl);
		dialog.show();
	} 
 
	/** 
	 * Creates a User based on a Windows Azure Mobile Service JSON object