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
New Developer
fcoexposito
Posts: 12
Registered: ‎11-24-2009
My Device: Not Specified

How show alert dialog in waitscreen

Hello,

 

When I start an application I show a waitscreen while some things are done. One of these things is to check coverage and if it hasn't enough, an alert dialog must be shown. It works in the simulator, but not in the real device. In the real device the dialog is shown, but it is hidden immediately.

 

I use the next waitscreen class at the beginning of the application:

 

public class PleaseWaitMainScreen extends MainScreen
{
	private PleaseWaitMainScreen(String shortMessage, int[] paddingShortMessage,
								 String imageName, int[] paddingImage,
								 String longMessage, int[] paddingLongMessage) 
    {
		setPadding(10,10,10,10);
		
    	LabelField ourLabelField = new LabelField(shortMessage, Field.FIELD_HCENTER);
    	if (paddingShortMessage.length == 4)
    	{
    		ourLabelField.setPadding(paddingShortMessage[0], paddingShortMessage[1], paddingShortMessage[2], paddingShortMessage[3]);
    	}
        
    	GIFEncodedImage image = (GIFEncodedImage) GIFEncodedImage.getEncodedImageResource(imageName);
        AnimatedGIFField ourAnimation = new AnimatedGIFField(image, Field.FIELD_HCENTER);
        if (paddingImage.length == 4)
        {
        	ourAnimation.setPadding(paddingImage[0], paddingImage[1], paddingImage[2], paddingImage[3]);
        }
        
        LabelField ourExplanationField = new LabelField(longMessage);
        
        if (paddingLongMessage.length == 4)
        {
        	ourExplanationField.setPadding(paddingLongMessage[0], paddingLongMessage[1], paddingLongMessage[2], paddingLongMessage[3]);
        }
        
        add(ourLabelField);
        add(ourAnimation);
        add(ourExplanationField);
    }

    public static void showScreenAndWait(final Runnable runThis, String shortMessage, int[] paddingShortMessage,
			 							 String imageName, int[] paddingImage, String longMessage, int[] paddingLongMessage) 
    {
        final PleaseWaitMainScreen thisScreen = new PleaseWaitMainScreen(shortMessage, paddingShortMessage, imageName, paddingImage, longMessage, paddingLongMessage);
        Thread threadToRun = new Thread() {
            public void run() {
                // First, display this screen
            	
            	UiApplication.getUiApplication().invokeLater(new Runnable() {
                    public void run() 
                    {
                    	UiApplication.getUiApplication().pushScreen(thisScreen);                    	
                    }
                });
                // Now run the code that must be executed in the Background
                try {
                	
                    runThis.run();
                } catch (Throwable t) {
                    t.printStackTrace();
                    //throw new RuntimeException("Exception detected while waiting: " + t.toString());
                }
                // Now dismiss this screen
                UiApplication.getUiApplication().invokeLater(new Runnable() {
                    public void run() {
                        UiApplication.getUiApplication().popScreen(thisScreen);
                    }
                });
            }
        };
        threadToRun.start();
    }
}

 

Then the main class is the next one:

 

public class CopyOfTabApplication extends UiApplication 
{
	public static void main(String[] args)
	{
		CopyOfTabApplication theApp = new CopyOfTabApplication();
		theApp.enterEventDispatcher();
	}
	
	public CopyOfTabApplication() 
	{
		String shortMessage = "Loading";
		String longMessage = "Slow if coverage problems";
		
		
		if (Display.getWidth() == 480)
		{
			int[] paddingShort = {20,0,0,0};
			int[] paddingImage = {80,0,0,0};
			int[] paddingLong = {80,0,0,0};
			
			PleaseWaitMainScreen.showScreenAndWait(new InitApplication(), shortMessage, paddingShort, 
												   Constants.WAITING_IMAGE, paddingImage, longMessage, paddingLong);	
		}
		else
		{
			int[] paddingShort = {10,0,0,0};
			int[] paddingImage = {50,0,0,0};
			int[] paddingLong = {30,0,0,0};
			
			PleaseWaitMainScreen.showScreenAndWait(new InitApplication(), shortMessage, paddingShort, 
												   Constants.WAITING_IMAGE, paddingImage, longMessage, paddingLong);
		}
		
			
	}
	
	class InitApplication implements Runnable 
    {
    	public void run() 
    	{   
    		checkConnection();
    		
    		MaestrosUtil.getInitialInfo();
    		
    		boolean isUpdated = MaestrosUtil.isAppInfoUpdated();
        	
        	if (isUpdated == false)
			{
        		BBAPIUtils.cleanLogs();
				try 
				{
					MaestrosUtil.writeAppDispInfo();
				} 
				catch (Exception e) 
				{
				}
			}
        	
    		UiApplication.getUiApplication().invokeLater(new Runnable() 
    		{
    			public void run() 
    			{
    				UiApplication.getUiApplication().pushScreen(new MaestrosScreen());
    			}
    		});    		
    	}
    }
	
	public static void checkConnection() 
	{
		boolean coverageBisB = CoverageInfo.isCoverageSufficient(CoverageInfo.COVERAGE_BIS_B);
		boolean coverageDirect = false;//CoverageInfo.isCoverageSufficient(CoverageInfo.COVERAGE_DIRECT);
		boolean coverageMDS = CoverageInfo.isCoverageSufficient(CoverageInfo.COVERAGE_MDS);
		
		if (coverageBisB == false && coverageDirect == false && coverageMDS == false)
		{
			Dialog.alert("Connection problem. Try it later.");

			System.exit(0);
		}
	}
}

 

