Loading src/com/android/settings/fuelgauge/BatteryHeaderPreferenceController.java +12 −7 Original line number Diff line number Diff line Loading @@ -11,8 +11,6 @@ * 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.settings.fuelgauge; Loading @@ -25,6 +23,7 @@ import android.icu.text.NumberFormat; import android.os.BatteryManager; import android.os.PowerManager; import android.text.TextUtils; import android.util.Log; import androidx.annotation.VisibleForTesting; import androidx.preference.PreferenceFragmentCompat; Loading @@ -48,6 +47,8 @@ import com.android.settingslib.widget.UsageProgressBarPreference; public class BatteryHeaderPreferenceController extends BasePreferenceController implements PreferenceControllerMixin, LifecycleObserver, OnStart, BatteryPreferenceController { private static final String TAG = "BatteryHeaderPreferenceController"; @VisibleForTesting static final String KEY_BATTERY_HEADER = "battery_header"; private static final int BATTERY_MAX_LEVEL = 100; Loading Loading @@ -109,7 +110,9 @@ public class BatteryHeaderPreferenceController extends BasePreferenceController } private CharSequence generateLabel(BatteryInfo info) { if (BatteryUtils.isBatteryDefenderOn(info)) { if (Utils.containsIncompatibleChargers(mContext, TAG)) { return mContext.getString(R.string.battery_info_status_not_charging); } else if (BatteryUtils.isBatteryDefenderOn(info)) { return null; } else if (info.remainingLabel == null || info.batteryStatus == BatteryManager.BATTERY_STATUS_NOT_CHARGING) { Loading Loading @@ -151,12 +154,14 @@ public class BatteryHeaderPreferenceController extends BasePreferenceController * Callback which receives text for the summary line. */ public void updateBatteryStatus(String label, BatteryInfo info) { mBatteryUsageProgressBarPref.setBottomSummary(label != null ? label : generateLabel(info)); final CharSequence summary = label != null ? label : generateLabel(info); mBatteryUsageProgressBarPref.setBottomSummary(summary); Log.d(TAG, "updateBatteryStatus: " + label + " summary: " + summary); } public void quickUpdateHeaderPreference() { Intent batteryBroadcast = mContext.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); Intent batteryBroadcast = com.android.settingslib.fuelgauge.BatteryUtils .getBatteryIntent(mContext); final int batteryLevel = Utils.getBatteryLevel(batteryBroadcast); final boolean discharging = batteryBroadcast.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1) == 0; Loading src/com/android/settings/fuelgauge/TopLevelBatteryPreferenceController.java +1 −0 Original line number Diff line number Diff line Loading @@ -164,6 +164,7 @@ public class TopLevelBatteryPreferenceController extends BasePreferenceControlle if (summary != null) { mPreference.setSummary(summary); } Log.d(TAG, "updateBatteryStatus: " + label + " summary: " + summary); } @VisibleForTesting Loading tests/robotests/src/com/android/settings/fuelgauge/BatteryHeaderPreferenceControllerTest.java +22 −3 Original line number Diff line number Diff line Loading @@ -11,10 +11,7 @@ * 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.settings.fuelgauge; import static com.google.common.truth.Truth.assertThat; Loading @@ -29,6 +26,9 @@ import static org.mockito.Mockito.when; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.hardware.usb.UsbManager; import android.hardware.usb.UsbPort; import android.hardware.usb.UsbPortStatus; import android.icu.text.NumberFormat; import android.os.BatteryManager; import android.os.PowerManager; Loading @@ -43,6 +43,7 @@ import com.android.settings.core.BasePreferenceController; import com.android.settings.fuelgauge.batterytip.tips.BatteryTip; import com.android.settings.fuelgauge.batterytip.tips.LowBatteryTip; import com.android.settings.fuelgauge.batterytip.tips.SmartBatteryTip; import com.android.settings.testutils.BatteryTestUtils; import com.android.settings.testutils.shadow.ShadowEntityHeaderController; import com.android.settings.testutils.shadow.ShadowUtils; import com.android.settings.widget.EntityHeaderController; Loading Loading @@ -85,6 +86,13 @@ public class BatteryHeaderPreferenceControllerTest { private UsageProgressBarPreference mBatteryUsageProgressBarPref; @Mock private BatteryStatusFeatureProvider mBatteryStatusFeatureProvider; @Mock private UsbPort mUsbPort; @Mock private UsbManager mUsbManager; @Mock private UsbPortStatus mUsbPortStatus; private BatteryHeaderPreferenceController mController; private Context mContext; private ShadowPowerManager mShadowPowerManager; Loading @@ -99,6 +107,7 @@ public class BatteryHeaderPreferenceControllerTest { mLifecycleOwner = () -> mLifecycle; mLifecycle = new Lifecycle(mLifecycleOwner); mContext = spy(RuntimeEnvironment.application); when(mContext.getSystemService(UsbManager.class)).thenReturn(mUsbManager); ShadowEntityHeaderController.setUseMock(mEntityHeaderController); mBatteryIntent = new Intent(); Loading Loading @@ -289,6 +298,16 @@ public class BatteryHeaderPreferenceControllerTest { verify(mBatteryUsageProgressBarPref).setBottomSummary(null); } @Test public void updatePreference_incompatibleCharger_showNotChargingState() { BatteryTestUtils.setupIncompatibleEvent(mUsbPort, mUsbManager, mUsbPortStatus); mController.updateHeaderPreference(mBatteryInfo); verify(mBatteryUsageProgressBarPref).setBottomSummary( mContext.getString(R.string.battery_info_status_not_charging)); } @Test public void quickUpdateHeaderPreference_onlyUpdateBatteryLevelAndChargingState() { mController.quickUpdateHeaderPreference(); Loading tests/robotests/src/com/android/settings/fuelgauge/TopLevelBatteryPreferenceControllerTest.java +2 −11 Original line number Diff line number Diff line Loading @@ -36,6 +36,7 @@ import androidx.preference.Preference; import androidx.test.core.app.ApplicationProvider; import com.android.settings.R; import com.android.settings.testutils.BatteryTestUtils; import org.junit.Before; import org.junit.Ignore; Loading Loading @@ -136,7 +137,7 @@ public class TopLevelBatteryPreferenceControllerTest { @Test public void getDashboardLabel_incompatibleCharger_returnsCorrectLabel() { setupIncompatibleEvent(); BatteryTestUtils.setupIncompatibleEvent(mUsbPort, mUsbManager, mUsbPortStatus); mController.mPreference = new Preference(mContext); BatteryInfo info = new BatteryInfo(); Loading @@ -150,14 +151,4 @@ public class TopLevelBatteryPreferenceControllerTest { assertThat(mController.getSummary()) .isEqualTo(mContext.getString(R.string.battery_missing_message)); } private void setupIncompatibleEvent() { final List<UsbPort> usbPorts = new ArrayList<>(); usbPorts.add(mUsbPort); when(mUsbManager.getPorts()).thenReturn(usbPorts); when(mUsbPort.getStatus()).thenReturn(mUsbPortStatus); when(mUsbPort.supportsComplianceWarnings()).thenReturn(true); when(mUsbPortStatus.isConnected()).thenReturn(true); when(mUsbPortStatus.getComplianceWarnings()).thenReturn(new int[]{1}); } } tests/robotests/src/com/android/settings/testutils/BatteryTestUtils.java +21 −1 Original line number Diff line number Diff line Loading @@ -16,11 +16,15 @@ package com.android.settings.testutils; import static org.mockito.Mockito.when; import android.content.Context; import android.content.Intent; import android.hardware.usb.UsbManager; import android.hardware.usb.UsbPort; import android.hardware.usb.UsbPortStatus; import android.os.BatteryManager; import android.os.UserManager; import androidx.room.Room; import com.android.settings.fuelgauge.batteryusage.BatteryInformation; Loading @@ -36,6 +40,9 @@ import com.google.common.collect.ImmutableList; import org.robolectric.Shadows; import java.util.ArrayList; import java.util.List; public class BatteryTestUtils { public static Intent getChargingIntent() { Loading Loading @@ -163,6 +170,7 @@ public class BatteryTestUtils { } } /** Gets customized battery changed intent. */ public static Intent getCustomBatteryIntent(int plugged, int level, int scale, int status) { Intent intent = new Intent(); intent.putExtra(BatteryManager.EXTRA_PLUGGED, plugged); Loading @@ -172,4 +180,16 @@ public class BatteryTestUtils { return intent; } /** Configures the incompatible charger environment. */ public static void setupIncompatibleEvent( UsbPort mockUsbPort, UsbManager mockUsbManager, UsbPortStatus mockUsbPortStatus) { final List<UsbPort> usbPorts = new ArrayList<>(); usbPorts.add(mockUsbPort); when(mockUsbManager.getPorts()).thenReturn(usbPorts); when(mockUsbPort.getStatus()).thenReturn(mockUsbPortStatus); when(mockUsbPort.supportsComplianceWarnings()).thenReturn(true); when(mockUsbPortStatus.isConnected()).thenReturn(true); when(mockUsbPortStatus.getComplianceWarnings()).thenReturn(new int[]{1}); } } Loading
src/com/android/settings/fuelgauge/BatteryHeaderPreferenceController.java +12 −7 Original line number Diff line number Diff line Loading @@ -11,8 +11,6 @@ * 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.settings.fuelgauge; Loading @@ -25,6 +23,7 @@ import android.icu.text.NumberFormat; import android.os.BatteryManager; import android.os.PowerManager; import android.text.TextUtils; import android.util.Log; import androidx.annotation.VisibleForTesting; import androidx.preference.PreferenceFragmentCompat; Loading @@ -48,6 +47,8 @@ import com.android.settingslib.widget.UsageProgressBarPreference; public class BatteryHeaderPreferenceController extends BasePreferenceController implements PreferenceControllerMixin, LifecycleObserver, OnStart, BatteryPreferenceController { private static final String TAG = "BatteryHeaderPreferenceController"; @VisibleForTesting static final String KEY_BATTERY_HEADER = "battery_header"; private static final int BATTERY_MAX_LEVEL = 100; Loading Loading @@ -109,7 +110,9 @@ public class BatteryHeaderPreferenceController extends BasePreferenceController } private CharSequence generateLabel(BatteryInfo info) { if (BatteryUtils.isBatteryDefenderOn(info)) { if (Utils.containsIncompatibleChargers(mContext, TAG)) { return mContext.getString(R.string.battery_info_status_not_charging); } else if (BatteryUtils.isBatteryDefenderOn(info)) { return null; } else if (info.remainingLabel == null || info.batteryStatus == BatteryManager.BATTERY_STATUS_NOT_CHARGING) { Loading Loading @@ -151,12 +154,14 @@ public class BatteryHeaderPreferenceController extends BasePreferenceController * Callback which receives text for the summary line. */ public void updateBatteryStatus(String label, BatteryInfo info) { mBatteryUsageProgressBarPref.setBottomSummary(label != null ? label : generateLabel(info)); final CharSequence summary = label != null ? label : generateLabel(info); mBatteryUsageProgressBarPref.setBottomSummary(summary); Log.d(TAG, "updateBatteryStatus: " + label + " summary: " + summary); } public void quickUpdateHeaderPreference() { Intent batteryBroadcast = mContext.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); Intent batteryBroadcast = com.android.settingslib.fuelgauge.BatteryUtils .getBatteryIntent(mContext); final int batteryLevel = Utils.getBatteryLevel(batteryBroadcast); final boolean discharging = batteryBroadcast.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1) == 0; Loading
src/com/android/settings/fuelgauge/TopLevelBatteryPreferenceController.java +1 −0 Original line number Diff line number Diff line Loading @@ -164,6 +164,7 @@ public class TopLevelBatteryPreferenceController extends BasePreferenceControlle if (summary != null) { mPreference.setSummary(summary); } Log.d(TAG, "updateBatteryStatus: " + label + " summary: " + summary); } @VisibleForTesting Loading
tests/robotests/src/com/android/settings/fuelgauge/BatteryHeaderPreferenceControllerTest.java +22 −3 Original line number Diff line number Diff line Loading @@ -11,10 +11,7 @@ * 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.settings.fuelgauge; import static com.google.common.truth.Truth.assertThat; Loading @@ -29,6 +26,9 @@ import static org.mockito.Mockito.when; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.hardware.usb.UsbManager; import android.hardware.usb.UsbPort; import android.hardware.usb.UsbPortStatus; import android.icu.text.NumberFormat; import android.os.BatteryManager; import android.os.PowerManager; Loading @@ -43,6 +43,7 @@ import com.android.settings.core.BasePreferenceController; import com.android.settings.fuelgauge.batterytip.tips.BatteryTip; import com.android.settings.fuelgauge.batterytip.tips.LowBatteryTip; import com.android.settings.fuelgauge.batterytip.tips.SmartBatteryTip; import com.android.settings.testutils.BatteryTestUtils; import com.android.settings.testutils.shadow.ShadowEntityHeaderController; import com.android.settings.testutils.shadow.ShadowUtils; import com.android.settings.widget.EntityHeaderController; Loading Loading @@ -85,6 +86,13 @@ public class BatteryHeaderPreferenceControllerTest { private UsageProgressBarPreference mBatteryUsageProgressBarPref; @Mock private BatteryStatusFeatureProvider mBatteryStatusFeatureProvider; @Mock private UsbPort mUsbPort; @Mock private UsbManager mUsbManager; @Mock private UsbPortStatus mUsbPortStatus; private BatteryHeaderPreferenceController mController; private Context mContext; private ShadowPowerManager mShadowPowerManager; Loading @@ -99,6 +107,7 @@ public class BatteryHeaderPreferenceControllerTest { mLifecycleOwner = () -> mLifecycle; mLifecycle = new Lifecycle(mLifecycleOwner); mContext = spy(RuntimeEnvironment.application); when(mContext.getSystemService(UsbManager.class)).thenReturn(mUsbManager); ShadowEntityHeaderController.setUseMock(mEntityHeaderController); mBatteryIntent = new Intent(); Loading Loading @@ -289,6 +298,16 @@ public class BatteryHeaderPreferenceControllerTest { verify(mBatteryUsageProgressBarPref).setBottomSummary(null); } @Test public void updatePreference_incompatibleCharger_showNotChargingState() { BatteryTestUtils.setupIncompatibleEvent(mUsbPort, mUsbManager, mUsbPortStatus); mController.updateHeaderPreference(mBatteryInfo); verify(mBatteryUsageProgressBarPref).setBottomSummary( mContext.getString(R.string.battery_info_status_not_charging)); } @Test public void quickUpdateHeaderPreference_onlyUpdateBatteryLevelAndChargingState() { mController.quickUpdateHeaderPreference(); Loading
tests/robotests/src/com/android/settings/fuelgauge/TopLevelBatteryPreferenceControllerTest.java +2 −11 Original line number Diff line number Diff line Loading @@ -36,6 +36,7 @@ import androidx.preference.Preference; import androidx.test.core.app.ApplicationProvider; import com.android.settings.R; import com.android.settings.testutils.BatteryTestUtils; import org.junit.Before; import org.junit.Ignore; Loading Loading @@ -136,7 +137,7 @@ public class TopLevelBatteryPreferenceControllerTest { @Test public void getDashboardLabel_incompatibleCharger_returnsCorrectLabel() { setupIncompatibleEvent(); BatteryTestUtils.setupIncompatibleEvent(mUsbPort, mUsbManager, mUsbPortStatus); mController.mPreference = new Preference(mContext); BatteryInfo info = new BatteryInfo(); Loading @@ -150,14 +151,4 @@ public class TopLevelBatteryPreferenceControllerTest { assertThat(mController.getSummary()) .isEqualTo(mContext.getString(R.string.battery_missing_message)); } private void setupIncompatibleEvent() { final List<UsbPort> usbPorts = new ArrayList<>(); usbPorts.add(mUsbPort); when(mUsbManager.getPorts()).thenReturn(usbPorts); when(mUsbPort.getStatus()).thenReturn(mUsbPortStatus); when(mUsbPort.supportsComplianceWarnings()).thenReturn(true); when(mUsbPortStatus.isConnected()).thenReturn(true); when(mUsbPortStatus.getComplianceWarnings()).thenReturn(new int[]{1}); } }
tests/robotests/src/com/android/settings/testutils/BatteryTestUtils.java +21 −1 Original line number Diff line number Diff line Loading @@ -16,11 +16,15 @@ package com.android.settings.testutils; import static org.mockito.Mockito.when; import android.content.Context; import android.content.Intent; import android.hardware.usb.UsbManager; import android.hardware.usb.UsbPort; import android.hardware.usb.UsbPortStatus; import android.os.BatteryManager; import android.os.UserManager; import androidx.room.Room; import com.android.settings.fuelgauge.batteryusage.BatteryInformation; Loading @@ -36,6 +40,9 @@ import com.google.common.collect.ImmutableList; import org.robolectric.Shadows; import java.util.ArrayList; import java.util.List; public class BatteryTestUtils { public static Intent getChargingIntent() { Loading Loading @@ -163,6 +170,7 @@ public class BatteryTestUtils { } } /** Gets customized battery changed intent. */ public static Intent getCustomBatteryIntent(int plugged, int level, int scale, int status) { Intent intent = new Intent(); intent.putExtra(BatteryManager.EXTRA_PLUGGED, plugged); Loading @@ -172,4 +180,16 @@ public class BatteryTestUtils { return intent; } /** Configures the incompatible charger environment. */ public static void setupIncompatibleEvent( UsbPort mockUsbPort, UsbManager mockUsbManager, UsbPortStatus mockUsbPortStatus) { final List<UsbPort> usbPorts = new ArrayList<>(); usbPorts.add(mockUsbPort); when(mockUsbManager.getPorts()).thenReturn(usbPorts); when(mockUsbPort.getStatus()).thenReturn(mockUsbPortStatus); when(mockUsbPort.supportsComplianceWarnings()).thenReturn(true); when(mockUsbPortStatus.isConnected()).thenReturn(true); when(mockUsbPortStatus.getComplianceWarnings()).thenReturn(new int[]{1}); } }