From 9da3cb109dd1a419b5bb795b67cb20f3307dfe3a Mon Sep 17 00:00:00 2001 From: Elias Lecomte Date: Sun, 26 Apr 2020 20:43:46 +0200 Subject: [PATCH 1/6] Support both disconnecting or removing network config for pre android 10. --- .../wifiutils/ConnectorUtils.java | 23 +++++++++++-------- .../wifiutils/WifiConnectorBuilder.java | 9 +++++--- .../thanosfisherman/wifiutils/WifiUtils.java | 15 ++++++++---- 3 files changed, 30 insertions(+), 17 deletions(-) diff --git a/wifiutils/src/main/java/com/thanosfisherman/wifiutils/ConnectorUtils.java b/wifiutils/src/main/java/com/thanosfisherman/wifiutils/ConnectorUtils.java index e141044..2cce9d4 100644 --- a/wifiutils/src/main/java/com/thanosfisherman/wifiutils/ConnectorUtils.java +++ b/wifiutils/src/main/java/com/thanosfisherman/wifiutils/ConnectorUtils.java @@ -18,17 +18,17 @@ import android.os.Build; import android.provider.Settings; -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; -import androidx.annotation.RequiresApi; -import androidx.annotation.RequiresPermission; - import com.thanosfisherman.elvis.Objects; import com.thanosfisherman.wifiutils.wifiWps.ConnectionWpsListener; import java.util.Collections; import java.util.List; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.annotation.RequiresApi; +import androidx.annotation.RequiresPermission; + import static android.Manifest.permission.ACCESS_FINE_LOCATION; import static android.Manifest.permission.ACCESS_WIFI_STATE; import static com.thanosfisherman.wifiutils.WifiUtils.wifiLog; @@ -171,9 +171,10 @@ static void unregisterReceiver(@NonNull final Context context, @Nullable final B } @RequiresPermission(ACCESS_WIFI_STATE) - static boolean disconnectFromWifi(@NonNull final Context context, @NonNull final ConnectivityManager connectivityManager, @NonNull final WifiManager wifiManager, @NonNull final String ssid) { + static boolean disconnectFromWifi(@NonNull final Context context, @NonNull final ConnectivityManager connectivityManager, @NonNull final WifiManager wifiManager, @NonNull final String ssid, final boolean alsoRemove) { if (isAndroidQOrLater()) { - if (networkCallback != null) { + if (networkCallback != null) + { connectivityManager.unregisterNetworkCallback(networkCallback); networkCallback = null; } @@ -181,8 +182,12 @@ static boolean disconnectFromWifi(@NonNull final Context context, @NonNull final return true; } - final WifiConfiguration wifiConfiguration = ConfigSecurities.getWifiConfiguration(wifiManager, ssid); - return cleanPreviousConfiguration(wifiManager, wifiConfiguration); + if (alsoRemove) { + final WifiConfiguration wifiConfiguration = ConfigSecurities.getWifiConfiguration(wifiManager, ssid); + return cleanPreviousConfiguration(wifiManager, wifiConfiguration); + } else { + return wifiManager.disconnect(); + } } @RequiresPermission(allOf = {ACCESS_FINE_LOCATION, ACCESS_WIFI_STATE}) diff --git a/wifiutils/src/main/java/com/thanosfisherman/wifiutils/WifiConnectorBuilder.java b/wifiutils/src/main/java/com/thanosfisherman/wifiutils/WifiConnectorBuilder.java index 962f74d..278841c 100644 --- a/wifiutils/src/main/java/com/thanosfisherman/wifiutils/WifiConnectorBuilder.java +++ b/wifiutils/src/main/java/com/thanosfisherman/wifiutils/WifiConnectorBuilder.java @@ -1,9 +1,6 @@ package com.thanosfisherman.wifiutils; import android.os.Build; -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; -import androidx.annotation.RequiresApi; import com.thanosfisherman.wifiutils.wifiConnect.ConnectionScanResultsListener; import com.thanosfisherman.wifiutils.wifiConnect.ConnectionSuccessListener; @@ -12,6 +9,10 @@ import com.thanosfisherman.wifiutils.wifiState.WifiStateListener; import com.thanosfisherman.wifiutils.wifiWps.ConnectionWpsListener; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.annotation.RequiresApi; + public interface WifiConnectorBuilder { void start(); @@ -33,6 +34,8 @@ interface WifiUtilsBuilder { void disconnectFrom(@NonNull String ssid, @NonNull DisconnectionSuccessListener disconnectionSuccessListener); + void disconnectFrom(@NonNull String ssid, boolean alsoRemove, @NonNull DisconnectionSuccessListener disconnectionSuccessListener); + @NonNull WifiSuccessListener connectWithScanResult(@NonNull String password, @Nullable ConnectionScanResultsListener connectionScanResultsListener); diff --git a/wifiutils/src/main/java/com/thanosfisherman/wifiutils/WifiUtils.java b/wifiutils/src/main/java/com/thanosfisherman/wifiutils/WifiUtils.java index 115dd85..2fc296a 100644 --- a/wifiutils/src/main/java/com/thanosfisherman/wifiutils/WifiUtils.java +++ b/wifiutils/src/main/java/com/thanosfisherman/wifiutils/WifiUtils.java @@ -9,10 +9,6 @@ import android.os.Build; import android.util.Log; -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; -import androidx.annotation.RequiresApi; - import com.thanosfisherman.wifiutils.wifiConnect.ConnectionErrorCode; import com.thanosfisherman.wifiutils.wifiConnect.ConnectionScanResultsListener; import com.thanosfisherman.wifiutils.wifiConnect.ConnectionSuccessListener; @@ -31,6 +27,10 @@ import java.util.ArrayList; import java.util.List; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.annotation.RequiresApi; + import static com.thanosfisherman.elvis.Elvis.of; import static com.thanosfisherman.wifiutils.ConnectorUtils.cleanPreviousConfiguration; import static com.thanosfisherman.wifiutils.ConnectorUtils.connectToWifi; @@ -234,6 +234,11 @@ public WifiConnectorBuilder scanWifi(final ScanResultsListener scanResultsListen @Override public void disconnectFrom(@NonNull final String ssid, @NonNull final DisconnectionSuccessListener disconnectionSuccessListener) { + this.disconnectFrom(ssid, false, disconnectionSuccessListener); + } + + @Override + public void disconnectFrom(@NonNull final String ssid, final boolean alsoRemove, @NonNull final DisconnectionSuccessListener disconnectionSuccessListener) { if (mConnectivityManager == null) { disconnectionSuccessListener.failed(DisconnectionErrorCode.COULD_NOT_GET_CONNECTIVITY_MANAGER); return; @@ -244,7 +249,7 @@ public void disconnectFrom(@NonNull final String ssid, @NonNull final Disconnect return; } - if (disconnectFromWifi(mContext, mConnectivityManager, mWifiManager, ssid)) { + if (disconnectFromWifi(mContext, mConnectivityManager, mWifiManager, ssid, alsoRemove)) { disconnectionSuccessListener.success(); } else { disconnectionSuccessListener.failed(DisconnectionErrorCode.COULD_NOT_DISCONNECT); From 5fde31bde87071394e8588c7084ce44b77509f09 Mon Sep 17 00:00:00 2001 From: Elias Lecomte Date: Fri, 1 May 2020 13:50:50 +0200 Subject: [PATCH 2/6] After all, this is required to be able to use internet from wifi network. --- .../java/com/thanosfisherman/wifiutils/ConnectorUtils.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/wifiutils/src/main/java/com/thanosfisherman/wifiutils/ConnectorUtils.java b/wifiutils/src/main/java/com/thanosfisherman/wifiutils/ConnectorUtils.java index 2cce9d4..35ba3f3 100644 --- a/wifiutils/src/main/java/com/thanosfisherman/wifiutils/ConnectorUtils.java +++ b/wifiutils/src/main/java/com/thanosfisherman/wifiutils/ConnectorUtils.java @@ -324,6 +324,9 @@ public void onAvailable(@NonNull Network network) { super.onAvailable(network); wifiLog("AndroidQ+ connected to wifi "); + + // bind so all api calls are performed over this new network + connectivityManager.bindProcessToNetwork(network); } @Override From 277944d07d0bb4fbdde5736176bc7d61d1da1288 Mon Sep 17 00:00:00 2001 From: Elias Lecomte Date: Fri, 1 May 2020 14:07:19 +0200 Subject: [PATCH 3/6] Only fine location is required. --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 3518487..7768539 100644 --- a/README.md +++ b/README.md @@ -159,7 +159,6 @@ Damn You are required to set a few permissions in order for this lib to work cor ```xml - ``` ### Add it to your project From 178421b02f81afcb01b716af66a8deeeef36fc38 Mon Sep 17 00:00:00 2001 From: Elias Lecomte Date: Fri, 1 May 2020 14:36:11 +0200 Subject: [PATCH 4/6] Add .project files to gitignore. --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 4db771a..c380c39 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,5 @@ .DS_Store /build /captures -.externalNativeBuild \ No newline at end of file +.externalNativeBuild +.project \ No newline at end of file From 12b3de65acfabab9875e481d4f4d34284379f7ad Mon Sep 17 00:00:00 2001 From: Elias Lecomte Date: Fri, 1 May 2020 15:50:58 +0200 Subject: [PATCH 5/6] Split removing and disconnecting in WifiUtils. Update docs & kotlin activity. --- README.md | 37 ++++++++++++++ .../wifiutils/sample/MainActivity.java | 2 +- .../wifiutils/sample/MainKotlinActivity.kt | 20 +++++++- sample/src/main/res/layout/activity_main.xml | 43 +++++++++++++--- sample/src/main/res/values/strings.xml | 1 + .../wifiutils/ConnectorUtils.java | 49 +++++++++++-------- .../wifiutils/WifiConnectorBuilder.java | 6 ++- .../thanosfisherman/wifiutils/WifiUtils.java | 31 ++++++++++-- .../wifiutils/wifiRemove/RemoveErrorCode.java | 7 +++ .../wifiRemove/RemoveSuccessListener.java | 9 ++++ 10 files changed, 173 insertions(+), 32 deletions(-) create mode 100644 wifiutils/src/main/java/com/thanosfisherman/wifiutils/wifiRemove/RemoveErrorCode.java create mode 100644 wifiutils/src/main/java/com/thanosfisherman/wifiutils/wifiRemove/RemoveSuccessListener.java diff --git a/README.md b/README.md index 7768539..862a899 100644 --- a/README.md +++ b/README.md @@ -150,6 +150,43 @@ WifiUtils.withContext(getApplicationContext()) .start(); ``` +### Disconnect + +You can disconnect from the currently connected network. + +```java +WifiUtils.withContext(context) + .disconnect(new DisconnectionSuccessListener() { + @Override + public void success() { + Toast.makeText(MainActivity.this, "Disconnect success!", Toast.LENGTH_SHORT).show(); + } + + @Override + public void failed(@NonNull DisconnectionErrorCode errorCode) { + Toast.makeText(MainActivity.this, "Failed to disconnect: " + errorCode.toString(), Toast.LENGTH_SHORT).show(); + } + }); +``` + +### Disconnect and remove saved network configuration + +You can also remove the saved wifi network configuration. On Android 10, this will just simply disconnect (as wifi configuration's made by WifiUtils are no longer saved). +Notice: WifiUtils can't remove network configurations created by the user or by another app. + +```kotlin +WifiUtils.withContext(context) + .remove(SSID, object : RemoveSuccessListener { + override fun success() { + Toast.makeText(context, "Remove success!", Toast.LENGTH_SHORT).show() + } + + override fun failed(errorCode: RemoveErrorCode) { + Toast.makeText(context, "Failed to disconnect and remove: $errorCode", Toast.LENGTH_SHORT).show() + } + }) +``` + ### Enable Logging If you want to receive some extra logging info comming from WiFi Utils you can enable its logging capabilities with `WifiUtils.enableLog(true);` diff --git a/sample/src/main/java/com/thanosfisherman/wifiutils/sample/MainActivity.java b/sample/src/main/java/com/thanosfisherman/wifiutils/sample/MainActivity.java index 0c7a1e0..c7ad9ae 100644 --- a/sample/src/main/java/com/thanosfisherman/wifiutils/sample/MainActivity.java +++ b/sample/src/main/java/com/thanosfisherman/wifiutils/sample/MainActivity.java @@ -63,7 +63,7 @@ private void connectWithWpa() { private void disconnect(final Context context) { WifiUtils.withContext(context) - .disconnectFrom(SSID, new DisconnectionSuccessListener() { + .disconnect(new DisconnectionSuccessListener() { @Override public void success() { Toast.makeText(MainActivity.this, "Disconnect success!", Toast.LENGTH_SHORT).show(); diff --git a/sample/src/main/java/com/thanosfisherman/wifiutils/sample/MainKotlinActivity.kt b/sample/src/main/java/com/thanosfisherman/wifiutils/sample/MainKotlinActivity.kt index 1eb6778..06d3561 100644 --- a/sample/src/main/java/com/thanosfisherman/wifiutils/sample/MainKotlinActivity.kt +++ b/sample/src/main/java/com/thanosfisherman/wifiutils/sample/MainKotlinActivity.kt @@ -11,6 +11,8 @@ import com.thanosfisherman.wifiutils.wifiConnect.ConnectionErrorCode import com.thanosfisherman.wifiutils.wifiConnect.ConnectionSuccessListener import com.thanosfisherman.wifiutils.wifiDisconnect.DisconnectionErrorCode import com.thanosfisherman.wifiutils.wifiDisconnect.DisconnectionSuccessListener +import com.thanosfisherman.wifiutils.wifiRemove.RemoveErrorCode +import com.thanosfisherman.wifiutils.wifiRemove.RemoveSuccessListener import kotlinx.android.synthetic.main.activity_main.* class MainKotlinActivity : AppCompatActivity() { @@ -23,8 +25,11 @@ class MainKotlinActivity : AppCompatActivity() { setContentView(R.layout.activity_main) ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), 555) WifiUtils.enableLog(true) + textview_ssid.text = SSID + textview_password.text = PASSWORD button_connect.setOnClickListener { connectWithWpa(applicationContext) } button_disconnect.setOnClickListener { disconnect(applicationContext) } + button_remove.setOnClickListener{ remove(applicationContext) } } private fun connectWithWpa(context: Context) { @@ -45,7 +50,7 @@ class MainKotlinActivity : AppCompatActivity() { private fun disconnect(context: Context) { WifiUtils.withContext(context) - .disconnectFrom(SSID, object : DisconnectionSuccessListener { + .disconnect(object : DisconnectionSuccessListener { override fun success() { Toast.makeText(context, "Disconnect success!", Toast.LENGTH_SHORT).show() } @@ -55,4 +60,17 @@ class MainKotlinActivity : AppCompatActivity() { } }) } + + private fun remove(context: Context) { + WifiUtils.withContext(context) + .remove(SSID, object : RemoveSuccessListener { + override fun success() { + Toast.makeText(context, "Remove success!", Toast.LENGTH_SHORT).show() + } + + override fun failed(errorCode: RemoveErrorCode) { + Toast.makeText(context, "Failed to disconnect and remove: $errorCode", Toast.LENGTH_SHORT).show() + } + }) + } } \ No newline at end of file diff --git a/sample/src/main/res/layout/activity_main.xml b/sample/src/main/res/layout/activity_main.xml index 79af269..d926e8e 100644 --- a/sample/src/main/res/layout/activity_main.xml +++ b/sample/src/main/res/layout/activity_main.xml @@ -7,26 +7,57 @@ android:layout_height="match_parent" tools:context="com.thanosfisherman.wifiutils.sample.MainActivity"> + + + +