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

Commit 6a234990 authored by Omair Kamil's avatar Omair Kamil
Browse files

Refactor the scanner map to reduce gatt/scan dependencies.

Bug: 267361243
Bug: 327849650
Flag: EXEMPT, mechanical refactor
Test: atest BluetoothInstrumentationTests, atest BumbleBluetoothTests, atest pts-bot:GATT

Change-Id: I490cd7cae68167d0b64e4baf3951cff2c60e0c72
parent 894e3ee0
Loading
Loading
Loading
Loading
+10 −108
Original line number Original line Diff line number Diff line
@@ -24,15 +24,10 @@ import android.os.IBinder;
import android.os.IInterface;
import android.os.IInterface;
import android.os.RemoteException;
import android.os.RemoteException;
import android.os.SystemClock;
import android.os.SystemClock;
import android.os.UserHandle;
import android.os.WorkSource;
import android.util.Log;
import android.util.Log;


import com.android.bluetooth.BluetoothMethodProxy;
import com.android.bluetooth.BluetoothMethodProxy;
import com.android.bluetooth.flags.Flags;
import com.android.bluetooth.flags.Flags;
import com.android.bluetooth.le_scan.AppScanStats;
import com.android.bluetooth.le_scan.TransitionalScanHelper;
import com.android.bluetooth.le_scan.TransitionalScanHelper.PendingIntentInfo;
import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.GuardedBy;


import com.google.common.collect.EvictingQueue;
import com.google.common.collect.EvictingQueue;
@@ -51,8 +46,10 @@ import java.util.function.Predicate;
/**
/**
 * Helper class that keeps track of registered GATT applications. This class manages application
 * Helper class that keeps track of registered GATT applications. This class manages application
 * callbacks and keeps track of GATT connections.
 * callbacks and keeps track of GATT connections.
 *
 * @param <C> the callback type for this map
 */
 */
public class ContextMap<C, T> {
public class ContextMap<C> {
    private static final String TAG = GattServiceConfig.TAG_PREFIX + "ContextMap";
    private static final String TAG = GattServiceConfig.TAG_PREFIX + "ContextMap";


