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

Commit 326744de authored by Hansong Zhang's avatar Hansong Zhang Committed by android-build-merger
Browse files

Merge "Scan manager: Suspend scan when location is off" into pi-dev

am: 25171dfe

Change-Id: I71505b80a24f093f42cfaf09adc73c2f218a8f63
parents 420a8878 25171dfe
Loading
Loading
Loading
Loading
+40 −4
Original line number Original line Diff line number Diff line
@@ -29,6 +29,7 @@ import android.content.Context;
import android.content.Intent;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.IntentFilter;
import android.hardware.display.DisplayManager;
import android.hardware.display.DisplayManager;
import android.location.LocationManager;
import android.os.Handler;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Looper;
@@ -102,6 +103,7 @@ public class ScanManager {
    private DisplayManager mDm;
    private DisplayManager mDm;


    private ActivityManager mActivityManager;
    private ActivityManager mActivityManager;
    private LocationManager mLocationManager;
    private static final int FOREGROUND_IMPORTANCE_CUTOFF =
    private static final int FOREGROUND_IMPORTANCE_CUTOFF =
            ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND_SERVICE;
            ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND_SERVICE;


@@ -115,8 +117,6 @@ public class ScanManager {
        }
        }
    }
    }


    ;

    ScanManager(GattService service) {
    ScanManager(GattService service) {
        mRegularScanClients =
        mRegularScanClients =
                Collections.newSetFromMap(new ConcurrentHashMap<ScanClient, Boolean>());
                Collections.newSetFromMap(new ConcurrentHashMap<ScanClient, Boolean>());
@@ -128,6 +128,7 @@ public class ScanManager {
        mCurUsedTrackableAdvertisements = 0;
        mCurUsedTrackableAdvertisements = 0;
        mDm = (DisplayManager) mService.getSystemService(Context.DISPLAY_SERVICE);
        mDm = (DisplayManager) mService.getSystemService(Context.DISPLAY_SERVICE);
        mActivityManager = (ActivityManager) mService.getSystemService(Context.ACTIVITY_SERVICE);
        mActivityManager = (ActivityManager) mService.getSystemService(Context.ACTIVITY_SERVICE);
        mLocationManager = (LocationManager) mService.getSystemService(Context.LOCATION_SERVICE);
    }
    }


    void start() {
    void start() {
@@ -141,6 +142,8 @@ public class ScanManager {
            mActivityManager.addOnUidImportanceListener(mUidImportanceListener,
            mActivityManager.addOnUidImportanceListener(mUidImportanceListener,
                    FOREGROUND_IMPORTANCE_CUTOFF);
                    FOREGROUND_IMPORTANCE_CUTOFF);
        }
        }
        IntentFilter locationIntentFilter = new IntentFilter(LocationManager.MODE_CHANGED_ACTION);
        mService.registerReceiver(mLocationReceiver, locationIntentFilter);
    }
    }


    void cleanup() {
    void cleanup() {
@@ -170,6 +173,12 @@ public class ScanManager {
            }
            }
            mHandler = null;
            mHandler = null;
        }
        }

        try {
            mService.unregisterReceiver(mLocationReceiver);
        } catch (IllegalArgumentException e) {
            Log.w(TAG, "exception when invoking unregisterReceiver(mLocationReceiver)", e);
        }
    }
    }


    void registerScanner(UUID uuid) {
    void registerScanner(UUID uuid) {
@@ -313,6 +322,17 @@ public class ScanManager {
                return;
                return;
            }
            }


            final boolean locationEnabled = mLocationManager.isLocationEnabled();
            if (!locationEnabled && !isFiltered && !client.legacyForegroundApp) {
                Log.i(TAG, "Cannot start unfiltered scan in location-off. This scan will be"
                        + " resumed when location is on: " + client.scannerId);
                mSuspendedScanClients.add(client);
                if (client.stats != null) {
                    client.stats.recordScanSuspend(client.scannerId);
                }
                return;
            }

            // Begin scan operations.
            // Begin scan operations.
            if (isBatchClient(client)) {
            if (isBatchClient(client)) {
                mBatchClients.add(client);
                mBatchClients.add(client);
@@ -396,7 +416,7 @@ public class ScanManager {
        void handleSuspendScans() {
        void handleSuspendScans() {
            for (ScanClient client : mRegularScanClients) {
            for (ScanClient client : mRegularScanClients) {
                if (!mScanNative.isOpportunisticScanClient(client) && (client.filters == null
                if (!mScanNative.isOpportunisticScanClient(client) && (client.filters == null
                        || client.filters.isEmpty())) {
                        || client.filters.isEmpty()) && !client.legacyForegroundApp) {
                    /*Suspend unfiltered scans*/
                    /*Suspend unfiltered scans*/
                    if (client.stats != null) {
                    if (client.stats != null) {
                        client.stats.recordScanSuspend(client.scannerId);
                        client.stats.recordScanSuspend(client.scannerId);
@@ -1303,7 +1323,7 @@ public class ScanManager {


                @Override
                @Override
                public void onDisplayChanged(int displayId) {
                public void onDisplayChanged(int displayId) {
                    if (isScreenOn()) {
                    if (isScreenOn() && mLocationManager.isLocationEnabled()) {
                        sendMessage(MSG_RESUME_SCANS, null);
                        sendMessage(MSG_RESUME_SCANS, null);
                    } else {
                    } else {
                        sendMessage(MSG_SUSPEND_SCANS, null);
                        sendMessage(MSG_SUSPEND_SCANS, null);
@@ -1324,6 +1344,22 @@ public class ScanManager {
                }
                }
            };
            };


    private BroadcastReceiver mLocationReceiver =
            new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    String action = intent.getAction();
                    if (LocationManager.MODE_CHANGED_ACTION.equals(action)) {
                        final boolean locationEnabled = mLocationManager.isLocationEnabled();
                        if (locationEnabled && isScreenOn()) {
                            sendMessage(MSG_RESUME_SCANS, null);
                        } else {
                            sendMessage(MSG_SUSPEND_SCANS, null);
                        }
                    }
                }
            };

    private void handleImportanceChange(UidImportance imp) {
    private void handleImportanceChange(UidImportance imp) {
        if (imp == null) {
        if (imp == null) {
            return;
            return;
+20 −0
Original line number Original line Diff line number Diff line
package com.android.bluetooth.gatt;
package com.android.bluetooth.gatt;


import static org.mockito.Mockito.*;

import android.content.Context;
import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.SmallTest;
import android.support.test.filters.SmallTest;
@@ -26,6 +28,7 @@ import org.mockito.MockitoAnnotations;
@SmallTest
@SmallTest
@RunWith(AndroidJUnit4.class)
@RunWith(AndroidJUnit4.class)
public class GattServiceTest {
public class GattServiceTest {
    private static final int TIMES_UP_AND_DOWN = 3;
    private Context mTargetContext;
    private Context mTargetContext;
    private GattService mService;
    private GattService mService;


@@ -61,6 +64,23 @@ public class GattServiceTest {
        Assert.assertNotNull(GattService.getGattService());
        Assert.assertNotNull(GattService.getGattService());
    }
    }


    @Test
    public void testServiceUpAndDown() throws Exception {
        for (int i = 0; i < TIMES_UP_AND_DOWN; i++) {
            GattService gattService = GattService.getGattService();
            TestUtils.stopService(mServiceRule, GattService.class);
            mService = GattService.getGattService();
            Assert.assertNull(mService);
            gattService.cleanup();
            TestUtils.clearAdapterService(mAdapterService);
            reset(mAdapterService);
            TestUtils.setAdapterService(mAdapterService);
            TestUtils.startService(mServiceRule, GattService.class);
            mService = GattService.getGattService();
            Assert.assertNotNull(mService);
        }
    }

    @Test
    @Test
    public void testParseBatchTimestamp() {
    public void testParseBatchTimestamp() {
        long timestampNanos = mService.parseTimestampNanos(new byte[]{
        long timestampNanos = mService.parseTimestampNanos(new byte[]{