AndroidSensorMessageService.java
Go to the documentation of this file.
00001 package org.ros.android.androidSensorMessage;
00002 
00003 import java.net.URI;
00004 import java.net.URISyntaxException;
00005 import java.util.List;
00006 
00007 import org.ros.address.InetAddressFactory;
00008 import org.ros.node.DefaultNodeMainExecutor;
00009 import org.ros.node.NodeConfiguration;
00010 import org.ros.node.NodeMain;
00011 import org.ros.node.NodeMainExecutor;
00012 
00013 import android.app.IntentService;
00014 import android.app.Notification;
00015 import android.app.PendingIntent;
00016 import android.content.Context;
00017 import android.content.Intent;
00018 import android.hardware.Sensor;
00019 import android.hardware.SensorManager;
00020 import android.location.LocationManager;
00021 import android.os.Bundle;
00022 import android.os.Binder;
00023 import android.os.IBinder;
00024 import android.util.Log;
00025 import android.widget.TextView;
00026 
00027 public class AndroidSensorMessageService extends IntentService {
00028 
00029         private SensorManager sensorManager;
00030         private List<Sensor> sensors;
00031         private AndroidSensorMessageNode androidSensorMessageNode;
00032         private Intent mintent;
00033         NodeMainExecutor nodeMainExecutor;
00034         private int[] availableSensor;
00035         private int[] publishedSensor;
00036         private String[] sensorName;
00037 
00038         final IBinder binder = new MyBinder();
00039 
00040         public class MyBinder extends Binder {
00041                 AndroidSensorMessageService getService() {
00042                         return AndroidSensorMessageService.this;
00043                 }
00044         }
00045 
00046         public AndroidSensorMessageService() {
00047                 super("AndroidSensorMessageService");
00048         }
00049 
00050         @Override
00051         public IBinder onBind(Intent intent) {
00052                 mintent = intent;
00053                 sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
00054                 sensors = sensorManager.getSensorList(Sensor.TYPE_ALL);
00055 
00056                 availableSensor = new int[13];
00057                 publishedSensor = new int[13];
00058                 for (int i = 0; i < 13; i++) {
00059                         availableSensor[i] = -1;
00060                         publishedSensor[i] = -1;
00061                 }
00062 
00063                 sensorName = new String[13];
00064 
00065                 for (Sensor sensor : sensors) {
00066                         int sensorType = sensor.getType();
00067                         switch (sensorType) {
00068                         /*
00069                          * case Sensor.TYPE_AMBIENT_TEMPERATURE: sensorName[sensorType - 1]
00070                          * = new String("Temperature"); availableSensor[sensorType - 1] = 1;
00071                          * break;
00072                          */
00073                         case Sensor.TYPE_ACCELEROMETER:
00074                                 sensorName[sensorType - 1] = new String("Accelerometer");
00075                                 availableSensor[sensorType - 1] = 1;
00076                                 break;
00077                         case Sensor.TYPE_TEMPERATURE:
00078                                 sensorName[sensorType - 1] = new String("Temperature");
00079                                 availableSensor[sensorType - 1] = 1;
00080                                 break;
00081                         case Sensor.TYPE_MAGNETIC_FIELD:
00082                                 sensorName[sensorType - 1] = new String("MagneticField");
00083                                 availableSensor[sensorType - 1] = 1;
00084                                 break;
00085                         case Sensor.TYPE_LIGHT:
00086                                 sensorName[sensorType - 1] = new String("Light");
00087                                 availableSensor[sensorType - 1] = 1;
00088                                 break;
00089                         case Sensor.TYPE_PROXIMITY:
00090                                 sensorName[sensorType - 1] = new String("Proximity");
00091                                 availableSensor[sensorType - 1] = 1;
00092                                 break;
00093                         case Sensor.TYPE_GYROSCOPE:
00094                                 sensorName[sensorType - 1] = new String("Gyroscope");
00095                                 availableSensor[sensorType - 1] = 1;
00096                                 break;
00097                         case Sensor.TYPE_PRESSURE:
00098                                 sensorName[sensorType - 1] = new String("Pressure");
00099                                 availableSensor[sensorType - 1] = 1;
00100                                 break;
00101                         case Sensor.TYPE_GRAVITY:
00102                                 sensorName[sensorType - 1] = new String("Gravity");
00103                                 availableSensor[sensorType - 1] = 1;
00104                                 break;
00105                         case Sensor.TYPE_ROTATION_VECTOR:
00106                                 sensorName[sensorType - 1] = new String("RotationVector");
00107                                 availableSensor[sensorType - 1] = 1;
00108                                 break;
00109                         /*
00110                          * case Sensor.TYPE_RELATIVE_HUMIDITY: sensorName[sensorType - 1] =
00111                          * new String("Humidity"); availableSensor[sensorType - 1] = 1;
00112                          * break;
00113                          */
00114 
00115                         }
00116 
00117                 }
00118 
00119                 return binder;
00120         }
00121 
00122         public String getSensorName(int i) {
00123                 return sensorName[i];
00124         }
00125 
00126         public int getSensor(int i) {
00127                 return availableSensor[i];
00128         }
00129 
00130         public void changeSensor(int num) {
00131                 publishedSensor[num] *= -1;
00132                 androidSensorMessageNode.changeSensor(num);
00133 
00134         }
00135 
00136         public void changeView(int num) {
00137                 androidSensorMessageNode.changeView(num);
00138         }
00139 
00140         public void setNode(int[] flag, AndroidSensorMessageView view,
00141                         TextView textViewX, TextView textViewY, TextView textViewZ) {
00142                 showNotification();
00143 
00144                 for (int i = 0; i < 13; i++) {
00145                         publishedSensor[i] = flag[i];
00146                 }
00147 
00148                 // We use this bundle
00149                 Bundle b = mintent.getExtras();
00150 
00151                 if (this.androidSensorMessageNode == null) {
00152                         try {
00153                                 URI masterUri = new URI(b.getString("masterUri"));
00154                                 String hostLocal = InetAddressFactory.newNonLoopback()
00155                                                 .getHostAddress();
00156                                 nodeMainExecutor = DefaultNodeMainExecutor.newDefault();
00157 
00158                                 NodeConfiguration nodeConfiguration = NodeConfiguration
00159                                                 .newPublic(hostLocal, masterUri);
00160                                 nodeConfiguration.setMasterUri(masterUri);
00161                                 nodeConfiguration.setNodeName("android_sensor_message");
00162 
00163                                 androidSensorMessageNode = new AndroidSensorMessageNode(
00164                                                 sensorManager, sensors, view, textViewX, textViewY,
00165                                                 textViewZ, publishedSensor);
00166 
00167                                 nodeMainExecutor.execute(androidSensorMessageNode,
00168                                                 nodeConfiguration);
00169 
00170                         } catch (URISyntaxException e) {
00171                                 e.printStackTrace();
00172                         }
00173                 }
00174         }
00175 
00176         @Override
00177         protected void onHandleIntent(Intent intent) {
00178                 try {
00179                         Thread.sleep(10000);
00180                 } catch (InterruptedException e) {
00181                         e.printStackTrace();
00182                 }
00183 
00184         }
00185 
00189         private void showNotification() {
00190 
00191                 Intent intent = new Intent(this, AndroidSensorMessage.class);
00192 
00193                 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
00194                                 | Intent.FLAG_ACTIVITY_SINGLE_TOP);
00195 
00196                 PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
00197                                 intent, 0);
00198 
00199         }
00200 
00201         @Override
00202         public void onDestroy() {
00203                 nodeMainExecutor.shutdownNodeMain(androidSensorMessageNode);
00204         }
00205 
00206 }


jsk_android_apps
Author(s): Kazuto Murase
autogenerated on Thu Jun 6 2019 18:03:46