maemo.org - Talk

maemo.org - Talk (https://talk.maemo.org/index.php)
-   Nokia N900 (https://talk.maemo.org/forumdisplay.php?f=44)
-   -   [HOW TO] Assign Ringtone by Caller-group for N900 (https://talk.maemo.org/showthread.php?t=52646)

9000 2010-05-14 08:36

[HOW TO] Assign Ringtone by Caller-group for N900
 
This script is made in response to all those "IT CAN'T BE DONE" posts. Consider this as a demonstration rather than an application. However, if you've a pressing needs of caller-group ringtones, you may use the script (with caution).

Hope this help.

Code:

#! /usr/bin/env python2.5
# callergroup - Assign Ringtone By Caller-Group for N900 v0.0.1
# By 9000 @ maemo.org 14-May-2010
#
# ATTENTION: Use the script at your own risk. You're responsible for any damage that'd cause by using this script.
#
# LIMITATION: Lots. Like lacking fancy UI,  insufficient format support (WAV only), etc.
#
# FEATURE: 1) Assign different ringtones to caller-groups you defined.
#
# Usage:
# (Move your default ringtone to /home/user/.local/share/sounds/callergroup/ , I choose Gradient.acc.wav in this example)
# mkdir -p /home/user/.local/share/sounds/callergroup/
# mv /home/user/.local/share/sounds/Gradient.acc.wav /home/user/.local/share/sounds/callergroup/.
#
# (Symlink your default ringtone back to /home/user/.local/share/sounds/ , Gradient.aac.wav in this example)
# ln -sf /home/user/.local/share/sounds/callergroup/Gradient.aac.wav /home/user/.local/share/sounds/Gradient.aac.wav
#
# (Copy any wav files you'd like to set for the groups. I use ringtone_for_group_X.wav in this example)
# cp /media/mmc1/ringtone/*.wav /home/user/.local/share/sounds/callergroup/. (just an example)
#
# (Run the script)
# /usr/bin/python ./callergroup.py
#


import gobject, dbus
import time
import os
from dbus.mainloop.glib import DBusGMainLoop

def handle_call(obj_path, callernumber):
        global blocklist
        print 'Calling from '+callernumber+'...'

# Caller group found:
        if callernumber in group_info:
                assigned_group = group_info[callernumber]
                group_ring = group_ringtones[assigned_group]
                print 'Caller belongs to Group:'+assigned_group+', ringtone assigned:'+group_ring+'.wav'
                bus = dbus.SessionBus()
                profiled = bus.get_object('com.nokia.profiled', '/com/nokia/profiled')
                proxy = dbus.Interface(profiled, 'com.nokia.profiled')
                system_ring = os.path.basename(proxy.get_value('general','ringing.alert.tone'))
                os.system('ln -sf /home/user/.local/share/sounds/callergroup/'+group_ring+'.wav /home/user/.local/share/sounds/'+system_ring+'.wav')
# Default:
        else:
                print 'Caller has no assigned group'
                bus = dbus.SessionBus()
                profiled = bus.get_object('com.nokia.profiled', '/com/nokia/profiled')
                proxy = dbus.Interface(profiled, 'com.nokia.profiled')
                system_ring = os.path.basename(proxy.get_value('general','ringing.alert.tone'))
                os.system('ln -sf /home/user/.local/share/sounds/callergroup/'+system_ring+'.wav /home/user/.local/share/sounds/'+system_ring+'.wav')

# ---Customize the following arrays yourself--- #

# Change the name of the ringtones for the corresponding groups
group_ringtones = {        'GroupA':'ringtone_for_group_a',
                        'GroupB':'ringtone_for_group_b',
                        'GroupC':'ringtone_for_group_c',
                        }

# Associate the numbers to the groups
group_info = {        '23820000':'GroupA',
                '28020021':'GroupB',
                '12345678':'GroupB',
                '87654321':'GroupC',
                }

# ---End of Customization--- #

DBusGMainLoop(set_as_default=True)
bus = dbus.SystemBus()
bus.add_signal_receiver(handle_call, path='/com/nokia/csd/call', dbus_interface='com.nokia.csd.Call', signal_name='Coming')
gobject.MainLoop().run()

Source reference:
http://maemocentral.com/2010/02/22/h...s-on-the-n900/
http://www.maemoers.com/archiver/tid-2364.html

Pigro 2010-05-14 08:40

Re: [HOW TO] Assign Ringtone by Caller-group for N900
 
system_ring = os.path.basename(proxy.get_value('general','ringin g.alert.tone'))

haven't tried this but I'm guessing that is a rogue space embedded in the above line?

9000 2010-05-14 08:44

Re: [HOW TO] Assign Ringtone by Caller-group for N900
 
Quote:

Originally Posted by Pigro (Post 658008)
system_ring = os.path.basename(proxy.get_value('general','ringin g.alert.tone'))

haven't tried this but I'm guessing that is a rogue space embedded in the above line?

Looks like it's inserted by the forum engine.

Thanks for pointing it out promptly.

pelago 2010-05-14 08:45

Re: [HOW TO] Assign Ringtone by Caller-group for N900
 
Thanks for this. For future reference, you might want to know that you can wrap code with [code] tags to make it appear nicer, keeping indents etc.

9000 2010-05-14 08:48

Re: [HOW TO] Assign Ringtone by Caller-group for N900
 
Quote:

Originally Posted by pelago (Post 658014)
Thanks for this. For future reference, you might want to know that you can wrap code with [code] tags to make it appear nicer, keeping indents etc.

Oh! That's great! Thank you for the tips. Corrected.

pelago 2010-05-14 11:06

Re: [HOW TO] Assign Ringtone by Caller-group for N900
 
Can I just make sure I've got the general idea of your program correct, based on my inexpert reading of the code: The program runs continuously in the background. When it detects an incoming call, it quickly changes the system-defined ringtone using a symbolic link, based on a lookup of which phone number should be in which group. The actual playing of the tone is done by the phone app itself. Is that right?

I'm just wondering what happens if for some reason the phone app gets there first and starts playing a different ringtone, before your app gets a chance to change the symlink. Will the ringtone change half-way through?

Does your app impact battery life, if it is running in the background all the time?

Thinking about a potential GUI, obviously there would need to be a way to define groups (giving each a custom name would be nice), plus a way to pick a ringtone per group. Then there would need to be a way to associate a contact with each group. I wonder if there's a way to hook into the contacts framework to do this. Maybe the group could be stored as a Note field in the contact, or something like that.

9000 2010-05-14 14:20

Re: [HOW TO] Assign Ringtone by Caller-group for N900
 
Hi Pelago,

Quote:

Originally Posted by pelago (Post 658147)
Can I just make sure I've got the general idea of your program correct, based on my inexpert reading of the code: The program runs continuously in the background. When it detects an incoming call, it quickly changes the system-defined ringtone using a symbolic link, based on a lookup of which phone number should be in which group. The actual playing of the tone is done by the phone app itself. Is that right?

Correct. As the phone will play the same clip for all calls. The program is then changing the pointer (aka symlink) of that clip according to the caller number, before the phone play that clip.

[/quote]
I'm just wondering what happens if for some reason the phone app gets there first and starts playing a different ringtone, before your app gets a chance to change the symlink. Will the ringtone change half-way through?
[/quote]

The script setup an event loop to pick up signal. I'm sure the race condition you've mention would happen. Anyone would suggest a better way to preempt the ringing would be welcome. ^^

Quote:

Does your app impact battery life, if it is running in the background all the time?
It'd absolutely increase the load thus drain battery, but it's pretty lightweight. Any suggest to benchmark electricity consumption?

Quote:

Thinking about a potential GUI, obviously there would need to be a way to define groups (giving each a custom name would be nice), plus a way to pick a ringtone per group. Then there would need to be a way to associate a contact with each group. I wonder if there's a way to hook into the contacts framework to do this. Maybe the group could be stored as a Note field in the contact, or something like that.
UI....but I'm not sure whether I'd continue its development. As I said, this script is served as a 'reply' to some negative comments against N900. A working program is better than thousands words. XD

However, creating extra column in each contact and have the program read that column to determine its ringtone is a very good idea. It doesn't require much effort to accomplish. May be I would enhance it a bit for that... later. ^^

Matan 2010-05-14 14:27

Re: [HOW TO] Assign Ringtone by Caller-group for N900
 
Did you actually test this?

9000 2010-05-14 14:34

Re: [HOW TO] Assign Ringtone by Caller-group for N900
 
Quote:

Originally Posted by Matan (Post 658399)
Did you actually test this?

Yes. If you want to try please read the instruction carefully. I didn't write code capturing errors.

Matan 2010-05-14 14:38

Re: [HOW TO] Assign Ringtone by Caller-group for N900
 
Funny. When I tried this method, my script was always running too late, and the phone application played the previous ringtone.


All times are GMT. The time now is 14:59.

vBulletin® Version 3.8.8