Merge "Fix NullPointerException when full charge unplug a device with work profile" into main

This commit is contained in:
YK Hung
2023-10-11 01:58:24 +00:00
committed by Android (Google) Code Review
2 changed files with 15 additions and 0 deletions

View File

@@ -63,6 +63,10 @@ public final class BatteryUsageBroadcastReceiver extends BroadcastReceiver {
} }
final String action = intent.getAction(); final String action = intent.getAction();
Log.d(TAG, "onReceive:" + action); Log.d(TAG, "onReceive:" + action);
if (DatabaseUtils.isWorkProfile(context)) {
Log.w(TAG, "do nothing for work profile action=" + action);
return;
}
DatabaseUtils.recordDateTime(context, action); DatabaseUtils.recordDateTime(context, action);
final String fullChargeIntentAction = FeatureFactory.getFeatureFactory() final String fullChargeIntentAction = FeatureFactory.getFeatureFactory()
.getPowerUsageFeatureProvider() .getPowerUsageFeatureProvider()

View File

@@ -30,6 +30,7 @@ import android.os.BatteryManager;
import android.os.SystemClock; import android.os.SystemClock;
import android.text.format.DateUtils; import android.text.format.DateUtils;
import com.android.settings.testutils.BatteryTestUtils;
import com.android.settings.testutils.FakeFeatureFactory; import com.android.settings.testutils.FakeFeatureFactory;
import org.junit.Before; import org.junit.Before;
@@ -67,6 +68,16 @@ public final class BatteryUsageBroadcastReceiverTest {
assertThat(mBatteryUsageBroadcastReceiver.mFetchBatteryUsageData).isFalse(); assertThat(mBatteryUsageBroadcastReceiver.mFetchBatteryUsageData).isFalse();
} }
@Test
public void onReceive_workProfile_doNothing() {
BatteryTestUtils.setWorkProfile(mContext);
mBatteryUsageBroadcastReceiver.onReceive(mContext,
new Intent(BatteryUsageBroadcastReceiver.ACTION_BATTERY_UNPLUGGING));
assertThat(mBatteryUsageBroadcastReceiver.mFetchBatteryUsageData).isFalse();
}
@Test @Test
public void onReceive_aospNotFullCharged_notFetchUsageData() { public void onReceive_aospNotFullCharged_notFetchUsageData() {
when(mFakeFeatureFactory.powerUsageFeatureProvider.getFullChargeIntentAction()) when(mFakeFeatureFactory.powerUsageFeatureProvider.getFullChargeIntentAction())