    /** Connection class helps map connection IDs to device addresses. */
    /** Connection class helps map connection IDs to device addresses. */
@@ -81,56 +78,23 @@ public class ContextMap<C, T> {
        /** The package name of the application */
        /** The package name of the application */
        public String name;
        public String name;


        /** Statistics for this app */
        public AppScanStats appScanStats;

        /** Application callbacks */
        /** Application callbacks */
        public C callback;
        public C callback;


        /** Context information */
        public T info;

        /** Death recipient */
        /** Death recipient */
        private IBinder.DeathRecipient mDeathRecipient;
        private IBinder.DeathRecipient mDeathRecipient;


        /** Flag to signal that transport is congested */
        /** Flag to signal that transport is congested */
        public Boolean isCongested = false;
        public Boolean isCongested = false;


        /** Whether the calling app has location permission */
        public boolean hasLocationPermission;

        /** Whether the calling app has bluetooth privileged permission */
        public boolean hasBluetoothPrivilegedPermission;

        /** The user handle of the app that started the scan */
        public UserHandle mUserHandle;

        /** Whether the calling app has the network settings permission */
        public boolean mHasNetworkSettingsPermission;

        /** Whether the calling app has the network setup wizard permission */
        public boolean mHasNetworkSetupWizardPermission;

        /** Whether the calling app has the network setup wizard permission */
        public boolean mHasScanWithoutLocationPermission;

        /** Whether the calling app has disavowed the use of bluetooth for location */
        public boolean mHasDisavowedLocation;

        public boolean mEligibleForSanitizedExposureNotification;

        public List<String> mAssociatedDevices;

        /** Internal callback info queue, waiting to be send on congestion clear */
        /** Internal callback info queue, waiting to be send on congestion clear */
        private List<CallbackInfo> mCongestionQueue = new ArrayList<CallbackInfo>();
        private List<CallbackInfo> mCongestionQueue = new ArrayList<CallbackInfo>();


        /** Creates a new app context. */
        /** Creates a new app context. */
        App(UUID uuid, C callback, T info, String name, AppScanStats appScanStats) {
        App(UUID uuid, C callback, String name) {
            this.uuid = uuid;
            this.uuid = uuid;
            this.callback = callback;
            this.callback = callback;
            this.info = info;
            this.name = name;
            this.name = name;
            this.appScanStats = appScanStats;
        }
        }


        /** Creates a new app context for advertiser. */
        /** Creates a new app context for advertiser. */
@@ -185,9 +149,6 @@ public class ContextMap<C, T> {
    @GuardedBy("mAppsLock")
    @GuardedBy("mAppsLock")
    private List<App> mApps = new ArrayList<App>();
    private List<App> mApps = new ArrayList<App>();


    /** Internal map to keep track of logging information by app name */
    private HashMap<Integer, AppScanStats> mAppScanStats = new HashMap<Integer, AppScanStats>();

    /** Internal map to keep track of logging information by advertise id */
    /** Internal map to keep track of logging information by advertise id */
    private final Map<Integer, AppAdvertiseStats> mAppAdvertiseStats =
    private final Map<Integer, AppAdvertiseStats> mAppAdvertiseStats =
            new HashMap<Integer, AppAdvertiseStats>();
            new HashMap<Integer, AppAdvertiseStats>();
@@ -203,37 +164,16 @@ public class ContextMap<C, T> {
    private final Object mConnectionsLock = new Object();
    private final Object mConnectionsLock = new Object();


    /** Add an entry to the application context list. */
    /** Add an entry to the application context list. */
    public App add(
    public App add(UUID uuid, C callback, Context context) {
            UUID uuid,
        int appUid = Binder.getCallingUid();
            WorkSource workSource,
        String appName = context.getPackageManager().getNameForUid(appUid);
            C callback,
            PendingIntentInfo piInfo,
            Context context,
            TransitionalScanHelper scanHelper) {
        int appUid;
        String appName = null;
        if (piInfo != null) {
            appUid = piInfo.callingUid;
            appName = piInfo.callingPackage;
        } else {
            appUid = Binder.getCallingUid();
            appName = context.getPackageManager().getNameForUid(appUid);
        }
        if (appName == null) {
        if (appName == null) {
            // Assign an app name if one isn't found
            // Assign an app name if one isn't found
            appName = "Unknown App (UID: " + appUid + ")";
            appName = "Unknown App (UID: " + appUid + ")";
        }
        }
        synchronized (mAppsLock) {
        synchronized (mAppsLock) {
            // TODO(b/327849650): AppScanStats appears to be only needed for the ScannerMap.
            App app = new App(uuid, callback, appName);
            //                    Consider refactoring this.
            AppScanStats appScanStats = mAppScanStats.get(appUid);
            if (appScanStats == null) {
                appScanStats = new AppScanStats(appName, workSource, this, context, scanHelper);
                mAppScanStats.put(appUid, appScanStats);
            }
            App app = new App(uuid, callback, (T) piInfo, appName, appScanStats);
            mApps.add(app);
            mApps.add(app);
            appScanStats.isRegistered = true;
            return app;
            return app;
        }
        }
    }
    }
@@ -273,7 +213,6 @@ public class ContextMap<C, T> {
                App entry = i.next();
                App entry = i.next();
                if (entry.uuid.equals(uuid)) {
                if (entry.uuid.equals(uuid)) {
                    entry.unlinkToDeath();
                    entry.unlinkToDeath();
                    entry.appScanStats.isRegistered = false;
                    i.remove();
                    i.remove();
                    break;
                    break;
                }
                }
@@ -291,7 +230,6 @@ public class ContextMap<C, T> {
                if (entry.id == id) {
                if (entry.id == id) {
                    find = true;
                    find = true;
                    entry.unlinkToDeath();
                    entry.unlinkToDeath();
                    entry.appScanStats.isRegistered = false;
                    i.remove();
                    i.remove();
                    break;
                    break;
                }
                }
@@ -377,38 +315,6 @@ public class ContextMap<C, T> {
        return app;
        return app;
    }
    }


    /** Get an application context by the calling Apps name. */
    public App getByName(String name) {
        App app = getAppByPredicate(entry -> entry.name.equals(name));
        if (app == null) {
            Log.e(TAG, "Context not found for name " + name);
        }
        return app;
    }

    /** Get an application context by the context info object. */
    public App getByContextInfo(T contextInfo) {
        App app = getAppByPredicate(entry -> entry.info != null && entry.info.equals(contextInfo));
        if (app == null) {
            Log.e(TAG, "Context not found for info " + contextInfo);
        }
        return app;
    }

    /** Get Logging info by ID */
    public AppScanStats getAppScanStatsById(int id) {
        App temp = getById(id);
        if (temp != null) {
            return temp.appScanStats;
        }
        return null;
    }

    /** Get Logging info by application UID */
    public AppScanStats getAppScanStatsByUid(int uid) {
        return mAppScanStats.get(uid);
    }

    /** Remove the context for a given application ID. */
    /** Remove the context for a given application ID. */
    void removeAppAdvertiseStats(int id) {
    void removeAppAdvertiseStats(int id) {
        synchronized (this) {
        synchronized (this) {
@@ -621,9 +527,6 @@ public class ContextMap<C, T> {
        synchronized (mAppsLock) {
        synchronized (mAppsLock) {
            for (App entry : mApps) {
            for (App entry : mApps) {
                entry.unlinkToDeath();
                entry.unlinkToDeath();
                if (entry.appScanStats != null) {
                    entry.appScanStats.isRegistered = false;
                }
            }
            }
            mApps.clear();
            mApps.clear();
        }
        }
@@ -651,9 +554,8 @@ public class ContextMap<C, T> {


    /** Logs debug information. */
    /** Logs debug information. */
    protected void dump(StringBuilder sb) {
    protected void dump(StringBuilder sb) {
        sb.append("  Entries: " + mAppScanStats.size() + "\n\n");
        synchronized (mAppsLock) {
        for (AppScanStats appScanStats : mAppScanStats.values()) {
            sb.append("  Entries: " + mApps.size() + "\n\n");
            appScanStats.dumpToString(sb);
        }
        }
    }
    }


+39 −46
Original line number Original line Diff line number Diff line
@@ -144,17 +144,17 @@ public class GattService extends ProfileService {
    private static GattService sGattService;
    private static GattService sGattService;


    /** List of our registered advertisers. */
    /** List of our registered advertisers. */
    static class AdvertiserMap extends ContextMap<IAdvertisingSetCallback, Void> {}
    static class AdvertiserMap extends ContextMap<IAdvertisingSetCallback> {}


    private AdvertiserMap mAdvertiserMap = new AdvertiserMap();
    private AdvertiserMap mAdvertiserMap = new AdvertiserMap();


    /** List of our registered clients. */
    /** List of our registered clients. */
    static class ClientMap extends ContextMap<IBluetoothGattCallback, Void> {}
    static class ClientMap extends ContextMap<IBluetoothGattCallback> {}


    ClientMap mClientMap = new ClientMap();
    ClientMap mClientMap = new ClientMap();


    /** List of our registered server apps. */
    /** List of our registered server apps. */
    static class ServerMap extends ContextMap<IBluetoothGattServerCallback, Void> {}
    static class ServerMap extends ContextMap<IBluetoothGattServerCallback> {}


    ServerMap mServerMap = new ServerMap();
    ServerMap mServerMap = new ServerMap();


@@ -1291,7 +1291,7 @@ public class GattService extends ProfileService {
            throws RemoteException {
            throws RemoteException {
        UUID uuid = new UUID(uuidMsb, uuidLsb);
        UUID uuid = new UUID(uuidMsb, uuidLsb);
        Log.d(TAG, "onClientRegistered() - UUID=" + uuid + ", clientIf=" + clientIf);
        Log.d(TAG, "onClientRegistered() - UUID=" + uuid + ", clientIf=" + clientIf);
        ContextMap<IBluetoothGattCallback, Void>.App app = mClientMap.getByUuid(uuid);
        ContextMap<IBluetoothGattCallback>.App app = mClientMap.getByUuid(uuid);
        if (app != null) {
        if (app != null) {
            if (status == 0) {
            if (status == 0) {
                app.id = clientIf;
                app.id = clientIf;
@@ -1323,7 +1323,7 @@ public class GattService extends ProfileService {
            }
            }
            connectionState = BluetoothProtoEnums.CONNECTION_STATE_CONNECTED;
            connectionState = BluetoothProtoEnums.CONNECTION_STATE_CONNECTED;
        }
        }
        ContextMap<IBluetoothGattCallback, Void>.App app = mClientMap.getById(clientIf);
        ContextMap<IBluetoothGattCallback>.App app = mClientMap.getById(clientIf);
        if (app != null) {
        if (app != null) {
            app.callback.onClientConnectionState(
            app.callback.onClientConnectionState(
                    status, clientIf, (status == BluetoothGatt.GATT_SUCCESS), address);
                    status, clientIf, (status == BluetoothGatt.GATT_SUCCESS), address);
@@ -1344,7 +1344,7 @@ public class GattService extends ProfileService {
                        + address);
                        + address);


        mClientMap.removeConnection(clientIf, connId);
        mClientMap.removeConnection(clientIf, connId);
        ContextMap<IBluetoothGattCallback, Void>.App app = mClientMap.getById(clientIf);
        ContextMap<IBluetoothGattCallback>.App app = mClientMap.getById(clientIf);


        if (Flags.gattCleanupRestrictedHandles()) {
        if (Flags.gattCleanupRestrictedHandles()) {
            mRestrictedHandles.remove(connId);
            mRestrictedHandles.remove(connId);
@@ -1385,7 +1385,7 @@ public class GattService extends ProfileService {
            return;
            return;
        }
        }


        ContextMap<IBluetoothGattCallback, Void>.App app = mClientMap.getByConnId(connId);
        ContextMap<IBluetoothGattCallback>.App app = mClientMap.getByConnId(connId);
        if (app == null) {
        if (app == null) {
            return;
            return;
        }
        }
@@ -1410,7 +1410,7 @@ public class GattService extends ProfileService {
            return;
            return;
        }
        }


        ContextMap<IBluetoothGattCallback, Void>.App app = mClientMap.getByConnId(connId);
        ContextMap<IBluetoothGattCallback>.App app = mClientMap.getByConnId(connId);
        if (app == null) {
        if (app == null) {
            return;
            return;
        }
        }
@@ -1427,7 +1427,7 @@ public class GattService extends ProfileService {
            return;
            return;
        }
        }


        ContextMap<IBluetoothGattCallback, Void>.App app = mClientMap.getByConnId(connId);
        ContextMap<IBluetoothGattCallback>.App app = mClientMap.getByConnId(connId);
        if (app == null) {
        if (app == null) {
            return;
            return;
        }
        }
@@ -1443,7 +1443,7 @@ public class GattService extends ProfileService {
            return;
            return;
        }
        }


        ContextMap<IBluetoothGattCallback, Void>.App app = mClientMap.getByConnId(connId);
        ContextMap<IBluetoothGattCallback>.App app = mClientMap.getByConnId(connId);
        if (app == null) {
        if (app == null) {
            return;
            return;
        }
        }
@@ -1461,7 +1461,7 @@ public class GattService extends ProfileService {
            return;
            return;
        }
        }


        ContextMap<IBluetoothGattCallback, Void>.App app = mClientMap.getByConnId(connId);
        ContextMap<IBluetoothGattCallback>.App app = mClientMap.getByConnId(connId);
        if (app == null) {
        if (app == null) {
            return;
            return;
        }
        }
@@ -1477,7 +1477,7 @@ public class GattService extends ProfileService {
            return;
            return;
        }
        }


        ContextMap<IBluetoothGattServerCallback, Void>.App app = mServerMap.getByConnId(connId);
        ContextMap<IBluetoothGattServerCallback>.App app = mServerMap.getByConnId(connId);
        if (app == null) {
        if (app == null) {
            return;
            return;
        }
        }
@@ -1495,7 +1495,7 @@ public class GattService extends ProfileService {
            return;
            return;
        }
        }


        ContextMap<IBluetoothGattServerCallback, Void>.App app = mServerMap.getByConnId(connId);
        ContextMap<IBluetoothGattServerCallback>.App app = mServerMap.getByConnId(connId);
        if (app == null) {
        if (app == null) {
            return;
            return;
        }
        }
@@ -1512,7 +1512,7 @@ public class GattService extends ProfileService {
            return;
            return;
        }
        }


        ContextMap<IBluetoothGattServerCallback, Void>.App app = mServerMap.getByConnId(connId);
        ContextMap<IBluetoothGattServerCallback>.App app = mServerMap.getByConnId(connId);
        if (app == null) {
        if (app == null) {
            return;
            return;
        }
        }
@@ -1530,7 +1530,7 @@ public class GattService extends ProfileService {
            return;
            return;
        }
        }


        ContextMap<IBluetoothGattServerCallback, Void>.App app = mServerMap.getByConnId(connId);
        ContextMap<IBluetoothGattServerCallback>.App app = mServerMap.getByConnId(connId);
        if (app == null) {
        if (app == null) {
            return;
            return;
        }
        }
@@ -1564,7 +1564,7 @@ public class GattService extends ProfileService {


        Log.d(TAG, "onGetGattDb() - address=" + address);
        Log.d(TAG, "onGetGattDb() - address=" + address);


        ContextMap<IBluetoothGattCallback, Void>.App app = mClientMap.getByConnId(connId);
        ContextMap<IBluetoothGattCallback>.App app = mClientMap.getByConnId(connId);
        if (app == null || app.callback == null) {
        if (app == null || app.callback == null) {
            Log.e(TAG, "app or callback is null");
            Log.e(TAG, "app or callback is null");
            return;
            return;
@@ -1674,7 +1674,7 @@ public class GattService extends ProfileService {
                        + ", length="
                        + ", length="
                        + data.length);
                        + data.length);


        ContextMap<IBluetoothGattCallback, Void>.App app = mClientMap.getByConnId(connId);
        ContextMap<IBluetoothGattCallback>.App app = mClientMap.getByConnId(connId);
        if (app != null) {
        if (app != null) {
            try {
            try {
                permissionCheck(connId, handle);
                permissionCheck(connId, handle);
@@ -1703,7 +1703,7 @@ public class GattService extends ProfileService {
                        + ", length="
                        + ", length="
                        + data.length);
                        + data.length);


        ContextMap<IBluetoothGattCallback, Void>.App app = mClientMap.getByConnId(connId);
        ContextMap<IBluetoothGattCallback>.App app = mClientMap.getByConnId(connId);
        if (app != null) {
        if (app != null) {
            app.callback.onCharacteristicRead(address, status, handle, data);
            app.callback.onCharacteristicRead(address, status, handle, data);
        }
        }
@@ -1726,7 +1726,7 @@ public class GattService extends ProfileService {
                        + ", length="
                        + ", length="
                        + data.length);
                        + data.length);


        ContextMap<IBluetoothGattCallback, Void>.App app = mClientMap.getByConnId(connId);
        ContextMap<IBluetoothGattCallback>.App app = mClientMap.getByConnId(connId);
        if (app == null) {
        if (app == null) {
            return;
            return;
        }
        }
@@ -1750,7 +1750,7 @@ public class GattService extends ProfileService {
        String address = mClientMap.addressByConnId(connId);
        String address = mClientMap.addressByConnId(connId);
        Log.v(TAG, "onExecuteCompleted() - address=" + address + ", status=" + status);
        Log.v(TAG, "onExecuteCompleted() - address=" + address + ", status=" + status);


        ContextMap<IBluetoothGattCallback, Void>.App app = mClientMap.getByConnId(connId);
        ContextMap<IBluetoothGattCallback>.App app = mClientMap.getByConnId(connId);
        if (app != null) {
        if (app != null) {
            app.callback.onExecuteWrite(address, status);
            app.callback.onExecuteWrite(address, status);
        }
        }
@@ -1768,7 +1768,7 @@ public class GattService extends ProfileService {
                        + ", length="
                        + ", length="
                        + data.length);
                        + data.length);


        ContextMap<IBluetoothGattCallback, Void>.App app = mClientMap.getByConnId(connId);
        ContextMap<IBluetoothGattCallback>.App app = mClientMap.getByConnId(connId);
        if (app != null) {
        if (app != null) {
            app.callback.onDescriptorRead(address, status, handle, data);
            app.callback.onDescriptorRead(address, status, handle, data);
        }
        }
@@ -1786,7 +1786,7 @@ public class GattService extends ProfileService {
                        + ", length="
                        + ", length="
                        + data.length);
                        + data.length);


        ContextMap<IBluetoothGattCallback, Void>.App app = mClientMap.getByConnId(connId);
        ContextMap<IBluetoothGattCallback>.App app = mClientMap.getByConnId(connId);
        if (app != null) {
        if (app != null) {
            app.callback.onDescriptorWrite(address, status, handle, data);
            app.callback.onDescriptorWrite(address, status, handle, data);
        }
        }
@@ -1805,7 +1805,7 @@ public class GattService extends ProfileService {
                        + ", status="
                        + ", status="
                        + status);
                        + status);


        ContextMap<IBluetoothGattCallback, Void>.App app = mClientMap.getById(clientIf);
        ContextMap<IBluetoothGattCallback>.App app = mClientMap.getById(clientIf);
        if (app != null) {
        if (app != null) {
            app.callback.onReadRemoteRssi(address, rssi, status);
            app.callback.onReadRemoteRssi(address, rssi, status);
        }
        }
@@ -1816,7 +1816,7 @@ public class GattService extends ProfileService {


        Log.d(TAG, "onConfigureMTU() address=" + address + ", status=" + status + ", mtu=" + mtu);
        Log.d(TAG, "onConfigureMTU() address=" + address + ", status=" + status + ", mtu=" + mtu);


        ContextMap<IBluetoothGattCallback, Void>.App app = mClientMap.getByConnId(connId);
        ContextMap<IBluetoothGattCallback>.App app = mClientMap.getByConnId(connId);
        if (app != null) {
        if (app != null) {
            app.callback.onConfigureMTU(address, mtu, status);
            app.callback.onConfigureMTU(address, mtu, status);
        }
        }
@@ -1825,7 +1825,7 @@ public class GattService extends ProfileService {
    void onClientCongestion(int connId, boolean congested) throws RemoteException {
    void onClientCongestion(int connId, boolean congested) throws RemoteException {
        Log.v(TAG, "onClientCongestion() - connId=" + connId + ", congested=" + congested);
        Log.v(TAG, "onClientCongestion() - connId=" + connId + ", congested=" + congested);


        ContextMap<IBluetoothGattCallback, Void>.App app = mClientMap.getByConnId(connId);
        ContextMap<IBluetoothGattCallback>.App app = mClientMap.getByConnId(connId);


        if (app != null) {
        if (app != null) {
            app.isCongested = congested;
            app.isCongested = congested;
@@ -2094,7 +2094,7 @@ public class GattService extends ProfileService {
        }
        }


        Log.d(TAG, "registerClient() - UUID=" + uuid);
        Log.d(TAG, "registerClient() - UUID=" + uuid);
        mClientMap.add(uuid, null, callback, null, this, mTransitionalScanHelper);
        mClientMap.add(uuid, callback, this);
        mNativeInterface.gattClientRegisterApp(
        mNativeInterface.gattClientRegisterApp(
                uuid.getLeastSignificantBits(), uuid.getMostSignificantBits(), eatt_support);
                uuid.getLeastSignificantBits(), uuid.getMostSignificantBits(), eatt_support);
    }
    }
@@ -2769,7 +2769,7 @@ public class GattService extends ProfileService {


        UUID uuid = new UUID(uuidMsb, uuidLsb);
        UUID uuid = new UUID(uuidMsb, uuidLsb);
        Log.d(TAG, "onServerRegistered() - UUID=" + uuid + ", serverIf=" + serverIf);
        Log.d(TAG, "onServerRegistered() - UUID=" + uuid + ", serverIf=" + serverIf);
        ContextMap<IBluetoothGattServerCallback, Void>.App app = mServerMap.getByUuid(uuid);
        ContextMap<IBluetoothGattServerCallback>.App app = mServerMap.getByUuid(uuid);
        if (app != null) {
        if (app != null) {
            app.id = serverIf;
            app.id = serverIf;
            app.linkToDeath(new ServerDeathRecipient(serverIf, app.name));
            app.linkToDeath(new ServerDeathRecipient(serverIf, app.name));
@@ -2833,7 +2833,7 @@ public class GattService extends ProfileService {
        }
        }
        mHandleMap.setStarted(serverIf, srvcHandle, true);
        mHandleMap.setStarted(serverIf, srvcHandle, true);


        ContextMap<IBluetoothGattServerCallback, Void>.App app = mServerMap.getById(serverIf);
        ContextMap<IBluetoothGattServerCallback>.App app = mServerMap.getById(serverIf);
        if (app != null) {
        if (app != null) {
            app.callback.onServiceAdded(status, svc);
            app.callback.onServiceAdded(status, svc);
        }
        }
@@ -2864,7 +2864,7 @@ public class GattService extends ProfileService {
                        + ", connected="
                        + ", connected="
                        + connected);
                        + connected);


        ContextMap<IBluetoothGattServerCallback, Void>.App app = mServerMap.getById(serverIf);
        ContextMap<IBluetoothGattServerCallback>.App app = mServerMap.getById(serverIf);
        if (app == null) {
        if (app == null) {
            return;
            return;
        }
        }
@@ -2916,7 +2916,7 @@ public class GattService extends ProfileService {


        mHandleMap.addRequest(transId, handle);
        mHandleMap.addRequest(transId, handle);


        ContextMap<IBluetoothGattServerCallback, Void>.App app = mServerMap.getById(entry.serverIf);
        ContextMap<IBluetoothGattServerCallback>.App app = mServerMap.getById(entry.serverIf);
        if (app == null) {
        if (app == null) {
            return;
            return;
        }
        }
@@ -2947,7 +2947,7 @@ public class GattService extends ProfileService {


        mHandleMap.addRequest(transId, handle);
        mHandleMap.addRequest(transId, handle);


        ContextMap<IBluetoothGattServerCallback, Void>.App app = mServerMap.getById(entry.serverIf);
        ContextMap<IBluetoothGattServerCallback>.App app = mServerMap.getById(entry.serverIf);
        if (app == null) {
        if (app == null) {
            return;
            return;
        }
        }
@@ -2988,7 +2988,7 @@ public class GattService extends ProfileService {


        mHandleMap.addRequest(transId, handle);
        mHandleMap.addRequest(transId, handle);


        ContextMap<IBluetoothGattServerCallback, Void>.App app = mServerMap.getById(entry.serverIf);
        ContextMap<IBluetoothGattServerCallback>.App app = mServerMap.getById(entry.serverIf);
        if (app == null) {
        if (app == null) {
            return;
            return;
        }
        }
@@ -3030,7 +3030,7 @@ public class GattService extends ProfileService {


        mHandleMap.addRequest(transId, handle);
        mHandleMap.addRequest(transId, handle);


        ContextMap<IBluetoothGattServerCallback, Void>.App app = mServerMap.getById(entry.serverIf);
        ContextMap<IBluetoothGattServerCallback>.App app = mServerMap.getById(entry.serverIf);
        if (app == null) {
        if (app == null) {
            return;
            return;
        }
        }
@@ -3050,7 +3050,7 @@ public class GattService extends ProfileService {
                        + ", transId="
                        + ", transId="
                        + transId);
                        + transId);


        ContextMap<IBluetoothGattServerCallback, Void>.App app = mServerMap.getByConnId(connId);
        ContextMap<IBluetoothGattServerCallback>.App app = mServerMap.getByConnId(connId);
        if (app == null) {
        if (app == null) {
            return;
            return;
        }
        }
@@ -3070,7 +3070,7 @@ public class GattService extends ProfileService {
            return;
            return;
        }
        }


        ContextMap<IBluetoothGattServerCallback, Void>.App app = mServerMap.getByConnId(connId);
        ContextMap<IBluetoothGattServerCallback>.App app = mServerMap.getByConnId(connId);
        if (app == null) {
        if (app == null) {
            return;
            return;
        }
        }
@@ -3088,7 +3088,7 @@ public class GattService extends ProfileService {
    void onServerCongestion(int connId, boolean congested) throws RemoteException {
    void onServerCongestion(int connId, boolean congested) throws RemoteException {
        Log.d(TAG, "onServerCongestion() - connId=" + connId + ", congested=" + congested);
        Log.d(TAG, "onServerCongestion() - connId=" + connId + ", congested=" + congested);


        ContextMap<IBluetoothGattServerCallback, Void>.App app = mServerMap.getByConnId(connId);
        ContextMap<IBluetoothGattServerCallback>.App app = mServerMap.getByConnId(connId);
        if (app == null) {
        if (app == null) {
            return;
            return;
        }
        }
@@ -3111,7 +3111,7 @@ public class GattService extends ProfileService {
            return;
            return;
        }
        }


        ContextMap<IBluetoothGattServerCallback, Void>.App app = mServerMap.getByConnId(connId);
        ContextMap<IBluetoothGattServerCallback>.App app = mServerMap.getByConnId(connId);
        if (app == null) {
        if (app == null) {
            return;
            return;
        }
        }
@@ -3135,7 +3135,7 @@ public class GattService extends ProfileService {
        }
        }


        Log.d(TAG, "registerServer() - UUID=" + uuid);
        Log.d(TAG, "registerServer() - UUID=" + uuid);
        mServerMap.add(uuid, null, callback, null, this, mTransitionalScanHelper);
        mServerMap.add(uuid, callback, this);
        mNativeInterface.gattServerRegisterApp(
        mNativeInterface.gattServerRegisterApp(
                uuid.getLeastSignificantBits(), uuid.getMostSignificantBits(), eatt_support);
                uuid.getLeastSignificantBits(), uuid.getMostSignificantBits(), eatt_support);
    }
    }
@@ -3504,14 +3504,7 @@ public class GattService extends ProfileService {


    void dumpRegisterId(StringBuilder sb) {
    void dumpRegisterId(StringBuilder sb) {
        sb.append("  Scanner:\n");
        sb.append("  Scanner:\n");
        for (Integer appId : mTransitionalScanHelper.getScannerMap().getAllAppsIds()) {
        mTransitionalScanHelper.getScannerMap().dumpApps(sb, ProfileService::println);
            println(
                    sb,
                    "    app_if: "
                            + appId
                            + ", appName: "
                            + mTransitionalScanHelper.getScannerMap().getById(appId).name);
        }
        sb.append("  Client:\n");
        sb.append("  Client:\n");
        for (Integer appId : mClientMap.getAllAppsIds()) {
        for (Integer appId : mClientMap.getAllAppsIds()) {
            println(sb, "    app_if: " + appId + ", appName: " + mClientMap.getById(appId).name);
            println(sb, "    app_if: " + appId + ", appName: " + mClientMap.getById(appId).name);
+7 −23
Original line number Original line Diff line number Diff line
@@ -30,7 +30,6 @@ import com.android.bluetooth.BluetoothStatsLog;
import com.android.bluetooth.btservice.AdapterService;
import com.android.bluetooth.btservice.AdapterService;
import com.android.bluetooth.btservice.MetricsLogger;
import com.android.bluetooth.btservice.MetricsLogger;
import com.android.bluetooth.flags.Flags;
import com.android.bluetooth.flags.Flags;
import com.android.bluetooth.gatt.ContextMap;
import com.android.bluetooth.util.WorkSourceUtil;
import com.android.bluetooth.util.WorkSourceUtil;
import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.annotations.VisibleForTesting;
@@ -41,7 +40,6 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.Arrays;
import java.util.Date;
import java.util.Date;
import java.util.HashMap;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.List;
import java.util.Objects;
import java.util.Objects;


@@ -61,8 +59,8 @@ public class AppScanStats {


    static final int LARGE_SCAN_TIME_GAP_MS = 24000;
    static final int LARGE_SCAN_TIME_GAP_MS = 24000;


    // ContextMap here is needed to grab Apps and Connections
    // ScannerMap here is needed to grab Apps
    ContextMap mContextMap;
    ScannerMap mScannerMap;


    // TransitionalScanHelper is needed to add scan event protos to be dumped later
    // TransitionalScanHelper is needed to add scan event protos to be dumped later
    final TransitionalScanHelper mScanHelper;
    final TransitionalScanHelper mScanHelper;
@@ -166,11 +164,11 @@ public class AppScanStats {
    public AppScanStats(
    public AppScanStats(
            String name,
            String name,
            WorkSource source,
            WorkSource source,
            ContextMap map,
            ScannerMap map,
            Context context,
            Context context,
            TransitionalScanHelper scanHelper) {
            TransitionalScanHelper scanHelper) {
        appName = name;
        appName = name;
        mContextMap = map;
        mScannerMap = map;
        mScanHelper = scanHelper;
        mScanHelper = scanHelper;
        mBatteryStatsManager = context.getSystemService(BatteryStatsManager.class);
        mBatteryStatsManager = context.getSystemService(BatteryStatsManager.class);


@@ -1132,24 +1130,10 @@ public class AppScanStats {
            }
            }
        }
        }


        ContextMap.App appEntry = mContextMap.getByName(appName);
        ScannerMap.ScannerApp appEntry = mScannerMap.getByName(appName);
        if (appEntry != null && isRegistered) {
        if (appEntry != null && isRegistered) {
            sb.append("\n  Application ID                     : " + appEntry.id);
            sb.append("\n  Application ID                     : " + appEntry.mId);
            sb.append("\n  UUID                               : " + appEntry.uuid);
            sb.append("\n  UUID                               : " + appEntry.mUuid);

            List<ContextMap.Connection> connections = mContextMap.getConnectionByApp(appEntry.id);

            sb.append("\n  Connections: " + connections.size());

            Iterator<ContextMap.Connection> ii = connections.iterator();
            while (ii.hasNext()) {
                ContextMap.Connection connection = ii.next();
                long connectionTime = currTime - connection.startTime;
                Date timestamp = new Date(currentTime - currTime + connection.startTime);
                sb.append("\n    " + DATE_FORMAT.format(timestamp) + " - ");
                sb.append((connectionTime) + "ms ");
                sb.append(": " + connection.address + " (" + connection.connId + ")");
            }
        }
        }
        sb.append("\n\n");
        sb.append("\n\n");
    }
    }
+280 −0

File added.

Preview size limit exceeded, changes collapsed.

+54 −65

File changed.

Preview size limit exceeded, changes collapsed.

Loading