Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
behrk2
Posts: 367
Registered: ‎11-25-2009
My Device: Not Specified

Using UiApplication with MainScreen

Hello,

 

I am trying to add a Browser Field onto my Main Screen. Currently, I have:

 

 

public class BrowserFieldDemo extends UiApplication {

	private MainScreen _browserScreen;
	private BrowserField _bf2;
	private BrowserFieldConfig _bfConfig;

	public BrowserFieldDemo(final String url) {
		_browserScreen = new MainScreen();
		_bfConfig = new BrowserFieldConfig();
		_bfConfig.setProperty(BrowserFieldConfig.NAVIGATION_MODE,
				BrowserFieldConfig.NAVIGATION_MODE_POINTER);
		_bfConfig.setProperty(BrowserFieldConfig.JAVASCRIPT_ENABLED,
				Boolean.TRUE);
		_bf2 = new BrowserField(_bfConfig);
		_browserScreen.add(_bf2);

		invokeLater(new Runnable() {
			public void run() {
				_bf2.requestContent(url);
				pushScreen(_browserScreen);
			}
		});
	}
}

 

 

What is the correct way to insert the browser to into Main Screen class, amongst other UI elements? For example, add(myBrowserField)? I am unsure how to set up the BrowserFieldDemo class to do this, because I need to extend UIApplication for the Thread...

 

 

public class LoginScreen extends MainScreen implements FieldChangeListener{}

 

Thanks!

Please use plain text.
Developer
klerisson
Posts: 78
Registered: ‎12-03-2009
My Device: Not Specified

Re: Using UiApplication with MainScreen

[ Edited ]

Hey!!

 

So, I did not get your question, but I had been work in a similar issue (I guess). Your issue might be to implement a screen that behave like a browser. Am I right?

 

