szanata / PhoneStateChangeListener

Cordova plugin to provide callback when Telephony state changes
8 stars 4 forks source link

Get the phone number #1

Open royduin opened 11 years ago

royduin commented 11 years ago

Hi,

Can you extend this plugin with the possibility to get the phone number from the incoming caller?

I'll really appreciate that!

royduin commented 11 years ago

And.. I can't get it working. I've to put the js file in the assets/www folder and the java file in the src/com/szanata/cordova/plugins folder?

If I use the sample index.js file and put it in the on device ready function it should work?

onDeviceReady: function(){
    plugins.PhoneStateChangeListener.start(function (state){
       alert(state); 
    });
}

Meanwhile I've added this in the AndroidManifest.xml to:

<uses-permission android:name="android.permission.READ_PHONE_STATE" />

And added this line in the res/xml/config.xml file:

<plugin name="PhoneListener" value="com.szanata.cordova.plugins.PhoneListener"/>

And last, added this in the assets/www/index.html file:

<script type="text/javascript" src="plugins.PhoneStateChangeListener.js"></script>

But it still doesn't work. I hope you can help me out!

szanata commented 11 years ago

In the file res/xml/config.xml you must put this

<plugin name="PhoneListener" value="com.szanata.cordova.plugins.PhoneStateChangeListener"/>

PhoneStateChangeListener and not PhoneListener

royduin commented 11 years ago

Just changed it but it still doesn't work. In LogCat in Eclipse I doesn't get any errors to? Can you provide me maybe a working example?

royduin commented 11 years ago

After check again in LogCat there's a output! With the above code I don't get an alert and even when I use console.log(state); it doesn't return anything. But I get a information message in LogCat with the tag Device and the Text Telephone RINGING. But I've no idea how to capture this and do something with it.

But where I'm really interested in is getting the phone number from the incoming caller. Can you help me with that?

szanata commented 11 years ago

console.log works fine in the eclipse, but to do an Alert with cordova you must use:

navigator.notification.alert(message,callback,title);

As seen in the docs

And yes, I'll add the feature to get the incoming call number, but only if it doesn't require more permissions.

Please, let me know when you make your code work with my plugin

royduin commented 11 years ago

I've got this now:

test: function() {
    console.log('test called');
    plugins.PhoneStateChangeListener.start(function (state){
        console.log('Phone state:' + state);
    });
}

And this is what I get in LogCat: alt text The console.log isn't called within the anonymous function.

There's already a extended version of PhoneListener which can get the incoming call number. But this version doesn't work with the newest Phonegap versions. Maybe it can help you extend PhoneStateChangeListener: https://github.com/devgeeks/PhoneListener/blob/extended/PhoneListener.java

If I could code Java I've created it by myself but you really help me if you would do this for me!

szanata commented 11 years ago

Which version of phonegap are you using?

royduin commented 11 years ago

The newest 2.7

Oeps, closed it but reopened it

szanata commented 11 years ago

I'm using this plugin on version 2.7 and works fine.

Are your sure this code is running after ondeviceready event?

royduin commented 11 years ago

Yes, 100%. Any other ideas?

szanata commented 11 years ago

Put a java log in the line 48 of the .java file, or try to do a break point on line 47, verify if the flow is passing thru this point.

royduin commented 11 years ago

I'm totally new with Java but I've added this

Log.i("Test","CALLED");

On line 48, when I check LogCat, this message, isn't there. Hereby my complete Javascript code, maybe it helps. Nothing special, just the example code with a extra test function with the plugin code.

var app = {
    // Application Constructor
    initialize: function() {
        this.bindEvents();
    },
    // Bind Event Listeners
    //
    // Bind any events that are required on startup. Common events are:
    // 'load', 'deviceready', 'offline', and 'online'.
    bindEvents: function() {
        document.addEventListener('deviceready', this.onDeviceReady, false);
    },
    // deviceready Event Handler
    //
    // The scope of 'this' is the event. In order to call the 'receivedEvent'
    // function, we must explicity call 'app.receivedEvent(...);'
    onDeviceReady: function() {
        app.receivedEvent('deviceready');
    },
    // Update DOM on a Received Event
    receivedEvent: function(id) {
        var parentElement = document.getElementById(id);
        var listeningElement = parentElement.querySelector('.listening');
        var receivedElement = parentElement.querySelector('.received');

        listeningElement.setAttribute('style', 'display:none;');
        receivedElement.setAttribute('style', 'display:block;');

        console.log('Received Event: ' + id);

        app.test();
    },
    test: function() {
        console.log('test called');
        plugins.PhoneStateChangeListener.start(function (state){
            console.log('Phone state:' + state);
        });
    }
};
royduin commented 11 years ago

