diff --git a/res/values/strings.xml b/res/values/strings.xml index cc7690e2c1e..355b4ba4275 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -6607,6 +6607,8 @@ No usage for past 24 hr + + Other users Battery left estimate is based on your device usage diff --git a/src/com/android/settings/accessibility/DividerAllowedBelowPreference.java b/src/com/android/settings/accessibility/DividerAllowedBelowPreference.java deleted file mode 100644 index 1be281f614b..00000000000 --- a/src/com/android/settings/accessibility/DividerAllowedBelowPreference.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright (C) 2016 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * 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.accessibility; - -import android.content.Context; -import android.util.AttributeSet; - -import androidx.preference.Preference; -import androidx.preference.PreferenceViewHolder; - -/* - * Preference that always has a divider below. Used for SUW Accessibility Settings Summary text. - */ -public class DividerAllowedBelowPreference extends Preference { - public DividerAllowedBelowPreference(Context context) { - super(context); - } - - public DividerAllowedBelowPreference(Context context, AttributeSet attrs) { - super(context, attrs); - } - - public DividerAllowedBelowPreference(Context context, AttributeSet attrs, int defStyleAttrs) { - super(context, attrs, defStyleAttrs); - } - - @Override - public void onBindViewHolder(PreferenceViewHolder holder) { - super.onBindViewHolder(holder); - holder.setDividerAllowedBelow(true); - } -} - diff --git a/src/com/android/settings/accessibility/DividerSwitchPreference.java b/src/com/android/settings/accessibility/DividerSwitchPreference.java deleted file mode 100644 index 2c44789384a..00000000000 --- a/src/com/android/settings/accessibility/DividerSwitchPreference.java +++ /dev/null @@ -1,89 +0,0 @@ -/* - * Copyright (C) 2020 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * 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.accessibility; - -import android.content.Context; -import android.view.View; - -import androidx.preference.PreferenceViewHolder; -import androidx.preference.SwitchPreference; - -/** - * A switch preference that has a divider below and above. Used for Accessibility Settings use - * service. - */ -public final class DividerSwitchPreference extends SwitchPreference { - - private Boolean mDividerAllowedAbove; - private Boolean mDividerAllowBelow; - private int mSwitchVisibility; - - public DividerSwitchPreference(Context context) { - super(context); - mDividerAllowedAbove = true; - mDividerAllowBelow = true; - mSwitchVisibility = View.VISIBLE; - } - - @Override - public void onBindViewHolder(PreferenceViewHolder holder) { - super.onBindViewHolder(holder); - holder.setDividerAllowedAbove(mDividerAllowedAbove); - holder.setDividerAllowedBelow(mDividerAllowBelow); - - final View switchView = holder.itemView.findViewById(android.R.id.widget_frame); - if (switchView != null) { - switchView.setVisibility(mSwitchVisibility); - } - } - - /** - * Sets divider whether to show in preference above. - * - * @param allowed true will be drawn on above this item - */ - public void setDividerAllowedAbove(boolean allowed) { - if (mDividerAllowedAbove != allowed) { - mDividerAllowedAbove = allowed; - notifyChanged(); - } - } - - /** - * Sets divider whether to show in preference below. - * - * @param allowed true will be drawn on below this item - */ - public void setDividerAllowedBelow(boolean allowed) { - if (mDividerAllowedAbove != allowed) { - mDividerAllowBelow = allowed; - notifyChanged(); - } - } - - /** - * Sets the visibility state of Settings view. - * - * @param visibility one of {@link View#VISIBLE}, {@link View#INVISIBLE}, or {@link View#GONE}. - */ - public void setSwitchVisibility(@View.Visibility int visibility) { - if (mSwitchVisibility != visibility) { - mSwitchVisibility = visibility; - notifyChanged(); - } - } -} diff --git a/src/com/android/settings/display/ScreenResolutionFragment.java b/src/com/android/settings/display/ScreenResolutionFragment.java index bc825149c9c..b9777032004 100644 --- a/src/com/android/settings/display/ScreenResolutionFragment.java +++ b/src/com/android/settings/display/ScreenResolutionFragment.java @@ -26,6 +26,7 @@ import android.content.res.Resources; import android.graphics.Point; import android.graphics.drawable.Drawable; import android.hardware.display.DisplayManager; +import android.provider.Settings; import android.text.TextUtils; import android.view.Display; @@ -56,6 +57,7 @@ public class ScreenResolutionFragment extends RadioButtonPickerFragment { private Resources mResources; private static final int FHD_INDEX = 0; private static final int QHD_INDEX = 1; + private static final String SCREEN_RESOLUTION = "user_selected_resolution"; private Display mDefaultDisplay; private String[] mScreenResolutionOptions; private Set mResolutions; @@ -156,8 +158,19 @@ public class ScreenResolutionFragment extends RadioButtonPickerFragment { /** Using display manager to set the display mode. */ @VisibleForTesting public void setDisplayMode(final int width) { + Display.Mode mode = getPreferMode(width); + mDisplayObserver.startObserve(); - mDefaultDisplay.setUserPreferredDisplayMode(getPreferMode(width)); + + /** For store settings globally. */ + /** TODO(b/238061217): Moving to an atom with the same string */ + Settings.System.putString( + getContext().getContentResolver(), + SCREEN_RESOLUTION, + mode.getPhysicalWidth() + "x" + mode.getPhysicalHeight()); + + /** Apply the resolution change. */ + mDefaultDisplay.setUserPreferredDisplayMode(mode); } /** Get the key corresponding to the resolution. */ @@ -186,7 +199,7 @@ public class ScreenResolutionFragment extends RadioButtonPickerFragment { protected boolean setDefaultKey(final String key) { int width = getWidthForResoluitonKey(key); if (width < 0) { - return false; + return false; } setDisplayMode(width); @@ -200,9 +213,8 @@ public class ScreenResolutionFragment extends RadioButtonPickerFragment { String selectedKey = selected.getKey(); int selectedWidth = getWidthForResoluitonKey(selectedKey); if (!mDisplayObserver.setPendingResolutionChange(selectedWidth)) { - return; + return; } - super.onRadioButtonClicked(selected); } @@ -318,7 +330,7 @@ public class ScreenResolutionFragment extends RadioButtonPickerFragment { } if (!isDensityChanged() || !isResolutionChangeApplied()) { - return; + return; } restoreDensity(); @@ -353,10 +365,10 @@ public class ScreenResolutionFragment extends RadioButtonPickerFragment { int currentWidth = getCurrentWidth(); if (selectedWidth == currentWidth) { - return false; + return false; } if (mPreviousWidth.get() != -1 && !isResolutionChangeApplied()) { - return false; + return false; } mPreviousWidth.set(currentWidth); @@ -366,7 +378,7 @@ public class ScreenResolutionFragment extends RadioButtonPickerFragment { private boolean isResolutionChangeApplied() { if (mPreviousWidth.get() == getCurrentWidth()) { - return false; + return false; } return true; diff --git a/src/com/android/settings/fuelgauge/BatteryUtils.java b/src/com/android/settings/fuelgauge/BatteryUtils.java index 3f6c1657165..a6c48a48d6b 100644 --- a/src/com/android/settings/fuelgauge/BatteryUtils.java +++ b/src/com/android/settings/fuelgauge/BatteryUtils.java @@ -69,6 +69,8 @@ public class BatteryUtils { public static final int UID_REMOVED_APPS = -4; /** Special UID value for data usage by tethering. */ public static final int UID_TETHERING = -5; + /** Special UID for aggregated other users. */ + public static final long UID_OTHER_USERS = Long.MIN_VALUE; @Retention(RetentionPolicy.SOURCE) @IntDef({StatusType.SCREEN_USAGE, diff --git a/src/com/android/settings/fuelgauge/batteryusage/BatteryDiffEntry.java b/src/com/android/settings/fuelgauge/batteryusage/BatteryDiffEntry.java index 8b995c859f9..d4d7a00fdc2 100644 --- a/src/com/android/settings/fuelgauge/batteryusage/BatteryDiffEntry.java +++ b/src/com/android/settings/fuelgauge/batteryusage/BatteryDiffEntry.java @@ -113,6 +113,9 @@ public class BatteryDiffEntry { /** Gets the app label name for this entry. */ public String getAppLabel() { + if (isOtherUsers()) { + return mContext.getString(R.string.battery_usage_other_users); + } loadLabelAndIcon(); // Returns default applicationn label if we cannot find it. return mAppLabel == null || mAppLabel.length() == 0 @@ -122,6 +125,9 @@ public class BatteryDiffEntry { /** Gets the app icon {@link Drawable} for this entry. */ public Drawable getAppIcon() { + if (isOtherUsers()) { + return mContext.getDrawable(R.drawable.ic_power_system); + } loadLabelAndIcon(); return mAppIcon != null && mAppIcon.getConstantState() != null ? mAppIcon.getConstantState().newDrawable() @@ -156,6 +162,9 @@ public class BatteryDiffEntry { /** Whether the current BatteryDiffEntry is system component or not. */ public boolean isSystemEntry() { + if (isOtherUsers()) { + return true; + } switch (mBatteryHistEntry.mConsumerType) { case ConvertUtils.CONSUMER_TYPE_USER_BATTERY: case ConvertUtils.CONSUMER_TYPE_SYSTEM_BATTERY: @@ -175,6 +184,11 @@ public class BatteryDiffEntry { return false; } + private boolean isOtherUsers() { + return mBatteryHistEntry.mConsumerType == ConvertUtils.CONSUMER_TYPE_UID_BATTERY + && mBatteryHistEntry.mUid == BatteryUtils.UID_OTHER_USERS; + } + void loadLabelAndIcon() { if (mIsLoaded) { return; diff --git a/src/com/android/settings/fuelgauge/batteryusage/ConvertUtils.java b/src/com/android/settings/fuelgauge/batteryusage/ConvertUtils.java index 76f9419ac41..168fe0f5383 100644 --- a/src/com/android/settings/fuelgauge/batteryusage/ConvertUtils.java +++ b/src/com/android/settings/fuelgauge/batteryusage/ConvertUtils.java @@ -21,6 +21,7 @@ import android.content.Context; import android.os.BatteryUsageStats; import android.os.LocaleList; import android.os.UserHandle; +import android.os.UserManager; import android.text.format.DateFormat; import android.text.format.DateUtils; import android.util.ArraySet; @@ -28,6 +29,8 @@ import android.util.Log; import androidx.annotation.VisibleForTesting; +import com.android.settings.Utils; +import com.android.settings.fuelgauge.BatteryUtils; import com.android.settings.overlay.FeatureFactory; import java.lang.annotation.Retention; @@ -265,17 +268,55 @@ public final class ConvertUtils { } } insert24HoursData(BatteryChartView.SELECTED_INDEX_ALL, resultMap); + resolveMultiUsersData(context, resultMap); if (purgeLowPercentageAndFakeData) { purgeLowPercentageAndFakeData(context, resultMap); } return resultMap; } + @VisibleForTesting + static void resolveMultiUsersData( + final Context context, + final Map> indexedUsageMap) { + final int currentUserId = context.getUserId(); + final UserHandle userHandle = + Utils.getManagedProfile(context.getSystemService(UserManager.class)); + final int workProfileUserId = + userHandle != null ? userHandle.getIdentifier() : Integer.MIN_VALUE; + // Loops for all BatteryDiffEntry in the different slots. + for (List entryList : indexedUsageMap.values()) { + double consumePowerFromOtherUsers = 0f; + double consumePercentageFromOtherUsers = 0f; + final Iterator iterator = entryList.iterator(); + while (iterator.hasNext()) { + final BatteryDiffEntry entry = iterator.next(); + final BatteryHistEntry batteryHistEntry = entry.mBatteryHistEntry; + if (batteryHistEntry.mConsumerType != CONSUMER_TYPE_UID_BATTERY) { + continue; + } + // Whether the BatteryHistEntry represents the current user data? + if (batteryHistEntry.mUserId == currentUserId + || batteryHistEntry.mUserId == workProfileUserId) { + continue; + } + // Removes and aggregates non-current users data from the list. + iterator.remove(); + consumePowerFromOtherUsers += entry.mConsumePower; + consumePercentageFromOtherUsers += entry.getPercentOfTotal(); + } + if (consumePercentageFromOtherUsers != 0) { + entryList.add(createOtherUsersEntry(context, consumePowerFromOtherUsers, + consumePercentageFromOtherUsers)); + } + } + } + private static void insert24HoursData( final int desiredIndex, final Map> indexedUsageMap) { final Map resultMap = new HashMap<>(); - double totalConsumePower = 0.0; + double totalConsumePower = 0f; // Loops for all BatteryDiffEntry and aggregate them together. for (List entryList : indexedUsageMap.values()) { for (BatteryDiffEntry entry : entryList) { @@ -361,4 +402,22 @@ public final class ConvertUtils { return locales != null && !locales.isEmpty() ? locales.get(0) : Locale.getDefault(); } + + private static BatteryDiffEntry createOtherUsersEntry( + Context context, double consumePower, double consumePercentage) { + final ContentValues values = new ContentValues(); + values.put(BatteryHistEntry.KEY_UID, BatteryUtils.UID_OTHER_USERS); + values.put(BatteryHistEntry.KEY_USER_ID, BatteryUtils.UID_OTHER_USERS); + values.put(BatteryHistEntry.KEY_CONSUMER_TYPE, CONSUMER_TYPE_UID_BATTERY); + // We will show the percentage for the "other users" item only, the aggregated + // running time information is useless for users to identify individual apps. + final BatteryDiffEntry batteryDiffEntry = new BatteryDiffEntry( + context, + /*foregroundUsageTimeInMs=*/ 0, + /*backgroundUsageTimeInMs=*/ 0, + consumePower, + new BatteryHistEntry(values)); + batteryDiffEntry.setTotalConsumePower(100 * consumePower / consumePercentage); + return batteryDiffEntry; + } } diff --git a/tests/robotests/src/com/android/settings/accessibility/DividerAllowedBelowPreferenceTest.java b/tests/robotests/src/com/android/settings/accessibility/DividerAllowedBelowPreferenceTest.java deleted file mode 100644 index 27f0623d0aa..00000000000 --- a/tests/robotests/src/com/android/settings/accessibility/DividerAllowedBelowPreferenceTest.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright (C) 2020 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * 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.accessibility; - -import static org.mockito.Mockito.spy; -import static org.mockito.Mockito.verify; -import static org.mockito.internal.verification.VerificationModeFactory.times; - -import android.content.Context; -import android.view.LayoutInflater; -import android.view.View; - -import androidx.preference.PreferenceViewHolder; - -import com.android.settings.R; - -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.robolectric.RobolectricTestRunner; -import org.robolectric.RuntimeEnvironment; - -/** Tests for {@link DividerAllowedBelowPreference}. */ -@RunWith(RobolectricTestRunner.class) -public class DividerAllowedBelowPreferenceTest { - private final Context mContext = RuntimeEnvironment.application; - private PreferenceViewHolder mViewHolder; - - @Before - public void setUp() { - final LayoutInflater inflater = LayoutInflater.from(mContext); - final View rootView = - inflater.inflate(R.layout.preference, /* root= */ null); - mViewHolder = spy(PreferenceViewHolder.createInstanceForTests(rootView)); - } - - @Test - public void onBindViewHolder_dividerAllowedBelow() { - final DividerAllowedBelowPreference dividerAllowedBelowPreference = - new DividerAllowedBelowPreference(mContext); - - dividerAllowedBelowPreference.onBindViewHolder(mViewHolder); - - // One time was in parent, the other time was in child. - verify(mViewHolder, times(2)).setDividerAllowedBelow(true); - } -} diff --git a/tests/robotests/src/com/android/settings/accessibility/DividerSwitchPreferenceTest.java b/tests/robotests/src/com/android/settings/accessibility/DividerSwitchPreferenceTest.java deleted file mode 100644 index d80a4e810e8..00000000000 --- a/tests/robotests/src/com/android/settings/accessibility/DividerSwitchPreferenceTest.java +++ /dev/null @@ -1,95 +0,0 @@ -/* - * Copyright (C) 2020 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * 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.accessibility; - -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.verify; -import static org.mockito.internal.verification.VerificationModeFactory.times; - -import android.content.Context; -import android.view.LayoutInflater; -import android.view.View; - -import androidx.preference.PreferenceViewHolder; - -import com.android.settings.R; - -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.robolectric.RobolectricTestRunner; -import org.robolectric.RuntimeEnvironment; - -/** Tests for {@link DividerSwitchPreference}. */ -@RunWith(RobolectricTestRunner.class) -public class DividerSwitchPreferenceTest { - private final Context mContext = RuntimeEnvironment.application; - private View mRootView; - private PreferenceViewHolder mViewHolder; - private DividerSwitchPreference mDividerSwitchPreference; - - @Before - public void setUp() { - initRootView(); - initPreference(); - } - - @Test - public void setDividerAllowedAbove_allowed_success() { - mDividerSwitchPreference.setDividerAllowedAbove(true); - mDividerSwitchPreference.onBindViewHolder(mViewHolder); - - // One time was in parent, the other time was in child. - verify(mViewHolder, times(2)).setDividerAllowedAbove(true); - } - - @Test - public void setDividerAllowedBelow_allowed_success() { - mDividerSwitchPreference.setDividerAllowedBelow(true); - mDividerSwitchPreference.onBindViewHolder(mViewHolder); - - // One time was in parent, the other time was in child. - verify(mViewHolder, times(2)).setDividerAllowedBelow(true); - } - - @Test - public void setSwitchVisibility_visible_success() { - final View view = spy(new View(mContext)); - doReturn(view).when(mRootView).findViewById(android.R.id.widget_frame); - - mDividerSwitchPreference.setSwitchVisibility(View.VISIBLE); - mDividerSwitchPreference.onBindViewHolder(mViewHolder); - - assertThat(view.getVisibility()).isEqualTo(View.VISIBLE); - } - - private void initRootView() { - final LayoutInflater inflater = LayoutInflater.from(mContext); - mRootView = spy(inflater.inflate(R.layout.preference_widget_switch, /* root= */ null)); - mViewHolder = spy(PreferenceViewHolder.createInstanceForTests(mRootView)); - } - - private void initPreference() { - mDividerSwitchPreference = new DividerSwitchPreference(mContext); - mDividerSwitchPreference.setDividerAllowedAbove(false); - mDividerSwitchPreference.setDividerAllowedBelow(false); - mDividerSwitchPreference.setSwitchVisibility(View.INVISIBLE); - } -} diff --git a/tests/robotests/src/com/android/settings/fuelgauge/BatteryBackupHelperTest.java b/tests/robotests/src/com/android/settings/fuelgauge/BatteryBackupHelperTest.java index 14bbeea7187..283df7bcc43 100644 --- a/tests/robotests/src/com/android/settings/fuelgauge/BatteryBackupHelperTest.java +++ b/tests/robotests/src/com/android/settings/fuelgauge/BatteryBackupHelperTest.java @@ -70,6 +70,7 @@ import org.robolectric.annotation.Resetter; import java.util.Arrays; import java.util.List; +import java.util.Set; import java.util.concurrent.TimeUnit; @RunWith(RobolectricTestRunner.class) @@ -343,9 +344,17 @@ public final class BatteryBackupHelperTest { private void verifyBackupData(String expectedResult) throws Exception { final byte[] expectedBytes = expectedResult.getBytes(); + final ArgumentCaptor captor = ArgumentCaptor.forClass(byte[].class); + final Set expectedResultSet = + Set.of(expectedResult.split(BatteryBackupHelper.DELIMITER)); + verify(mBackupDataOutput).writeEntityHeader( BatteryBackupHelper.KEY_OPTIMIZATION_LIST, expectedBytes.length); - verify(mBackupDataOutput).writeEntityData(expectedBytes, expectedBytes.length); + verify(mBackupDataOutput).writeEntityData(captor.capture(), eq(expectedBytes.length)); + final String actualResult = new String(captor.getValue()); + final Set actualResultSet = + Set.of(actualResult.split(BatteryBackupHelper.DELIMITER)); + assertThat(actualResultSet).isEqualTo(expectedResultSet); } private void createTestingData( diff --git a/tests/robotests/src/com/android/settings/fuelgauge/batteryusage/BatteryDiffEntryTest.java b/tests/robotests/src/com/android/settings/fuelgauge/batteryusage/BatteryDiffEntryTest.java index bb19b5a53dc..683f0fd2df4 100644 --- a/tests/robotests/src/com/android/settings/fuelgauge/batteryusage/BatteryDiffEntryTest.java +++ b/tests/robotests/src/com/android/settings/fuelgauge/batteryusage/BatteryDiffEntryTest.java @@ -138,7 +138,7 @@ public final class BatteryDiffEntryTest { // Generates fake testing data. final ContentValues values = getContentValuesWithType( ConvertUtils.CONSUMER_TYPE_SYSTEM_BATTERY); - values.put("drainType", + values.put(BatteryHistEntry.KEY_DRAIN_TYPE, Integer.valueOf(BatteryConsumer.POWER_COMPONENT_AMBIENT_DISPLAY)); final BatteryHistEntry batteryHistEntry = new BatteryHistEntry(values); @@ -164,7 +164,7 @@ public final class BatteryDiffEntryTest { // Generates fake testing data. final ContentValues values = getContentValuesWithType( ConvertUtils.CONSUMER_TYPE_USER_BATTERY); - values.put("userId", Integer.valueOf(1001)); + values.put(BatteryHistEntry.KEY_USER_ID, Integer.valueOf(1001)); final BatteryHistEntry batteryHistEntry = new BatteryHistEntry(values); final BatteryDiffEntry entry = createBatteryDiffEntry(10, batteryHistEntry); @@ -189,8 +189,8 @@ public final class BatteryDiffEntryTest { final String fakePackageName = "com.fake.google.com"; final ContentValues values = getContentValuesWithType( ConvertUtils.CONSUMER_TYPE_UID_BATTERY); - values.put("uid", /*invalid uid*/ 10001); - values.put("packageName", fakePackageName); + values.put(BatteryHistEntry.KEY_UID, /*invalid uid*/ 10001); + values.put(BatteryHistEntry.KEY_PACKAGE_NAME, fakePackageName); doReturn(mMockAppInfo).when(mMockPackageManager) .getApplicationInfo(fakePackageName, 0); doReturn(expectedAppLabel).when(mMockPackageManager) @@ -233,7 +233,7 @@ public final class BatteryDiffEntryTest { final String expectedAppLabel = "fake app label"; final ContentValues values = getContentValuesWithType( ConvertUtils.CONSUMER_TYPE_UID_BATTERY); - values.put("appLabel", expectedAppLabel); + values.put(BatteryHistEntry.KEY_APP_LABEL, expectedAppLabel); final BatteryHistEntry batteryHistEntry = new BatteryHistEntry(values); final BatteryDiffEntry entry = createBatteryDiffEntry(10, batteryHistEntry); @@ -391,8 +391,8 @@ public final class BatteryDiffEntryTest { final String fakePackageName = "com.fake.google.com"; final ContentValues values = getContentValuesWithType( ConvertUtils.CONSUMER_TYPE_UID_BATTERY); - values.put("uid", /*invalid uid*/ 10001); - values.put("packageName", fakePackageName); + values.put(BatteryHistEntry.KEY_UID, /*invalid uid*/ 10001); + values.put(BatteryHistEntry.KEY_PACKAGE_NAME, fakePackageName); final BatteryDiffEntry entry = createBatteryDiffEntry(10, new BatteryHistEntry(values)); @@ -424,7 +424,7 @@ public final class BatteryDiffEntryTest { final String expectedPackageName = "com.fake.google.com"; final ContentValues values = getContentValuesWithType( ConvertUtils.CONSUMER_TYPE_UID_BATTERY); - values.put("packageName", expectedPackageName); + values.put(BatteryHistEntry.KEY_PACKAGE_NAME, expectedPackageName); final BatteryDiffEntry entry = createBatteryDiffEntry(10, new BatteryHistEntry(values)); @@ -437,7 +437,7 @@ public final class BatteryDiffEntryTest { final ContentValues values = getContentValuesWithType( ConvertUtils.CONSUMER_TYPE_UID_BATTERY); values.put( - "packageName", + BatteryHistEntry.KEY_PACKAGE_NAME, expectedPackageName + ":privileged_process0"); final BatteryDiffEntry entry = createBatteryDiffEntry(10, new BatteryHistEntry(values)); @@ -445,11 +445,24 @@ public final class BatteryDiffEntryTest { assertThat(entry.getPackageName()).isEqualTo(expectedPackageName); } + @Test + public void getAppLabel_withOtherUsersUid_returnExpectedLabel() { + final ContentValues values = getContentValuesWithType( + ConvertUtils.CONSUMER_TYPE_UID_BATTERY); + values.put(BatteryHistEntry.KEY_UID, BatteryUtils.UID_OTHER_USERS); + + final BatteryDiffEntry batteryDiffEntry = createBatteryDiffEntry( + /*consumePower=*/ 0, new BatteryHistEntry(values)); + + assertThat(batteryDiffEntry.getAppLabel()) + .isEqualTo(mContext.getString(R.string.battery_usage_other_users)); + } + private BatteryDiffEntry createBatteryDiffEntry( int consumerType, long uid, boolean isHidden) { final ContentValues values = getContentValuesWithType(consumerType); - values.put("isHidden", isHidden); - values.put("uid", uid); + values.put(BatteryHistEntry.KEY_IS_HIDDEN, isHidden); + values.put(BatteryHistEntry.KEY_UID, uid); return new BatteryDiffEntry( mContext, /*foregroundUsageTimeInMs=*/ 0, @@ -472,15 +485,15 @@ public final class BatteryDiffEntryTest { private static ContentValues getContentValuesWithType(int consumerType) { final ContentValues values = new ContentValues(); - values.put("consumerType", Integer.valueOf(consumerType)); + values.put(BatteryHistEntry.KEY_CONSUMER_TYPE, Integer.valueOf(consumerType)); return values; } private BatteryDiffEntry createBatteryDiffEntry(Drawable drawable) throws Exception { final ContentValues values = getContentValuesWithType( ConvertUtils.CONSUMER_TYPE_UID_BATTERY); - values.put("uid", 1001); - values.put("packageName", "com.a.b.c"); + values.put(BatteryHistEntry.KEY_UID, 1001); + values.put(BatteryHistEntry.KEY_PACKAGE_NAME, "com.a.b.c"); final BatteryHistEntry batteryHistEntry = new BatteryHistEntry(values); doReturn(drawable).when(mMockPackageManager).getDefaultActivityIcon(); doReturn(null).when(mMockPackageManager).getApplicationInfo("com.a.b.c", 0); diff --git a/tests/robotests/src/com/android/settings/fuelgauge/batteryusage/ConvertUtilsTest.java b/tests/robotests/src/com/android/settings/fuelgauge/batteryusage/ConvertUtilsTest.java index 0b8a98a742d..c1f981539c1 100644 --- a/tests/robotests/src/com/android/settings/fuelgauge/batteryusage/ConvertUtilsTest.java +++ b/tests/robotests/src/com/android/settings/fuelgauge/batteryusage/ConvertUtilsTest.java @@ -27,6 +27,7 @@ import android.os.BatteryUsageStats; import android.os.LocaleList; import android.os.UserHandle; +import com.android.settings.fuelgauge.BatteryUtils; import com.android.settings.fuelgauge.PowerUsageFeatureProvider; import com.android.settings.testutils.FakeFeatureFactory; @@ -39,6 +40,7 @@ import org.robolectric.RobolectricTestRunner; import org.robolectric.RuntimeEnvironment; import java.util.Arrays; +import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -371,6 +373,71 @@ public final class ConvertUtilsTest { assertThat(ConvertUtils.getLocale(mContext)).isEqualTo(Locale.getDefault()); } + @Test + public void resolveMultiUsersData_replaceOtherUsersItemWithExpectedEntry() { + final int currentUserId = mContext.getUserId(); + final Map> entryMap = new HashMap<>(); + // Without other users time slot. + entryMap.put(0, Arrays.asList( + createBatteryDiffEntry( + currentUserId, + ConvertUtils.CONSUMER_TYPE_UID_BATTERY, + /*consumePercentage=*/ 50))); + // With other users time slot. + final List withOtherUsersList = new ArrayList<>(); + entryMap.put(1, withOtherUsersList); + withOtherUsersList.add( + createBatteryDiffEntry( + currentUserId + 1, + ConvertUtils.CONSUMER_TYPE_SYSTEM_BATTERY, + /*consumePercentage=*/ 20)); + withOtherUsersList.add( + createBatteryDiffEntry( + currentUserId + 2, + ConvertUtils.CONSUMER_TYPE_UID_BATTERY, + /*consumePercentage=*/ 30)); + withOtherUsersList.add( + createBatteryDiffEntry( + currentUserId + 3, + ConvertUtils.CONSUMER_TYPE_UID_BATTERY, + /*consumePercentage=*/ 40)); + + ConvertUtils.resolveMultiUsersData(mContext, entryMap); + + assertThat(entryMap.get(0).get(0).getPercentOfTotal()).isEqualTo(50); + // Asserts with other users items. + final List entryList = entryMap.get(1); + assertThat(entryList).hasSize(2); + assertBatteryDiffEntry( + entryList.get(0), + currentUserId + 1, + /*uid=*/ 0, + ConvertUtils.CONSUMER_TYPE_SYSTEM_BATTERY, + /*consumePercentage=*/ 20); + assertBatteryDiffEntry( + entryList.get(1), + BatteryUtils.UID_OTHER_USERS, + BatteryUtils.UID_OTHER_USERS, + ConvertUtils.CONSUMER_TYPE_UID_BATTERY, + /*consumePercentage=*/ 70); + } + + private BatteryDiffEntry createBatteryDiffEntry( + long userId, int counsumerType, double consumePercentage) { + final ContentValues values = new ContentValues(); + values.put(BatteryHistEntry.KEY_USER_ID, userId); + values.put(BatteryHistEntry.KEY_CONSUMER_TYPE, counsumerType); + final BatteryDiffEntry batteryDiffEntry = + new BatteryDiffEntry( + mContext, + /*foregroundUsageTimeInMs=*/ 0, + /*backgroundUsageTimeInMs=*/ 0, + /*consumePower=*/ consumePercentage, + new BatteryHistEntry(values)); + batteryDiffEntry.setTotalConsumePower(100f); + return batteryDiffEntry; + } + private static BatteryHistEntry createBatteryHistEntry( String packageName, String appLabel, double consumePower, long uid, long foregroundUsageTimeInMs, long backgroundUsageTimeInMs) { @@ -389,6 +456,15 @@ public final class ConvertUtilsTest { return new BatteryHistEntry(values); } + private static void assertBatteryDiffEntry( + BatteryDiffEntry entry, long userId, long uid, int counsumerType, + double consumePercentage) { + assertThat(entry.mBatteryHistEntry.mUid).isEqualTo(uid); + assertThat(entry.mBatteryHistEntry.mUserId).isEqualTo(userId); + assertThat(entry.mBatteryHistEntry.mConsumerType).isEqualTo(counsumerType); + assertThat(entry.getPercentOfTotal()).isEqualTo(consumePercentage); + } + private static void assertBatteryDiffEntry( BatteryDiffEntry entry, int percentOfTotal, long foregroundUsageTimeInMs, long backgroundUsageTimeInMs) { diff --git a/tests/unit/src/com/android/settings/display/ScreenResolutionFragmentTest.java b/tests/unit/src/com/android/settings/display/ScreenResolutionFragmentTest.java index b7d37df0d19..4e674d271cb 100644 --- a/tests/unit/src/com/android/settings/display/ScreenResolutionFragmentTest.java +++ b/tests/unit/src/com/android/settings/display/ScreenResolutionFragmentTest.java @@ -27,15 +27,12 @@ import android.view.Display; import androidx.test.annotation.UiThreadTest; import androidx.test.core.app.ApplicationProvider; -import androidx.test.ext.junit.runners.AndroidJUnit4; import com.android.settingslib.widget.SelectorWithWidgetPreference; import org.junit.Before; import org.junit.Test; -import org.junit.runner.RunWith; -@RunWith(AndroidJUnit4.class) public class ScreenResolutionFragmentTest { private Context mContext; @@ -56,6 +53,7 @@ public class ScreenResolutionFragmentTest { public void getDefaultKey_FHD() { Display.Mode mode = new Display.Mode(0, FHD_WIDTH, 0, 0); doReturn(mode).when(mFragment).getDisplayMode(); + doReturn(mContext).when(mFragment).getContext(); mFragment.onAttach(mContext); assertThat(mFragment.getDefaultKey()).isEqualTo(mFragment.getKeyForResolution(FHD_WIDTH)); @@ -66,6 +64,7 @@ public class ScreenResolutionFragmentTest { public void getDefaultKey_QHD() { Display.Mode mode = new Display.Mode(0, QHD_WIDTH, 0, 0); doReturn(mode).when(mFragment).getDisplayMode(); + doReturn(mContext).when(mFragment).getContext(); mFragment.onAttach(mContext); assertThat(mFragment.getDefaultKey()).isEqualTo(mFragment.getKeyForResolution(QHD_WIDTH)); @@ -74,6 +73,7 @@ public class ScreenResolutionFragmentTest { @Test @UiThreadTest public void setDefaultKey_FHD() { + doReturn(mContext).when(mFragment).getContext(); mFragment.onAttach(mContext); mFragment.setDefaultKey(mFragment.getKeyForResolution(FHD_WIDTH)); @@ -84,6 +84,7 @@ public class ScreenResolutionFragmentTest { @Test @UiThreadTest public void setDefaultKey_QHD() { + doReturn(mContext).when(mFragment).getContext(); mFragment.onAttach(mContext); mFragment.setDefaultKey(mFragment.getKeyForResolution(QHD_WIDTH)); @@ -94,6 +95,7 @@ public class ScreenResolutionFragmentTest { @Test @UiThreadTest public void bindPreferenceExtra_setSummary() { + doReturn(mContext).when(mFragment).getContext(); mFragment.onAttach(mContext); SelectorWithWidgetPreference preference = new SelectorWithWidgetPreference(mContext); ScreenResolutionFragment.ScreenResolutionCandidateInfo candidates =