Fix power usage detail page is launched in wrong user

Two issues:
1. UID of settings app == system UID. But isSystemUid failed to recognize
Settings in secondary user is also a system uid.
2. For USER drain type, we should launch the detail page in current
   user.

Fix: 64506728
Test: make ROBOTEST_FILTER=AdvancedPowerUsageDetailTest -j40 RunSettingsRoboTests
Test: Switch to seconday user. Go to Settings->Battery, tap owner user
      battery entry, observe that detail page is shown.
Test: Stay long enough in Settings app, make sure no
      either Settings nor Android Framework battery entry.

Change-Id: I8d66ad55f18fcb3d9567b3bf753f737f5b98c609
This commit is contained in:
Tony Mak
2017-08-14 14:36:52 +01:00
parent 91f4e8981d
commit 49aea6af1f
4 changed files with 66 additions and 3 deletions

View File

@@ -16,7 +16,9 @@
package com.android.settings.fuelgauge;
import android.annotation.UserIdInt;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.LoaderManager;
import android.app.admin.DevicePolicyManager;
import android.content.Context;
@@ -152,7 +154,14 @@ public class AdvancedPowerUsageDetail extends DashboardFragment implements
caller.startPreferencePanelAsUser(fragment, AdvancedPowerUsageDetail.class.getName(), args,
R.string.battery_details_title, null,
new UserHandle(UserHandle.getUserId(sipper.getUid())));
new UserHandle(getUserIdToLaunchAdvancePowerUsageDetail(sipper)));
}
private static @UserIdInt int getUserIdToLaunchAdvancePowerUsageDetail(BatterySipper bs) {
if (bs.drainType == BatterySipper.DrainType.USER) {
return ActivityManager.getCurrentUser();
}
return UserHandle.getUserId(bs.getUid());
}
public static void startBatteryDetailPage(SettingsActivity caller, PreferenceFragment fragment,

View File

@@ -428,7 +428,8 @@ public class PowerUsageSummary extends PowerUsageBase implements
}
private static boolean isSystemUid(int uid) {
return uid >= Process.SYSTEM_UID && uid < Process.FIRST_APPLICATION_UID;
final int appUid = UserHandle.getAppId(uid);
return appUid >= Process.SYSTEM_UID && appUid < Process.FIRST_APPLICATION_UID;
}
/**

View File

@@ -53,6 +53,7 @@ import com.android.settings.TestConfig;
import com.android.settings.applications.LayoutPreference;
import com.android.settings.R;
import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settings.testutils.shadow.ShadowActivityManager;
import com.android.settings.testutils.shadow.ShadowEntityHeaderController;
import com.android.settings.widget.EntityHeaderController;
import com.android.settingslib.applications.AppUtils;
@@ -78,7 +79,7 @@ import java.util.List;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION,
shadows = ShadowEntityHeaderController.class)
shadows = {ShadowEntityHeaderController.class, ShadowActivityManager.class})
public class AdvancedPowerUsageDetailTest {
private static final String APP_LABEL = "app label";
private static final String SUMMARY = "summary";
@@ -349,6 +350,22 @@ public class AdvancedPowerUsageDetailTest {
nullable(CharSequence.class), eq(new UserHandle(10)));
}
@Test
public void testStartBatteryDetailPage_typeUser_startByCurrentUser() {
mBatterySipper.drainType = BatterySipper.DrainType.USER;
mBatterySipper.userId = 10;
final int currentUser = 20;
ShadowActivityManager.setCurrentUser(currentUser);
AdvancedPowerUsageDetail.startBatteryDetailPage(mTestActivity, mBatteryUtils, null,
mBatteryStatsHelper, 0, mBatteryEntry, USAGE_PERCENT, null);
verify(mTestActivity).startPreferencePanelAsUser(
nullable(Fragment.class), nullable(String.class), nullable(Bundle.class), anyInt(),
nullable(CharSequence.class), eq(new UserHandle(currentUser)));
}
@Test
public void testStartBatteryDetailPage_noBatteryUsage_hasBasicData() {
final ArgumentCaptor<Bundle> captor = ArgumentCaptor.forClass(Bundle.class);

View File

@@ -0,0 +1,36 @@
/*
* Copyright (C) 2017 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.testutils.shadow;
import android.app.ActivityManager;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
@Implements(ActivityManager.class)
public class ShadowActivityManager {
private static int sCurrentUserId = 0;
@Implementation
public static int getCurrentUser() {
return sCurrentUserId;
}
public static void setCurrentUser(int userId) {
sCurrentUserId = userId;
}
}