From 747e89c74253ad8b5430c9de7efdbd12e79262a5 Mon Sep 17 00:00:00 2001 From: rostopira Date: Sun, 3 Dec 2023 13:52:45 +0200 Subject: [PATCH] add signal strength indicator --- app/src/main/AndroidManifest.xml | 2 +- .../java/n/eus/wifiqs/WifiStateListener.java | 2 +- .../java/n/eus/wifiqs/WifiStateReceiver.java | 7 ++++--- .../main/java/n/eus/wifiqs/WifiTileService.java | 17 +++++++++++++++-- app/src/main/res/drawable/rss_4.xml | 2 +- app/src/main/res/drawable/rss_5.xml | 9 +++++++++ 6 files changed, 31 insertions(+), 8 deletions(-) create mode 100644 app/src/main/res/drawable/rss_5.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 54fa83c..ce85667 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -26,7 +26,7 @@ diff --git a/app/src/main/java/n/eus/wifiqs/WifiStateListener.java b/app/src/main/java/n/eus/wifiqs/WifiStateListener.java index 14c39ed..13ec875 100644 --- a/app/src/main/java/n/eus/wifiqs/WifiStateListener.java +++ b/app/src/main/java/n/eus/wifiqs/WifiStateListener.java @@ -1,5 +1,5 @@ package n.eus.wifiqs; public interface WifiStateListener { - void onWifiStateChanged(WifiState state, String ssid); + void onWifiStateChanged(WifiState state, String ssid, int rss); } diff --git a/app/src/main/java/n/eus/wifiqs/WifiStateReceiver.java b/app/src/main/java/n/eus/wifiqs/WifiStateReceiver.java index f1443d6..2ba3a51 100644 --- a/app/src/main/java/n/eus/wifiqs/WifiStateReceiver.java +++ b/app/src/main/java/n/eus/wifiqs/WifiStateReceiver.java @@ -21,7 +21,7 @@ public void onReceive(Context context, Intent _) { final WifiManager wifiMan = context.getSystemService(WifiManager.class); final boolean isEnabled = wifiMan.isWifiEnabled(); if (!isEnabled) { - listener.onWifiStateChanged(WifiState.DISABLED, null); + listener.onWifiStateChanged(WifiState.DISABLED, null, -1); return; } final WifiInfo wifiInfo = wifiMan.getConnectionInfo(); @@ -33,9 +33,10 @@ public void onReceive(Context context, Intent _) { // Remove quotes ssid = ssid.substring(1, ssid.length()-1); } - listener.onWifiStateChanged(WifiState.CONNECTED, ssid); + final int rss = WifiManager.calculateSignalLevel(wifiInfo.getRssi(), 7); + listener.onWifiStateChanged(WifiState.CONNECTED, ssid, rss); } else { - listener.onWifiStateChanged(WifiState.ENABLED, null); + listener.onWifiStateChanged(WifiState.ENABLED, null, -1); } } diff --git a/app/src/main/java/n/eus/wifiqs/WifiTileService.java b/app/src/main/java/n/eus/wifiqs/WifiTileService.java index 56ecb05..3c25533 100644 --- a/app/src/main/java/n/eus/wifiqs/WifiTileService.java +++ b/app/src/main/java/n/eus/wifiqs/WifiTileService.java @@ -21,7 +21,7 @@ public void onStartListening() { } @Override - public void onWifiStateChanged(WifiState state, String ssid) { + public void onWifiStateChanged(WifiState state, String ssid, int rss) { final Tile tile = getQsTile(); switch (state) { case DISABLED: @@ -45,7 +45,7 @@ public void onWifiStateChanged(WifiState state, String ssid) { tile.setState(Tile.STATE_ACTIVE); break; case CONNECTED: - tile.setIcon(Icon.createWithResource(this, R.drawable.rss_4)); + tile.setIcon(iconFor(rss)); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { tile.setLabel(getString(R.string.wifi)); tile.setSubtitle(ssid != null ? ssid : getString(R.string.connected)); @@ -58,6 +58,19 @@ public void onWifiStateChanged(WifiState state, String ssid) { tile.updateTile(); } + private Icon iconFor(int rss) { + final int resId; + switch (rss) { + case 0: resId = R.drawable.rss_0; break; + case 1: resId = R.drawable.rss_1; break; + case 2: resId = R.drawable.rss_2; break; + case 3: resId = R.drawable.rss_3; break; + case 4: resId = R.drawable.rss_4; break; + default: resId = R.drawable.rss_5; break; + } + return Icon.createWithResource(this, resId); + } + @Override public void onClick() { super.onClick(); diff --git a/app/src/main/res/drawable/rss_4.xml b/app/src/main/res/drawable/rss_4.xml index db57070..621deb9 100644 --- a/app/src/main/res/drawable/rss_4.xml +++ b/app/src/main/res/drawable/rss_4.xml @@ -5,5 +5,5 @@ android:viewportHeight="960"> + android:pathData="M480,840 L0,360q95,-97 219.5,-148.5T480,160q137,0 261,51t219,149L480,840ZM174,420q67,-48 145,-74t161,-26q83,0 161,26t145,74l58,-58q-79,-60 -172,-91t-192,-31q-99,0 -192,31t-172,91l58,58Z"/> diff --git a/app/src/main/res/drawable/rss_5.xml b/app/src/main/res/drawable/rss_5.xml new file mode 100644 index 0000000..db57070 --- /dev/null +++ b/app/src/main/res/drawable/rss_5.xml @@ -0,0 +1,9 @@ + + +