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
Posts: 121
Registered: ‎03-25-2009
My Device: Z30

Re: Downloading email attachments

Here is the latest:

 

If I take the following code, it seems to work... almost.

 

package com.altkey.handprint.bb.screens;

import java.io.IOException;

import net.rim.blackberry.api.mail.AttachmentDownloadManager;
import net.rim.blackberry.api.mail.BodyPart;
import net.rim.blackberry.api.mail.DownloadProgressListener;
import net.rim.blackberry.api.mail.SupportedAttachmentPart;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.container.PopupScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;

import com.altkey.handprint.bb.util.logging.Logger;

public class AttachmentDownloaderPopup extends PopupScreen implements DownloadProgressListener
{
	private LabelField statusField;
	private ButtonField cancelButton;

	private SupportedAttachmentPart attachment;
	private AttachmentDownloadManager downloader;
	private boolean downloadComplete;
	private final int fileSize;
	private final String downloadedFileName;
	
	/**
	 * Create screen for downloading an attachment.
	 */
	public AttachmentDownloaderPopup( SupportedAttachmentPart attach )
	{
		super(new VerticalFieldManager(VerticalFieldManager.FIELD_HCENTER));
		attachment = attach;
		downloader = new AttachmentDownloadManager();
		downloadedFileName = downloader.getDownloadedFileName(attachment);
		fileSize = downloader.getFileSize(attachment);
		initGui();
	}
	
	private void initGui()
	{
		LabelField labelField = new LabelField("Downloading " + attachment.getName(), Field.FIELD_HCENTER);
		add(labelField);
		add(new SeparatorField(SeparatorField.LINE_HORIZONTAL|Field.FIELD_HCENTER));

		statusField = new LabelField("Connecting...", Field.FIELD_HCENTER);
		add(statusField);
		
		cancelButton = new ButtonField("Cancel", ButtonField.CONSUME_CLICK|Field.FIELD_HCENTER);
		cancelButton.setChangeListener(new FieldChangeListener()
		{
			public void fieldChanged( Field field, int context )
			{
				cancelDownload();
			}
		});
		add(cancelButton);
	}
	
	private void cancelDownload()
	{
		Logger.getDefault().warn("cancelling the download");
		if( downloader == null )
		{
			finalize(false);
		}
		else
		{
			downloader.cancelDownload(attachment);
		}
	}
	
	private void startDownload()
	{
		try
		{
			Logger.getDefault().info("starting download");
			downloader.download(attachment, null, this);
			return;
		}
		catch( IOException e )
		{
			Logger.getDefault().error("error in start download: " + e.getMessage());
			e.printStackTrace();
			statusField.setText(e.getMessage());
			return;
		}
	}
	
	private void finalize(final boolean complete)
	{
		UiApplication.getUiApplication().invokeAndWait(new Runnable()
		{
			public void run()
			{
				downloadComplete = complete;
				attachment = null;//free resources
				downloader = null;
			}
		});
		if( isDisplayed() )
		{
			Logger.getDefault().info("closing screen, off ui loop");
			close();
		}
		else
		{
			Logger.getDefault().info("not closing display, because isDisplayed() is false");
		}
	}

	public void downloadCancelled( Object element )
	{
		Logger.getDefault().info("cancelled");
		finalize(false);
	}

	public void downloadCompleted( Object element )
	{
		Logger.getDefault().info("download completed");
		finalize(true);
	}

	public void updateProgress( Object element, final int current, final int total )
	{
		UiApplication.getUiApplication().invokeLater(new Runnable()
		{
			public void run()
			{
				if( total == 0 )
				{
					return;
				}
				int percent = (current * 100) / total;
				String status = percent + "% complete ";
				statusField.setText(status);
			}
		});
	}
	
	public int getFileSize() { return fileSize; }
	public boolean downloadComplete() { return downloadComplete; }
	public String downloadedFileName() { return downloadedFileName; }
	
	public static AttachmentDownloaderPopup execute( SupportedAttachmentPart a )
	{
		AttachmentDownloaderPopup screen = new AttachmentDownloaderPopup(a);
		screen.startDownload();
		UiApplication.getUiApplication().pushModalScreen(screen);
		if( screen.downloadComplete() )
		{
			return screen;
		}
		else
		{
			return null;
		}
	}
}

 

In my AttachmentHandler class,  do a postGlobalEvent() to my app.  Then in my app, I call AttachmentDownloaderPopup.execute().  This seems to work, and even completes correctly, and downloadComplete() gets called at the end of the download, but the call to close() in my finalize() method does not seem to do anything.  So basically I am unable to close the window.  Even hammering cancel button, just prints out "closing window", and close() is called, but it will not close.

 

Next I tried passing the SupportedAttachment with the postGlobalEvent(), but not calling AttachmentDownloaderPopup.execute(), until the user hits a button.  In this case the screen just closes right away, even before the download complete is triggered.

 

This is going to drive me nuts!

 

 

BlackBerry Development Advisor
Posts: 15,021
Registered: ‎07-09-2008
My Device: BlackBerry Passport

Re: Downloading email attachments

The reason why close is not closing your screen is because the methods fired by the DownloadProgressListener are run in a separate process from your application (displaying the PopupScreen), which is true for any listener in the BlackBerry API set.

You'll need to communicate between these two process to close your screen. You can use a GlobalEvent or the design shown in this article.

Allow a background listener to detect and update a GUI application
http://supportforums.blackberry.com/t5/Java-Development/Allow-a-background-listener-to-detect-and-up...
Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Developer
Posts: 121
Registered: ‎03-25-2009
My Device: Z30

