Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c427daee authored by Chienyuan Huang's avatar Chienyuan Huang Committed by Gerrit Code Review
Browse files

Merge "Le Scan: Add ambient discovery mode (2/2)"

parents 48353932 3d9b2436
Loading
Loading
Loading
Loading
+25 −5
Original line number Diff line number Diff line
@@ -47,8 +47,10 @@ import java.util.Objects;

    static final DateFormat DATE_FORMAT = new SimpleDateFormat("MM-dd HH:mm:ss");

    // Weight is the duty cycle of the scan mode
    static final int OPPORTUNISTIC_WEIGHT = 0;
    static final int LOW_POWER_WEIGHT = 10;
    static final int AMBIENT_DISCOVERY_WEIGHT = 20;
    static final int BALANCED_WEIGHT = 25;
    static final int LOW_LATENCY_WEIGHT = 100;

@@ -123,10 +125,12 @@ import java.util.Objects;
    private long mLowPowerScanTime = 0;
    private long mBalancedScanTime = 0;
    private long mLowLantencyScanTime = 0;
    private long mAmbientDiscoveryScanTime = 0;
    private int mOppScan = 0;
    private int mLowPowerScan = 0;
    private int mBalancedScan = 0;
    private int mLowLantencyScan = 0;
    private int mAmbientDiscoveryScan = 0;
    private List<LastScan> mLastScans = new ArrayList<LastScan>(NUM_SCAN_DURATIONS_KEPT);
    private HashMap<Integer, LastScan> mOngoingScans = new HashMap<Integer, LastScan>();
    public long startTime = 0;
@@ -206,6 +210,9 @@ import java.util.Objects;
                case ScanSettings.SCAN_MODE_LOW_LATENCY:
                    mLowLantencyScan++;
                    break;
                case ScanSettings.SCAN_MODE_AMBIENT_DISCOVERY:
                    mAmbientDiscoveryScan++;
                    break;
            }
        }

@@ -287,6 +294,9 @@ import java.util.Objects;
            case ScanSettings.SCAN_MODE_LOW_LATENCY:
                mLowLantencyScanTime += activeDuration;
                break;
            case ScanSettings.SCAN_MODE_AMBIENT_DISCOVERY:
                mAmbientDiscoveryScanTime += activeDuration;
                break;
        }

        try {
@@ -427,6 +437,8 @@ import java.util.Objects;
                return "BALANCED";
            case ScanSettings.SCAN_MODE_LOW_POWER:
                return "LOW_POWER";
            case ScanSettings.SCAN_MODE_AMBIENT_DISCOVERY:
                return "AMBIENT_DISCOVERY";
            default:
                return "UNKNOWN(" + scanMode + ")";
        }
