1 /*
   2  * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package sensorstest;
  26 
  27 import javafx.event.EventHandler;
  28 import javafx.scene.Group;
  29 import javafx.scene.input.MouseEvent;
  30 import javafx.scene.paint.Color;
  31 import javafx.scene.text.Font;
  32 import javafx.scene.text.Text;
  33 
  34 public class SensorsPanel extends Group {
  35 
  36     static Text text(final int x, final int y) {
  37         final Text text = new Text();
  38         text.setFont(Font.font("Courier", 16));
  39         text.setFill(Color.WHITE);
  40         text.setTranslateX(x);
  41         text.setTranslateY(y);
  42         return text;
  43     }
  44 
  45     static Text label(final int x, final int y, final String text) {
  46         final Text label = text(x, y);
  47         label.setText(text);
  48         return label;
  49     }
  50 
  51     public SensorsPanel() {
  52         final int DY = 0x0E;
  53         final int LX = 0x00;
  54         final int VX = 0xC0;
  55         int y = 0x18;
  56 
  57         final Text textLocation1       = label(LX, y += DY, "Location");
  58         final Text textLocation2       = label(LX, y += DY, "=================================");
  59         final Text textLatitude        = label(LX, y += DY, "Latitude");
  60         final Text textLatitudeValue   = text (VX, y);
  61         final Text textLongitude       = label(LX, y += DY, "Longitude");
  62         final Text textLongitudeValue  = text (VX, y);
  63         final Text textAltitude        = label(LX, y += DY, "Altitude");
  64         final Text textAltitudeValue   = text (VX, y);
  65         final Text textCourse          = label(LX, y += DY, "Course");
  66         final Text textCourseValue     = text (VX, y);
  67         final Text textSpeed           = label(LX, y += DY, "Speed");
  68         final Text textSpeedValue      = text (VX, y);
  69         y += DY;
  70 
  71         final Text textAccelerometer1  = label(LX, y += DY, "Accelerometer");
  72         final Text textAccelerometerA  = text (VX, y);
  73         final Text textAccelerometer2  = label(LX, y += DY, "=================================");
  74         final Text textAccX            = label(LX, y += DY, "Acceleration X");
  75         final Text textAccXValue       = text (VX, y);
  76         final Text textAccY            = label(LX, y += DY, "Acceleration Y");
  77         final Text textAccYValue       = text (VX, y);
  78         final Text textAccZ            = label(LX, y += DY, "Acceleration Z");
  79         final Text textAccZValue       = text (VX, y);
  80         y += DY;
  81 
  82         final Text textGyroscope1      = label(LX, y += DY, "Gyroscope");
  83         final Text textGyroscopeA      = text (VX, y);
  84         final Text textGyroscope2      = label(LX, y += DY, "=================================");
  85         final Text textRotX            = label(LX, y += DY, "Rotation X");
  86         final Text textRotXValue       = text (VX, y);
  87         final Text textRotY            = label(LX, y += DY, "Rotation Y");
  88         final Text textRotYValue       = text (VX, y);
  89         final Text textRotZ            = label(LX, y += DY, "Rotation Z");
  90         final Text textRotZValue       = text (VX, y);
  91         y += DY;
  92 
  93         final Text textMagnetometer1   = label(LX, y += DY, "Magnetometer");
  94         final Text textMagnetometerA   = text (VX, y);
  95         final Text textMagnetometer2   = label(LX, y += DY, "=================================");
  96         final Text textMHeading        = label(LX, y += DY, "Magnetic heading");
  97         final Text textMHeadingValue   = text (VX, y);
  98         final Text textTHeading        = label(LX, y += DY, "True heading");
  99         final Text textTHeadingValue   = text (VX, y);
 100         final Text textFieldX          = label(LX, y += DY, "Field X");
 101         final Text textFieldXValue     = text (VX, y);
 102         final Text textFieldY          = label(LX, y += DY, "Field Y");
 103         final Text textFieldYValue     = text (VX, y);
 104         final Text textFieldZ          = label(LX, y += DY, "Field Z");
 105         final Text textFieldZValue     = text (VX, y);
 106         y += DY;
 107 
 108         final Text textBattery1        = label(LX, y += DY, "Battery");
 109         final Text textBattery2        = label(LX, y += DY, "=================================");
 110         final Text textBatLevel        = label(LX, y += DY, "Level");
 111         final Text textBatLevelValue   = text (VX, y);
 112         final Text textBatState        = label(LX, y += DY, "State");
 113         final Text textBatStateValue   = text (VX, y);
 114 
 115         getChildren().addAll(
 116                 textLocation1,
 117                 textLocation2,
 118                 textLatitude,  textLatitudeValue,
 119                 textLongitude, textLongitudeValue,
 120                 textAltitude,  textAltitudeValue,
 121                 textCourse,    textCourseValue,
 122                 textSpeed,     textSpeedValue,
 123 
 124                 textAccelerometer1, textAccelerometerA,
 125                 textAccelerometer2,
 126                 textAccX, textAccXValue,
 127                 textAccY, textAccYValue,
 128                 textAccZ, textAccZValue,
 129 
 130                 textGyroscope1, textGyroscopeA,
 131                 textGyroscope2,
 132                 textRotX, textRotXValue,
 133                 textRotY, textRotYValue,
 134                 textRotZ, textRotZValue,
 135 
 136                 textMagnetometer1, textMagnetometerA,
 137                 textMagnetometer2,
 138                 textMHeading,   textMHeadingValue,
 139                 textTHeading,   textTHeadingValue,
 140                 textFieldX,     textFieldXValue,
 141                 textFieldY,     textFieldYValue,
 142                 textFieldZ,     textFieldZValue,
 143 
 144                 textBattery1,
 145                 textBattery2,
 146                 textBatLevel,
 147                 textBatLevelValue,
 148                 textBatState,
 149                 textBatStateValue
 150         );
 151 
 152         // SENSORS STUFF
 153         // --------------------------------------------------------------------------------
 154         com.sun.javafx.ext.device.ios.sensors.IOSLocationManager.addLocationListener(
 155                 new com.sun.javafx.ext.device.ios.sensors.IOSLocationManager.LocationListener() {
 156                     @Override
 157                     public void locationUpdated(double latitude,
 158                                                 double longitude,
 159                                                 double altitude,
 160                                                 double course,
 161                                                 double speed) {
 162                         textLatitudeValue.setText(String.valueOf(latitude));
 163                         textLongitudeValue.setText(String.valueOf(longitude));
 164                         textAltitudeValue.setText(String.valueOf(altitude));
 165                         textCourseValue.setText(String.valueOf(course));
 166                         textSpeedValue.setText(String.valueOf(speed));
 167                     }
 168         });
 169 
 170         textAccelerometerA.setText(com.sun.javafx.ext.device.ios.sensors.IOSMotionManager.isAccelerometerAvailable() ? "AVAILABLE" : "NOT AVAILABLE");
 171         com.sun.javafx.ext.device.ios.sensors.IOSMotionManager.addAccelerationListener(new com.sun.javafx.ext.device.ios.sensors.IOSMotionManager.Listener() {
 172             public void handleMotion(float x, float y, float z) {
 173                 textAccXValue.setText(String.valueOf(x));
 174                 textAccYValue.setText(String.valueOf(y));
 175                 textAccZValue.setText(String.valueOf(z));
 176             }
 177         });
 178 
 179         textGyroscopeA.setText(com.sun.javafx.ext.device.ios.sensors.IOSMotionManager.isGyroAvailable() ? "AVAILABLE" : "NOT AVAILABLE");
 180         com.sun.javafx.ext.device.ios.sensors.IOSMotionManager.addRotationListener(new com.sun.javafx.ext.device.ios.sensors.IOSMotionManager.Listener() {
 181             public void handleMotion(float x, float y, float z) {
 182                 textRotXValue.setText(String.valueOf(x));
 183                 textRotYValue.setText(String.valueOf(y));
 184                 textRotZValue.setText(String.valueOf(z));
 185             }
 186         });
 187 
 188         textMagnetometerA.setText(com.sun.javafx.ext.device.ios.sensors.IOSLocationManager.isHeadingAvailable() ? "AVAILABLE" : "NOT AVAILABLE");
 189         com.sun.javafx.ext.device.ios.sensors.IOSLocationManager.addHeadingListener(new com.sun.javafx.ext.device.ios.sensors.IOSLocationManager.HeadingListener() {
 190             public void headingUpdated(double m, double t, double x, double y, double z) {
 191                 textMHeadingValue.setText(String.valueOf(m));
 192                 textTHeadingValue.setText(String.valueOf(t));
 193                 textFieldXValue.setText(String.valueOf(x));
 194                 textFieldYValue.setText(String.valueOf(y));
 195                 textFieldZValue.setText(String.valueOf(z));
 196             }
 197         });
 198 
 199         com.sun.javafx.ext.device.ios.sensors.IOSDevice.getCurrentDevice().setProximityMonitoringEnabled(true);
 200         setOnMousePressed(new EventHandler<MouseEvent>() {
 201             public void handle(MouseEvent me) {
 202                 System.out.println("PROXIMITY: " + com.sun.javafx.ext.device.ios.sensors.IOSDevice.getCurrentDevice().getProximityState());
 203             }
 204         });
 205 
 206         com.sun.javafx.ext.device.ios.sensors.IOSDevice.getCurrentDevice().setBatteryMonitoringEnabled(true);
 207         textBatLevelValue.setText(String.valueOf(com.sun.javafx.ext.device.ios.sensors.IOSDevice.getCurrentDevice().getBatteryLevel()));
 208         textBatStateValue.setText(String.valueOf(com.sun.javafx.ext.device.ios.sensors.IOSDevice.getCurrentDevice().getBatteryState()));
 209     }
 210 }