|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+/*
|
|
|
2
|
+ * Copyright @ 2017-present Atlassian Pty Ltd
|
|
|
3
|
+ *
|
|
|
4
|
+ * Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
5
|
+ * you may not use this file except in compliance with the License.
|
|
|
6
|
+ * You may obtain a copy of the License at
|
|
|
7
|
+ *
|
|
|
8
|
+ * http://www.apache.org/licenses/LICENSE-2.0
|
|
|
9
|
+ *
|
|
|
10
|
+ * Unless required by applicable law or agreed to in writing, software
|
|
|
11
|
+ * distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
12
|
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
13
|
+ * See the License for the specific language governing permissions and
|
|
|
14
|
+ * limitations under the License.
|
|
|
15
|
+ */
|
|
|
16
|
+
|
|
|
17
|
+package org.jitsi.meet.sdk;
|
|
|
18
|
+
|
|
|
19
|
+import android.content.Context;
|
|
|
20
|
+import android.net.wifi.WifiInfo;
|
|
|
21
|
+import android.net.wifi.WifiManager;
|
|
|
22
|
+import android.os.Handler;
|
|
|
23
|
+import android.os.Looper;
|
|
|
24
|
+import android.util.Log;
|
|
|
25
|
+
|
|
|
26
|
+import com.facebook.react.bridge.Promise;
|
|
|
27
|
+import com.facebook.react.bridge.ReactApplicationContext;
|
|
|
28
|
+import com.facebook.react.bridge.ReactContextBaseJavaModule;
|
|
|
29
|
+import com.facebook.react.bridge.ReactMethod;
|
|
|
30
|
+
|
|
|
31
|
+import org.json.JSONArray;
|
|
|
32
|
+import org.json.JSONObject;
|
|
|
33
|
+
|
|
|
34
|
+import java.net.InetAddress;
|
|
|
35
|
+import java.net.NetworkInterface;
|
|
|
36
|
+import java.net.SocketException;
|
|
|
37
|
+import java.net.UnknownHostException;
|
|
|
38
|
+import java.util.Enumeration;
|
|
|
39
|
+
|
|
|
40
|
+/**
|
|
|
41
|
+ * Module exposing WiFi statistics.
|
|
|
42
|
+ *
|
|
|
43
|
+ * Gathers rssi, signal in percentage, timestamp and the addresses
|
|
|
44
|
+ * of the wifi device.
|
|
|
45
|
+ */
|
|
|
46
|
+class WiFiStatsModule extends ReactContextBaseJavaModule {
|
|
|
47
|
+ /**
|
|
|
48
|
+ * The name of {@code WiFiStatsModule} to be used in the React Native
|
|
|
49
|
+ * bridge.
|
|
|
50
|
+ */
|
|
|
51
|
+ private static final String MODULE_NAME = "WiFiStats";
|
|
|
52
|
+
|
|
|
53
|
+ /**
|
|
|
54
|
+ * The {@code Log} tag {@code WiFiStatsModule} is to log messages with.
|
|
|
55
|
+ */
|
|
|
56
|
+ static final String TAG = MODULE_NAME;
|
|
|
57
|
+
|
|
|
58
|
+ /**
|
|
|
59
|
+ * The scale used for the signal value.
|
|
|
60
|
+ * A level of the signal, given in the range
|
|
|
61
|
+ * of 0 to SIGNAL_LEVEL_SCALE-1 (both inclusive).
|
|
|
62
|
+ */
|
|
|
63
|
+ public final static int SIGNAL_LEVEL_SCALE = 101;
|
|
|
64
|
+
|
|
|
65
|
+ /**
|
|
|
66
|
+ * {@link Handler} for running all operations on the main thread.
|
|
|
67
|
+ */
|
|
|
68
|
+ private final Handler mainThreadHandler
|
|
|
69
|
+ = new Handler(Looper.getMainLooper());
|
|
|
70
|
+
|
|
|
71
|
+ /**
|
|
|
72
|
+ * Initializes a new module instance. There shall be a single instance of
|
|
|
73
|
+ * this module throughout the lifetime of the application.
|
|
|
74
|
+ *
|
|
|
75
|
+ * @param reactContext the {@link ReactApplicationContext} where this module
|
|
|
76
|
+ * is created.
|
|
|
77
|
+ */
|
|
|
78
|
+ public WiFiStatsModule(ReactApplicationContext reactContext) {
|
|
|
79
|
+ super(reactContext);
|
|
|
80
|
+ }
|
|
|
81
|
+
|
|
|
82
|
+ /**
|
|
|
83
|
+ * Gets the name for this module to be used in the React Native bridge.
|
|
|
84
|
+ *
|
|
|
85
|
+ * @return a string with the module name.
|
|
|
86
|
+ */
|
|
|
87
|
+ @Override
|
|
|
88
|
+ public String getName() {
|
|
|
89
|
+ return MODULE_NAME;
|
|
|
90
|
+ }
|
|
|
91
|
+
|
|
|
92
|
+ /**
|
|
|
93
|
+ * Returns the {@link InetAddress} represented by this int.
|
|
|
94
|
+ *
|
|
|
95
|
+ * @param value the int representation of the ip address.
|
|
|
96
|
+ * @return the {@link InetAddress}.
|
|
|
97
|
+ * @throws UnknownHostException - if IP address is of illegal length.
|
|
|
98
|
+ */
|
|
|
99
|
+ public static InetAddress toInetAddress(int value)
|
|
|
100
|
+ throws UnknownHostException {
|
|
|
101
|
+ return InetAddress.getByAddress(
|
|
|
102
|
+ new byte[] {
|
|
|
103
|
+ (byte) value,
|
|
|
104
|
+ (byte) (value >> 8),
|
|
|
105
|
+ (byte) (value >> 16),
|
|
|
106
|
+ (byte) (value >> 24)
|
|
|
107
|
+ });
|
|
|
108
|
+ }
|
|
|
109
|
+
|
|
|
110
|
+ /**
|
|
|
111
|
+ * Public method to retrieve WiFi stats.
|
|
|
112
|
+ *
|
|
|
113
|
+ * @param promise a {@link Promise} which will be resolved if WiFi stats are
|
|
|
114
|
+ * retrieved successfully, and it will be rejected otherwise.
|
|
|
115
|
+ */
|
|
|
116
|
+ @ReactMethod
|
|
|
117
|
+ public void getWiFiStats(final Promise promise) {
|
|
|
118
|
+ Runnable r = new Runnable() {
|
|
|
119
|
+ @Override
|
|
|
120
|
+ public void run() {
|
|
|
121
|
+ try {
|
|
|
122
|
+
|
|
|
123
|
+ Context context
|
|
|
124
|
+ = getReactApplicationContext().getApplicationContext();
|
|
|
125
|
+ WifiManager wifiManager
|
|
|
126
|
+ = (WifiManager) context
|
|
|
127
|
+ .getSystemService(Context.WIFI_SERVICE);
|
|
|
128
|
+
|
|
|
129
|
+ if (!wifiManager.isWifiEnabled()) {
|
|
|
130
|
+ promise.reject(new Exception("Wifi not enabled"));
|
|
|
131
|
+ return;
|
|
|
132
|
+ }
|
|
|
133
|
+
|
|
|
134
|
+ WifiInfo wifiInfo = wifiManager.getConnectionInfo();
|
|
|
135
|
+
|
|
|
136
|
+ if (wifiInfo.getNetworkId() == -1) {
|
|
|
137
|
+ promise.reject(new Exception("Wifi not connected"));
|
|
|
138
|
+ return;
|
|
|
139
|
+ }
|
|
|
140
|
+
|
|
|
141
|
+ int rssi = wifiInfo.getRssi();
|
|
|
142
|
+ int signalLevel
|
|
|
143
|
+ = WifiManager.calculateSignalLevel(
|
|
|
144
|
+ rssi, SIGNAL_LEVEL_SCALE);
|
|
|
145
|
+
|
|
|
146
|
+ JSONObject result = new JSONObject();
|
|
|
147
|
+ result.put("rssi", rssi)
|
|
|
148
|
+ .put("signal", signalLevel)
|
|
|
149
|
+ .put("timestamp",
|
|
|
150
|
+ String.valueOf(System.currentTimeMillis()));
|
|
|
151
|
+
|
|
|
152
|
+ JSONArray addresses = new JSONArray();
|
|
|
153
|
+
|
|
|
154
|
+ InetAddress wifiAddress
|
|
|
155
|
+ = toInetAddress(wifiInfo.getIpAddress());
|
|
|
156
|
+
|
|
|
157
|
+ try {
|
|
|
158
|
+ Enumeration<NetworkInterface> e
|
|
|
159
|
+ = NetworkInterface.getNetworkInterfaces();
|
|
|
160
|
+ while (e.hasMoreElements()) {
|
|
|
161
|
+ NetworkInterface networkInterface = e.nextElement();
|
|
|
162
|
+ boolean found = false;
|
|
|
163
|
+
|
|
|
164
|
+ // first check whether this is the desired interface
|
|
|
165
|
+ Enumeration<InetAddress> as
|
|
|
166
|
+ = networkInterface.getInetAddresses();
|
|
|
167
|
+ while (as.hasMoreElements()) {
|
|
|
168
|
+ InetAddress a = as.nextElement();
|
|
|
169
|
+ if(a.equals(wifiAddress)) {
|
|
|
170
|
+ found = true;
|
|
|
171
|
+ break;
|
|
|
172
|
+ }
|
|
|
173
|
+ }
|
|
|
174
|
+
|
|
|
175
|
+ if (found) {
|
|
|
176
|
+ // interface found let's put addresses
|
|
|
177
|
+ // to the result object
|
|
|
178
|
+ as = networkInterface.getInetAddresses();
|
|
|
179
|
+ while (as.hasMoreElements()) {
|
|
|
180
|
+ InetAddress a = as.nextElement();
|
|
|
181
|
+ if (a.isLinkLocalAddress())
|
|
|
182
|
+ continue;
|
|
|
183
|
+
|
|
|
184
|
+ addresses.put(a.getHostAddress());
|
|
|
185
|
+ }
|
|
|
186
|
+ }
|
|
|
187
|
+
|
|
|
188
|
+ }
|
|
|
189
|
+ } catch (SocketException e) {
|
|
|
190
|
+ Log.wtf(TAG,
|
|
|
191
|
+ "Unable to NetworkInterface.getNetworkInterfaces()"
|
|
|
192
|
+ );
|
|
|
193
|
+ }
|
|
|
194
|
+
|
|
|
195
|
+ result.put("addresses", addresses);
|
|
|
196
|
+ promise.resolve(result.toString());
|
|
|
197
|
+
|
|
|
198
|
+ Log.d(TAG, "WiFi stats: " + result.toString());
|
|
|
199
|
+ } catch (Throwable e) {
|
|
|
200
|
+ Log.e(TAG, "Failed to obtain wifi stats", e);
|
|
|
201
|
+ promise.reject(
|
|
|
202
|
+ new Exception("Failed to obtain wifi stats"));
|
|
|
203
|
+ }
|
|
|
204
|
+ }
|
|
|
205
|
+ };
|
|
|
206
|
+ mainThreadHandler.post(r);
|
|
|
207
|
+ }
|
|
|
208
|
+}
|