Then, here we just implement a class that extends MainScreen and  implements RenderingApplication. Although the implementation is alike BrowserFieldDemo in our case we need a screen. Honestly it`s just an BrowserFieldDemos copy and paste, thanks to RIM!!

 

See the code bellow, just to provide you some clues:

 

 

import java.io.IOException;
import java.io.OutputStream;

import javax.microedition.io.HttpConnection;

import net.rim.device.api.browser.field.BrowserContent;
import net.rim.device.api.browser.field.Event;
import net.rim.device.api.browser.field.RedirectEvent;
import net.rim.device.api.browser.field.RenderingApplication;
import net.rim.device.api.browser.field.RenderingException;
import net.rim.device.api.browser.field.RenderingOptions;
import net.rim.device.api.browser.field.RenderingSession;
import net.rim.device.api.browser.field.RequestedResource;
import net.rim.device.api.browser.field.UrlRequestedEvent;
import net.rim.device.api.io.http.HttpHeaders;
import net.rim.device.api.io.http.HttpProtocolConstants;
import net.rim.device.api.system.Application;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.Status;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.util.StringUtilities;

public class BrowserScreen extends MainScreen implements RenderingApplication {

	private static final String REFERER = "referer";   
	private RenderingSession renderingSession;   
	private HttpConnection  currentConnection;

	private String feature = null;
	private String screenName = null;
	private String body = null;

	public static final int BUTTON_BACK = 0;
	public static final int BUTTON_NEXT = 1;



	public BrowserScreen(A_R_G_S) {
		this.renderingSession = RenderingSession.getNewInstance();
		this.renderingSession.getRenderingOptions().setProperty(RenderingOptions.CORE_OPTIONS_GUID,  RenderingOptions.ENABLE_CSS, true);
		this.renderingSession.getRenderingOptions().setProperty(RenderingOptions.CORE_OPTIONS_GUID,  RenderingOptions.CSS_MEDIA_TYPE, "screen");
		this.renderingSession.getRenderingOptions().setProperty(RenderingOptions.CORE_OPTIONS_GUID,  RenderingOptions.JAVASCRIPT_ENABLED, true);
		this.navigate(1, "popular");
	}

	public void navigate(int pageNumber, String category) {
		ResourceFetchThread thread = new ResourceFetchThread(
				U_R_L
				null, 
				null, 
				null, 
				this);
		thread.start(); 
	}

	void processConnection(HttpConnection connection, Event e) 
	{
		// Cancel previous request.
		if (this.currentConnection != null) 
		{
			try 
			{
				this.currentConnection.close();
			} 
			catch (IOException e1) 
			{

			}
		}
		this.currentConnection = connection;
		BrowserContent browserContent = null;
		try 
		{
			browserContent = this.renderingSession.getBrowserContent(connection, this, e);

			if (browserContent != null) 
			{
				Field field = browserContent.getDisplayableContent();
				if (field != null) 
				{
					synchronized (Application.getEventLock()) 
					{
						this.deleteAll();
						this.add(field);
					}
				}
				browserContent.finishLoading();
			}
		} 
		catch (RenderingException re) 
		{

		} 
	} 

	public Object eventOccurred(Event event) {
		int eventId = event.getUID();
		switch (eventId) 
		{
		case Event.EVENT_URL_REQUESTED : 
		{
			UrlRequestedEvent urlRequestedEvent = (UrlRequestedEvent) event;    
			ResourceFetchThread thread = new ResourceFetchThread(urlRequestedEvent.getURL(),
					urlRequestedEvent.getHeaders(), 
					urlRequestedEvent.getPostData(),
					event, this);
			thread.start();
			break;
		} 
		case Event.EVENT_REDIRECT : 
		{
			RedirectEvent e = (RedirectEvent) event;
			String referrer = e.getSourceURL();
			switch (e.getType()) 
			{  
			case RedirectEvent.TYPE_SINGLE_FRAME_REDIRECT :
				// Show redirect message.
				Application.getApplication().invokeAndWait(new Runnable() 
				{
					public void run() 
					{
						Status.show("You are being redirected to a different page...");
					}
				});

				break;

			case RedirectEvent.TYPE_JAVASCRIPT:
				break;

			case RedirectEvent.TYPE_META :
				// MSIE and Mozilla don't send a Referer for META Refresh.
				referrer = null;   
	
								break;

			case RedirectEvent.TYPE_300_REDIRECT :
				// MSIE, Mozilla, and Opera all send the original
				// request's Referer as the Referer for the new
				// request.
				Object eventSource = e.getSource();
				if (eventSource instanceof HttpConnection) 
				{
					referrer = ((HttpConnection)eventSource).getRequestProperty(REFERER);
				}
				break;
			}

			/*  HttpHeaders requestHeaders = new HttpHeaders();
                    requestHeaders.setProperty(REFERER, referrer);
                    ResourceFetchThread thread = new ResourceFetchThread(e.getLocation(), requestHeaders,null, event, this);
                    thread.start();*/
			break;
		} 
		case Event.EVENT_CLOSE :
			break;

		case Event.EVENT_SET_HEADER :        // No cache support.
		case Event.EVENT_SET_HTTP_COOKIE :   // No cookie support.
		case Event.EVENT_HISTORY :           // No history support.
		case Event.EVENT_EXECUTING_SCRIPT :  // No progress bar is supported.
		case Event.EVENT_FULL_WINDOW :       // No full window support.
		case Event.EVENT_STOP :              // No stop loading support.
		default :
		}
		return null;
	}

	public int getAvailableHeight(BrowserContent browserContent) {
		// Field has full screen.
		return Display.getHeight();
	}

	public int getAvailableWidth(BrowserContent browserContent) {
		// Field has full screen.
		return Display.getWidth();
	}

	/* (non-Javadoc)
	 * @see net.rim.device.api.browser.field.RenderingApplication#getHTTPCookie(java.lang.String)
	 */
	public String getHTTPCookie(String url) {
		//No cookie support
		return null;
	}

	/* (non-Javadoc)
	 * @see net.rim.device.api.browser.field.RenderingApplication#getHistoryPosition(net.rim.device.api.browser.field.BrowserContent)
	 */
	public int getHistoryPosition(BrowserContent browserContent) {
		// No history support.
		return 0;
	}
	
	public HttpConnection getResource(RequestedResource resource, BrowserContent referrer) 
	{
		if (resource == null) 
		{
			return null;
		}

		// Check if this is cache-only request.
		if (resource.isCacheOnly()) 
		{
			// No cache support.
			return null;
		}

		String url = resource.getUrl();
		if (url == null) 
		{
			return null;
		}

		// If referrer is null we must return the connection.
		if (referrer == null) 
		{
			HttpConnection connection = Utilities.makeConnection(resource.getUrl(), resource.getRequestHeaders(), null);
			return connection;
		} 

		return null;
	}

	public void invokeRunnable(Runnable runnable) {
		(new Thread(runnable)).start();
	}
}

class ResourceFetchThread extends Thread 
{
	private BrowserScreen application;
	private Event event;
	private byte[] postData;
	private HttpHeaders requestHeaders;
	private String url;

	ResourceFetchThread(String url, HttpHeaders requestHeaders, byte[] postData, 
			Event event, BrowserScreen application) 
			{
		this.url = url;
		this.requestHeaders = requestHeaders;
		this.postData = postData;
		this.application = application;
		this.event = event;
			}

	public void run() 
	{
		HttpConnection connection = Utilities.makeConnection(this.url, this.requestHeaders, this.postData);
		this.application.processConnection(connection, this.event);
	}
}

class Utilities {

	public static HttpConnection makeConnection(String url,
			HttpHeaders requestHeaders, byte[] postData) {
		HttpConnection conn = null;
		OutputStream out = null;
		
		try {
			//conn = (HttpConnection) Connector.open(url);
			
			HttpConnectionFactory factory = new HttpConnectionFactory( url);
			conn = factory.getNextConnection();
			
			if (requestHeaders != null) {
				String referer = requestHeaders.getPropertyValue("referer");
				boolean sendReferrer = true;

				if (referer != null
						&& StringUtilities.startsWithIgnoreCase(referer,
						"https:")
						&& !StringUtilities.startsWithIgnoreCase(url, "https:")) {
					sendReferrer = false;
				}

				int size = requestHeaders.size();
				for (int i = 0; i < size;) {
					String header = requestHeaders.getPropertyKey(i);

					// Remove referer header if needed.
					if (!sendReferrer && header.equals("referer")) {
						requestHeaders.removeProperty(i);
						--size;
						continue;
					}

					String value = requestHeaders.getPropertyValue(i++);
					if (value != null) {
						conn.setRequestProperty(header, value);
					}
				}
			}

			if (postData == null) {
				conn.setRequestMethod(HttpConnection.GET);
			} else {
				conn.setRequestMethod(HttpConnection.POST);

				conn.setRequestProperty(
						HttpProtocolConstants.HEADER_CONTENT_LENGTH, String
						.valueOf(postData.length));

				out = conn.openOutputStream();
				out.write(postData);

			}

		} catch (IOException e1) {
		} catch (NoMoreTransportsException nc) {
			nc.printStackTrace();
		} finally {
			if (out != null) {
				try {
					out.close();
				} catch (IOException e2) {
				}
			}
		}

		return conn;
	}
}

 See you!

 

 

 

--
Feel free to press the kudos button on the left side to thank the user that helped you.
Please mark posts as solved if you found a solution.
Please use plain text.
Developer
behrk2
Posts: 367
Registered: ‎11-25-2009
My Device: Not Specified

Re: Using UiApplication with MainScreen

Hey klerrison,

 

Thanks for your post! Sorry for the late reply.

 

I tried out your code, and when I push the BrowserScreen, it just appears as a white screen.

 

I've been working with some other code, but I keep receiving a message from Netflix saying that Javascript is not enabled, and I do have the code inserted to enable it. So, I'm trying to figure out what to do there. You can see a post about it here: http://supportforums.blackberry.com/t5/Java-Development/Browser-Field-will-not-show-Javascript/td-p/...

 

Thanks!

 

Kevin

Please use plain text.
Developer
klerisson
Posts: 78
Registered: ‎12-03-2009
My Device: Not Specified

Re: Using UiApplication with MainScreen

Hey man!

Could you, please, send us the link which you want to show on the screen?

Take care!

--
Feel free to press the kudos button on the left side to thank the user that helped you.
Please mark posts as solved if you found a solution.
Please use plain text.
Developer
behrk2
Posts: 367
Registered: ‎11-25-2009
My Device: Not Specified

Re: Using UiApplication with MainScreen

Hey klerrison-

 

I'm not sure if this link will still be active by the time you try to use it (it runs on a timestamp), but here it is:

 

https://api-user.netflix.com/oauth/login?application_name=Behr+Movie+Maker&oauth_callback=http://www.reslifescranton.net/oauth.php&oauth_consumer_key=5xpsty524wuc2pkkmz2ne3qf&oauth_token=6yeqk45z9f...

 

It seems that when I open this page on my computer (or on the BlackBerry's native browser via a browser session), it will quickly show the javascript disabled message and then "forward" or dynamically change into the login screen...

 

Also, I am having difficulties building a cross-embedded browser for 4.7 AND 5.0, any suggestions?

 

Thanks!

Please use plain text.
Developer
hades_6
Posts: 178
Registered: ‎08-26-2009
My Device: Not Specified

Re: Using UiApplication with MainScreen

try running the MSD simulator and then running the application. I get the white screen too if i try to connect via Wifi, it works on the actual device using BIS.

Please use plain text.
Developer
behrk2
Posts: 367
Registered: ‎11-25-2009
My Device: Not Specified

Re: Using UiApplication with MainScreen

I tried running it on the device, and the browser field no longer just stays white.

 

However, I am still receiving a Netflix page that says "Please Activate JavaScript"...

Please use plain text.
Developer
klerisson
Posts: 78
Registered: ‎12-03-2009
My Device: Not Specified

Re: Using UiApplication with MainScreen

Sorry man, but I have no clue... the only information that I can share is the link http://docs.blackberry.com/en/developers/deliverables/11958/Creating_a_field_to_display_web_content_...

 

But probably you already read it.

--
Feel free to press the kudos button on the left side to thank the user that helped you.
Please mark posts as solved if you found a solution.
Please use plain text.