Show me the code! – By Davanum Srinivas

December 27, 2007

Android – Listen to Phone Notifications using android.os.Mailbox

Filed under: Uncategorized — Davanum Srinivas @ 10:53 am

Here is a screen shot.

1

Notes


When i was looking at the strace output closely. I noticed a service named android.telephony.PhoneNotifier. It stuck somewhere in my head. A few(?) days later i ran info the android.os.Mailbox class. Another few days later, when poking into the dexdump output of Phone.apk, it all came together. Here’s the result of experimentation. Basically you can add use a mailbox to receive the notifications from the process that controls the phone. It’s an alternative to registering and listening for intents.

We basically create a mailbox and send a message to the PhoneNotifier to send messages to that mailbox

package org.apache.android.ril;

import android.app.ListActivity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Mailbox;
import android.os.Message;
import android.util.Log;
import android.widget.ArrayAdapter;

import java.util.ArrayList;
import java.util.HashMap;

public class RILSandbox extends ListActivity {

    MyHandler handler = new MyHandler();
    Mailbox myMB = Mailbox.createAnonymous(handler);
    ArrayList items = new ArrayList();

    /**
     * Called with the activity is first created.
     */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        registerForNotification(1);
        registerForNotification(2);
        registerForNotification(3);
        registerForNotification(4);
        registerForNotification(5);
        registerForNotification(6);
        registerForNotification(7);
    }

    private void registerForNotification(int type) {
        Message msg = Message.obtain();
        msg.what = type;
        HashMap map = new HashMap();
        map.put("mailbox", myMB);
        msg.setData(map);
        Mailbox.sendToPublished("android.telephony.PhoneNotifier", msg, null);
    }

    public class MyHandler extends Handler implements Mailbox.Receiver {
        public Object onNewMail(Message message, Mailbox.Completion completion) {
            switch (message.what) {
                case 1:
                    items.add("PhoneState: " + message.getData());
                    break;
                case 2:
                    items.add("ServiceState: " + message.getData());
                    break;
                case 3:
                    items.add("SignalStrength - 1: " + message.getData());
                    items.add("SignalStrength - 2: " + message.arg1);
                    break;
                case 4:
                    items.add("DataConnection: " + message.getData());
                    break;
                case 5:
                    items.add("MessageWaitingChanged - 1: " + message.getData());
                    items.add("MessageWaitingChanged - 2: " + message.arg1);
                    break;
                case 6:
                    items.add("CallForwardingChanged - 1: " + message.getData());
                    items.add("CallForwardingChanged - 2: " + message.arg1);
                    break;
                default:
                    items.add("Unknown - 1: " + message.what);
                    items.add("Unknown - 2: " + message.getData());
                    break;
            }
            updateListAdapter();
            return null;
        }
    }

    private void updateListAdapter() {
        setListAdapter(new ArrayAdapter(this,
                android.R.layout.simple_list_item_1, items));
    }
}

Notes



  • When the app starts up, you will see a few messages float by

  • Try “gsm call +223424” after telnet-ing to 5554 to see additional notifications.

Download Source and APK from here – RILSandbox.zip

2 Comments »

  1. Thanks for your ‘Show me the code’ website. It is very helpful.

    Comment by Karl Stamm — February 29, 2008 @ 1:43 pm


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: