Call me a cheapskate, as I still have not subscribe to a mobile data plan after purchasing my second smartphone, namely Nokia N9. There’s this ‘allow background connections’ option but it doesn’t care whether the connected network is a WLAN network or mobile data network. After finding out that Nokia has no interest in creating another separate option so that each type of network has their respective ‘allow background connections’ switch, I decided to make one for my own.
Turns out it is not too difficult to implement it via Python and PySide, thanks to djszapi, frals, itsabigtruck for their guidance over #harmattan IRC channel :). Writing the daemon part is easy (but still took me half a day to get it work properly), however I have not figure out how to put everything together in Qt Creator. As I have not done any daemon in the past (I am a web developer, remember?), and Python not being my favorite language, it did took me quite a while to get the daemon part done. I followed the example published by Sander Marechal and have my simple daemon done.
Then I proceeded with the actual script, apparently toggling on and off the settings is not as complicated as it seems. Using python-gconf, the actual code to do the work is just:
gconf.client_get_default().set_bool('/system/osso/connectivity/network_type/restricted_mode', True);
So I proceeded with the actual QSystemAlignedTimer code (I used QTimer initially to get the idea) as this is the recommended way of creating a daemon as stated here. It took me quite some time to get everything working, and to my surprise it involves using of threads. If this is not confusing enough, apparently there’s a right way of working with threads.
However, after pulling a considerable amount of hair, and some help from Stackoverflow, I managed to vomit out something that works, as follows
class Updater(QObject):
def update_mode(self):
gconf.client_get_default().set_bool('/system/osso/connectivity/network_type/restricted_mode', self.is_wlan() == False);
def is_wlan(self):
return QSystemNetworkInfo().networkStatus(QSystemNetworkInfo.WlanMode) == QSystemNetworkInfo.Connected;
def run(self):
timer = QSystemAlignedTimer(self);
timer.timeout.connect(self.update_mode);
timer.start(300, 330);
class MyDaemon(Daemon):
def run(self):
app = QApplication(sys.argv);
thread = QThread();
thread.start();
update = Updater();
update.moveToThread(thread);
update.run();
app.exec_();
Now, the only question left is, how do I put everything together as a Qt project in Qt Creator so I can deploy this on my N9 properly. I am currently reading this so that I can make my daemon to run at startup.