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

Commit 25ea2368 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Removed BatteryStatsService hidden API calls from Bluetooth" into sc-dev-plus-aosp

parents 4ecfe3df bcab2e47
Loading
Loading
Loading
Loading
+4 −10
Original line number Diff line number Diff line
@@ -68,6 +68,7 @@ import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.AsyncTask;
import android.os.BatteryStats;
import android.os.BatteryStatsManager;
import android.os.Binder;
import android.os.Bundle;
import android.os.BytesMatcher;
@@ -80,7 +81,6 @@ import android.os.PowerManager;
import android.os.RemoteCallbackList;
import android.os.RemoteException;
import android.os.ResultReceiver;
import android.os.ServiceManager;
import android.os.SystemClock;
import android.os.SystemProperties;
import android.os.UserHandle;
@@ -120,7 +120,6 @@ import com.android.bluetooth.vc.VolumeControlService;
import com.android.internal.R;
import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.app.IBatteryStats;
import com.android.internal.os.BackgroundThread;
import com.android.internal.os.BinderCallsStats;

@@ -260,7 +259,7 @@ public class AdapterService extends Service {

    private AlarmManager mAlarmManager;
    private PendingIntent mPendingAlarm;
    private IBatteryStats mBatteryStats;
    private BatteryStatsManager mBatteryStatsManager;
    private PowerManager mPowerManager;
    private PowerManager.WakeLock mWakeLock;
    private String mWakeLockName;
@@ -532,8 +531,7 @@ public class AdapterService extends Service {
        getAdapterPropertyNative(AbstractionLayer.BT_PROPERTY_CLASS_OF_DEVICE);
        mAlarmManager = getSystemService(AlarmManager.class);
        mPowerManager = getSystemService(PowerManager.class);
        mBatteryStats = IBatteryStats.Stub.asInterface(
                ServiceManager.getService(BatteryStats.SERVICE_NAME));
        mBatteryStatsManager = getSystemService(BatteryStatsManager.class);
        mCompanionDeviceManager = getSystemService(CompanionDeviceManager.class);

        mBluetoothKeystoreService.initJni();
@@ -658,11 +656,7 @@ public class AdapterService extends Service {

        mJniCallbacks.init(mBondStateMachine, mRemoteDevices);

        try {
            mBatteryStats.noteResetBleScan();
        } catch (RemoteException e) {
            Log.w(TAG, "RemoteException trying to send a reset to BatteryStats");
        }
        mBatteryStatsManager.reportBleScanReset();
        BluetoothStatsLog.write_non_chained(BluetoothStatsLog.BLE_SCAN_STATE_CHANGED, -1, null,
                BluetoothStatsLog.BLE_SCAN_STATE_CHANGED__STATE__RESET, false, false, false);

+18 −31
Original line number Diff line number Diff line
@@ -17,16 +17,14 @@ package com.android.bluetooth.gatt;

import android.bluetooth.le.ScanFilter;
import android.bluetooth.le.ScanSettings;
import android.os.BatteryStatsManager;
import android.os.Binder;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.os.SystemClock;
import android.os.WorkSource;

import com.android.bluetooth.BluetoothMetricsProto;
import com.android.bluetooth.BluetoothStatsLog;
import com.android.bluetooth.btservice.AdapterService;
import com.android.internal.app.IBatteryStats;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
@@ -55,13 +53,14 @@ import java.util.Objects;
    static final int BALANCED_WEIGHT = 25;
    static final int LOW_LATENCY_WEIGHT = 100;

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

    /* GattService is needed to add scan event protos to be dumped later */ GattService
            mGattService;
    // GattService is needed to add scan event protos to be dumped later
    GattService mGattService;

    /* Battery stats is used to keep track of scans and result stats */ IBatteryStats
            mBatteryStats;
    // Battery stats is used to keep track of scans and result stats
    BatteryStatsManager mBatteryStatsManager;

    class LastScan {
        public long duration;
@@ -120,7 +119,7 @@ import java.util.Objects;
    }

    public String appName;
    public WorkSource mWorkSource; // Used for BatteryStats and BluetoothStatsLog
    public WorkSource mWorkSource; // Used for BatteryStatsManager and BluetoothStatsLog
    private int mScansStarted = 0;
    private int mScansStopped = 0;
    public boolean isRegistered = false;
@@ -148,7 +147,7 @@ import java.util.Objects;
        appName = name;
        mContextMap = map;
        mGattService = service;
        mBatteryStats = IBatteryStats.Stub.asInterface(ServiceManager.getService("batterystats"));
        mBatteryStatsManager = service.getSystemService(BatteryStatsManager.class);

        if (source == null) {
            // Bill the caller if the work source isn't passed through
@@ -165,11 +164,7 @@ import java.util.Objects;
            // Only update battery stats after receiving 100 new results in order
            // to lower the cost of the binder transaction
            if (scan.results % 100 == 0) {
                try {
                    mBatteryStats.noteBleScanResults(mWorkSource, 100);
                } catch (RemoteException e) {
                    /* ignore */
                }
                mBatteryStatsManager.reportBleScanResults(mWorkSource, 100);
                BluetoothStatsLog.write(
                        BluetoothStatsLog.BLE_SCAN_RESULT_RECEIVED, mWorkSource, 100);
            }
@@ -241,13 +236,9 @@ import java.util.Objects;
        if (!isScanning()) {
            mScanStartTime = startTime;
        }
        try {
        boolean isUnoptimized =
                !(scan.isFilterScan || scan.isBackgroundScan || scan.isOpportunisticScan);
            mBatteryStats.noteBleScanStarted(mWorkSource, isUnoptimized);
        } catch (RemoteException e) {
            /* ignore */
        }
        mBatteryStatsManager.reportBleScanStarted(mWorkSource, isUnoptimized);
        BluetoothStatsLog.write(BluetoothStatsLog.BLE_SCAN_STATE_CHANGED, mWorkSource,
                BluetoothStatsLog.BLE_SCAN_STATE_CHANGED__STATE__ON,
                scan.isFilterScan, scan.isBackgroundScan, scan.isOpportunisticScan);
@@ -306,15 +297,11 @@ import java.util.Objects;
                break;
        }

        try {
        // Inform battery stats of any results it might be missing on scan stop
        boolean isUnoptimized =
                !(scan.isFilterScan || scan.isBackgroundScan || scan.isOpportunisticScan);
            mBatteryStats.noteBleScanResults(mWorkSource, scan.results % 100);
            mBatteryStats.noteBleScanStopped(mWorkSource, isUnoptimized);
        } catch (RemoteException e) {
            /* ignore */
        }
        mBatteryStatsManager.reportBleScanResults(mWorkSource, scan.results % 100);
        mBatteryStatsManager.reportBleScanStopped(mWorkSource, isUnoptimized);
        BluetoothStatsLog.write(
                BluetoothStatsLog.BLE_SCAN_RESULT_RECEIVED, mWorkSource, scan.results % 100);
        BluetoothStatsLog.write(BluetoothStatsLog.BLE_SCAN_STATE_CHANGED, mWorkSource,
+11 −0
Original line number Diff line number Diff line
@@ -34,6 +34,7 @@ import android.content.pm.PermissionInfo;
import android.content.res.Resources;
import android.media.AudioManager;
import android.os.AsyncTask;
import android.os.BatteryStatsManager;
import android.os.Binder;
import android.os.Bundle;
import android.os.Looper;
@@ -53,6 +54,7 @@ import androidx.test.runner.AndroidJUnit4;
import com.android.bluetooth.R;
import com.android.bluetooth.TestUtils;
import com.android.bluetooth.Utils;
import com.android.internal.app.IBatteryStats;

import libcore.util.HexEncoding;

@@ -96,6 +98,11 @@ public class AdapterServiceTest {
    private @Mock AudioManager mAudioManager;
    private @Mock android.app.Application mApplication;

    // BatteryStatsManager is final and cannot be mocked with regular mockito, so just mock the
    // underlying binder calls.
    final BatteryStatsManager mBatteryStatsManager =
            new BatteryStatsManager(mock(IBatteryStats.class));

    private static final int CONTEXT_SWITCH_MS = 100;
    private static final int PROFILE_SERVICE_TOGGLE_TIME_MS = 200;
    private static final int GATT_START_TIME_MS = 500;
@@ -183,6 +190,10 @@ public class AdapterServiceTest {
        when(mMockContext.getSystemService(Context.AUDIO_SERVICE)).thenReturn(mAudioManager);
        when(mMockContext.getSystemServiceName(AudioManager.class))
                .thenReturn(Context.AUDIO_SERVICE);
        when(mMockContext.getSystemService(Context.BATTERY_STATS_SERVICE))
                .thenReturn(mBatteryStatsManager);
        when(mMockContext.getSystemServiceName(BatteryStatsManager.class))
                .thenReturn(Context.BATTERY_STATS_SERVICE);
        when(mMockContext.getAttributionSource()).thenReturn(mAttributionSource);
        doAnswer(invocation -> {
            Object[] args = invocation.getArguments();