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

Commit 31546bd8 authored by William Escande's avatar William Escande Committed by Gerrit Code Review
Browse files

Merge changes I9018aa6e,I34560be4 into main

* changes:
  OppTransfer: use final variable from constructor
  OppService: use final variable from constructor
parents 0ba20e52 31860023
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);
    }
+20 −24
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 {
@@ -901,11 +894,12 @@ public class BluetoothOppService extends ProfileService implements IObexConnecti
                    Log.v(TAG,
                            "Service create new Batch " + newBatch.mId + " for OUTBOUND info "
                                    + info.mId);
                    mTransfer = new BluetoothOppTransfer(this, newBatch);
                    mTransfer = new BluetoothOppTransfer(mAdapterService, newBatch);
                } else if (info.mDirection == BluetoothShare.DIRECTION_INBOUND) {
                    Log.v(TAG, "Service create new Batch " + newBatch.mId + " for INBOUND info "
                            + info.mId);
                    mServerTransfer = new BluetoothOppTransfer(this, newBatch, mServerSession);
                    mServerTransfer =
                            new BluetoothOppTransfer(mAdapterService, newBatch, mServerSession);
                }

                if (info.mDirection == BluetoothShare.DIRECTION_OUTBOUND && mTransfer != null) {
@@ -1138,7 +1132,7 @@ public class BluetoothOppService extends ProfileService implements IObexConnecti
                    // just finish a transfer, start pending outbound transfer
                    if (nextBatch.mDirection == BluetoothShare.DIRECTION_OUTBOUND) {
                        Log.v(TAG, "Start pending outbound batch " + nextBatch.mId);
                        mTransfer = new BluetoothOppTransfer(this, nextBatch);
                        mTransfer = new BluetoothOppTransfer(mAdapterService, nextBatch);
                        mTransfer.start();
                        return;
                    } else if (nextBatch.mDirection == BluetoothShare.DIRECTION_INBOUND
@@ -1146,7 +1140,9 @@ public class BluetoothOppService extends ProfileService implements IObexConnecti
                        // have to support pending inbound transfer
                        // if an outbound transfer and incoming socket happens together
                        Log.v(TAG, "Start pending inbound batch " + nextBatch.mId);
                        mServerTransfer = new BluetoothOppTransfer(this, nextBatch, mServerSession);
                        mServerTransfer =
                                new BluetoothOppTransfer(
                                        mAdapterService, nextBatch, mServerSession);
                        mServerTransfer.start();
                        if (nextBatch.getPendingShare() != null
                                && nextBatch.getPendingShare().mConfirm
@@ -1296,7 +1292,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);
+8 −6
Original line number Diff line number Diff line
@@ -32,6 +32,8 @@

package com.android.bluetooth.opp;

import static java.util.Objects.requireNonNull;

import android.app.NotificationManager;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
@@ -84,14 +86,14 @@ public class BluetoothOppTransfer implements BluetoothOppBatch.BluetoothOppBatch

    private static final Object INSTANCE_LOCK = new Object();

    private Context mContext;
    private final Context mContext;

    private BluetoothAdapter mAdapter;

    @VisibleForTesting
    BluetoothDevice mDevice;

    private BluetoothOppBatch mBatch;
    private final BluetoothOppBatch mBatch;

    private BluetoothOppObexSession mSession;

@@ -124,7 +126,7 @@ public class BluetoothOppTransfer implements BluetoothOppBatch.BluetoothOppBatch
                                    .BLUETOOTH_CONTENT_PROFILE_ERROR_REPORTED__TYPE__LOG_ERROR,
                            0);
                    return;
                } else if (mBatch == null || mCurrentShare == null) {
                } else if (mCurrentShare == null) {
                    Log.e(
                            TAG,
                            "device : "
@@ -228,11 +230,11 @@ public class BluetoothOppTransfer implements BluetoothOppBatch.BluetoothOppBatch

    private OppConnectionReceiver mBluetoothReceiver;

    public BluetoothOppTransfer(Context context, BluetoothOppBatch batch,
            BluetoothOppObexSession session) {
    public BluetoothOppTransfer(
            Context context, BluetoothOppBatch batch, BluetoothOppObexSession session) {

        mContext = context;
        mBatch = batch;
        mBatch = requireNonNull(batch);
        mSession = session;

        mBatch.registerListener(this);
+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)