Search code examples
blackberry

Blackberry - run menu item from dialer Phone App


I'd like to describe one trick I learned at supportforums.blackberry.com

There is a native dialer Phone Application in BlackBerry.

The trick is to programmatically run menu items of dialer after incoming call, call fail or any other call event.


Solution

  • There is a PhoneListener interface, which gives an ability to listen to the status of incoming and outgoing phone calls.

    See Listen for and handle phone events

    A quote from supportforums.blackberry.com - Re: How to exit an Ui application (by simon_hain):

    Listeners are hard referenced by the application they are added to. Figuratively speaking, they become part of the rim application.

    If you add a listener to the phone application this listener is executed in the context of the phone app.
    You can check this by using Ui.getUiEngine().getActiveScreen() in a listener method. The returned screen is the call screen of the phone application.

    I use this to execute commands on phone calls:
    - on callInitiated or callConnected i store a reference to the phone screen.
    - i call phoneScreen.getMenu(0)

    now i want to execute a command:
    - i change the locale to "en"
    - i iterate through the menu using menu.getSize() and menu.getItem(i)
    - i check if menuItem.toString equals my command
    - i call menuItem.run()
    - and change the locale back (if it was changed)

    you can use this to:
    mute
    unmute
    activate speakerphone
    view speeddiallist
    end the call (only prior to 4.5/4.6, not sure which one)
    and many more. just print the available menu items :)

    A sample code for this trick, on incoming call print all menu to console, on answer call mute phone on end call - unmute phone:

    public class UseScreenMenu extends Application implements PhoneListener {
        String MENU_ITEM_MUTE = "Mute";
        String MENU_ITEM_UNMUTE = "Unmute";
        public UseScreenMenu() {
            Phone.addPhoneListener(this);
        }
    
        public static void main(String[] args) {
            UseScreenMenu app = new UseScreenMenu();
            app.enterEventDispatcher();
        }
    
        public void callIncoming(int callId) {
            printMenu();    
        }
    
        public void callAnswered(int callId) {
            runMenuItem(MENU_ITEM_MUTE);
        }
    
        public void callEndedByUser(int callId) {
            runMenuItem(MENU_ITEM_UNMUTE);  
        }
    
        private void printMenu() {      
            Screen screen = Ui.getUiEngine().getActiveScreen();
            Menu menu = screen.getMenu(0);
            System.out.println("Menu of BB Dialler - Begin");
            for (int i = 0, cnt = menu.getSize(); i < cnt; i++)
                System.out.println("Menu of BB Dialler - "
                    +menu.getItem(i).toString());
            System.out.println("Menu of BB Dialler - End");     
        }
    
        private void runMenuItem(String menuItemText) {
            Screen screen = Ui.getUiEngine().getActiveScreen();
            Menu menu = screen.getMenu(0);
            for (int i = 0, cnt = menu.getSize(); i < cnt; i++)
                if(menu.getItem(i).toString().equalsIgnoreCase(menuItemText))
                    menu.getItem(i).run();
        }
    
    
        public void callAdded(int callId) {}
        public void callConferenceCallEstablished(int callId) {}
        public void callConnected(int callId) {}
        public void callDirectConnectConnected(int callId) {}
        public void callDirectConnectDisconnected(int callId) {}
        public void callDisconnected(int callId) {}
        public void callFailed(int callId, int reason) {}
        public void callHeld(int callId) {}
        public void callInitiated(int callid) {}
        public void callRemoved(int callId) {}
        public void callResumed(int callId) {}
        public void callWaiting(int callid) {}
        public void conferenceCallDisconnected(int callId) {}
    }