@@ -31,14 +31,14 @@ public class Hardware {
3131 /**
3232 * Vibrate for s seconds.
3333 */
34- static void vibrate (double s ) {
34+ public static void vibrate (double s ) {
3535 Vibrator v = (Vibrator ) context .getSystemService (Context .VIBRATOR_SERVICE );
3636 if (v != null ) {
3737 v .vibrate ((int ) (1000 * s ));
3838 }
3939 }
4040
41- static SensorEvent lastEvent = null ;
41+ public static SensorEvent lastEvent = null ;
4242
4343 static class AccelListener implements SensorEventListener {
4444 public void onSensorChanged (SensorEvent ev ) {
@@ -55,7 +55,7 @@ public void onAccuracyChanged(Sensor sensor , int accuracy) {
5555 /**
5656 * Enable or Disable the accelerometer.
5757 */
58- static void accelerometerEnable (boolean enable ) {
58+ public static void accelerometerEnable (boolean enable ) {
5959 SensorManager sm = (SensorManager ) context .getSystemService (Context .SENSOR_SERVICE );
6060 Sensor accel = sm .getDefaultSensor (Sensor .TYPE_ACCELEROMETER );
6161
@@ -72,7 +72,7 @@ static void accelerometerEnable(boolean enable) {
7272 }
7373
7474
75- static float [] accelerometerReading () {
75+ public static float [] accelerometerReading () {
7676 if (lastEvent != null ) {
7777 return lastEvent .values ;
7878 } else {
@@ -81,27 +81,109 @@ static float[] accelerometerReading() {
8181 }
8282 }
8383
84+ static SensorEvent lastOrientationEvent = null ;
85+
86+ static class OrientationListener implements SensorEventListener {
87+ public void onSensorChanged (SensorEvent ev ) {
88+ lastOrientationEvent = ev ;
89+ }
90+
91+ public void onAccuracyChanged (Sensor sensor , int accuracy ) {
92+ }
93+ }
94+
95+ static OrientationListener orientationListener = new OrientationListener ();
96+
97+ /**
98+ * Enable or Disable the OrientationSensor.
99+ */
100+ public static void orientationSensorEnable (boolean enable ) {
101+ SensorManager sm = (SensorManager ) context .getSystemService (Context .SENSOR_SERVICE );
102+ Sensor orientationSensor = sm .getDefaultSensor (Sensor .TYPE_ORIENTATION );
103+
104+ if (orientationSensor == null ) {
105+ return ;
106+ }
107+
108+ if (enable ) {
109+ sm .registerListener (orientationListener , orientationSensor , SensorManager .SENSOR_DELAY_NORMAL );
110+ } else {
111+ sm .unregisterListener (orientationListener , orientationSensor );
112+ }
113+ }
114+
115+ public static float [] orientationSensorReading () {
116+ if (lastOrientationEvent != null ) {
117+ return lastOrientationEvent .values ;
118+ } else {
119+ float rv [] = { 0f , 0f , 0f };
120+ return rv ;
121+ }
122+ }
123+
124+ static SensorEvent lastMagneticFieldEvent = null ;
125+
126+ static class MagneticFieldListener implements SensorEventListener {
127+ public void onSensorChanged (SensorEvent ev ) {
128+ lastMagneticFieldEvent = ev ;
129+ }
130+
131+ public void onAccuracyChanged (Sensor sensor , int accuracy ) {
132+ }
133+ }
134+
135+ static MagneticFieldListener magneticFieldListener = new MagneticFieldListener ();
136+
137+ /**
138+ * Enable or Disable the MagneticFieldSensor.
139+ */
140+ public static void magneticFieldSensorEnable (boolean enable ) {
141+ SensorManager sm = (SensorManager ) context .getSystemService (Context .SENSOR_SERVICE );
142+ Sensor magneticFieldSensor = sm .getDefaultSensor (Sensor .TYPE_MAGNETIC_FIELD );
143+
144+ if (magneticFieldSensor == null ) {
145+ return ;
146+ }
147+
148+ if (enable ) {
149+ sm .registerListener (magneticFieldListener , magneticFieldSensor , SensorManager .SENSOR_DELAY_NORMAL );
150+ } else {
151+ sm .unregisterListener (magneticFieldListener , magneticFieldSensor );
152+ }
153+ }
154+
155+
156+ public static float [] magneticFieldSensorReading () {
157+ if (lastMagneticFieldEvent != null ) {
158+ return lastMagneticFieldEvent .values ;
159+ } else {
160+ float rv [] = { 0f , 0f , 0f };
161+ return rv ;
162+ }
163+ }
164+
165+
84166 static DisplayMetrics metrics = new DisplayMetrics ();
85167
86168 /**
87169 * Get display DPI.
88170 */
89- static int getDPI () {
171+ public static int getDPI () {
90172 return metrics .densityDpi ;
91173 }
92174
93175 /**
94176 * Show the soft keyboard.
95177 */
96- static void showKeyboard () {
178+ public static void showKeyboard () {
97179 InputMethodManager imm = (InputMethodManager ) context .getSystemService (Context .INPUT_METHOD_SERVICE );
98180 imm .showSoftInput (view , InputMethodManager .SHOW_FORCED );
99181 }
100182
101183 /**
102184 * Hide the soft keyboard.
103185 */
104- static void hideKeyboard () {
186+ public static void hideKeyboard () {
105187 InputMethodManager imm = (InputMethodManager ) context .getSystemService (Context .INPUT_METHOD_SERVICE );
106188 imm .hideSoftInputFromWindow (view .getWindowToken (), 0 );
107189 }
@@ -111,7 +193,7 @@ static void hideKeyboard() {
111193 */
112194 static List <ScanResult > latestResult ;
113195
114- static void enableWifiScanner ()
196+ public static void enableWifiScanner ()
115197 {
116198 IntentFilter i = new IntentFilter ();
117199 i .addAction (WifiManager .SCAN_RESULTS_AVAILABLE_ACTION );
@@ -129,7 +211,7 @@ public void onReceive(Context c, Intent i) {
129211
130212 }
131213
132- static String scanWifi () {
214+ public static String scanWifi () {
133215
134216 // Now you can call this and it should execute the broadcastReceiver's
135217 // onReceive()
0 commit comments