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

Commit 8dacc5b6 authored by Ömer Faruk Yılmaz's avatar Ömer Faruk Yılmaz
Browse files

Move scan related objects from GattObjectsFactory to new ScanObjectsFactory

Bug: 344711538
Bug: 267361243
Bug: 327503826
Bug: 344711538
Flag: Exempt mechanical refactor only
Test: m com.android.btservices
Change-Id: I1ec059bca861b37481255dc5d0038cc7b79414f0
parent bc8d7d3e
Loading
Loading
Loading
Loading
+0 −34
Original line number Diff line number Diff line
@@ -16,17 +16,10 @@

package com.android.bluetooth.gatt;

import android.content.Context;
import android.os.Looper;
import android.util.Log;

import com.android.bluetooth.Utils;
import com.android.bluetooth.btservice.AdapterService;
import com.android.bluetooth.btservice.BluetoothAdapterProxy;
import com.android.bluetooth.le_scan.PeriodicScanManager;
import com.android.bluetooth.le_scan.ScanManager;
import com.android.bluetooth.le_scan.ScanNativeInterface;
import com.android.bluetooth.le_scan.TransitionalScanHelper;

/** Factory class for object initialization to help with unit testing */
public class GattObjectsFactory {
@@ -67,33 +60,6 @@ public class GattObjectsFactory {
        return GattNativeInterface.getInstance();
    }

    public ScanNativeInterface getScanNativeInterface() {
        return ScanNativeInterface.getInstance();
    }

    /**
     * Create an instance of ScanManager
     *
     * @param context a Context instance
     * @param scanHelper a TransitionalScanHelper instance
     * @param adapterService an AdapterService instance
     * @param bluetoothAdapterProxy a bluetoothAdapterProxy instance
     * @param looper the looper to be used for processing messages
     * @return the created ScanManager instance
     */
    public ScanManager createScanManager(
            Context context,
            TransitionalScanHelper scanHelper,
            AdapterService adapterService,
            BluetoothAdapterProxy bluetoothAdapterProxy,
            Looper looper) {
        return new ScanManager(context, scanHelper, adapterService, bluetoothAdapterProxy, looper);
    }

    public PeriodicScanManager createPeriodicScanManager(AdapterService adapterService) {
        return new PeriodicScanManager(adapterService);
    }

