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

Commit 5ef6c686 authored by William Escande's avatar William Escande
Browse files

OppService: use final variable from constructor

Adapter is already provided in constructor as a context.
Also remove unnecessary null check on theses non nullable variable

Bug: 331948250
Test: atest BluetoothInstrumentationTests | No behavior changes
Flag: Exempt, no behavior changes
Change-Id: I34560be456541d600816a1f99f04c81babb2e2cb
parent c6394f6f
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -369,7 +369,7 @@ public class AdapterService extends Service {
    }

    @VisibleForTesting
    AdapterService(Context ctx) {
    public AdapterService(Context ctx) {
        this(Looper.getMainLooper());
        attachBaseContext(ctx);
    }
+13 −20
Original line number Diff line number Diff line
@@ -74,6 +74,7 @@ import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
@@ -136,14 +137,14 @@ public class BluetoothOppService extends ProfileService implements IObexConnecti

    private boolean mUpdateThreadRunning;

    @VisibleForTesting ArrayList<BluetoothOppShareInfo> mShares;
    @VisibleForTesting ArrayList<BluetoothOppBatch> mBatches;
    @VisibleForTesting final List<BluetoothOppShareInfo> mShares = new ArrayList<>();
    @VisibleForTesting final List<BluetoothOppBatch> mBatches = new ArrayList<>();

    private BluetoothOppTransfer mTransfer;

    private BluetoothOppTransfer mServerTransfer;

    private int mBatchId;
    private int mBatchId = 1;

    /**
     * Array used when extracting strings from content provider
@@ -166,7 +167,7 @@ public class BluetoothOppService extends ProfileService implements IObexConnecti

    boolean mAcceptNewConnections;

    private AdapterService mAdapterService;
    private final AdapterService mAdapterService;

    private static final String INVISIBLE =
            BluetoothShare.VISIBILITY + "=" + BluetoothShare.VISIBILITY_HIDDEN;
@@ -202,12 +203,10 @@ public class BluetoothOppService extends ProfileService implements IObexConnecti
     */
    private BluetoothOppObexServerSession mServerSession;

    public BluetoothOppService(Context ctx) {
        super(ctx);
    public BluetoothOppService(AdapterService adapterService) {
        super(adapterService);

        mShares = new ArrayList();
        mBatches = new ArrayList();
        mBatchId = 1;
        mAdapterService = adapterService;

        IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
        filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
@@ -262,7 +261,6 @@ public class BluetoothOppService extends ProfileService implements IObexConnecti
            }
        }.start();

        mAdapterService = AdapterService.getAdapterService();
        mObserver = new BluetoothShareContentObserver();
        getContentResolver().registerContentObserver(BluetoothShare.CONTENT_URI, true, mObserver);
        mNotifier = new BluetoothOppNotification(this);
@@ -541,16 +539,11 @@ public class BluetoothOppService extends ProfileService implements IObexConnecti
        if (!Flags.oppServiceFixIndexOutOfBoundsExceptionInUpdateThread()) {
            stopListeners();
        }
        if (mBatches != null) {

        mBatches.clear();
        }
        if (mShares != null) {
        mShares.clear();
        }
        if (mHandler != null) {
        mHandler.removeCallbacksAndMessages(null);
    }
    }

    private void unregisterReceivers() {
        try {
@@ -1296,7 +1289,7 @@ public class BluetoothOppService extends ProfileService implements IObexConnecti

    private void stopListeners() {
        SdpManagerNativeInterface nativeInterface = SdpManagerNativeInterface.getInstance();
        if (mAdapterService != null && mOppSdpHandle >= 0 && nativeInterface.isAvailable()) {
        if (mOppSdpHandle >= 0 && nativeInterface.isAvailable()) {
            Log.d(TAG, "Removing SDP record mOppSdpHandle :" + mOppSdpHandle);
            boolean status = nativeInterface.removeSdpRecord(mOppSdpHandle);
            Log.d(TAG, "RemoveSDPrecord returns " + status);
+5 −29
Original line number Diff line number Diff line
@@ -29,17 +29,12 @@ import androidx.test.filters.SmallTest;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.runner.AndroidJUnit4;

import com.android.bluetooth.BluetoothMethodProxy;
import com.android.bluetooth.TestUtils;
import com.android.bluetooth.btservice.AdapterService;
import com.android.bluetooth.flags.Flags;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;

@@ -47,31 +42,10 @@ import org.mockito.junit.MockitoRule;
@RunWith(AndroidJUnit4.class)
public class BluetoothOppServiceCleanupTest {
    @Rule public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();

    private boolean mIsAdapterServiceSet;

    private Context mTargetContext;

    @Rule public MockitoRule mockitoRule = MockitoJUnit.rule();

    @Mock private AdapterService mAdapterService;

    @Before
    public void setUp() throws Exception {
        mTargetContext = InstrumentationRegistry.getInstrumentation().getTargetContext();

        TestUtils.setAdapterService(mAdapterService);
        mIsAdapterServiceSet = true;
    }

    @After
    public void tearDown() throws Exception {
        BluetoothMethodProxy.setInstanceForTesting(null);

        if (mIsAdapterServiceSet) {
            TestUtils.clearAdapterService(mAdapterService);
        }
    }
    private final Context mTargetContext =
            InstrumentationRegistry.getInstrumentation().getTargetContext();

    @Test
    @UiThreadTest
@@ -79,6 +53,8 @@ public class BluetoothOppServiceCleanupTest {
        mSetFlagsRule.enableFlags(
                Flags.FLAG_OPP_SERVICE_FIX_INDEX_OUT_OF_BOUNDS_EXCEPTION_IN_UPDATE_THREAD);

        AdapterService adapterService = new AdapterService(mTargetContext);

        // Don't need to disable again since it will be handled in OppService.stop
        enableBtOppProvider();

@@ -89,7 +65,7 @@ public class BluetoothOppServiceCleanupTest {
        }

        try {
            BluetoothOppService service = new BluetoothOppService(mTargetContext);
            BluetoothOppService service = new BluetoothOppService(adapterService);
            service.start();
            service.setAvailable(true);

+15 −23
Original line number Diff line number Diff line
@@ -39,7 +39,6 @@ import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.runner.AndroidJUnit4;

import com.android.bluetooth.BluetoothMethodProxy;
import com.android.bluetooth.TestUtils;
import com.android.bluetooth.btservice.AdapterService;

import org.junit.After;
@@ -56,24 +55,20 @@ import org.mockito.junit.MockitoRule;
@MediumTest
@RunWith(AndroidJUnit4.class)
public class BluetoothOppServiceTest {
    private BluetoothOppService mService = null;
    private boolean mIsAdapterServiceSet;
    private boolean mIsBluetoothOppServiceStarted;

    @Rule public MockitoRule mockitoRule = MockitoJUnit.rule();

    @Mock BluetoothMethodProxy mBluetoothMethodProxy;
    @Mock private BluetoothMethodProxy mBluetoothMethodProxy;

    @Mock private AdapterService mAdapterService;
    private final Context mTargetContext =
            InstrumentationRegistry.getInstrumentation().getTargetContext();

    private BluetoothOppService mService;
    private boolean mIsBluetoothOppServiceStarted;

    @Before
    public void setUp() throws Exception {
        if (Looper.myLooper() == null) {
            Looper.prepare();
        }
        Context targetContext = InstrumentationRegistry.getInstrumentation().getTargetContext();

        BluetoothMethodProxy.setInstanceForTesting(mBluetoothMethodProxy);

        // BluetoothOppService can create a UpdateThread, which will call
        // BluetoothOppNotification#updateNotification(), which in turn create a new
        // NotificationUpdateThread. Both threads may cause the tests to fail because they try to
@@ -81,9 +76,12 @@ public class BluetoothOppServiceTest {
        // is no mocking). Since we have no intention to test those threads, avoid running them
        doNothing().when(mBluetoothMethodProxy).threadStart(any());

        TestUtils.setAdapterService(mAdapterService);
        mIsAdapterServiceSet = true;
        mService = new BluetoothOppService(targetContext);
        if (Looper.myLooper() == null) {
            Looper.prepare();
        }

        AdapterService adapterService = new AdapterService(mTargetContext);
        mService = new BluetoothOppService(adapterService);
        mService.start();
        mService.setAvailable(true);
        mIsBluetoothOppServiceStarted = true;
@@ -113,10 +111,7 @@ public class BluetoothOppServiceTest {

        BluetoothMethodProxy.setInstanceForTesting(null);
        if (mIsBluetoothOppServiceStarted) {
            mService.stop();
        }
        if (mIsAdapterServiceSet) {
            TestUtils.clearAdapterService(mAdapterService);
            service.stop();
        }
    }

@@ -193,10 +188,7 @@ public class BluetoothOppServiceTest {

    @Test
    public void trimDatabase_trimsOldOrInvisibleRecords() {
        ContentResolver contentResolver =
                InstrumentationRegistry.getInstrumentation()
                        .getTargetContext()
                        .getContentResolver();
        ContentResolver contentResolver = mTargetContext.getContentResolver();

        doReturn(1 /* any int is Ok */)
                .when(mBluetoothMethodProxy)