@@ -461,10 +473,12 @@ import java.util.Objects;
        long lowPowerScanTime = mLowPowerScanTime;
        long balancedScanTime = mBalancedScanTime;
        long lowLatencyScanTime = mLowLantencyScanTime;
        long ambientDiscoveryScanTime = mAmbientDiscoveryScanTime;
        int oppScan = mOppScan;
        int lowPowerScan = mLowPowerScan;
        int balancedScan = mBalancedScan;
        int lowLatencyScan = mLowLantencyScan;
        int ambientDiscoveryScan = mAmbientDiscoveryScan;

        if (!mOngoingScans.isEmpty()) {
            for (Integer key : mOngoingScans.keySet()) {
@@ -493,11 +507,15 @@ import java.util.Objects;
                    case ScanSettings.SCAN_MODE_LOW_LATENCY:
                        lowLatencyScanTime += activeDuration;
                        break;
                    case ScanSettings.SCAN_MODE_AMBIENT_DISCOVERY:
                        ambientDiscoveryScan += activeDuration;
                        break;
                }
            }
        }
        Score = (oppScanTime * OPPORTUNISTIC_WEIGHT + lowPowerScanTime * LOW_POWER_WEIGHT
              + balancedScanTime * BALANCED_WEIGHT + lowLatencyScanTime * LOW_LATENCY_WEIGHT) / 100;
              + balancedScanTime * BALANCED_WEIGHT + lowLatencyScanTime * LOW_LATENCY_WEIGHT
              + ambientDiscoveryScanTime * AMBIENT_DISCOVERY_WEIGHT) / 100;

        sb.append("  " + appName);
        if (isRegistered) {
@@ -508,11 +526,13 @@ import java.util.Objects;
                + mScansStarted + " / " + mScansStopped);
        sb.append("\n  Scan time in ms (active/suspend/total)                      : "
                + totalActiveTime + " / " + totalSuspendTime + " / " + totalScanTime);
        sb.append("\n  Scan time with mode in ms (Opp/LowPower/Balanced/LowLatency): "
        sb.append("\n  Scan time with mode in ms "
                + "(Opp/LowPower/Balanced/LowLatency/AmbientDiscovery):"
                + oppScanTime + " / " + lowPowerScanTime + " / " + balancedScanTime + " / "
                + lowLatencyScanTime);
        sb.append("\n  Scan mode counter (Opp/LowPower/Balanced/LowLatency)        : " + oppScan
                + " / " + lowPowerScan + " / " + balancedScan + " / " + lowLatencyScan);
                + lowLatencyScanTime + " / " + ambientDiscoveryScanTime);
        sb.append("\n  Scan mode counter (Opp/LowPower/Balanced/LowLatency/AmbientDiscovery):"
                + oppScan + " / " + lowPowerScan + " / " + balancedScan + " / " + lowLatencyScan
                + " / " + ambientDiscoveryScan);
        sb.append("\n  Score                                                       : " + Score);
        sb.append("\n  Total number of results                                     : " + results);

