Skip to content

Commit

Permalink
add signal strength indicator
Browse files Browse the repository at this point in the history
  • Loading branch information
rostopira committed Dec 3, 2023
1 parent f6de0f6 commit 747e89c
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 8 deletions.
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<service
android:name=".WifiTileService"
android:label="@string/wifi"
android:icon="@drawable/rss_4"
android:icon="@drawable/rss_5"
android:permission="android.permission.BIND_QUICK_SETTINGS_TILE"
android:exported="true">
<intent-filter>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/n/eus/wifiqs/WifiStateListener.java
Original file line number Diff line number Diff line change
@@ -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);
}
7 changes: 4 additions & 3 deletions app/src/main/java/n/eus/wifiqs/WifiStateReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
}
}

Expand Down
17 changes: 15 additions & 2 deletions app/src/main/java/n/eus/wifiqs/WifiTileService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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));
Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/drawable/rss_4.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
android:viewportHeight="960">
<path
android:fillColor="#FFF"
android:pathData="M480,840 L0,360q95,-97 219.5,-148.5T480,160q136,0 260.5,51.5T960,360L480,840Z"/>
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"/>
</vector>
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/rss_5.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="960"
android:viewportHeight="960">
<path
android:fillColor="#FFF"
android:pathData="M480,840 L0,360q95,-97 219.5,-148.5T480,160q136,0 260.5,51.5T960,360L480,840Z"/>
</vector>

0 comments on commit 747e89c

Please sign in to comment.