When a connection error happens, the Dialog.alert("Connection problem. Try it later."); it shown but hidden immediately and the user can not interact with it.

 

Anybody knows how to solve it?

 

Thanks in advance

 

Best regards,

Francisco

Please use plain text.
Developer
peter_strange
Posts: 19,607
Registered: ‎07-14-2008
My Device: Not Specified

Re: How show alert dialog in waitscreen

I am surprised you are even seeing the Dialog.  Given that checkConnection() is being run from InitApplication, which is itself being run in a Background Thread, this means that the statement:

Dialog.alert("Connection problem. Try it later.");
is being run from a background Thread.  This should fail, even on the Simulator, because this is not being run on the Event Thread.  Are you sure this code is all there is?

 

If I was doing this, I would create the Runnable (InitApplication), pass it to the PleaseWait processing, and when that returns, check the result.  In this case, it means that you create a InitApplication instance, supply it to the pleaseWait process, then check some sort of return code that you get from InitApplication.  if this is bad, then display a Dialog indicating the error, or the proper screen if not. 

 

However there is potentially another problem with this.  You are actually initiating the PleaseWait processing before you application has actually started up.  I don't think it was designed to be used like this.  You could probably 'hack this', by putting all the logic into a Runnable, that you then invoked via

UiApplicatin.getUiApplication().invokeLater(...);

This would mean that it was run once the Application had actually been properly started.  I think what is happening now is that your Dialog is being covered up by the PleaseWait screen, because it is invoked with an invokeLater.  So if the above does not solve it, then we might have to try something else as well.  But give the above a shot first. 

Please use plain text.
New Developer
fcoexposito
Posts: 12
Registered: ‎11-24-2009
My Device: Not Specified

Re: How show alert dialog in waitscreen (SOLVED)

Hi Peter,

 

you are right.... I didn't see the dialog. I saw the dialog when I used the invokeLater, but it was hidden immediately. I solved it using invokeAndWait.

 

Thanks in advance.

Please use plain text.
Developer
peter_strange
Posts: 19,607
Registered: ‎07-14-2008
My Device: Not Specified

Re: How show alert dialog in waitscreen (SOLVED)

As noted above, this is not how I would do it, but if you have solved the problem can you mark it so?  Thanks.

 

Instructions in the Help link near the top if you don't know how.

Please use plain text.
New Developer
fcoexposito
Posts: 12
Registered: ‎11-24-2009
My Device: Not Specified

Re: How show alert dialog in waitscreen (SOLVED)

I don't know if the next code is what you mean. If it is, could you tell me why this is better than the other or what are the differences?

 

public TabApplication() 
	{
		String shortMessage = "Loading";
		String longMessage = "Slow if coverage problem";
		
		InitApplication app = new InitApplication();
		
		if (Display.getWidth() == 480)
		{
			shortMessage = "Iniciando aplicación";
			int[] paddingShort = {20,0,0,0};
			int[] paddingImage = {80,0,0,0};
			int[] paddingLong = {80,0,0,0};
			
			PleaseWaitMainScreen.showScreenAndWait(app, shortMessage, paddingShort,
							   Constants.WAITING_IMAGE, paddingImage, longMessage, paddingLong);	
		}
		else
		{
			int[] paddingShort = {10,0,0,0};
			int[] paddingImage = {50,0,0,0};
			int[] paddingLong = {30,0,0,0};
			
			PleaseWaitMainScreen.showScreenAndWait(app, shortMessage, paddingShort, 
							  Constants.WAITING_IMAGE, paddingImage, longMessage, paddingLong);
		}
		
		if (!app.getConnectionOk())
		{
			UiApplication.getUiApplication().invokeLater(new Runnable() 
    		{
    			public void run() 
    			{
    				Dialog.alert("Coverage problem");
    			}
    		});
		}	
	}
	
	class InitApplication implements Runnable 
    {
		private boolean connectionOk; 
    	public void run() 
    	{   
    		if (!checkConnectionsWaitScreen())
    		{
    			connectionOk = false;
    			return;
    		}
    		
    		connectionOk = true;
    		
    		MaestrosUtil.getInitialInfo();
    		
    		BBAPIUtils.writeLog("Version 4.5.0");
    		
    		boolean isUpdated = MaestrosUtil.isAppInfoUpdated();
        	
        	if (isUpdated == false)
			{
        		BBAPIUtils.cleanLogs();
				try 
				{
					MaestrosUtil.writeAppDispInfo();
				} 
				catch (Exception e) 
				{
				}
			}
        	
    		UiApplication.getUiApplication().invokeLater(new Runnable() 
    		{
    			public void run() 
    			{
    				UiApplication.getUiApplication().pushScreen(new MaestrosScreen());
    			}
    		});    		
    	}
    	
    	public boolean getConnectionOk()
    	{
    		return connectionOk;
    	}
    }
	
	public static boolean checkConnectionsWaitScreen() 
	{
		boolean coverageBisB = CoverageInfo.isCoverageSufficient(CoverageInfo.COVERAGE_BIS_B);
		boolean coverageDirect = CoverageInfo.isCoverageSufficient(CoverageInfo.COVERAGE_DIRECT);
		boolean coverageMDS = CoverageInfo.isCoverageSufficient(CoverageInfo.COVERAGE_MDS);
		
		if (coverageBisB == false && coverageDirect == false && coverageMDS == false)
		{
			return false;	
		}
		
		return true;
	}

 

Thanks again.

Please use plain text.