Re: Downloading email attachments

Thanks Mark, that makes sense.  I'll give it a try today.

~S

Developer
Posts: 121
Registered: ‎03-25-2009
My Device: Z30

Re: Downloading email attachments - see code

This approach seems to work fine.  But I have had a few users report that they can't open PDF attachemnts, and others are reporting they can't see message bodies, others are reporting that the whole email shows up as 'multipart/alternative'.  Basically my app messes up everything to do with email attachements.

 

Has anyone ever seen this before?

 

My implementation is pretty simple.

1. Install the attachment handler:

	private static void setupAttachmentHandler()
	{
		//register content handler.
		final AttachmentHandlerManager attachHandler = AttachmentHandlerManager.getInstance();
		final MyAttachmentHandler  handler = new MyAttachmentHandler();
		attachHandler.addAttachmentHandler(handler );
	}

 

The function is called frrom my main method:

	public static void main( String[] args )
	{
		if( args != null && args.length > 0 && args[0].equals("startup") )
		{
			setupIcons();
			setupAttachmentHandler();
			setupGlobalMenuEntries();
		}
		else
		{
			MyApp.getApp().run();
		}
	}

 The code for MyAttachmentHandler looks like this:

public class MyAttachmentHandler implements AttachmentHandler
{	
	public String menuString()
	{
		return StringTable.get(StringTable.MENU_PRINT_ATTACHMENT);
	}

	public void run( Message msg, SupportedAttachmentPart attach )
	{
		AttachmentDownloader downloader = new AttachmentDownloader(attach);
		try
		{
			downloader.start();
		}
		catch( IOException e1 )
		{
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}

	public boolean supports( String type )
	{
		return true;
	}
}

 

 The code for AttachmentHandler looks like this:

public class AttachmentDownloader implements DownloadProgressListener
{
	private final AttachmentDownloadManager downloader;
	private final SupportedAttachmentPart attachment;
	
	public AttachmentDownloader( SupportedAttachmentPart attach )
	{
		downloader = new AttachmentDownloadManager();
		attachment = attach;
		
		if( attach == null )
		{
			throw new IllegalArgumentException("the attachment passed to AttachmentDownloader can not be null");
		}
	}
	
	public void start() throws IOException
	{
		final String defaultPath = null;
		downloader.download(attachment, defaultPath, this);
	}

	public void downloadCancelled( Object item )
	{
	}

	public void downloadCompleted( Object item )
	{
		//post global message to my app
	}

	public void updateProgress( Object item, int current, int total )
	{
	}
}

 

 Any insight would be great, as this can't be debugged on the simulator, and debugging on the handset is painful.

Thanks,

~S

 

 

 

BlackBerry Development Advisor
Posts: 15,021
Registered: ‎07-09-2008
My Device: BlackBerry Passport

Re: Downloading email attachments - see code

What BlackBerry Smartphone model and BlackBerry device software version are you testing on?  You can find this under Options, About on the BlackBerry Smartphone.

 

Note that not all BlackBerry Smartphones come with a viewer to open local PDF files (although all can open them when sent as email attachments using the built in attachment service).

Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Developer
Posts: 121
Registered: ‎03-25-2009
My Device: Z30

Re: Downloading email attachments - see code

My device I use for development is a Bold 9780. Customers reporting the problem are on a Torch, and a Tour. All three running 6.0.

 

With the blackberry having the ability to view PDF or not, how would that affect my software? I am not trying to view it, I just need the ability to download any given attachment. I am not exactly sure what you are getting at here.

 

~S

BlackBerry Development Advisor
Posts: 15,021
Registered: ‎07-09-2008
My Device: BlackBerry Passport

Re: Downloading email attachments - see code

You said "But I have had a few users report that they can't open PDF attachemnts, and others are reporting they can't see message bodies, others are reporting that the whole email shows up as 'multipart/alternative'."

 

Are they trying to view this PDF within your application?  Where are you/they seeing this?

Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Developer
Posts: 121
Registered: ‎03-25-2009
My Device: Z30

Re: Downloading email attachments - see code

Sorry, poor wording on my part.

 

When I said that they could not see the PDF files, I mean that the PDF attachment does not even appear as an attachment.  For example if I email them a message with a PDF attachment, it will appear to them as an email without any attachment. Yet if they uninstall my software, they PDF shows up fine.

 

This doesn't exactly instill confidence of my product in my customers :Beta1:

 

I am thinking that I must be doing something to mess with with the attachment handling table or something... But it is the code I have posted above that causes the issue, as disabling that fixes the issue.

 

Thanks,
~S 

BlackBerry Development Advisor
Posts: 15,021
Registered: ‎07-09-2008
My Device: BlackBerry Passport

Re: Downloading email attachments - see code

Ok, I understand now.

 

Are they using a BlackBerry Enterprise Server or the BlackBerry Internet Service?  Can you get the full, 4 digit BlackBerry device software version for any of these devices?

Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Developer
Posts: 121
Registered: ‎03-25-2009
My Device: Z30

Re: Downloading email attachments - see code

Hello,

 

My bold 9780 is running 6.0.0.534. It works for me.

 

One customer is running 6.0.0.431 on a 9650.  Attachments do not work for him.

 

I am unable to get a hold of my other customer whom it was not working for, but he was using a Torch on Rogers, so likely whatever Rogers ships with.

 

Thanks,

 

~Shea M.