1 /*
2  * Copyright (C) 2013 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.bluetooth.le_scan;
18 
19 import android.bluetooth.le.ScanFilter;
20 import android.bluetooth.le.ScanSettings;
21 import android.os.Binder;
22 import android.os.UserHandle;
23 
24 import java.util.List;
25 import java.util.Objects;
26 
27 /** Helper class identifying a client that has requested LE scan results. */
28 public class ScanClient {
29     public int scannerId;
30     public ScanSettings settings;
31     public int scanModeApp;
32     public boolean started = false;
33     public int appUid;
34     public List<ScanFilter> filters;
35     // App associated with the scan client died.
36     public boolean appDied;
37     public boolean hasLocationPermission;
38     public UserHandle userHandle;
39     public boolean isQApp;
40     public boolean eligibleForSanitizedExposureNotification;
41     public boolean hasNetworkSettingsPermission;
42     public boolean hasNetworkSetupWizardPermission;
43     public boolean hasScanWithoutLocationPermission;
44     public boolean hasDisavowedLocation;
45     public List<String> associatedDevices;
46 
47     public AppScanStats stats = null;
48 
49     private static final ScanSettings DEFAULT_SCAN_SETTINGS =
50             new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).build();
51 
ScanClient(int scannerId)52     public ScanClient(int scannerId) {
53         this(scannerId, DEFAULT_SCAN_SETTINGS, null);
54     }
55 
ScanClient(int scannerId, ScanSettings settings, List<ScanFilter> filters)56     public ScanClient(int scannerId, ScanSettings settings, List<ScanFilter> filters) {
57         this(scannerId, settings, filters, Binder.getCallingUid());
58     }
59 
ScanClient(int scannerId, ScanSettings settings, List<ScanFilter> filters, int appUid)60     public ScanClient(int scannerId, ScanSettings settings, List<ScanFilter> filters, int appUid) {
61         this.scannerId = scannerId;
62         this.settings = settings;
63         this.scanModeApp = settings.getScanMode();
64         this.filters = filters;
65         this.appUid = appUid;
66     }
67 
68     @Override
equals(Object obj)69     public boolean equals(Object obj) {
70         if (this == obj) {
71             return true;
72         }
73         if (obj == null || getClass() != obj.getClass()) {
74             return false;
75         }
76         ScanClient other = (ScanClient) obj;
77         return scannerId == other.scannerId;
78     }
79 
80     @Override
hashCode()81     public int hashCode() {
82         return Objects.hash(scannerId);
83     }
84 
85     @Override
toString()86     public String toString() {
87         StringBuilder sb = new StringBuilder();
88         sb.append(" [ScanClient")
89                 .append(" scanModeApp ")
90                 .append(scanModeApp)
91                 .append(" scanModeUsed ")
92                 .append(settings.getScanMode());
93         if (stats != null && stats.appName != null) {
94             sb.append(" [appScanStats ").append(stats.appName).append("]");
95         }
96         sb.append("]");
97         return sb.toString();
98     }
99 
100     /**
101      * Update scan settings with the new scan mode.
102      *
103      * @return true if scan settings are updated, false otherwise.
104      */
updateScanMode(int newScanMode)105     boolean updateScanMode(int newScanMode) {
106         if (settings.getScanMode() == newScanMode) {
107             return false;
108         }
109 
110         ScanSettings.Builder builder = new ScanSettings.Builder();
111         settings =
112                 builder.setScanMode(newScanMode)
113                         .setCallbackType(settings.getCallbackType())
114                         .setScanResultType(settings.getScanResultType())
115                         .setReportDelay(settings.getReportDelayMillis())
116                         .setNumOfMatches(settings.getNumOfMatches())
117                         .setMatchMode(settings.getMatchMode())
118                         .setLegacy(settings.getLegacy())
119                         .setPhy(settings.getPhy())
120                         .build();
121         return true;
122     }
123 }
124