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
Contributor
graymatter
Posts: 28
Registered: ‎07-29-2010
My Device: Torch 9850

Monitoring the message store

Hello all!

 

While I'm certainly not new to programming, I'm still very new to java and application development on the BlackBerry.  To get my feet wet I wanted to build a simple application for a friend.  The purpose of the application is to provide a homescreen icon which provides information on the current number of unread messages (be they email, SMS, MMS etc).  Effectively, a larger version of the notification icon that appears in the notification area.

 

So far I have come up with the following:

 

 

package net.graymatteronline.messagebuddy;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.UiApplication;
import net.rim.blackberry.api.mail.Session;
import net.rim.blackberry.api.mail.Store;
import net.rim.blackberry.api.mail.event.StoreEvent;
import net.rim.blackberry.api.mail.event.StoreListener;
import net.rim.blackberry.api.homescreen.HomeScreen;
import net.rim.blackberry.api.mail.NoSuchServiceException;
import net.rim.device.api.applicationcontrol.ApplicationPermissions;
import net.rim.device.api.applicationcontrol.ApplicationPermissionsManager;

public class MessageBuddy extends UiApplication implements StoreListener
{
    /**
     * Entry point for application
     */ 
    public static void main(String[] args)
    {
    	if (args[0].equals("Icon")) {
    		checkPermissions();
    		updateHSIcon();
    		System.exit(0);
    	}
    	
    	if (args[0].equals("AutoStart")) {
    		MessageBuddy theApp = new MessageBuddy();
    		theApp.enterEventDispatcher();
    	}
    }
    
    public MessageBuddy()
    {
    	// Check and request (if necessary) permissions
    	checkPermissions();
    	
        // Update the icon immediately on first startup
    	updateHSIcon();
    	
    	Store store = null;
    	
		try {
			store = Session.waitForDefaultSession().getStore();
		} catch (NoSuchServiceException e) {
			System.out.println(e.toString());
		}
		store.addStoreListener(this);
    }
    
    private static void checkPermissions() {
    	ApplicationPermissionsManager apm = ApplicationPermissionsManager.getInstance();
        ApplicationPermissions original = apm.getApplicationPermissions();
        
        if(original.getPermission(ApplicationPermissions.PERMISSION_CROSS_APPLICATION_COMMUNICATION) == ApplicationPermissions.VALUE_ALLOW &&
                original.getPermission(ApplicationPermissions.PERMISSION_EMAIL) == ApplicationPermissions.VALUE_ALLOW)
             {
                 // All of the necessary permissions are currently available
                 return;
             }

             // Create a permission request for each of the permissions your application
             // needs. Note that you do not want to list all of the possible permission
             // values since that provides little value for the application or the user.  
             // Please only request the permissions needed for your application.
             ApplicationPermissions permRequest = new ApplicationPermissions();
             permRequest.addPermission(ApplicationPermissions.PERMISSION_CROSS_APPLICATION_COMMUNICATION);
             permRequest.addPermission(ApplicationPermissions.PERMISSION_EMAIL);

             boolean acceptance = ApplicationPermissionsManager.getInstance().invokePermissionsRequest(permRequest);

             if(acceptance)
             {
                 // User has accepted all of the permissions
                 return;
             }
             else
             {
                 // The user has only accepted some or none of the permissions
            	 return;
             }
    }


	private static void updateHSIcon() {
		// Update the icon with the current unread count
		Store store = Session.getDefaultInstance().getStore();
		String numUnread = Integer.toString(store.getUnreadMessageCount());
		
		
		// Overlay the icon graphic with the unread count
		Bitmap bitmap = Bitmap.getBitmapResource("mbicon.png");
		Graphics work = Graphics.create(bitmap);
		work.setColor(Color.RED);
		work.drawText(numUnread, 10, 20);
		HomeScreen.updateIcon(bitmap);
	}


	public void batchOperation(StoreEvent e) {
		updateHSIcon();
	}
}

 Updating the homescreen icon  and drawing the unread amount is simple enough and has caused no problems, however, where I am having difficulty is in linking my app into the message system so that it is informed whenever the current number of unread items may have changed.  There are some examples to be found on the internet, however, they seem incomplete or are perhaps assuming that I have a basic understanding which I don't :smileyhappy:  For example, I am not sure if I should be monitoring the messagestore or something else to achieve what I need.

 

If anyone could offer any assistance in this area I would be most grateful.

 

 

On a side note, I've noticed that some BlackBerry apps that have tasks running constantly in the background will appear on the application switcher list and some won't.  What is the difference between them that defines this behaviour?  Is it whether or not they are extending UIApplication in the background task?  It seems to me that a much cleaner approach would be to have any background monitor thread not showing up in the application switcher.

 

Thank you in advance for any help offered.

 

Russ.

Developer
Developer
CMY
Posts: 1,123
Registered: ‎02-10-2009
My Device: 8130 / 8350 / 9530 / 9550 / 9850 / PlayBook

Re: Monitoring the message store

I haven't dealt with SMS/MMs but for emails you can check the message store to see how many unread emails there are. I think there might even be a listener to see when a mailbox or folder has changed indicating a message has been read.

As far as the background tasks, you can derive from Application or setAccepts foreground to false to kepp from appearing on the application switcher.

Contributor
graymatter
Posts: 28
Registered: ‎07-29-2010
My Device: Torch 9850

Re: Monitoring the message store

Thank you for your reply, CMY.  It seems then that I am on the right path by using the message store for monitoring.  The issue I am having is in coding my application so that it is indeed invoked whenever the message store is added to / deleted from / changed.  My app just doesn't seem to be receiving those notifications.

 

Could anyone please offer me any assistance in this area or point me in the direction of a thorough example?

 

Russ.

Contributor
marKram
Posts: 33
Registered: ‎02-13-2010
My Device: 9800

Re: Monitoring the message store

In your code above you're using a StoreListener. I think that will only tell you about bulk changes to items in a store. You probably want to use a FolderListener. There is a good example application called "FolderListenerExample" that you can easily add to your Eclipse workspace if you use File -> Import -> BlackBerry -> Import BlackBerry Samples. Hope this helps.

Contributor
graymatter
Posts: 28
Registered: ‎07-29-2010
My Device: Torch 9850

Re: Monitoring the message store

Thank you!  I'll check it out.