Support incompatible charger state in the battery main page
https://screenshot.googleplex.com/PrSzAtMAhsyqbsR Bug: 271775549 Test: make test RunSettingsRoboTests Change-Id: I0bb912eaab9c8837eaa3a9b998b3ebb5a8e6f99f
This commit is contained in:
@@ -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;
|
||||
@@ -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;
|
||||
@@ -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;
|
||||
@@ -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) {
|
||||
@@ -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;
|
||||
|
@@ -164,6 +164,7 @@ public class TopLevelBatteryPreferenceController extends BasePreferenceControlle
|
||||
if (summary != null) {
|
||||
mPreference.setSummary(summary);
|
||||
}
|
||||
Log.d(TAG, "updateBatteryStatus: " + label + " summary: " + summary);
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
|
@@ -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;
|
||||
@@ -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;
|
||||
@@ -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;
|
||||
@@ -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;
|
||||
@@ -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();
|
||||
@@ -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();
|
||||
|
@@ -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;
|
||||
@@ -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();
|
||||
|
||||
@@ -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});
|
||||
}
|
||||
}
|
||||
|
@@ -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;
|
||||
@@ -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() {
|
||||
@@ -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);
|
||||
@@ -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});
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user