Skip to content

Commit

Permalink
- [wifi_basic,android] WiFiBasic, WifiBasicPlugin: setEnabled -…
Browse files Browse the repository at this point in the history
… removed `shouldOpenSettings` arg

- [wifi_basic,android,test] `WiFiBasic`, `WifiBasicPlugin`: `openSettings` method added, implemented and test for it added
- [wifi_basic,example] smaller example with just buttons
  • Loading branch information
daadu committed Nov 28, 2021
1 parent 6a34081 commit d76c381
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,12 @@ class WifiBasicPlugin : FlutterPlugin, MethodCallHandler {
}
"setEnabled" -> {
val enabled = call.argument<Boolean>("enabled")
if (enabled != null) {
val shouldOpenSettings = call.argument<Boolean>("shouldOpenSettings")
val isSuccess = setEnabled(enabled, shouldOpenSettings ?: false)
result.success(isSuccess)
} else {
result.error("InvalidArg", "enabled argument is null", null)
}
?: return result.error("InvalidArg", "enabled argument is null", null)
result.success(setEnabled(enabled))
}
"openSettings" -> {
openSettings()
result.success(null)
}
else -> {
result.notImplemented()
Expand All @@ -67,16 +66,16 @@ class WifiBasicPlugin : FlutterPlugin, MethodCallHandler {

private fun isEnabled(): Boolean = wifi!!.isWifiEnabled

private fun setEnabled(enabled: Boolean, shouldOpenSettings: Boolean): Boolean =
private fun setEnabled(enabled: Boolean): Boolean =
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
wifi!!.setWifiEnabled(enabled)
} else {
val isSuccess = wifi!!.setWifiEnabled(enabled)
if (!isSuccess && shouldOpenSettings) {
val intent = Intent(Settings.ACTION_WIFI_SETTINGS)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
context!!.startActivity(intent)
}
isSuccess
false;
}

private fun openSettings() {
val intent = Intent(Settings.ACTION_WIFI_SETTINGS)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
context!!.startActivity(intent)
}
}
83 changes: 36 additions & 47 deletions packages/wifi_basic/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,55 +24,44 @@ class _MyAppState extends State<MyApp> {
Widget build(BuildContext context) {
return MaterialApp(
home: Builder(
builder: (context) => Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
builder: (context) => Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Padding(
padding: const EdgeInsets.symmetric(vertical: 12, horizontal: 20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
ElevatedButton(
child: const Text("WiFiBasic.hasCapability();"),
onPressed: () async => showSnackbar(context,
"hasCapability: ${await WiFiBasic.hasCapability()}"),
),
body: Padding(
padding:
const EdgeInsets.symmetric(vertical: 12, horizontal: 20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Row(
children: [
const Text("WiFiBasic.hasCapability:"),
TextButton(
child: const Text("call"),
onPressed: () async => showSnackbar(context,
"hasCapability: ${await WiFiBasic.hasCapability()}"),
),
],
),
Row(
children: [
const Text("WiFiBasic.isEnabled:"),
TextButton(
child: const Text("call"),
onPressed: () async => showSnackbar(context,
"isEnabled: ${await WiFiBasic.isEnabled()}"),
),
],
),
Row(
children: [
const Text("WiFiBasic.setEnabled:"),
TextButton(
child: const Text("enable"),
onPressed: () async => showSnackbar(context,
"setEnabled(true): ${await WiFiBasic.setEnabled(true, shouldOpenSettings: true)}"),
),
TextButton(
child: const Text("disable"),
onPressed: () async => showSnackbar(context,
"setEnabled(false): ${await WiFiBasic.setEnabled(false, shouldOpenSettings: true)}"),
),
],
),
],
),
ElevatedButton(
child: const Text("WiFiBasic.isEnabled();"),
onPressed: () async => showSnackbar(
context, "isEnabled: ${await WiFiBasic.isEnabled()}"),
),
)),
ElevatedButton(
child: const Text("WiFiBasic.setEnabled(true);"),
onPressed: () async => showSnackbar(context,
"setEnabled(true): success? ${await WiFiBasic.setEnabled(true)}"),
),
ElevatedButton(
child: const Text("WiFiBasic.setEnabled(false);"),
onPressed: () async => showSnackbar(context,
"setEnabled(false): success? ${await WiFiBasic.setEnabled(false)}"),
),
ElevatedButton(
child: const Text("WiFiBasic.openSettings();"),
onPressed: () async => await WiFiBasic.openSettings(),
),
],
),
),
),
),
);
}
}
11 changes: 5 additions & 6 deletions packages/wifi_basic/lib/wifi_basic.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ class WiFiBasic {
static Future<bool> isEnabled() async =>
await _channel.invokeMethod('isEnabled');

static Future<bool> setEnabled(bool enabled,
{bool shouldOpenSettings = false}) async =>
await _channel.invokeMethod('setEnabled', {
"enabled": enabled,
"shouldOpenSettings": shouldOpenSettings,
});
static Future<bool> setEnabled(bool enabled) async =>
await _channel.invokeMethod('setEnabled', {"enabled": enabled});

static Future<void> openSettings() async =>
await _channel.invokeMethod("openSettings");
}
3 changes: 3 additions & 0 deletions packages/wifi_basic/test/wifi_basic_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ void main() {
case "setEnabled":
isEnabled = methodCall.arguments["enabled"];
return true;
case "openSettings":
return null;
default:
throw MissingPluginException();
}
Expand All @@ -33,5 +35,6 @@ void main() {
expect(await WiFiBasic.isEnabled(), true);
expect(await WiFiBasic.setEnabled(false), true);
expect(await WiFiBasic.isEnabled(), false);
await WiFiBasic.openSettings();
});
}

0 comments on commit d76c381

Please sign in to comment.