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

Commit aefa05ed authored by Dave Mankoff's avatar Dave Mankoff
Browse files

Remove call to Dependency.get() from BatteryControllerImpl.

This specifically targets Dependendy.get(BG_HANDLER) and
Dependendy.get(MAIN_HANDLER), though some other related refactoring
is undertaken along the way.

Bug: 141882822
Test: atest SystemUITests
Change-Id: I93b70534c262bff87333c0491e0def2ca2db8ac8
parent 76939b7e
Loading
Loading
Loading
Loading
+0 −2
Original line number Diff line number Diff line
@@ -103,14 +103,12 @@ public class DependencyProvider {
        return Looper.getMainLooper();
    }

    @Singleton
    @Provides
    @BgHandler
    public Handler provideBgHandler(@BgLooper Looper bgLooper) {
        return new Handler(bgLooper);
    }

    @Singleton
    @Provides
    @MainHandler
    public Handler provideMainHandler(@MainLooper Looper mainLooper) {
+22 −26
Original line number Diff line number Diff line
@@ -33,13 +33,13 @@ import com.android.internal.annotations.VisibleForTesting;
import com.android.settingslib.fuelgauge.BatterySaverUtils;
import com.android.settingslib.fuelgauge.Estimate;
import com.android.settingslib.utils.PowerUtil;
import com.android.systemui.Dependency;
import com.android.systemui.broadcast.BroadcastDispatcher;
import com.android.systemui.dagger.qualifiers.BgHandler;
import com.android.systemui.dagger.qualifiers.MainHandler;
import com.android.systemui.power.EnhancedEstimates;

import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.text.NumberFormat;
import java.util.ArrayList;

import javax.inject.Inject;
@@ -53,42 +53,39 @@ import javax.inject.Singleton;
public class BatteryControllerImpl extends BroadcastReceiver implements BatteryController {
    private static final String TAG = "BatteryController";

    public static final String ACTION_LEVEL_TEST = "com.android.systemui.BATTERY_LEVEL_TEST";
    private static final String ACTION_LEVEL_TEST = "com.android.systemui.BATTERY_LEVEL_TEST";

    private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
    private static final int UPDATE_GRANULARITY_MSEC = 1000 * 60;

    private final EnhancedEstimates mEstimates;
    private final BroadcastDispatcher mBroadcastDispatcher;
    private final ArrayList<BatteryController.BatteryStateChangeCallback> mChangeCallbacks = new ArrayList<>();
    private final ArrayList<BatteryController.BatteryStateChangeCallback>
            mChangeCallbacks = new ArrayList<>();
    private final ArrayList<EstimateFetchCompletion> mFetchCallbacks = new ArrayList<>();
    private final PowerManager mPowerManager;
    private final Handler mHandler;
    private final Handler mMainHandler;
    private final Handler mBgHandler;
    private final Context mContext;

    protected int mLevel;
    protected boolean mPluggedIn;
    protected boolean mCharging;
    protected boolean mCharged;
    protected boolean mPowerSave;
    protected boolean mAodPowerSave;
    private int mLevel;
    private boolean mPluggedIn;
    private boolean mCharging;
    private boolean mCharged;
    private boolean mPowerSave;
    private boolean mAodPowerSave;
    private boolean mTestmode = false;
    private boolean mHasReceivedBattery = false;
    private Estimate mEstimate;
    private boolean mFetchingEstimate = false;

    @Inject
    public BatteryControllerImpl(Context context, EnhancedEstimates enhancedEstimates,
            BroadcastDispatcher broadcastDispatcher) {
        this(context, enhancedEstimates, context.getSystemService(PowerManager.class),
                broadcastDispatcher);
    }

    @VisibleForTesting
    @Inject
    BatteryControllerImpl(Context context, EnhancedEstimates enhancedEstimates,
            PowerManager powerManager, BroadcastDispatcher broadcastDispatcher) {
            PowerManager powerManager, BroadcastDispatcher broadcastDispatcher,
            @MainHandler Handler mainHandler, @BgHandler Handler bgHandler) {
        mContext = context;
        mHandler = new Handler();
        mMainHandler = mainHandler;
        mBgHandler = bgHandler;
        mPowerManager = powerManager;
        mEstimates = enhancedEstimates;
        mBroadcastDispatcher = broadcastDispatcher;
@@ -162,7 +159,7 @@ public class BatteryControllerImpl extends BroadcastReceiver implements BatteryC
            setPowerSave(intent.getBooleanExtra(PowerManager.EXTRA_POWER_SAVE_MODE, false));
        } else if (action.equals(ACTION_LEVEL_TEST)) {
            mTestmode = true;
            mHandler.post(new Runnable() {
            mMainHandler.post(new Runnable() {
                int curLevel = 0;
                int incr = 1;
                int saveLevel = mLevel;
@@ -189,7 +186,7 @@ public class BatteryControllerImpl extends BroadcastReceiver implements BatteryC
                    if (curLevel == 100) {
                        incr *= -1;
                    }
                    mHandler.postDelayed(this, 200);
                    mMainHandler.postDelayed(this, 200);
                }
            });
        }
@@ -222,7 +219,6 @@ public class BatteryControllerImpl extends BroadcastReceiver implements BatteryC
                return null;
            }

            String percentage = NumberFormat.getPercentInstance().format((double) mLevel / 100.0);
            return PowerUtil.getBatteryRemainingShortStringFormatted(
                    mContext, mEstimate.getEstimateMillis());
        }
@@ -235,7 +231,7 @@ public class BatteryControllerImpl extends BroadcastReceiver implements BatteryC
        }

        mFetchingEstimate = true;
        Dependency.get(Dependency.BG_HANDLER).post(() -> {
        mBgHandler.post(() -> {
            // Only fetch the estimate if they are enabled
            synchronized (mFetchCallbacks) {
                mEstimate = null;
@@ -244,7 +240,7 @@ public class BatteryControllerImpl extends BroadcastReceiver implements BatteryC
                }
            }
            mFetchingEstimate = false;
            Dependency.get(Dependency.MAIN_HANDLER).post(this::notifyEstimateFetchCallbacks);
            mMainHandler.post(this::notifyEstimateFetchCallbacks);
        });
    }

+2 −1
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import android.content.Intent;
import android.os.Handler;
import android.os.PowerManager;
import android.os.PowerSaveState;
import android.test.suitebuilder.annotation.SmallTest;
@@ -53,7 +54,7 @@ public class BatteryControllerTest extends SysuiTestCase {
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        mBatteryController = new BatteryControllerImpl(getContext(), mock(EnhancedEstimates.class),
                mPowerManager, mBroadcastDispatcher);
                mPowerManager, mBroadcastDispatcher, new Handler(), new Handler());
    }

    @Test