• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 class ScanClient {
29     private static final ScanSettings DEFAULT_SCAN_SETTINGS =
30             new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).build();
31 
32     final int mScannerId;
33     final int mAppUid;
34     final List<ScanFilter> mFilters;
35 
36     ScanSettings mSettings;
37     int mScanModeApp;
38     boolean mStarted = false;
39     boolean mIsInternalClient = false;
40     // App associated with the scan client died.
41     boolean mAppDied;
42     boolean mHasLocationPermission;
43     UserHandle mUserHandle;
44     boolean mIsQApp;
45     boolean mEligibleForSanitizedExposureNotification;
46     boolean mHasNetworkSettingsPermission;
47     boolean mHasNetworkSetupWizardPermission;
48     boolean mHasScanWithoutLocationPermission;
49     boolean mHasDisavowedLocation;
50     List<String> mAssociatedDevices;
51     AppScanStats mStats = null;
52 
ScanClient(int scannerId)53     ScanClient(int scannerId) {
54         this(scannerId, DEFAULT_SCAN_SETTINGS, null);
55     }
56 
ScanClient(int scannerId, ScanSettings settings, List<ScanFilter> filters)57     ScanClient(int scannerId, ScanSettings settings, List<ScanFilter> filters) {
58         this(scannerId, settings, filters, Binder.getCallingUid());
59     }
60 
ScanClient(int scannerId, ScanSettings settings, List<ScanFilter> filters, int appUid)61     ScanClient(int scannerId, ScanSettings settings, List<ScanFilter> filters, int appUid) {
62         mScannerId = scannerId;
63         mSettings = settings;
64         mScanModeApp = settings.getScanMode();
65         mFilters = filters;
66         mAppUid = appUid;
67     }
68 
69     @Override
equals(Object obj)70     public boolean equals(Object obj) {
71         if (this == obj) {
72             return true;
73         }
74         if (!(obj instanceof ScanClient other)) {
75             return false;
76         }
77         return mScannerId == other.mScannerId;
78     }
79 
80     @Override
hashCode()81     public int hashCode() {
82         return Objects.hash(mScannerId);
83     }
84 
85     @Override
toString()86     public String toString() {
87         StringBuilder sb = new StringBuilder(" [ScanClient");
88         sb.append(" scanModeApp ")
89                 .append(mScanModeApp)
90                 .append(" scanModeUsed ")
91                 .append(mSettings.getScanMode())
92                 .append(" scannerId ")
93                 .append(mScannerId);
94 
95         if (mStats != null && mStats.mAppName != null) {
96             sb.append(" [appScanStats ").append(mStats.mAppName).append("]");
97         }
98 
99         return sb.append("]").toString();
100     }
101 
102     /**
103      * Update scan settings with the new scan mode.
104      *
105      * @return true if scan settings are updated, false otherwise.
106      */
updateScanMode(int newScanMode)107     boolean updateScanMode(int newScanMode) {
108         if (mSettings.getScanMode() == newScanMode) {
109             return false;
110         }
111 
112         mSettings =
113                 new ScanSettings.Builder()
114                         .setScanMode(newScanMode)
115                         .setCallbackType(mSettings.getCallbackType())
116                         .setScanResultType(mSettings.getScanResultType())
117                         .setReportDelay(mSettings.getReportDelayMillis())
118                         .setNumOfMatches(mSettings.getNumOfMatches())
119                         .setMatchMode(mSettings.getMatchMode())
120                         .setLegacy(mSettings.getLegacy())
121                         .setPhy(mSettings.getPhy())
122                         .build();
123         return true;
124     }
125 }
126