File indexing completed on 2024-12-22 04:41:38

0001 /*
0002  * SPDX-FileCopyrightText: 2014 The Android Open Source Project
0003  *
0004  * SPDX-License-Identifier: Apache-2.0
0005  */
0006 
0007 package org.kde.kdeconnect.Plugins.ConnectivityReportPlugin;
0008 
0009 import android.os.Build;
0010 import android.telephony.CellInfo;
0011 import android.telephony.SignalStrength;
0012 
0013 public class ASUUtils {
0014     /**
0015      * Implementation of SignalStrength.toLevel usable from API Level 7+
0016      */
0017     public static int signalStrengthToLevel(SignalStrength signalStrength) {
0018         int level;
0019         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
0020             level = signalStrength.getLevel();
0021         } else {
0022             // Should work on all supported versions, uses copied functions from modern SDKs
0023             // Needs testing
0024 
0025             int gsmLevel = signalStrength.getGsmSignalStrength();
0026             if (gsmLevel >= 0 && gsmLevel <= 31) {
0027                 // Convert getGsmSignalStrength range (0..31) to getLevel range (0..4)
0028                 gsmLevel = gsmLevel * 4 / 31;
0029             } else {
0030                 gsmLevel = 0;
0031             }
0032 
0033             int cdmaLevel = getCdmaLevel(signalStrength.getCdmaDbm(), signalStrength.getCdmaEcio());
0034             int evdoLevel = getEvdoLevel(signalStrength.getEvdoDbm(), signalStrength.getEvdoSnr());
0035 
0036             level = Math.max(gsmLevel, Math.max(cdmaLevel, evdoLevel));
0037         }
0038         return level;
0039     }
0040 
0041 
0042     /**
0043      * Get cdma as level 0..4
0044      * Adapted from CellSignalStrengthCdma.java
0045      */
0046     private static int getCdmaLevel(int cdmaDbm, int cdmaEcio) {
0047         int levelDbm;
0048         int levelEcio;
0049 
0050         if (cdmaDbm == CellInfo.UNAVAILABLE) levelDbm = 0;
0051         else if (cdmaDbm >= -75) levelDbm = 4;
0052         else if (cdmaDbm >= -85) levelDbm = 3;
0053         else if (cdmaDbm >= -95) levelDbm = 2;
0054         else if (cdmaDbm >= -100) levelDbm = 1;
0055         else levelDbm = 0;
0056 
0057         // Ec/Io are in dB*10
0058         if (cdmaEcio == CellInfo.UNAVAILABLE) levelEcio = 0;
0059         else if (cdmaEcio >= -90) levelEcio = 4;
0060         else if (cdmaEcio >= -110) levelEcio = 3;
0061         else if (cdmaEcio >= -130) levelEcio = 2;
0062         else if (cdmaEcio >= -150) levelEcio = 1;
0063         else levelEcio = 0;
0064 
0065         return Math.min(levelDbm, levelEcio);
0066     }
0067 
0068     /**
0069      * Get Evdo as level 0..4
0070      * Adapted from CellSignalStrengthCdma.java
0071      */
0072     private static int getEvdoLevel(int evdoDbm, int evdoSnr) {
0073         int levelEvdoDbm;
0074         int levelEvdoSnr;
0075 
0076         if (evdoDbm == CellInfo.UNAVAILABLE) levelEvdoDbm = 0;
0077         else if (evdoDbm >= -65) levelEvdoDbm = 4;
0078         else if (evdoDbm >= -75) levelEvdoDbm = 3;
0079         else if (evdoDbm >= -90) levelEvdoDbm = 2;
0080         else if (evdoDbm >= -105) levelEvdoDbm = 1;
0081         else levelEvdoDbm = 0;
0082 
0083         if (evdoSnr == CellInfo.UNAVAILABLE) levelEvdoSnr = 0;
0084         else if (evdoSnr >= 7) levelEvdoSnr = 4;
0085         else if (evdoSnr >= 5) levelEvdoSnr = 3;
0086         else if (evdoSnr >= 3) levelEvdoSnr = 2;
0087         else if (evdoSnr >= 1) levelEvdoSnr = 1;
0088         else levelEvdoSnr = 0;
0089 
0090         return Math.min(levelEvdoDbm, levelEvdoSnr);
0091     }
0092 }