    public DistanceMeasurementManager createDistanceMeasurementManager(
            AdapterService adapterService) {
        return new DistanceMeasurementManager(adapterService);
+1 −2
Original line number Diff line number Diff line
@@ -49,7 +49,6 @@ import com.android.bluetooth.btservice.AdapterService;
import com.android.bluetooth.btservice.BluetoothAdapterProxy;
import com.android.bluetooth.flags.Flags;
import com.android.bluetooth.gatt.FilterParams;
import com.android.bluetooth.gatt.GattObjectsFactory;
import com.android.bluetooth.gatt.GattServiceConfig;
import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;
@@ -982,7 +981,7 @@ public class ScanManager {
        private ScanNativeInterface mNativeInterface;

        ScanNative(TransitionalScanHelper scanHelper) {
            mNativeInterface = GattObjectsFactory.getInstance().getScanNativeInterface();
            mNativeInterface = ScanObjectsFactory.getInstance().getScanNativeInterface();
            mNativeInterface.init(scanHelper);
            mFilterIndexStack = new ArrayDeque<Integer>();
            mClientFilterIndexMap = new HashMap<Integer, Deque<Integer>>();
+88 −0
Original line number Diff line number Diff line
/*
 * Copyright 2024 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.bluetooth.le_scan;

import android.content.Context;
import android.os.Looper;
import android.util.Log;

import com.android.bluetooth.Utils;
import com.android.bluetooth.btservice.AdapterService;
import com.android.bluetooth.btservice.BluetoothAdapterProxy;

/** Factory class for object initialization to help with unit testing */
public class ScanObjectsFactory {
    private static final String TAG = ScanObjectsFactory.class.getSimpleName();
    private static ScanObjectsFactory sInstance;
    private static final Object INSTANCE_LOCK = new Object();

    private ScanObjectsFactory() {}

    /**
     * Get the singleton instance of object factory
     *
     * @return the singleton instance, guaranteed not null
     */
    public static ScanObjectsFactory getInstance() {
        synchronized (INSTANCE_LOCK) {
            if (sInstance == null) {
                sInstance = new ScanObjectsFactory();
            }
        }
        return sInstance;
    }

    /**
     * Allow unit tests to substitute ScanObjectsFactory with a test instance
     *
     * @param objectsFactory a test instance of the ScanObjectsFactory
     */
    public static void setInstanceForTesting(ScanObjectsFactory objectsFactory) {
        Utils.enforceInstrumentationTestMode();
        synchronized (INSTANCE_LOCK) {
            Log.d(TAG, "setInstanceForTesting(), set to " + objectsFactory);
            sInstance = objectsFactory;
        }
    }

    public ScanNativeInterface getScanNativeInterface() {
        return ScanNativeInterface.getInstance();
    }

    /**
     * Create an instance of ScanManager
     *
     * @param context a Context instance
     * @param scanHelper a TransitionalScanHelper instance
     * @param adapterService an AdapterService instance
     * @param bluetoothAdapterProxy a bluetoothAdapterProxy instance
     * @param looper the looper to be used for processing messages
     * @return the created ScanManager instance
     */
    public ScanManager createScanManager(
            Context context,
            TransitionalScanHelper scanHelper,
            AdapterService adapterService,
            BluetoothAdapterProxy bluetoothAdapterProxy,
            Looper looper) {
        return new ScanManager(context, scanHelper, adapterService, bluetoothAdapterProxy, looper);
    }

    public PeriodicScanManager createPeriodicScanManager(AdapterService adapterService) {
        return new PeriodicScanManager(adapterService);
    }
}
+2 −3
Original line number Diff line number Diff line
@@ -57,7 +57,6 @@ import com.android.bluetooth.btservice.AdapterService;
import com.android.bluetooth.btservice.BluetoothAdapterProxy;
import com.android.bluetooth.flags.Flags;
import com.android.bluetooth.gatt.ContextMap;
import com.android.bluetooth.gatt.GattObjectsFactory;
import com.android.bluetooth.gatt.GattServiceConfig;
import com.android.bluetooth.util.NumberUtils;
import com.android.internal.annotations.VisibleForTesting;
@@ -197,7 +196,7 @@ public class TransitionalScanHelper {
        mCompanionManager = mContext.getSystemService(CompanionDeviceManager.class);
        mAdapterService = AdapterService.getAdapterService();
        mScanManager =
                GattObjectsFactory.getInstance()
                ScanObjectsFactory.getInstance()
                        .createScanManager(
                                mContext,
                                this,
@@ -206,7 +205,7 @@ public class TransitionalScanHelper {
                                looper);

        mPeriodicScanManager =
                GattObjectsFactory.getInstance().createPeriodicScanManager(mAdapterService);
                ScanObjectsFactory.getInstance().createPeriodicScanManager(mAdapterService);
    }

    /** Stops the scanning component. */
+11 −5
Original line number Diff line number Diff line
@@ -50,6 +50,7 @@ import com.android.bluetooth.btservice.AdapterService;
import com.android.bluetooth.btservice.CompanionManager;
import com.android.bluetooth.flags.Flags;
import com.android.bluetooth.le_scan.ScanManager;
import com.android.bluetooth.le_scan.ScanObjectsFactory;
import com.android.bluetooth.le_scan.TransitionalScanHelper;

import org.junit.After;
@@ -104,7 +105,8 @@ public class GattServiceTest {

    @Mock private Resources mResources;
    @Mock private AdapterService mAdapterService;
    @Mock private GattObjectsFactory mFactory;
    @Mock private GattObjectsFactory mGattObjectsFactory;
    @Mock private ScanObjectsFactory mScanObjectsFactory;
    @Mock private GattNativeInterface mNativeInterface;
    private BluetoothDevice mCurrentDevice;
    private CompanionManager mBtCompanionManager;
@@ -115,12 +117,15 @@ public class GattServiceTest {

        TestUtils.setAdapterService(mAdapterService);

        GattObjectsFactory.setInstanceForTesting(mFactory);
        doReturn(mNativeInterface).when(mFactory).getNativeInterface();
        doReturn(mScanManager).when(mFactory).createScanManager(any(), any(), any(), any(), any());
        GattObjectsFactory.setInstanceForTesting(mGattObjectsFactory);
        ScanObjectsFactory.setInstanceForTesting(mScanObjectsFactory);
        doReturn(mNativeInterface).when(mGattObjectsFactory).getNativeInterface();
        doReturn(mDistanceMeasurementManager)
                .when(mFactory)
                .when(mGattObjectsFactory)
                .createDistanceMeasurementManager(any());
        doReturn(mScanManager)
                .when(mScanObjectsFactory)
                .createScanManager(any(), any(), any(), any(), any());

        mAdapter = BluetoothAdapter.getDefaultAdapter();
        mAttributionSource = mAdapter.getAttributionSource();
@@ -158,6 +163,7 @@ public class GattServiceTest {

        TestUtils.clearAdapterService(mAdapterService);
        GattObjectsFactory.setInstanceForTesting(null);
        ScanObjectsFactory.setInstanceForTesting(null);
    }

    @Test
Loading