Skip to content

Commit 1baf588

Browse files
committed
formatting
1 parent 56b6ade commit 1baf588

File tree

1 file changed

+78
-69
lines changed

1 file changed

+78
-69
lines changed

packages/wifi_iot/android/src/main/java/com/alternadom/wifiiot/WifiIotPlugin.java

Lines changed: 78 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import android.util.Log;
3131
import androidx.annotation.NonNull;
3232
import androidx.annotation.RequiresApi;
33-
import androidx.core.app.ActivityCompat;
3433
import info.whitebyte.hotspotmanager.ClientScanResult;
3534
import info.whitebyte.hotspotmanager.FinishScanListener;
3635
import info.whitebyte.hotspotmanager.WIFI_AP_STATE;
@@ -51,7 +50,7 @@
5150
import org.json.JSONObject;
5251

5352
public class WifiIotPlugin
54-
implements FlutterPlugin,
53+
implements FlutterPlugin,
5554
ActivityAware,
5655
MethodCallHandler,
5756
EventChannel.StreamHandler,
@@ -80,12 +79,11 @@ public class WifiIotPlugin
8079
private static final int PERMISSIONS_REQUEST_CODE_ACCESS_FINE_LOCATION_LOAD_WIFI_LIST = 65655435;
8180
private static final int PERMISSIONS_REQUEST_CODE_ACCESS_FINE_LOCATION_ON_LISTEN = 65655436;
8281
private static final int PERMISSIONS_REQUEST_CODE_ACCESS_FINE_LOCATION_FIND_AND_CONNECT =
83-
65655437;
82+
65655437;
8483
private static final int PERMISSIONS_REQUEST_CODE_ACCESS_NETWORK_STATE_IS_CONNECTED = 65655438;
8584

8685
private FlutterPlugin.FlutterPluginBinding flutterPluginBinding; // Add this
8786

