Ignore broadcast intent from additional profile.
- Use isAdditionalProfile() to ignore intent from either work or private profile. Bug: 354828134 Test: atest SettingsRoboTests:com.android.settings.fuelgauge.batteryusage Flag: EXEMPT bug fix Change-Id: Ic0c91d956e4bfcd53576629efab4be847c94155e
This commit is contained in:
@@ -63,8 +63,8 @@ public final class BatteryUsageBroadcastReceiver extends BroadcastReceiver {
|
||||
}
|
||||
final String action = intent.getAction();
|
||||
Log.d(TAG, "onReceive:" + action);
|
||||
if (com.android.settingslib.fuelgauge.BatteryUtils.isWorkProfile(context)) {
|
||||
Log.w(TAG, "do nothing for work profile action=" + action);
|
||||
if (com.android.settingslib.fuelgauge.BatteryUtils.isAdditionalProfile(context)) {
|
||||
Log.w(TAG, "do nothing for an additional profile action=" + action);
|
||||
return;
|
||||
}
|
||||
DatabaseUtils.recordDateTime(context, action);
|
||||
|
@@ -110,8 +110,8 @@ public class BatteryUsageContentProvider extends ContentProvider {
|
||||
|
||||
@Override
|
||||
public boolean onCreate() {
|
||||
if (BatteryUtils.isWorkProfile(getContext())) {
|
||||
Log.w(TAG, "do not create provider for work profile");
|
||||
if (BatteryUtils.isAdditionalProfile(getContext())) {
|
||||
Log.w(TAG, "do not create provider for an additional profile");
|
||||
return false;
|
||||
}
|
||||
mClock = Clock.systemUTC();
|
||||
|
@@ -54,8 +54,8 @@ public final class BootBroadcastReceiver extends BroadcastReceiver {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
final String action = intent == null ? "" : intent.getAction();
|
||||
if (BatteryUtils.isWorkProfile(context)) {
|
||||
Log.w(TAG, "do not start job for work profile action=" + action);
|
||||
if (BatteryUtils.isAdditionalProfile(context)) {
|
||||
Log.w(TAG, "do not start job for an additional profile action=" + action);
|
||||
return;
|
||||
}
|
||||
|
||||
|
@@ -70,6 +70,7 @@ public final class DatabaseUtils {
|
||||
|
||||
/** Clear memory threshold for device booting phase. */
|
||||
private static final long CLEAR_MEMORY_THRESHOLD_MS = Duration.ofMinutes(5).toMillis();
|
||||
|
||||
private static final long CLEAR_MEMORY_DELAYED_MS = Duration.ofSeconds(2).toMillis();
|
||||
private static final long INVALID_TIMESTAMP = 0L;
|
||||
|
||||
@@ -527,9 +528,11 @@ public final class DatabaseUtils {
|
||||
return startCalendar.getTimeInMillis();
|
||||
}
|
||||
|
||||
/** Returns the context with profile parent identity when current user is work profile. */
|
||||
/**
|
||||
* Returns the context with profile parent identity when current user is an additional profile.
|
||||
*/
|
||||
public static Context getParentContext(Context context) {
|
||||
if (com.android.settingslib.fuelgauge.BatteryUtils.isWorkProfile(context)) {
|
||||
if (com.android.settingslib.fuelgauge.BatteryUtils.isAdditionalProfile(context)) {
|
||||
try {
|
||||
return context.createPackageContextAsUser(
|
||||
/* packageName= */ context.getPackageName(),
|
||||
|
@@ -50,10 +50,10 @@ public final class PeriodicJobReceiver extends BroadcastReceiver {
|
||||
Log.w(TAG, "receive unexpected action=" + action);
|
||||
return;
|
||||
}
|
||||
if (BatteryUtils.isWorkProfile(context)) {
|
||||
if (BatteryUtils.isAdditionalProfile(context)) {
|
||||
BatteryUsageLogUtils.writeLog(
|
||||
context, Action.SCHEDULE_JOB, "do not refresh job for work profile");
|
||||
Log.w(TAG, "do not refresh job for work profile action=" + action);
|
||||
context, Action.SCHEDULE_JOB, "do not refresh job for an additional profile");
|
||||
Log.w(TAG, "do not refresh job for an additional profile action=" + action);
|
||||
return;
|
||||
}
|
||||
BatteryUsageLogUtils.writeLog(context, Action.EXECUTE_JOB, "");
|
||||
|
@@ -49,8 +49,8 @@ public final class BugReportContentProvider extends ContentProvider {
|
||||
Log.w(TAG, "failed to dump BatteryUsage state: null application context");
|
||||
return;
|
||||
}
|
||||
if (BatteryUtils.isWorkProfile(context)) {
|
||||
Log.w(TAG, "ignore battery usage states dump in the work profile");
|
||||
if (BatteryUtils.isAdditionalProfile(context)) {
|
||||
Log.w(TAG, "ignore battery usage states dump in the additional profile");
|
||||
return;
|
||||
}
|
||||
writer.println("dump BatteryUsage and AppUsage states:");
|
||||
|
@@ -28,9 +28,9 @@ import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.BatteryManager;
|
||||
import android.os.SystemClock;
|
||||
import android.os.UserManager;
|
||||
import android.text.format.DateUtils;
|
||||
|
||||
import com.android.settings.testutils.BatteryTestUtils;
|
||||
import com.android.settings.testutils.FakeFeatureFactory;
|
||||
|
||||
import org.junit.Before;
|
||||
@@ -49,6 +49,7 @@ public final class BatteryUsageBroadcastReceiverTest {
|
||||
private FakeFeatureFactory mFakeFeatureFactory;
|
||||
|
||||
@Mock private PackageManager mPackageManager;
|
||||
@Mock private UserManager mUserManager;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
@@ -57,6 +58,7 @@ public final class BatteryUsageBroadcastReceiverTest {
|
||||
mFakeFeatureFactory = FakeFeatureFactory.setupForTest();
|
||||
mBatteryUsageBroadcastReceiver = new BatteryUsageBroadcastReceiver();
|
||||
doReturn(mPackageManager).when(mContext).getPackageManager();
|
||||
doReturn(mUserManager).when(mContext).getSystemService(UserManager.class);
|
||||
DatabaseUtils.getSharedPreferences(mContext).edit().clear().apply();
|
||||
}
|
||||
|
||||
@@ -69,7 +71,17 @@ public final class BatteryUsageBroadcastReceiverTest {
|
||||
|
||||
@Test
|
||||
public void onReceive_workProfile_doNothing() {
|
||||
BatteryTestUtils.setWorkProfile(mContext);
|
||||
doReturn(true).when(mUserManager).isManagedProfile();
|
||||
|
||||
mBatteryUsageBroadcastReceiver.onReceive(
|
||||
mContext, new Intent(BatteryUsageBroadcastReceiver.ACTION_BATTERY_UNPLUGGING));
|
||||
|
||||
assertThat(mBatteryUsageBroadcastReceiver.mFetchBatteryUsageData).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onReceive_privateProfile_doNothing() {
|
||||
doReturn(true).when(mUserManager).isPrivateProfile();
|
||||
|
||||
mBatteryUsageBroadcastReceiver.onReceive(
|
||||
mContext, new Intent(BatteryUsageBroadcastReceiver.ACTION_BATTERY_UNPLUGGING));
|
||||
|
@@ -19,12 +19,16 @@ package com.android.settings.fuelgauge.batteryusage;
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.junit.Assert.assertThrows;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.content.ContentResolver;
|
||||
import android.content.ContentValues;
|
||||
import android.content.Context;
|
||||
import android.database.Cursor;
|
||||
import android.net.Uri;
|
||||
import android.os.UserManager;
|
||||
|
||||
import androidx.test.core.app.ApplicationProvider;
|
||||
|
||||
@@ -39,6 +43,8 @@ import com.android.settings.testutils.FakeClock;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
|
||||
import java.time.Duration;
|
||||
@@ -62,9 +68,14 @@ public final class BatteryUsageContentProviderTest {
|
||||
private Context mContext;
|
||||
private BatteryUsageContentProvider mProvider;
|
||||
|
||||
@Mock
|
||||
private UserManager mUserManager;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
mContext = ApplicationProvider.getApplicationContext();
|
||||
MockitoAnnotations.initMocks(this);
|
||||
mContext = spy(ApplicationProvider.getApplicationContext());
|
||||
when(mContext.getSystemService(UserManager.class)).thenReturn(mUserManager);
|
||||
mProvider = new BatteryUsageContentProvider();
|
||||
mProvider.attachInfo(mContext, /* info= */ null);
|
||||
BatteryTestUtils.setUpBatteryStateDatabase(mContext);
|
||||
@@ -77,7 +88,13 @@ public final class BatteryUsageContentProviderTest {
|
||||
|
||||
@Test
|
||||
public void onCreate_withWorkProfileMode_returnsFalse() {
|
||||
BatteryTestUtils.setWorkProfile(mContext);
|
||||
doReturn(true).when(mUserManager).isManagedProfile();
|
||||
assertThat(mProvider.onCreate()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onCreate_withPrivateProfileMode_returnsFalse() {
|
||||
doReturn(true).when(mUserManager).isPrivateProfile();
|
||||
assertThat(mProvider.onCreate()).isFalse();
|
||||
}
|
||||
|
||||
|
@@ -18,6 +18,9 @@ package com.android.settings.fuelgauge.batteryusage;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.robolectric.Shadows.shadowOf;
|
||||
|
||||
import android.app.AlarmManager;
|
||||
@@ -26,6 +29,7 @@ import android.app.usage.UsageStatsManager;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.os.UserManager;
|
||||
|
||||
import androidx.test.core.app.ApplicationProvider;
|
||||
|
||||
@@ -37,6 +41,8 @@ import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.Shadows;
|
||||
import org.robolectric.shadows.ShadowAlarmManager;
|
||||
@@ -55,10 +61,15 @@ public final class BootBroadcastReceiverTest {
|
||||
private ShadowAlarmManager mShadowAlarmManager;
|
||||
private PeriodicJobManager mPeriodicJobManager;
|
||||
|
||||
@Mock
|
||||
private UserManager mUserManager;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
|
||||
mContext = ApplicationProvider.getApplicationContext();
|
||||
mContext = spy(ApplicationProvider.getApplicationContext());
|
||||
when(mContext.getSystemService(UserManager.class)).thenReturn(mUserManager);
|
||||
mPeriodicJobManager = PeriodicJobManager.getInstance(mContext);
|
||||
mShadowAlarmManager = shadowOf(mContext.getSystemService(AlarmManager.class));
|
||||
mReceiver = new BootBroadcastReceiver();
|
||||
@@ -78,7 +89,15 @@ public final class BootBroadcastReceiverTest {
|
||||
|
||||
@Test
|
||||
public void onReceive_withWorkProfile_notRefreshesJob() {
|
||||
BatteryTestUtils.setWorkProfile(mContext);
|
||||
doReturn(true).when(mUserManager).isManagedProfile();
|
||||
mReceiver.onReceive(mContext, new Intent(Intent.ACTION_BOOT_COMPLETED));
|
||||
|
||||
assertThat(mShadowAlarmManager.peekNextScheduledAlarm()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onReceive_withPrivateProfile_notRefreshesJob() {
|
||||
doReturn(true).when(mUserManager).isPrivateProfile();
|
||||
mReceiver.onReceive(mContext, new Intent(Intent.ACTION_BOOT_COMPLETED));
|
||||
|
||||
assertThat(mShadowAlarmManager.peekNextScheduledAlarm()).isNull();
|
||||
|
@@ -47,7 +47,6 @@ import android.os.UserManager;
|
||||
|
||||
import com.android.settings.fuelgauge.batteryusage.db.AppUsageEventEntity;
|
||||
import com.android.settings.fuelgauge.batteryusage.db.BatteryEventEntity;
|
||||
import com.android.settings.testutils.BatteryTestUtils;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
@@ -450,6 +449,26 @@ public final class DatabaseUtilsTest {
|
||||
assertThat(batteryHistMap).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getHistoryMap_withPrivateProfile_returnExpectedMap()
|
||||
throws PackageManager.NameNotFoundException {
|
||||
doReturn("com.fake.package").when(mContext).getPackageName();
|
||||
doReturn(mMockContext)
|
||||
.when(mContext)
|
||||
.createPackageContextAsUser("com.fake.package", /* flags= */ 0, UserHandle.OWNER);
|
||||
doReturn(mUserManager).when(mContext).getSystemService(UserManager.class);
|
||||
doReturn(UserHandle.CURRENT).when(mContext).getUser();
|
||||
doReturn(true).when(mUserManager).isPrivateProfile();
|
||||
doReturn(UserHandle.SYSTEM).when(mUserManager).getProfileParent(UserHandle.CURRENT);
|
||||
|
||||
DatabaseUtils.sFakeSupplier = () -> getMatrixCursor();
|
||||
|
||||
final Map<Long, Map<String, BatteryHistEntry>> batteryHistMap =
|
||||
DatabaseUtils.getHistoryMapSinceQueryTimestamp(mContext, 0);
|
||||
|
||||
assertThat(batteryHistMap).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void removeUsageSource_hasNoData() {
|
||||
DatabaseUtils.removeUsageSource(mContext);
|
||||
|
@@ -18,11 +18,15 @@ package com.android.settings.fuelgauge.batteryusage;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.robolectric.Shadows.shadowOf;
|
||||
|
||||
import android.app.AlarmManager;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.UserManager;
|
||||
|
||||
import androidx.test.core.app.ApplicationProvider;
|
||||
|
||||
@@ -34,6 +38,8 @@ import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.shadows.ShadowAlarmManager;
|
||||
|
||||
@@ -53,12 +59,17 @@ public final class PeriodicJobReceiverTest {
|
||||
private PeriodicJobManager mPeriodicJobManager;
|
||||
private ShadowAlarmManager mShadowAlarmManager;
|
||||
|
||||
@Mock
|
||||
private UserManager mUserManager;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
mContext = ApplicationProvider.getApplicationContext();
|
||||
MockitoAnnotations.initMocks(this);
|
||||
mContext = spy(ApplicationProvider.getApplicationContext());
|
||||
mPeriodicJobManager = PeriodicJobManager.getInstance(mContext);
|
||||
mShadowAlarmManager = shadowOf(mContext.getSystemService(AlarmManager.class));
|
||||
mReceiver = new PeriodicJobReceiver();
|
||||
when(mContext.getSystemService(UserManager.class)).thenReturn(mUserManager);
|
||||
|
||||
// Inserts fake data into database for testing.
|
||||
final BatteryStateDatabase database = BatteryTestUtils.setUpBatteryStateDatabase(mContext);
|
||||
@@ -114,7 +125,14 @@ public final class PeriodicJobReceiverTest {
|
||||
|
||||
@Test
|
||||
public void onReceive_inWorkProfileMode_notRefreshesJob() {
|
||||
BatteryTestUtils.setWorkProfile(mContext);
|
||||
doReturn(true).when(mUserManager).isManagedProfile();
|
||||
mReceiver.onReceive(mContext, JOB_UPDATE_INTENT);
|
||||
assertThat(mShadowAlarmManager.peekNextScheduledAlarm()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onReceive_inPrivateProfileMode_notRefreshesJob() {
|
||||
doReturn(true).when(mUserManager).isPrivateProfile();
|
||||
mReceiver.onReceive(mContext, JOB_UPDATE_INTENT);
|
||||
assertThat(mShadowAlarmManager.peekNextScheduledAlarm()).isNull();
|
||||
}
|
||||
|
@@ -18,7 +18,11 @@ package com.android.settings.fuelgauge.batteryusage.bugreport;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.spy;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.UserManager;
|
||||
|
||||
import androidx.test.core.app.ApplicationProvider;
|
||||
|
||||
@@ -27,6 +31,8 @@ import com.android.settings.testutils.BatteryTestUtils;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
|
||||
import java.io.FileDescriptor;
|
||||
@@ -46,11 +52,17 @@ public final class BugReportContentProviderTest {
|
||||
private StringWriter mStringWriter;
|
||||
private BugReportContentProvider mBugReportContentProvider;
|
||||
|
||||
@Mock
|
||||
private UserManager mUserManager;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
mStringWriter = new StringWriter();
|
||||
mPrintWriter = new PrintWriter(mStringWriter);
|
||||
mContext = ApplicationProvider.getApplicationContext();
|
||||
mContext = spy(ApplicationProvider.getApplicationContext());
|
||||
doReturn(mContext).when(mContext).getApplicationContext();
|
||||
doReturn(mUserManager).when(mContext).getSystemService(UserManager.class);
|
||||
mBugReportContentProvider = new BugReportContentProvider();
|
||||
mBugReportContentProvider.attachInfo(mContext, /* info= */ null);
|
||||
// Inserts fake data into database for testing.
|
||||
@@ -77,7 +89,14 @@ public final class BugReportContentProviderTest {
|
||||
|
||||
@Test
|
||||
public void dump_inWorkProfileMode_notDumpsBatteryUsageData() {
|
||||
BatteryTestUtils.setWorkProfile(mContext);
|
||||
doReturn(true).when(mUserManager).isManagedProfile();
|
||||
mBugReportContentProvider.dump(FileDescriptor.out, mPrintWriter, new String[] {});
|
||||
assertThat(mStringWriter.toString()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void dump_inPrivateProfileMode_notDumpsBatteryUsageData() {
|
||||
doReturn(true).when(mUserManager).isPrivateProfile();
|
||||
mBugReportContentProvider.dump(FileDescriptor.out, mPrintWriter, new String[] {});
|
||||
assertThat(mStringWriter.toString()).isEmpty();
|
||||
}
|
||||
|
Reference in New Issue
Block a user