Search code examples
blackberryreplacemessagepreview

Blackberry Notification Bar: How to replace Message Preview Window


Hi I'm trying to make my first blackberry app using eclipse, the main idea of the app is only to show current location based on internal GPS LatLong << skip this part.

now the question is I've been looking for almost two weeks about how to replace "Message Preview" window that appear if custom message is clicked Here's my code

Class DemoMessage

public final class DemoMessage implements ApplicationMessage
{
static final int DEMO_MESSAGE_TYPE = 0x01;

private String _sender;
private String _subject;
private String _message;
private long _receivedTime;
private boolean _isNew;
private boolean _deleted;
private String _replyMessage;
private long _replyTime;
private EncodedImage _previewPicture;

public DemoMessage()
{
    _isNew = true;
}

DemoMessage(String sender, String subject, String message, long receivedTime)
{
    _sender = sender;
    _subject = subject;
    _message = message;
    _receivedTime = receivedTime;
    _isNew = true;
}

void reply(String message)
{
    markRead();
    _replyMessage = message;
    _replyTime = System.currentTimeMillis();
}

void messageDeleted()
{
    _isNew = false;
    _deleted = true;
}

void markAsNew()
{
    _isNew = true;
    _replyMessage = null;
}

void markRead()
{
    _isNew = false;
}

boolean isNew()
{
    return _isNew;
}

boolean hasReplied()
{
    return _replyMessage != null;
}

void setSender(String sender)
{
    _sender = sender;
}

void setSubject(String subject)
{
    _subject = subject;
}

void setReceivedTime(long receivedTime)
{
    _receivedTime = receivedTime;
}

void setMessage(String message)
{
    _message = message;
}

String getMessage()
{
    return _message;
}

void setPreviewPicture(EncodedImage image)
{
_previewPicture = image;
}

public String getContact()
{
return _sender;
}

public int getStatus()
{
    if(_isNew)
    {
        return MyApp.STATUS_NEW;
    }

    if(_deleted)
    {
        return MyApp.STATUS_DELETED;
    }

    if(_replyMessage != null)
    {
        return MyApp.STATUS_REPLIED;
    }

    return MyApp.STATUS_OPENED;
}

public String getSubject()
{
    if(_replyMessage != null)
    {
        return "Re: " + _subject;
    }
    else
    {
        return _subject;
    }
}

public long getTimestamp()
{
    return _receivedTime;
}

public int getType()
{
    return DEMO_MESSAGE_TYPE;
}

public String getPreviewText()
{
    if(_message == null)
    {
        return null;
    }

    StringBuffer buffer = new StringBuffer(_message);

    if(_replyMessage != null)
    {
        buffer.append(". You replied on ").append(new Date(_replyTime)).append(": ").append(_replyMessage);
    }

    return buffer.length() > 100 ? buffer.toString().substring(0, 100) + " ..." : buffer.toString();
}

public Object getCookie(int cookieId)
{
    return null;
}

public Object getPreviewPicture()
{
    return _previewPicture;
}
}

Class DemoMessageScreen

public final class DemoMessageScreen extends MainScreen
{
public DemoMessageScreen()
{       
    setTitle("Title");

    ReadableListImpl mylist= new ReadableListImpl();
    ApplicationMessageFolder folder = null;
    ApplicationFolderIntegrationConfig config = new ApplicationFolderIntegrationConfig(true, true, ApplicationDescriptor.currentApplicationDescriptor());

    if(ApplicationMessageFolderRegistry.getInstance().getApplicationFolder(0x33c7ce29883abe5fL)==null)
    {
         folder = ApplicationMessageFolderRegistry.getInstance().registerFolder(0x33c7ce29883abe5fL, "Test Folder", new ReadableListImpl(),config );
    }
    else
    {
         folder = ApplicationMessageFolderRegistry.getInstance().getApplicationFolder(0x33c7ce29883abe5fL);
    }

    DemoMessage msg = new DemoMessage("[email protected]", "Pizza Toppings","What would you like on your pizza?", System.currentTimeMillis());

    mylist.addMessage(msg);

    folder.fireElementAdded(msg,true);

    System.out.println("nr of messages"+folder.hasNewMessages());

    ApplicationIndicatorRegistry reg = ApplicationIndicatorRegistry.getInstance();

    EncodedImage image = EncodedImage.getEncodedImageResource("indicatorOn.png" );

    ApplicationIcon icon = new ApplicationIcon( image );

    ApplicationIndicator indicator = reg.register( icon, false, true);

    indicator.setNotificationState(true);

    ApplicationIndicator appIndicator = reg.getApplicationIndicator();

    appIndicator.setIcon(icon);

    appIndicator.setValue(appIndicator.getValue() + 1);

    //appIndicator.setNotificationState(true);

    appIndicator.setVisible(true);
}

public boolean onClose()
{
    close();
    return true;
}

static class ReadableListImpl implements ReadableList
{
    private Vector messages;

    ReadableListImpl()
    {
        messages = new Vector();
    }

    public Object getAt(int index)
    {
        return messages.elementAt(index);
    }

    public int getAt(int index, int count, Object[] elements, int destIndex)
    {
        return 0;
    }

    public int getIndex(Object element)
    {
        return messages.indexOf(element);
    }

    public int size()
    {
        return messages.size();
    }

   void addMessage(DemoMessage message)
    {
        messages.addElement(message);
    }

    void removeMessage(DemoMessage message)
    {
        messages.removeElement(message);
    }
}
}

and Class MyApp

public class MyApp extends UiApplication
{
static final int FLAG_REPLIED = 1 << 16;
static final int FLAG_DELETED = 1 << 17;
static final int BASE_STATUS = ApplicationMessage.Status.INCOMING;
static final int STATUS_NEW = BASE_STATUS | ApplicationMessage.Status.UNOPENED;
static final int STATUS_OPENED = BASE_STATUS | ApplicationMessage.Status.OPENED;
static final int STATUS_REPLIED = BASE_STATUS | ApplicationMessage.Status.OPENED | FLAG_REPLIED;
static final int STATUS_DELETED = BASE_STATUS | FLAG_DELETED;

public static void main(String[] args)
{
    MyApp theApp = new MyApp();      
    theApp.enterEventDispatcher();
}

public MyApp()
{   
        pushScreen(new DemoMessageScreen());
}
}

the codes are fine, i can see the notification at homescreen and message folder, but it calls "Message Preview" window if clicked, i don't know how to register a click handler to launch my application from notification message bar and message folder, how to do that? thanks in advance


Solution

  • Problem solved, i have to declare ApplicationDescriptor in the body of the class.