88-
8987
// initialize members of this class with Context
9088
private void initWithContext(Context context) {
9189
moContext = context;
@@ -98,14 +96,12 @@ private void initWithActivity(Activity activity) {
9896
moActivity = activity;
9997
}
10098

101-
102-
10399
@Override
104100
public void onAttachedToEngine(@NonNull FlutterPluginBinding binding) {
105101
// initialize method and event channel and set handlers
106102
channel = new MethodChannel(binding.getBinaryMessenger(), "wifi_iot");
107103
eventChannel =
108-
new EventChannel(binding.getBinaryMessenger(), "plugins.wififlutter.io/wifi_scan");
104+
new EventChannel(binding.getBinaryMessenger(), "plugins.wififlutter.io/wifi_scan");
109105
channel.setMethodCallHandler(this);
110106
eventChannel.setStreamHandler(this);
111107
this.flutterPluginBinding = binding; // Store the binding
@@ -152,31 +148,28 @@ public void onDetachedFromActivity() {
152148
((ActivityPluginBinding) flutterPluginBinding).removeRequestPermissionsResultListener(this);
153149
}
154150
moActivity = null;
155-
156151
}
157152

158-
159-
160153
@Override
161154
public boolean onRequestPermissionsResult(
162-
int requestCode, String[] permissions, int[] grantResults) {
155+
int requestCode, String[] permissions, int[] grantResults) {
163156
final boolean wasPermissionGranted =
164-
grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED;
157+
grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED;
165158
switch (requestCode) {
166159
case PERMISSIONS_REQUEST_CODE_ACCESS_FINE_LOCATION_LOAD_WIFI_LIST:
167160
if (wasPermissionGranted) {
168161
_loadWifiList(permissionRequestResultCallback);
169162
} else {
170163
permissionRequestResultCallback.error(
171-
"WifiIotPlugin.Permission", "Fine location permission denied", null);
164+
"WifiIotPlugin.Permission", "Fine location permission denied", null);
172165
}
173166
requestingPermission = false;
174167
return true;
175168

176169
case PERMISSIONS_REQUEST_CODE_ACCESS_FINE_LOCATION_ON_LISTEN:
177170
if (wasPermissionGranted) {
178171
final EventChannel.EventSink eventSink =
179-
(EventChannel.EventSink) permissionRequestCookie.get(0);
172+
(EventChannel.EventSink) permissionRequestCookie.get(0);
180173
_onListen(eventSink);
181174
}
182175
requestingPermission = false;
@@ -188,7 +181,7 @@ public boolean onRequestPermissionsResult(
188181
_findAndConnect(poCall, permissionRequestResultCallback);
189182
} else {
190183
permissionRequestResultCallback.error(
191-
"WifiIotPlugin.Permission", "Fine location permission denied", null);
184+
"WifiIotPlugin.Permission", "Fine location permission denied", null);
192185
}
193186
requestingPermission = false;
194187
return true;
@@ -198,7 +191,7 @@ public boolean onRequestPermissionsResult(
198191
_isConnected(permissionRequestResultCallback);
199192
} else {
200193
permissionRequestResultCallback.error(
201-
"WifiIotPlugin.Permission", "Network state permission denied", null);
194+
"WifiIotPlugin.Permission", "Network state permission denied", null);
202195
}
203196
requestingPermission = false;
204197
return true;
@@ -209,11 +202,15 @@ public boolean onRequestPermissionsResult(
209202

210203
// cleanup
211204
private void cleanup() {
212-
// *** Permission Check ***
205+
// *** Permission Check ***
213206
// We still keep this check for ACCESS_FINE_LOCATION because getConfiguredNetworks() needs it pre-Q.
214-
// Ensure moContext is not null
215-
if (moContext != null && moContext.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
216-
Log.w(TAG, "ACCESS_FINE_LOCATION permission not granted during cleanup. Some networks may not be removed.");
207+
// Ensure moContext is not null
208+
if (moContext != null
209+
&& moContext.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION)
210+
!= PackageManager.PERMISSION_GRANTED) {
211+
Log.w(
212+
TAG,
213+
"ACCESS_FINE_LOCATION permission not granted during cleanup. Some networks may not be removed.");
217214
// We can't call poResult.error() here, but we log a warning.
218215
// We continue with the rest of the cleanup, as other parts don't require location.
219216
}
@@ -239,8 +236,6 @@ private void cleanup() {
239236
moWiFiAPManager = null;
240237
}
241238

242-
243-
244239
@Override
245240
public void onMethodCall(MethodCall poCall, Result poResult) {
246241
switch (poCall.method) {
@@ -294,9 +289,9 @@ public void onMethodCall(MethodCall poCall, Result poResult) {
294289
isRegisteredWifiNetwork(poCall, poResult);
295290
else
296291
poResult.error(
297-
"Error",
298-
"isRegisteredWifiNetwork not supported for Android SDK " + Build.VERSION.SDK_INT,
299-
null);
292+
"Error",
293+
"isRegisteredWifiNetwork not supported for Android SDK " + Build.VERSION.SDK_INT,
294+
null);
300295
break;
301296
case "isWiFiAPEnabled":
302297
isWiFiAPEnabled(poResult);
@@ -360,9 +355,9 @@ private void getWiFiAPSSID(Result poResult) {
360355
poResult.success(wifiConfiguration.SSID);
361356
} else {
362357
poResult.error(
363-
"Exception [getWiFiAPSSID]",
364-
"Security type is not WifiConfiguration.KeyMgmt.None or WifiConfiguration.KeyMgmt.WPA2_PSK",
365-
null);
358+
"Exception [getWiFiAPSSID]",
359+
"Security type is not WifiConfiguration.KeyMgmt.None or WifiConfiguration.KeyMgmt.WPA2_PSK",
360+
null);
366361
}
367362
} else {
368363
SoftApConfiguration softApConfiguration = apReservation.getSoftApConfiguration();
@@ -387,9 +382,9 @@ private void setWiFiAPSSID(MethodCall poCall, Result poResult) {
387382
poResult.success(null);
388383
} else {
389384
poResult.error(
390-
"Exception [setWiFiAPSSID]",
391-
"Setting SSID name is not supported on API level >= 26",
392-
null);
385+
"Exception [setWiFiAPSSID]",
386+
"Setting SSID name is not supported on API level >= 26",
387+
null);
393388
}
394389
}
395390

@@ -722,8 +717,9 @@ JSONArray handleNetworkScanResult() {
722717
JSONArray wifiArray = new JSONArray();
723718

724719
// *** Permission Check ***
725-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M &&
726-
moContext.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
720+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
721+
&& moContext.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION)
722+
!= PackageManager.PERMISSION_GRANTED) {
727723
// Permission is not granted. Return an empty array or handle the error.
728724
Log.w(TAG, "ACCESS_FINE_LOCATION permission not granted. Cannot scan for Wi-Fi networks.");
729725
return wifiArray; // Return an empty array
@@ -1043,12 +1039,15 @@ public void run() {
10431039
Boolean joinOnce = poCall.argument("join_once");
10441040
Boolean withInternet = poCall.argument("with_internet");
10451041
Integer timeoutInSeconds = poCall.argument("timeout_in_seconds");
1046-
// *** Permission Check ***
1047-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M &&
1048-
moContext.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
1042+
// *** Permission Check ***
1043+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
1044+
&& moContext.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION)
1045+
!= PackageManager.PERMISSION_GRANTED) {
10491046
// Permission is not granted.
1050-
Log.w(TAG, "ACCESS_FINE_LOCATION permission not granted. Cannot scan for Wi-Fi networks.");
1051-
poResult.error("ERROR_PERMISSION", "Fine location permission required to scan for networks.", null);
1047+
Log.w(
1048+
TAG, "ACCESS_FINE_LOCATION permission not granted. Cannot scan for Wi-Fi networks.");
1049+
poResult.error(
1050+
"ERROR_PERMISSION", "Fine location permission required to scan for networks.", null);
10521051
return; // Exit the runnable
10531052
}
10541053
String security = null;
@@ -1233,17 +1232,19 @@ private void removeWifiNetwork(MethodCall poCall, Result poResult) {
12331232
boolean removed = false;
12341233

12351234
// *** Permission Check ***
1236-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M &&
1237-
moContext.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
1235+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
1236+
&& moContext.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION)
1237+
!= PackageManager.PERMISSION_GRANTED) {
12381238
Log.w(TAG, "ACCESS_FINE_LOCATION permission not granted. Cannot remove Wi-Fi network.");
1239-
poResult.error("ERROR_PERMISSION", "Fine location permission required to remove networks.", null);
1239+
poResult.error(
1240+
"ERROR_PERMISSION", "Fine location permission required to remove networks.", null);
12401241
return;
12411242
}
12421243

