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.BroadcastReceiver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.PowerManager;
/**
* A sensor for if the screen is on or off.
*
* @author nick <palmer@cs.vu.nl>
*
*/publicclass ScreenSensor extends AbstractVdbSensor {
/**
* The configuration activity for this sensor.
*
* @author nick <palmer@cs.vu.nl>
*
*/publicstaticclass ConfigurationActivity extends
AbstractConfigurationActivity {
@Override
publicfinalint getPreferencesXML() {
return R.xml.screen_preferences;
}
}
/**
* Is screen on field.
*/publicstaticfinal String IS_SCREEN_ON_FIELD = "is_screen_on";
/**
* 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': 'screen', "
+ "'namespace': 'interdroid.context.sensor.screen',"
+ "\n'fields': [" + SCHEMA_TIMESTAMP_FIELDS + "\n{'name': '"
+ IS_SCREEN_ON_FIELD + "', 'type': 'string'}" + "\n]" + "}";
return scheme.replace('\'', '"');
}
/**
* The receiver of screen information.
*/private BroadcastReceiver screenReceiver = new BroadcastReceiver() {
@Override
publicvoid onReceive(final Context context, final Intent intent) {
long now = System.currentTimeMillis();
ContentValues values = new ContentValues();
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
values.put(IS_SCREEN_ON_FIELD, "false");
} elseif (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
values.put(IS_SCREEN_ON_FIELD, "true");
}
putValues(values, now);
}
};
@Override
publicfinal String[] getValuePaths() {
returnnew String[] { IS_SCREEN_ON_FIELD };
}
@Override
publicvoid initDefaultConfiguration(final Bundle defaults) {
}
@Override
publicfinal String getScheme() {
return SCHEME;
}
@Override
publicfinalvoid onConnected() {
}
@Override
publicfinalvoid register(final String id, final String valuePath,
final Bundle configuration) {
if (registeredConfigurations.size() == 1) {
PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
ContentValues values = new ContentValues();
values.put(IS_SCREEN_ON_FIELD, "" + pm.isScreenOn());
putValues(values, System.currentTimeMillis());
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
registerReceiver(screenReceiver, filter);
}
}
@Override
publicfinalvoid unregister(final String id) {
if (registeredConfigurations.size() == 0) {
unregisterReceiver(screenReceiver);
}
}
@Override
publicfinalvoid onDestroySensor() {
unregisterReceiver(screenReceiver);
}
}