Just found something in LogCat with the debug level, maybe that's the problem: Tag PluginManager and the Text exec() call to unknown plugin: PhoneStateChangeListener

UPDATE Just found this page when searching on that error: http://tanhaishi.blogspot.nl/2013/05/childbrowser-in-cordova-phonegap-26.html and after checking the res/xml/config.xml file I've changed this:

<plugin name="PhoneListener" value="com.szanata.cordova.plugins.PhoneStateChangeListener"/>

To this:

<plugin name="PhoneStateChangeListener" value="com.szanata.cordova.plugins.PhoneStateChangeListener"/>

And now it looks like it's working! Please change this in your README.md

Can you help me with getting the incoming call number to?

UPDATE 2 When I log the state, like this:

plugins.PhoneStateChangeListener.start(function (state){
    document.getElementById('test').innerHTML += '<br>' + state;
});

And when the app starts the div with a id of test is empty, when the phone is ringing, the content will be RINGING, but that's it. I don't get other states like IDLE when the caller stops calling and when I call again I don't get another RINGING. It looks like it stops automatically after a state change. Is this right and do I have to run plugins.PhoneStateChangeListener.start another time or is this some kind of bug?

UPDATE 3 When I do this:

test: function() {
    plugins.PhoneStateChangeListener.start(function (state){
        document.getElementById('test').innerHTML += '<br>' + state;
        app.test();
    });
}

It keeps listening and I get the other statuses to, but is this the way it should?

royduin commented 11 years ago

I keep posting :) Just found the solution to get the phone number to!

public void startPhoneListener() {

    if (this.receiver == null) {
        this.receiver = new BroadcastReceiver() {

            @Override
            public void onReceive(final Context context, final Intent intent) { 

                if(intent != null && intent.getAction().equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {
                    String state = intent.hasExtra(TelephonyManager.EXTRA_STATE) ? intent.getStringExtra(TelephonyManager.EXTRA_STATE) : "NONE";
                    String number = "";

                    if(state != "NONE" && state.equals(TelephonyManager.EXTRA_STATE_RINGING)){
                        number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
                    }

                    if (callbackContext != null){
                        callbackContext.success("{\"state\":\""+state+"\",\"number\":\""+number+"\"}");
                    }
                }

            }
        };
        this.context.registerReceiver(this.receiver, new IntentFilter(TelephonyManager.ACTION_PHONE_STATE_CHANGED));
    }
}

With Javascript you just have to parse it:

test: function() {
    plugins.PhoneStateChangeListener.start(function(result){
        var object = JSON.parse(result);
        document.getElementById('test').innerHTML += '<br>' + object['state'] + ' ' + object['number'];
        app.test();
    });
}
szanata commented 11 years ago

Hey, thanks for posting!

I'll change my docs and change my code to return the number!

Didn't know the this was triggering only one time, I'll fix that too.

Thansk a lot! Tomorrow I'll update this with the changes

royduin commented 11 years ago

Thank you for helping me out ;)

szanata commented 11 years ago

New version 1.1 with second parameter to get the incoming number:

plugins.PhoneStateChangeListener.start(function(state, number){
   // bla bla bla
});

I checked carefully and didn't noticed the callback was being called only once. On my tests, I called the start funcition only once and it worked every time.

royduin commented 11 years ago

Nice! Thank you!

The last thing I want to ask you, but has nothing to do with this plugin: I'd like to create a app like Truecaller. So: If my app is active and there's an incoming caller the app will notice this (thanks to your plugin!) and when it does it's sending the phone number of the incoming call by Ajax to the server. The server is getting some information and send some information like a name and address back to the app. Now, I'd like to create some kind of popup on the incoming caller screen (but the user must still be able to "pick up"): alt text

Can you help me out with this to (which can work with Phonegap)? If desired, payment is not a problem.

szanata commented 11 years ago

Sorry, but I can't do that, my knowledge in Android development is poor, I'm a javascript guy =/

But as a start point, check out this plugin: StatusBarNotification.

Thanks

royduin commented 11 years ago

Thanks! But the statusbar isn't working when the phone is ringing. So I can't drag it down to see the notifications. But, thanks for the suggestion! If you know someone who can do this for me, please let me know!

Again, thanks for everything!

Nadhiro commented 11 years ago

i can't debug the start function

to be sure that i do the right thing, can u post the full index.html on the plugin description? can you help me on that?

szanata commented 11 years ago

Hey friend, I was on vacations o/. Do you have problems yet?

mohamedsalehamin commented 9 years ago

any news about this issue ... can i implement name and image in the call window ? can i use it with phonegap 3 ?