Merge "[Dagger] Remove BatteryController from BatteryMeterView." into sc-v2-dev
This commit is contained in:
committed by
Android (Google) Code Review
commit
b64b64a776
@@ -41,24 +41,22 @@ import android.widget.LinearLayout;
|
|||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
|
||||||
import androidx.annotation.StyleRes;
|
import androidx.annotation.StyleRes;
|
||||||
|
import androidx.annotation.VisibleForTesting;
|
||||||
|
|
||||||
import com.android.settingslib.graph.ThemedBatteryDrawable;
|
import com.android.settingslib.graph.ThemedBatteryDrawable;
|
||||||
import com.android.systemui.Dependency;
|
|
||||||
import com.android.systemui.DualToneHandler;
|
import com.android.systemui.DualToneHandler;
|
||||||
import com.android.systemui.R;
|
import com.android.systemui.R;
|
||||||
import com.android.systemui.animation.Interpolators;
|
import com.android.systemui.animation.Interpolators;
|
||||||
import com.android.systemui.plugins.DarkIconDispatcher;
|
import com.android.systemui.plugins.DarkIconDispatcher;
|
||||||
import com.android.systemui.plugins.DarkIconDispatcher.DarkReceiver;
|
import com.android.systemui.plugins.DarkIconDispatcher.DarkReceiver;
|
||||||
import com.android.systemui.statusbar.policy.BatteryController;
|
import com.android.systemui.statusbar.policy.BatteryController;
|
||||||
import com.android.systemui.statusbar.policy.BatteryController.BatteryStateChangeCallback;
|
|
||||||
|
|
||||||
import java.io.FileDescriptor;
|
import java.io.FileDescriptor;
|
||||||
import java.io.PrintWriter;
|
import java.io.PrintWriter;
|
||||||
import java.lang.annotation.Retention;
|
import java.lang.annotation.Retention;
|
||||||
import java.text.NumberFormat;
|
import java.text.NumberFormat;
|
||||||
|
|
||||||
public class BatteryMeterView extends LinearLayout implements
|
public class BatteryMeterView extends LinearLayout implements DarkReceiver {
|
||||||
BatteryStateChangeCallback, DarkReceiver {
|
|
||||||
|
|
||||||
@Retention(SOURCE)
|
@Retention(SOURCE)
|
||||||
@IntDef({MODE_DEFAULT, MODE_ON, MODE_OFF, MODE_ESTIMATE})
|
@IntDef({MODE_DEFAULT, MODE_ON, MODE_OFF, MODE_ESTIMATE})
|
||||||
@@ -72,7 +70,6 @@ public class BatteryMeterView extends LinearLayout implements
|
|||||||
private final ImageView mBatteryIconView;
|
private final ImageView mBatteryIconView;
|
||||||
private TextView mBatteryPercentView;
|
private TextView mBatteryPercentView;
|
||||||
|
|
||||||
private BatteryController mBatteryController;
|
|
||||||
private final @StyleRes int mPercentageStyleId;
|
private final @StyleRes int mPercentageStyleId;
|
||||||
private int mTextColor;
|
private int mTextColor;
|
||||||
private int mLevel;
|
private int mLevel;
|
||||||
@@ -90,6 +87,8 @@ public class BatteryMeterView extends LinearLayout implements
|
|||||||
private int mNonAdaptedForegroundColor;
|
private int mNonAdaptedForegroundColor;
|
||||||
private int mNonAdaptedBackgroundColor;
|
private int mNonAdaptedBackgroundColor;
|
||||||
|
|
||||||
|
private BatteryEstimateFetcher mBatteryEstimateFetcher;
|
||||||
|
|
||||||
public BatteryMeterView(Context context, AttributeSet attrs) {
|
public BatteryMeterView(Context context, AttributeSet attrs) {
|
||||||
this(context, attrs, 0);
|
this(context, attrs, 0);
|
||||||
}
|
}
|
||||||
@@ -178,22 +177,7 @@ public class BatteryMeterView extends LinearLayout implements
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
void onBatteryLevelChanged(int level, boolean pluggedIn) {
|
||||||
public void onAttachedToWindow() {
|
|
||||||
super.onAttachedToWindow();
|
|
||||||
mBatteryController = Dependency.get(BatteryController.class);
|
|
||||||
mBatteryController.addCallback(this);
|
|
||||||
updateShowPercent();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onDetachedFromWindow() {
|
|
||||||
super.onDetachedFromWindow();
|
|
||||||
mBatteryController.removeCallback(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onBatteryLevelChanged(int level, boolean pluggedIn, boolean charging) {
|
|
||||||
mDrawable.setCharging(pluggedIn);
|
mDrawable.setCharging(pluggedIn);
|
||||||
mDrawable.setBatteryLevel(level);
|
mDrawable.setBatteryLevel(level);
|
||||||
mCharging = pluggedIn;
|
mCharging = pluggedIn;
|
||||||
@@ -201,8 +185,7 @@ public class BatteryMeterView extends LinearLayout implements
|
|||||||
updatePercentText();
|
updatePercentText();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
void onPowerSaveChanged(boolean isPowerSave) {
|
||||||
public void onPowerSaveChanged(boolean isPowerSave) {
|
|
||||||
mDrawable.setPowerSaveEnabled(isPowerSave);
|
mDrawable.setPowerSaveEnabled(isPowerSave);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -222,19 +205,28 @@ public class BatteryMeterView extends LinearLayout implements
|
|||||||
updateShowPercent();
|
updateShowPercent();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the fetcher that should be used to get the estimated time remaining for the user's
|
||||||
|
* battery.
|
||||||
|
*/
|
||||||
|
void setBatteryEstimateFetcher(BatteryEstimateFetcher fetcher) {
|
||||||
|
mBatteryEstimateFetcher = fetcher;
|
||||||
|
}
|
||||||
|
|
||||||
void updatePercentText() {
|
void updatePercentText() {
|
||||||
if (mBatteryStateUnknown) {
|
if (mBatteryStateUnknown) {
|
||||||
setContentDescription(getContext().getString(R.string.accessibility_battery_unknown));
|
setContentDescription(getContext().getString(R.string.accessibility_battery_unknown));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mBatteryController == null) {
|
if (mBatteryEstimateFetcher == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mBatteryPercentView != null) {
|
if (mBatteryPercentView != null) {
|
||||||
if (mShowPercentMode == MODE_ESTIMATE && !mCharging) {
|
if (mShowPercentMode == MODE_ESTIMATE && !mCharging) {
|
||||||
mBatteryController.getEstimatedTimeRemainingString((String estimate) -> {
|
mBatteryEstimateFetcher.fetchBatteryTimeRemainingEstimate(
|
||||||
|
(String estimate) -> {
|
||||||
if (mBatteryPercentView == null) {
|
if (mBatteryPercentView == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -310,8 +302,7 @@ public class BatteryMeterView extends LinearLayout implements
|
|||||||
return mUnknownStateDrawable;
|
return mUnknownStateDrawable;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
void onBatteryUnknownStateChanged(boolean isUnknown) {
|
||||||
public void onBatteryUnknownStateChanged(boolean isUnknown) {
|
|
||||||
if (mBatteryStateUnknown == isUnknown) {
|
if (mBatteryStateUnknown == isUnknown) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -390,5 +381,16 @@ public class BatteryMeterView extends LinearLayout implements
|
|||||||
pw.println(" mLevel: " + mLevel);
|
pw.println(" mLevel: " + mLevel);
|
||||||
pw.println(" mMode: " + mShowPercentMode);
|
pw.println(" mMode: " + mShowPercentMode);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@VisibleForTesting
|
||||||
|
CharSequence getBatteryPercentViewText() {
|
||||||
|
return mBatteryPercentView.getText();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** An interface that will fetch the estimated time remaining for the user's battery. */
|
||||||
|
public interface BatteryEstimateFetcher {
|
||||||
|
void fetchBatteryTimeRemainingEstimate(
|
||||||
|
BatteryController.EstimateFetchCompletion completion);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -31,6 +31,7 @@ import com.android.systemui.broadcast.BroadcastDispatcher;
|
|||||||
import com.android.systemui.dagger.qualifiers.Main;
|
import com.android.systemui.dagger.qualifiers.Main;
|
||||||
import com.android.systemui.settings.CurrentUserTracker;
|
import com.android.systemui.settings.CurrentUserTracker;
|
||||||
import com.android.systemui.statusbar.phone.StatusBarIconController;
|
import com.android.systemui.statusbar.phone.StatusBarIconController;
|
||||||
|
import com.android.systemui.statusbar.policy.BatteryController;
|
||||||
import com.android.systemui.statusbar.policy.ConfigurationController;
|
import com.android.systemui.statusbar.policy.ConfigurationController;
|
||||||
import com.android.systemui.tuner.TunerService;
|
import com.android.systemui.tuner.TunerService;
|
||||||
import com.android.systemui.util.ViewController;
|
import com.android.systemui.util.ViewController;
|
||||||
@@ -42,6 +43,7 @@ public class BatteryMeterViewController extends ViewController<BatteryMeterView>
|
|||||||
private final ConfigurationController mConfigurationController;
|
private final ConfigurationController mConfigurationController;
|
||||||
private final TunerService mTunerService;
|
private final TunerService mTunerService;
|
||||||
private final ContentResolver mContentResolver;
|
private final ContentResolver mContentResolver;
|
||||||
|
private final BatteryController mBatteryController;
|
||||||
|
|
||||||
private final String mSlotBattery;
|
private final String mSlotBattery;
|
||||||
private final SettingObserver mSettingObserver;
|
private final SettingObserver mSettingObserver;
|
||||||
@@ -66,6 +68,24 @@ public class BatteryMeterViewController extends ViewController<BatteryMeterView>
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
private final BatteryController.BatteryStateChangeCallback mBatteryStateChangeCallback =
|
||||||
|
new BatteryController.BatteryStateChangeCallback() {
|
||||||
|
@Override
|
||||||
|
public void onBatteryLevelChanged(int level, boolean pluggedIn, boolean charging) {
|
||||||
|
mView.onBatteryLevelChanged(level, pluggedIn);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onPowerSaveChanged(boolean isPowerSave) {
|
||||||
|
mView.onPowerSaveChanged(isPowerSave);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onBatteryUnknownStateChanged(boolean isUnknown) {
|
||||||
|
mView.onBatteryUnknownStateChanged(isUnknown);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// Some places may need to show the battery conditionally, and not obey the tuner
|
// Some places may need to show the battery conditionally, and not obey the tuner
|
||||||
private boolean mIgnoreTunerUpdates;
|
private boolean mIgnoreTunerUpdates;
|
||||||
private boolean mIsSubscribedForTunerUpdates;
|
private boolean mIsSubscribedForTunerUpdates;
|
||||||
@@ -77,11 +97,15 @@ public class BatteryMeterViewController extends ViewController<BatteryMeterView>
|
|||||||
TunerService tunerService,
|
TunerService tunerService,
|
||||||
BroadcastDispatcher broadcastDispatcher,
|
BroadcastDispatcher broadcastDispatcher,
|
||||||
@Main Handler mainHandler,
|
@Main Handler mainHandler,
|
||||||
ContentResolver contentResolver) {
|
ContentResolver contentResolver,
|
||||||
|
BatteryController batteryController) {
|
||||||
super(view);
|
super(view);
|
||||||
mConfigurationController = configurationController;
|
mConfigurationController = configurationController;
|
||||||
mTunerService = tunerService;
|
mTunerService = tunerService;
|
||||||
mContentResolver = contentResolver;
|
mContentResolver = contentResolver;
|
||||||
|
mBatteryController = batteryController;
|
||||||
|
|
||||||
|
mView.setBatteryEstimateFetcher(mBatteryController::getEstimatedTimeRemainingString);
|
||||||
|
|
||||||
mSlotBattery = getResources().getString(com.android.internal.R.string.status_bar_battery);
|
mSlotBattery = getResources().getString(com.android.internal.R.string.status_bar_battery);
|
||||||
mSettingObserver = new SettingObserver(mainHandler);
|
mSettingObserver = new SettingObserver(mainHandler);
|
||||||
@@ -99,16 +123,21 @@ public class BatteryMeterViewController extends ViewController<BatteryMeterView>
|
|||||||
protected void onViewAttached() {
|
protected void onViewAttached() {
|
||||||
mConfigurationController.addCallback(mConfigurationListener);
|
mConfigurationController.addCallback(mConfigurationListener);
|
||||||
subscribeForTunerUpdates();
|
subscribeForTunerUpdates();
|
||||||
|
mBatteryController.addCallback(mBatteryStateChangeCallback);
|
||||||
|
|
||||||
registerShowBatteryPercentObserver(ActivityManager.getCurrentUser());
|
registerShowBatteryPercentObserver(ActivityManager.getCurrentUser());
|
||||||
registerGlobalBatteryUpdateObserver();
|
registerGlobalBatteryUpdateObserver();
|
||||||
mCurrentUserTracker.startTracking();
|
mCurrentUserTracker.startTracking();
|
||||||
|
|
||||||
|
mView.updateShowPercent();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onViewDetached() {
|
protected void onViewDetached() {
|
||||||
mConfigurationController.removeCallback(mConfigurationListener);
|
mConfigurationController.removeCallback(mConfigurationListener);
|
||||||
unsubscribeFromTunerUpdates();
|
unsubscribeFromTunerUpdates();
|
||||||
|
mBatteryController.removeCallback(mBatteryStateChangeCallback);
|
||||||
|
|
||||||
mCurrentUserTracker.stopTracking();
|
mCurrentUserTracker.stopTracking();
|
||||||
mContentResolver.unregisterContentObserver(mSettingObserver);
|
mContentResolver.unregisterContentObserver(mSettingObserver);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1143,7 +1143,8 @@ public class StatusBar extends SystemUI implements
|
|||||||
mTunerService,
|
mTunerService,
|
||||||
mBroadcastDispatcher,
|
mBroadcastDispatcher,
|
||||||
mMainHandler,
|
mMainHandler,
|
||||||
mContext.getContentResolver()
|
mContext.getContentResolver(),
|
||||||
|
mBatteryController
|
||||||
);
|
);
|
||||||
mBatteryMeterViewController.init();
|
mBatteryMeterViewController.init();
|
||||||
|
|
||||||
|
|||||||
@@ -35,6 +35,7 @@ import androidx.test.filters.SmallTest;
|
|||||||
|
|
||||||
import com.android.systemui.SysuiTestCase;
|
import com.android.systemui.SysuiTestCase;
|
||||||
import com.android.systemui.broadcast.BroadcastDispatcher;
|
import com.android.systemui.broadcast.BroadcastDispatcher;
|
||||||
|
import com.android.systemui.statusbar.policy.BatteryController;
|
||||||
import com.android.systemui.statusbar.policy.ConfigurationController;
|
import com.android.systemui.statusbar.policy.ConfigurationController;
|
||||||
import com.android.systemui.tuner.TunerService;
|
import com.android.systemui.tuner.TunerService;
|
||||||
|
|
||||||
@@ -58,6 +59,8 @@ public class BatteryMeterViewControllerTest extends SysuiTestCase {
|
|||||||
private Handler mHandler;
|
private Handler mHandler;
|
||||||
@Mock
|
@Mock
|
||||||
private ContentResolver mContentResolver;
|
private ContentResolver mContentResolver;
|
||||||
|
@Mock
|
||||||
|
private BatteryController mBatteryController;
|
||||||
|
|
||||||
private BatteryMeterViewController mController;
|
private BatteryMeterViewController mController;
|
||||||
|
|
||||||
@@ -74,7 +77,8 @@ public class BatteryMeterViewControllerTest extends SysuiTestCase {
|
|||||||
mTunerService,
|
mTunerService,
|
||||||
mBroadcastDispatcher,
|
mBroadcastDispatcher,
|
||||||
mHandler,
|
mHandler,
|
||||||
mContentResolver
|
mContentResolver,
|
||||||
|
mBatteryController
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -92,6 +96,7 @@ public class BatteryMeterViewControllerTest extends SysuiTestCase {
|
|||||||
anyBoolean(),
|
anyBoolean(),
|
||||||
any()
|
any()
|
||||||
);
|
);
|
||||||
|
verify(mBatteryController).addCallback(any());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -104,6 +109,7 @@ public class BatteryMeterViewControllerTest extends SysuiTestCase {
|
|||||||
verify(mConfigurationController).removeCallback(any());
|
verify(mConfigurationController).removeCallback(any());
|
||||||
verify(mTunerService).removeTunable(any());
|
verify(mTunerService).removeTunable(any());
|
||||||
verify(mContentResolver).unregisterContentObserver(any());
|
verify(mContentResolver).unregisterContentObserver(any());
|
||||||
|
verify(mBatteryController).removeCallback(any());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
@@ -0,0 +1,71 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2021 The Android Open Source Project
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package com.android.systemui.battery
|
||||||
|
|
||||||
|
import android.testing.AndroidTestingRunner
|
||||||
|
import android.testing.TestableLooper.RunWithLooper
|
||||||
|
import androidx.test.filters.SmallTest
|
||||||
|
import com.android.systemui.SysuiTestCase
|
||||||
|
import com.android.systemui.battery.BatteryMeterView.BatteryEstimateFetcher
|
||||||
|
import com.android.systemui.statusbar.policy.BatteryController.EstimateFetchCompletion
|
||||||
|
import com.google.common.truth.Truth.assertThat
|
||||||
|
import org.junit.Before
|
||||||
|
import org.junit.Test
|
||||||
|
import org.junit.runner.RunWith
|
||||||
|
import org.mockito.MockitoAnnotations
|
||||||
|
|
||||||
|
@SmallTest
|
||||||
|
@RunWith(AndroidTestingRunner::class)
|
||||||
|
@RunWithLooper
|
||||||
|
class BatteryMeterViewTest : SysuiTestCase() {
|
||||||
|
|
||||||
|
private lateinit var mBatteryMeterView: BatteryMeterView
|
||||||
|
|
||||||
|
@Before
|
||||||
|
fun setUp() {
|
||||||
|
MockitoAnnotations.initMocks(this)
|
||||||
|
mBatteryMeterView = BatteryMeterView(mContext, null)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun updatePercentText_estimateModeAndNotCharging_estimateFetched() {
|
||||||
|
mBatteryMeterView.setPercentShowMode(BatteryMeterView.MODE_ESTIMATE)
|
||||||
|
mBatteryMeterView.setBatteryEstimateFetcher(Fetcher())
|
||||||
|
|
||||||
|
mBatteryMeterView.updatePercentText()
|
||||||
|
|
||||||
|
assertThat(mBatteryMeterView.batteryPercentViewText).isEqualTo(ESTIMATE)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun updatePercentText_noBatteryEstimateFetcher_noCrash() {
|
||||||
|
mBatteryMeterView.setPercentShowMode(BatteryMeterView.MODE_ESTIMATE)
|
||||||
|
|
||||||
|
mBatteryMeterView.updatePercentText()
|
||||||
|
// No assert needed
|
||||||
|
}
|
||||||
|
|
||||||
|
private class Fetcher : BatteryEstimateFetcher {
|
||||||
|
override fun fetchBatteryTimeRemainingEstimate(
|
||||||
|
completion: EstimateFetchCompletion) {
|
||||||
|
completion.onBatteryRemainingEstimateRetrieved(ESTIMATE)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private companion object {
|
||||||
|
const val ESTIMATE = "2 hours 2 minutes"
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user