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.
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*
|
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.android.settings.fuelgauge;
|
package com.android.settings.fuelgauge;
|
||||||
@@ -25,6 +23,7 @@ import android.icu.text.NumberFormat;
|
|||||||
import android.os.BatteryManager;
|
import android.os.BatteryManager;
|
||||||
import android.os.PowerManager;
|
import android.os.PowerManager;
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
import androidx.annotation.VisibleForTesting;
|
import androidx.annotation.VisibleForTesting;
|
||||||
import androidx.preference.PreferenceFragmentCompat;
|
import androidx.preference.PreferenceFragmentCompat;
|
||||||
@@ -48,6 +47,8 @@ import com.android.settingslib.widget.UsageProgressBarPreference;
|
|||||||
public class BatteryHeaderPreferenceController extends BasePreferenceController
|
public class BatteryHeaderPreferenceController extends BasePreferenceController
|
||||||
implements PreferenceControllerMixin, LifecycleObserver, OnStart,
|
implements PreferenceControllerMixin, LifecycleObserver, OnStart,
|
||||||
BatteryPreferenceController {
|
BatteryPreferenceController {
|
||||||
|
private static final String TAG = "BatteryHeaderPreferenceController";
|
||||||
|
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
static final String KEY_BATTERY_HEADER = "battery_header";
|
static final String KEY_BATTERY_HEADER = "battery_header";
|
||||||
private static final int BATTERY_MAX_LEVEL = 100;
|
private static final int BATTERY_MAX_LEVEL = 100;
|
||||||
@@ -86,7 +87,7 @@ public class BatteryHeaderPreferenceController extends BasePreferenceController
|
|||||||
public void displayPreference(PreferenceScreen screen) {
|
public void displayPreference(PreferenceScreen screen) {
|
||||||
super.displayPreference(screen);
|
super.displayPreference(screen);
|
||||||
mBatteryUsageProgressBarPref = screen.findPreference(getPreferenceKey());
|
mBatteryUsageProgressBarPref = screen.findPreference(getPreferenceKey());
|
||||||
//Set up loading text first to prevent layout flaky before info loaded.
|
// Set up loading text first to prevent layout flaky before info loaded.
|
||||||
mBatteryUsageProgressBarPref.setBottomSummary(
|
mBatteryUsageProgressBarPref.setBottomSummary(
|
||||||
mContext.getString(R.string.settings_license_activity_loading));
|
mContext.getString(R.string.settings_license_activity_loading));
|
||||||
|
|
||||||
@@ -109,7 +110,9 @@ public class BatteryHeaderPreferenceController extends BasePreferenceController
|
|||||||
}
|
}
|
||||||
|
|
||||||
private CharSequence generateLabel(BatteryInfo info) {
|
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;
|
return null;
|
||||||
} else if (info.remainingLabel == null
|
} else if (info.remainingLabel == null
|
||||||
|| info.batteryStatus == BatteryManager.BATTERY_STATUS_NOT_CHARGING) {
|
|| info.batteryStatus == BatteryManager.BATTERY_STATUS_NOT_CHARGING) {
|
||||||
@@ -151,12 +154,14 @@ public class BatteryHeaderPreferenceController extends BasePreferenceController
|
|||||||
* Callback which receives text for the summary line.
|
* Callback which receives text for the summary line.
|
||||||
*/
|
*/
|
||||||
public void updateBatteryStatus(String label, BatteryInfo info) {
|
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() {
|
public void quickUpdateHeaderPreference() {
|
||||||
Intent batteryBroadcast = mContext.registerReceiver(null,
|
Intent batteryBroadcast = com.android.settingslib.fuelgauge.BatteryUtils
|
||||||
new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
|
.getBatteryIntent(mContext);
|
||||||
final int batteryLevel = Utils.getBatteryLevel(batteryBroadcast);
|
final int batteryLevel = Utils.getBatteryLevel(batteryBroadcast);
|
||||||
final boolean discharging =
|
final boolean discharging =
|
||||||
batteryBroadcast.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1) == 0;
|
batteryBroadcast.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1) == 0;
|
||||||
|
@@ -164,6 +164,7 @@ public class TopLevelBatteryPreferenceController extends BasePreferenceControlle
|
|||||||
if (summary != null) {
|
if (summary != null) {
|
||||||
mPreference.setSummary(summary);
|
mPreference.setSummary(summary);
|
||||||
}
|
}
|
||||||
|
Log.d(TAG, "updateBatteryStatus: " + label + " summary: " + summary);
|
||||||
}
|
}
|
||||||
|
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
|
@@ -11,10 +11,7 @@
|
|||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*
|
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.android.settings.fuelgauge;
|
package com.android.settings.fuelgauge;
|
||||||
|
|
||||||
import static com.google.common.truth.Truth.assertThat;
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
@@ -29,6 +26,9 @@ import static org.mockito.Mockito.when;
|
|||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
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.icu.text.NumberFormat;
|
||||||
import android.os.BatteryManager;
|
import android.os.BatteryManager;
|
||||||
import android.os.PowerManager;
|
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.BatteryTip;
|
||||||
import com.android.settings.fuelgauge.batterytip.tips.LowBatteryTip;
|
import com.android.settings.fuelgauge.batterytip.tips.LowBatteryTip;
|
||||||
import com.android.settings.fuelgauge.batterytip.tips.SmartBatteryTip;
|
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.ShadowEntityHeaderController;
|
||||||
import com.android.settings.testutils.shadow.ShadowUtils;
|
import com.android.settings.testutils.shadow.ShadowUtils;
|
||||||
import com.android.settings.widget.EntityHeaderController;
|
import com.android.settings.widget.EntityHeaderController;
|
||||||
@@ -85,6 +86,13 @@ public class BatteryHeaderPreferenceControllerTest {
|
|||||||
private UsageProgressBarPreference mBatteryUsageProgressBarPref;
|
private UsageProgressBarPreference mBatteryUsageProgressBarPref;
|
||||||
@Mock
|
@Mock
|
||||||
private BatteryStatusFeatureProvider mBatteryStatusFeatureProvider;
|
private BatteryStatusFeatureProvider mBatteryStatusFeatureProvider;
|
||||||
|
@Mock
|
||||||
|
private UsbPort mUsbPort;
|
||||||
|
@Mock
|
||||||
|
private UsbManager mUsbManager;
|
||||||
|
@Mock
|
||||||
|
private UsbPortStatus mUsbPortStatus;
|
||||||
|
|
||||||
private BatteryHeaderPreferenceController mController;
|
private BatteryHeaderPreferenceController mController;
|
||||||
private Context mContext;
|
private Context mContext;
|
||||||
private ShadowPowerManager mShadowPowerManager;
|
private ShadowPowerManager mShadowPowerManager;
|
||||||
@@ -99,6 +107,7 @@ public class BatteryHeaderPreferenceControllerTest {
|
|||||||
mLifecycleOwner = () -> mLifecycle;
|
mLifecycleOwner = () -> mLifecycle;
|
||||||
mLifecycle = new Lifecycle(mLifecycleOwner);
|
mLifecycle = new Lifecycle(mLifecycleOwner);
|
||||||
mContext = spy(RuntimeEnvironment.application);
|
mContext = spy(RuntimeEnvironment.application);
|
||||||
|
when(mContext.getSystemService(UsbManager.class)).thenReturn(mUsbManager);
|
||||||
ShadowEntityHeaderController.setUseMock(mEntityHeaderController);
|
ShadowEntityHeaderController.setUseMock(mEntityHeaderController);
|
||||||
|
|
||||||
mBatteryIntent = new Intent();
|
mBatteryIntent = new Intent();
|
||||||
@@ -289,6 +298,16 @@ public class BatteryHeaderPreferenceControllerTest {
|
|||||||
verify(mBatteryUsageProgressBarPref).setBottomSummary(null);
|
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
|
@Test
|
||||||
public void quickUpdateHeaderPreference_onlyUpdateBatteryLevelAndChargingState() {
|
public void quickUpdateHeaderPreference_onlyUpdateBatteryLevelAndChargingState() {
|
||||||
mController.quickUpdateHeaderPreference();
|
mController.quickUpdateHeaderPreference();
|
||||||
|
@@ -36,6 +36,7 @@ import androidx.preference.Preference;
|
|||||||
import androidx.test.core.app.ApplicationProvider;
|
import androidx.test.core.app.ApplicationProvider;
|
||||||
|
|
||||||
import com.android.settings.R;
|
import com.android.settings.R;
|
||||||
|
import com.android.settings.testutils.BatteryTestUtils;
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Ignore;
|
import org.junit.Ignore;
|
||||||
@@ -136,7 +137,7 @@ public class TopLevelBatteryPreferenceControllerTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getDashboardLabel_incompatibleCharger_returnsCorrectLabel() {
|
public void getDashboardLabel_incompatibleCharger_returnsCorrectLabel() {
|
||||||
setupIncompatibleEvent();
|
BatteryTestUtils.setupIncompatibleEvent(mUsbPort, mUsbManager, mUsbPortStatus);
|
||||||
mController.mPreference = new Preference(mContext);
|
mController.mPreference = new Preference(mContext);
|
||||||
BatteryInfo info = new BatteryInfo();
|
BatteryInfo info = new BatteryInfo();
|
||||||
|
|
||||||
@@ -150,14 +151,4 @@ public class TopLevelBatteryPreferenceControllerTest {
|
|||||||
assertThat(mController.getSummary())
|
assertThat(mController.getSummary())
|
||||||
.isEqualTo(mContext.getString(R.string.battery_missing_message));
|
.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;
|
package com.android.settings.testutils;
|
||||||
|
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
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.BatteryManager;
|
||||||
import android.os.UserManager;
|
import android.os.UserManager;
|
||||||
|
|
||||||
import androidx.room.Room;
|
import androidx.room.Room;
|
||||||
|
|
||||||
import com.android.settings.fuelgauge.batteryusage.BatteryInformation;
|
import com.android.settings.fuelgauge.batteryusage.BatteryInformation;
|
||||||
@@ -36,6 +40,9 @@ import com.google.common.collect.ImmutableList;
|
|||||||
|
|
||||||
import org.robolectric.Shadows;
|
import org.robolectric.Shadows;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class BatteryTestUtils {
|
public class BatteryTestUtils {
|
||||||
|
|
||||||
public static Intent getChargingIntent() {
|
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) {
|
public static Intent getCustomBatteryIntent(int plugged, int level, int scale, int status) {
|
||||||
Intent intent = new Intent();
|
Intent intent = new Intent();
|
||||||
intent.putExtra(BatteryManager.EXTRA_PLUGGED, plugged);
|
intent.putExtra(BatteryManager.EXTRA_PLUGGED, plugged);
|
||||||
@@ -172,4 +180,16 @@ public class BatteryTestUtils {
|
|||||||
|
|
||||||
return intent;
|
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