1243-
12441244
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
1245-
List<android.net.wifi.WifiConfiguration> mWifiConfigList = moWiFi.getConfiguredNetworks(); // Requires permission
1246-
if(mWifiConfigList != null) { //getConfiguredNetworks can return null
1245+
List<android.net.wifi.WifiConfiguration> mWifiConfigList =
1246+
moWiFi.getConfiguredNetworks(); // Requires permission
1247+
if (mWifiConfigList != null) { //getConfiguredNetworks can return null
12471248
for (android.net.wifi.WifiConfiguration wifiConfig : mWifiConfigList) {
12481249
String comparableSSID = ('"' + prefix_ssid); //Add quotes because wifiConfig.SSID has them
12491250
if (wifiConfig.SSID.startsWith(comparableSSID)) {
@@ -1258,15 +1259,17 @@ private void removeWifiNetwork(MethodCall poCall, Result poResult) {
12581259

12591260
// remove network suggestion
12601261
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
1261-
List<WifiNetworkSuggestion> suggestions = moWiFi.getNetworkSuggestions(); // Requires permission (if targeting 29+)
1262+
List<WifiNetworkSuggestion> suggestions =
1263+
moWiFi.getNetworkSuggestions(); // Requires permission (if targeting 29+)
12621264
List<WifiNetworkSuggestion> removeSuggestions = new ArrayList<WifiNetworkSuggestion>();
12631265
for (int i = 0, suggestionsSize = suggestions.size(); i < suggestionsSize; i++) {
12641266
WifiNetworkSuggestion suggestion = suggestions.get(i);
12651267
if (suggestion.getSsid().startsWith(prefix_ssid)) {
12661268
removeSuggestions.add(suggestion);
12671269
}
12681270
}
1269-
if (!removeSuggestions.isEmpty()) { // Check if the list is not empty before calling removeNetworkSuggestions
1271+
if (!removeSuggestions
1272+
.isEmpty()) { // Check if the list is not empty before calling removeNetworkSuggestions
12701273
final int networksRemoved = moWiFi.removeNetworkSuggestions(removeSuggestions);
12711274
removed = networksRemoved == WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS;
12721275
} else {
@@ -1285,16 +1288,21 @@ private void isRegisteredWifiNetwork(MethodCall poCall, Result poResult) {
12851288
return;
12861289
}
12871290

1288-
12891291
// *** Permission Check ***
1290-
// Only needed pre-Q
1291-
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q && moContext.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
1292-
Log.w(TAG, "ACCESS_FINE_LOCATION permission not granted. Cannot check registered Wi-Fi networks.");
1293-
poResult.error("ERROR_PERMISSION", "Fine location permission required to check registered networks.", null);
1292+
// Only needed pre-Q
1293+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q
1294+
&& moContext.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION)
1295+
!= PackageManager.PERMISSION_GRANTED) {
1296+
Log.w(
1297+
TAG,
1298+
"ACCESS_FINE_LOCATION permission not granted. Cannot check registered Wi-Fi networks.");
1299+
poResult.error(
1300+
"ERROR_PERMISSION",
1301+
"Fine location permission required to check registered networks.",
1302+
null);
12941303
return;
12951304
}
12961305

1297-
12981306
List<android.net.wifi.WifiConfiguration> mWifiConfigList = moWiFi.getConfiguredNetworks();
12991307
String comparableSSID = ('"' + ssid + '"'); //Add quotes because wifiConfig.SSID has them
13001308
if (mWifiConfigList != null) {
@@ -1496,29 +1504,30 @@ private int registerWifiNetworkDeprecated(android.net.wifi.WifiConfiguration con
14961504
int registeredNetwork = -1;
14971505

14981506
// *** Permission Check ***
1499-
if (moContext.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
1500-
Log.w(TAG, "ACCESS_FINE_LOCATION permission not granted. Cannot register Wi-Fi network.");
1501-
// We can't use poResult here, as it is not passed down.
1502-
// Returning -1 is the best we can do in this case.
1503-
return -1;
1504-
}
1505-
if (moContext.checkSelfPermission(Manifest.permission.CHANGE_WIFI_STATE) != PackageManager.PERMISSION_GRANTED) {
1506-
Log.w(TAG, "CHANGE_WIFI_STATE permission not granted. Cannot register Wi-Fi network.");
1507-
return -1;
1508-
}
1509-
if(!Settings.System.canWrite(moContext)){
1510-
Log.w(TAG, "WRITE_SETTINGS permission not granted. Cannot register Wi-Fi network.");
1511-
return -1;
1512-
}
1513-
1507+
if (moContext.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION)
1508+
!= PackageManager.PERMISSION_GRANTED) {
1509+
Log.w(TAG, "ACCESS_FINE_LOCATION permission not granted. Cannot register Wi-Fi network.");
1510+
// We can't use poResult here, as it is not passed down.
1511+
// Returning -1 is the best we can do in this case.
1512+
return -1;
1513+
}
1514+
if (moContext.checkSelfPermission(Manifest.permission.CHANGE_WIFI_STATE)
1515+
!= PackageManager.PERMISSION_GRANTED) {
1516+
Log.w(TAG, "CHANGE_WIFI_STATE permission not granted. Cannot register Wi-Fi network.");
1517+
return -1;
1518+
}
1519+
if (!Settings.System.canWrite(moContext)) {
1520+
Log.w(TAG, "WRITE_SETTINGS permission not granted. Cannot register Wi-Fi network.");
1521+
return -1;
1522+
}
15141523

1515-
/// Remove the existing configuration for this network
1524+
/// Remove the existing configuration for this network
15161525
List<android.net.wifi.WifiConfiguration> mWifiConfigList = moWiFi.getConfiguredNetworks();
15171526

15181527
if (mWifiConfigList != null) {
15191528
for (android.net.wifi.WifiConfiguration wifiConfig : mWifiConfigList) {
15201529
if (wifiConfig.SSID.equals(conf.SSID)
1521-
&& (wifiConfig.BSSID == null
1530+
&& (wifiConfig.BSSID == null
15221531
|| conf.BSSID == null
15231532
|| wifiConfig.BSSID.equals(conf.BSSID))) {
15241533
conf.networkId = wifiConfig.networkId;

0 commit comments

Comments
 (0)