Copyright (c) 2008-2011 Vrije Universiteit, The Netherlands
All rights reserved.
Redistribution and use in source and binary forms,
with or without modification, are permitted provided
that the follo...
If you think the Android project interdroid-swan listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
package interdroid.swan.sensors.impl;
//fromwww.java2s.comimport interdroid.swan.R;
import interdroid.swan.sensors.AbstractConfigurationActivity;
import interdroid.swan.sensors.AbstractVdbSensor;
import interdroid.vdb.content.avro.AvroContentProviderProxy;
import android.content.ContentValues;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
/**
* Sensor for phone state.
*
* @author nick <palmer@cs.vu.nl>
*
*/publicclass CallSensor extends AbstractVdbSensor {
privatestaticfinal String TAG = "Call Sensor";
/**
* The configuration activity for this sensor.
*
* @author nick <palmer@cs.vu.nl>
*
*/publicstaticclass ConfigurationActivity extends
AbstractConfigurationActivity {
@Override
publicfinalint getPreferencesXML() {
return R.xml.call_preferences;
}
}
/**
* The call state.
*/publicstaticfinal String STATE_FIELD = "call_state";
/**
* The phone number associated with the state if any.
*/publicstaticfinal String PHONE_NUMBER_FIELD = "phone_number";
/**
* The schema for this sensor.
*/publicstaticfinal String SCHEME = getSchema();
/**
* The provider for this sensor.
*
* @author nick <palmer@cs.vu.nl>
*
*/publicstaticclass Provider extends AvroContentProviderProxy {
/**
* Construct the provider for this sensor.
*/public Provider() {
super(SCHEME);
}
}
/**
* @return the schema for this sensor.
*/privatestatic String getSchema() {
String scheme = "{'type': 'record', 'name': 'call', "
+ "'namespace': 'interdroid.context.sensor.call',"
+ "\n'fields': [" + SCHEMA_TIMESTAMP_FIELDS + "\n{'name': '"
+ STATE_FIELD + "', 'type': 'int'}," + "\n{'name': '"
+ PHONE_NUMBER_FIELD + "', 'type': 'string'}" + "\n]" + "}";
return scheme.replace('\'', '"');
}
/**
* The telephony manager we use.
* */private TelephonyManager telephonyManager;
/**
* The phone state listener which gets notified on call state changed.
*/private PhoneStateListener phoneStateListener = new PhoneStateListener() {
@Override
publicvoid onCallStateChanged(finalint state,
final String incomingNumber) {
Log.d(TAG, "Call State: " + state + " " + incomingNumber);
long now = System.currentTimeMillis();
ContentValues values = new ContentValues();
values.put(STATE_FIELD, state);
if (incomingNumber != null && incomingNumber.length() > 0) {
values.put(PHONE_NUMBER_FIELD, incomingNumber);
}
putValues(values, now);
}
};
@Override
publicfinal String[] getValuePaths() {
returnnew String[] { STATE_FIELD, PHONE_NUMBER_FIELD };
}
@Override
publicfinalvoid initDefaultConfiguration(final Bundle defaults) {
}
@Override
publicfinal String getScheme() {
return SCHEME;
}
@Override
publicfinalvoid onConnected() {
Log.d(TAG, "call sensor connected");
}
@Override
publicfinalvoid register(final String id, final String valuePath,
final Bundle configuration) {
if (registeredConfigurations.size() == 1) {
telephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
telephonyManager.listen(phoneStateListener,
PhoneStateListener.LISTEN_CALL_STATE);
}
}
@Override
publicfinalvoid unregister(final String id) {
if (registeredConfigurations.size() == 0) {
telephonyManager.listen(phoneStateListener,
PhoneStateListener.LISTEN_NONE);
}
}
@Override
publicvoid onDestroySensor() {
// TODO Auto-generated method stub
}
}