forked from kivy/python-for-android
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHardware.java
More file actions
235 lines (184 loc) · 6.71 KB
/
Hardware.java
File metadata and controls
235 lines (184 loc) · 6.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package org.renpy.android;
import android.content.Context;
import android.os.Vibrator;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.util.DisplayMetrics;
import android.view.inputmethod.InputMethodManager;
import android.view.View;
import java.util.List;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
/**
* Methods that are expected to be called via JNI, to access the
* device's non-screen hardware. (For example, the vibration and
* accelerometer.)
*/
public class Hardware {
// The context.
static Context context;
static View view;
/**
* Vibrate for s seconds.
*/
public static void vibrate(double s) {
Vibrator v = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
if (v != null) {
v.vibrate((int) (1000 * s));
}
}
public static SensorEvent lastEvent = null;
static class AccelListener implements SensorEventListener {
public void onSensorChanged(SensorEvent ev) {
lastEvent = ev;
}
public void onAccuracyChanged(Sensor sensor , int accuracy) {
}
}
static AccelListener accelListener = new AccelListener();
/**
* Enable or Disable the accelerometer.
*/
public static void accelerometerEnable(boolean enable) {
SensorManager sm = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
Sensor accel = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
if (accel == null) {
return;
}
if (enable) {
sm.registerListener(accelListener, accel, SensorManager.SENSOR_DELAY_GAME);
} else {
sm.unregisterListener(accelListener, accel);
}
}
public static float[] accelerometerReading() {
if (lastEvent != null) {
return lastEvent.values;
} else {
float rv[] = { 0f, 0f, 0f };
return rv;
}
}
static SensorEvent lastOrientationEvent = null;
static class OrientationListener implements SensorEventListener {
public void onSensorChanged(SensorEvent ev) {
lastOrientationEvent = ev;
}
public void onAccuracyChanged(Sensor sensor , int accuracy) {
}
}
static OrientationListener orientationListener = new OrientationListener();
/**
* Enable or Disable the OrientationSensor.
*/
public static void orientationSensorEnable(boolean enable) {
SensorManager sm = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
Sensor orientationSensor = sm.getDefaultSensor(Sensor.TYPE_ORIENTATION);
if (orientationSensor == null) {
return;
}
if (enable) {
sm.registerListener(orientationListener, orientationSensor, SensorManager.SENSOR_DELAY_NORMAL);
} else {
sm.unregisterListener(orientationListener, orientationSensor);
}
}
public static float[] orientationSensorReading() {
if (lastOrientationEvent != null) {
return lastOrientationEvent.values;
} else {
float rv[] = { 0f, 0f, 0f };
return rv;
}
}
static SensorEvent lastMagneticFieldEvent = null;
static class MagneticFieldListener implements SensorEventListener {
public void onSensorChanged(SensorEvent ev) {
lastMagneticFieldEvent = ev;
}
public void onAccuracyChanged(Sensor sensor , int accuracy) {
}
}
static MagneticFieldListener magneticFieldListener = new MagneticFieldListener();
/**
* Enable or Disable the MagneticFieldSensor.
*/
public static void magneticFieldSensorEnable(boolean enable) {
SensorManager sm = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
Sensor magneticFieldSensor = sm.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
if (magneticFieldSensor == null) {
return;
}
if (enable) {
sm.registerListener(magneticFieldListener, magneticFieldSensor, SensorManager.SENSOR_DELAY_NORMAL);
} else {
sm.unregisterListener(magneticFieldListener, magneticFieldSensor);
}
}
public static float[] magneticFieldSensorReading() {
if (lastMagneticFieldEvent != null) {
return lastMagneticFieldEvent.values;
} else {
float rv[] = { 0f, 0f, 0f };
return rv;
}
}
static DisplayMetrics metrics = new DisplayMetrics();
/**
* Get display DPI.
*/
public static int getDPI() {
return metrics.densityDpi;
}
/**
* Show the soft keyboard.
*/
public static void showKeyboard() {
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(view, InputMethodManager.SHOW_FORCED);
}
/**
* Hide the soft keyboard.
*/
public static void hideKeyboard() {
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
/**
* Scan WiFi networks
*/
static List<ScanResult> latestResult;
public static void enableWifiScanner()
{
IntentFilter i = new IntentFilter();
i.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
context.registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context c, Intent i) {
// Code to execute when SCAN_RESULTS_AVAILABLE_ACTION event occurs
WifiManager w = (WifiManager) c.getSystemService(Context.WIFI_SERVICE);
latestResult = w.getScanResults(); // Returns a <list> of scanResults
}
}, i);
}
public static String scanWifi() {
// Now you can call this and it should execute the broadcastReceiver's
// onReceive()
WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
boolean a = wm.startScan();
if (latestResult != null){
String latestResultString = "";
for (ScanResult result : latestResult)
{
latestResultString += String.format("%s\t%s\t%d\n", result.SSID, result.BSSID, result.level);
}
return latestResultString;
}
return "";
}
}