+5 −0
Original line number Diff line number Diff line
@@ -3246,6 +3246,11 @@ public class GattService extends ProfileService {
            return false;
        }

        // Ambient discovery mode, needs privileged permission.
        if (settings.getScanMode() == ScanSettings.SCAN_MODE_AMBIENT_DISCOVERY) {
            return true;
        }

        // Regular scan, no special permission.
        if (settings.getReportDelayMillis() == 0) {
            return false;
+23 −5
Original line number Diff line number Diff line
@@ -97,6 +97,7 @@ public class ScanManager {
    private Set<ScanClient> mRegularScanClients;
    private Set<ScanClient> mBatchClients;
    private Set<ScanClient> mSuspendedScanClients;
    private HashMap<Integer, Integer> mPriorityMap = new HashMap<Integer, Integer>();

    private CountDownLatch mLatch;

@@ -129,6 +130,12 @@ public class ScanManager {
        mDm = (DisplayManager) mService.getSystemService(Context.DISPLAY_SERVICE);
        mActivityManager = (ActivityManager) mService.getSystemService(Context.ACTIVITY_SERVICE);
        mLocationManager = (LocationManager) mService.getSystemService(Context.LOCATION_SERVICE);

        mPriorityMap.put(ScanSettings.SCAN_MODE_OPPORTUNISTIC, 0);
        mPriorityMap.put(ScanSettings.SCAN_MODE_LOW_POWER, 1);
        mPriorityMap.put(ScanSettings.SCAN_MODE_BALANCED, 2);
        mPriorityMap.put(ScanSettings.SCAN_MODE_AMBIENT_DISCOVERY, 3);
        mPriorityMap.put(ScanSettings.SCAN_MODE_LOW_LATENCY, 4);
    }

    void start() {
@@ -500,6 +507,8 @@ public class ScanManager {
        private static final int SCAN_MODE_BALANCED_INTERVAL_MS = 4096;
        private static final int SCAN_MODE_LOW_LATENCY_WINDOW_MS = 4096;
        private static final int SCAN_MODE_LOW_LATENCY_INTERVAL_MS = 4096;
        private static final int SCAN_MODE_AMBIENT_DISCOVERY_WINDOW_MS = 128;
        private static final int SCAN_MODE_AMBIENT_DISCOVERY_INTERVAL_MS = 640;

        /**
         * Onfound/onlost for scan settings
@@ -518,6 +527,8 @@ public class ScanManager {
        private static final int SCAN_MODE_BATCH_BALANCED_INTERVAL_MS = 15000;
        private static final int SCAN_MODE_BATCH_LOW_LATENCY_WINDOW_MS = 1500;
        private static final int SCAN_MODE_BATCH_LOW_LATENCY_INTERVAL_MS = 5000;
        private static final int SCAN_MODE_BATCH_AMBIENT_DISCOVERY_WINDOW_MS = 1500;
        private static final int SCAN_MODE_BATCH_AMBIENT_DISCOVERY_INTERVAL_MS = 15000;

        // The logic is AND for each filter field.
        private static final int LIST_LOGIC_TYPE = 0x1111111;
@@ -619,13 +630,12 @@ public class ScanManager {

        ScanClient getAggressiveClient(Set<ScanClient> cList) {
            ScanClient result = null;
            int curScanSetting = Integer.MIN_VALUE;
            int currentScanModePriority = Integer.MIN_VALUE;
            for (ScanClient client : cList) {
                // ScanClient scan settings are assumed to be monotonically increasing in value for
                // more power hungry(higher duty cycle) operation.
                if (client.settings.getScanMode() > curScanSetting) {
                int priority = mPriorityMap.get(client.settings.getScanMode());
                if (priority > currentScanModePriority) {
                    result = client;
                    curScanSetting = client.settings.getScanMode();
                    currentScanModePriority = priority;
                }
            }
            return result;
@@ -763,6 +773,8 @@ public class ScanManager {
                    return SCAN_MODE_BATCH_BALANCED_WINDOW_MS;
                case ScanSettings.SCAN_MODE_LOW_POWER:
                    return SCAN_MODE_BATCH_LOW_POWER_WINDOW_MS;
                case ScanSettings.SCAN_MODE_AMBIENT_DISCOVERY:
                    return SCAN_MODE_BATCH_AMBIENT_DISCOVERY_WINDOW_MS;
                default:
                    return SCAN_MODE_BATCH_LOW_POWER_WINDOW_MS;
            }
@@ -776,6 +788,8 @@ public class ScanManager {
                    return SCAN_MODE_BATCH_BALANCED_INTERVAL_MS;
                case ScanSettings.SCAN_MODE_LOW_POWER:
                    return SCAN_MODE_BATCH_LOW_POWER_INTERVAL_MS;
                case ScanSettings.SCAN_MODE_AMBIENT_DISCOVERY:
                    return SCAN_MODE_BATCH_AMBIENT_DISCOVERY_INTERVAL_MS;
                default:
                    return SCAN_MODE_BATCH_LOW_POWER_INTERVAL_MS;
            }
@@ -1141,6 +1155,8 @@ public class ScanManager {
                        resolver,
                        Settings.Global.BLE_SCAN_LOW_POWER_WINDOW_MS,
                        SCAN_MODE_LOW_POWER_WINDOW_MS);
                case ScanSettings.SCAN_MODE_AMBIENT_DISCOVERY:
                    return SCAN_MODE_AMBIENT_DISCOVERY_WINDOW_MS;
                default:
                    return Settings.Global.getInt(
                        resolver,
@@ -1173,6 +1189,8 @@ public class ScanManager {
                        resolver,
                        Settings.Global.BLE_SCAN_LOW_POWER_INTERVAL_MS,
                        SCAN_MODE_LOW_POWER_INTERVAL_MS);
                case ScanSettings.SCAN_MODE_AMBIENT_DISCOVERY:
                    return SCAN_MODE_AMBIENT_DISCOVERY_INTERVAL_MS;
                default:
                    return Settings.Global.getInt(
                        resolver,