Snap for 8151064 from d74f060971 to tm-release
Change-Id: Id208853ed5cea20a1345b49535faac90f6a71d36
This commit is contained in:
@@ -20,6 +20,8 @@ import static android.app.AppOpsManager.MODE_ALLOWED;
|
||||
import static android.app.AppOpsManager.MODE_DEFAULT;
|
||||
import static android.app.AppOpsManager.MODE_IGNORED;
|
||||
import static android.app.AppOpsManager.OPSTR_AUTO_REVOKE_PERMISSIONS_IF_UNUSED;
|
||||
import static android.permission.PermissionControllerManager.HIBERNATION_ELIGIBILITY_EXEMPT_BY_SYSTEM;
|
||||
import static android.permission.PermissionControllerManager.HIBERNATION_ELIGIBILITY_UNKNOWN;
|
||||
import static android.provider.DeviceConfig.NAMESPACE_APP_HIBERNATION;
|
||||
|
||||
import static com.android.settings.Utils.PROPERTY_APP_HIBERNATION_ENABLED;
|
||||
@@ -29,6 +31,7 @@ import android.app.AppOpsManager;
|
||||
import android.apphibernation.AppHibernationManager;
|
||||
import android.content.Context;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.permission.PermissionControllerManager;
|
||||
import android.provider.DeviceConfig;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Slog;
|
||||
@@ -51,7 +54,10 @@ public final class HibernationSwitchPreferenceController extends AppInfoPreferen
|
||||
private static final String TAG = "HibernationSwitchPrefController";
|
||||
private String mPackageName;
|
||||
private final AppOpsManager mAppOpsManager;
|
||||
private final PermissionControllerManager mPermissionControllerManager;
|
||||
private int mPackageUid;
|
||||
private boolean mHibernationEligibilityLoaded;
|
||||
private int mHibernationEligibility = HIBERNATION_ELIGIBILITY_UNKNOWN;
|
||||
@VisibleForTesting
|
||||
boolean mIsPackageSet;
|
||||
private boolean mIsPackageExemptByDefault;
|
||||
@@ -60,6 +66,7 @@ public final class HibernationSwitchPreferenceController extends AppInfoPreferen
|
||||
String preferenceKey) {
|
||||
super(context, preferenceKey);
|
||||
mAppOpsManager = context.getSystemService(AppOpsManager.class);
|
||||
mPermissionControllerManager = context.getSystemService(PermissionControllerManager.class);
|
||||
}
|
||||
|
||||
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
|
||||
@@ -109,10 +116,27 @@ public final class HibernationSwitchPreferenceController extends AppInfoPreferen
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isAppEligibleForHibernation() {
|
||||
return mHibernationEligibilityLoaded
|
||||
&& mHibernationEligibility != HIBERNATION_ELIGIBILITY_EXEMPT_BY_SYSTEM
|
||||
&& mHibernationEligibility != HIBERNATION_ELIGIBILITY_UNKNOWN;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateState(Preference preference) {
|
||||
super.updateState(preference);
|
||||
((SwitchPreference) preference).setChecked(!isPackageHibernationExemptByUser());
|
||||
((SwitchPreference) preference).setChecked(isAppEligibleForHibernation()
|
||||
&& !isPackageHibernationExemptByUser());
|
||||
preference.setEnabled(isAppEligibleForHibernation());
|
||||
if (!mHibernationEligibilityLoaded) {
|
||||
mPermissionControllerManager.getHibernationEligibility(mPackageName,
|
||||
mContext.getMainExecutor(),
|
||||
eligibility -> {
|
||||
mHibernationEligibility = eligibility;
|
||||
mHibernationEligibilityLoaded = true;
|
||||
updateState(preference);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
|
||||
@@ -16,19 +16,17 @@
|
||||
|
||||
package com.android.settings.display.darkmode;
|
||||
|
||||
import static android.provider.Settings.ACTION_BEDTIME_SETTINGS;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.pm.ResolveInfo;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
|
||||
/** Manages Digital Wellbeing bedtime settings' intents. */
|
||||
public final class BedtimeSettings {
|
||||
@VisibleForTesting
|
||||
public static final String ACTION_BEDTIME_SETTINGS = "android.settings.BEDTIME_SETTINGS";
|
||||
|
||||
private final Context mContext;
|
||||
private final PackageManager mPackageManager;
|
||||
private final String mWellbeingPackage;
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
|
||||
package com.android.settings.testutils;
|
||||
|
||||
import static com.android.settings.display.darkmode.BedtimeSettings.ACTION_BEDTIME_SETTINGS;
|
||||
import static android.provider.Settings.ACTION_BEDTIME_SETTINGS;
|
||||
|
||||
import static org.robolectric.Shadows.shadowOf;
|
||||
|
||||
|
||||
@@ -62,7 +62,6 @@ import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
@@ -89,6 +88,7 @@ public class UserDetailsSettingsTest {
|
||||
private static final String KEY_ENABLE_TELEPHONY = "enable_calling";
|
||||
private static final String KEY_REMOVE_USER = "remove_user";
|
||||
private static final String KEY_APP_AND_CONTENT_ACCESS = "app_and_content_access";
|
||||
private static final String KEY_APP_COPYING = "app_copying";
|
||||
|
||||
private static final int DIALOG_CONFIRM_REMOVE = 1;
|
||||
|
||||
@@ -105,6 +105,8 @@ public class UserDetailsSettingsTest {
|
||||
private Preference mRemoveUserPref;
|
||||
@Mock
|
||||
private Preference mAppAndContentAccessPref;
|
||||
@Mock
|
||||
private Preference mAppCopyingPref;
|
||||
|
||||
private FragmentActivity mActivity;
|
||||
private Context mContext;
|
||||
@@ -145,6 +147,7 @@ public class UserDetailsSettingsTest {
|
||||
doReturn(mRemoveUserPref).when(mFragment).findPreference(KEY_REMOVE_USER);
|
||||
doReturn(mAppAndContentAccessPref)
|
||||
.when(mFragment).findPreference(KEY_APP_AND_CONTENT_ACCESS);
|
||||
doReturn(mAppCopyingPref).when(mFragment).findPreference(KEY_APP_COPYING);
|
||||
}
|
||||
|
||||
@After
|
||||
@@ -163,7 +166,6 @@ public class UserDetailsSettingsTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void initialize_userSelected_shouldSetupSwitchPref() {
|
||||
setupSelectedUser();
|
||||
doReturn("Switch to " + mUserInfo.name)
|
||||
@@ -179,7 +181,6 @@ public class UserDetailsSettingsTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void initialize_guestSelected_shouldSetupSwitchPref() {
|
||||
setupSelectedGuest();
|
||||
doReturn("Switch to " + mUserInfo.name)
|
||||
@@ -195,7 +196,6 @@ public class UserDetailsSettingsTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void initialize_userSelected_shouldNotShowAppAndContentPref() {
|
||||
setupSelectedUser();
|
||||
|
||||
@@ -205,7 +205,6 @@ public class UserDetailsSettingsTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void initialize_guestSelected_shouldNotShowAppAndContentPref() {
|
||||
setupSelectedGuest();
|
||||
|
||||
@@ -259,7 +258,6 @@ public class UserDetailsSettingsTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void initialize_adminWithTelephony_shouldShowPhonePreference() {
|
||||
setupSelectedUser();
|
||||
doReturn(true).when(mTelephonyManager).isVoiceCapable();
|
||||
@@ -272,7 +270,6 @@ public class UserDetailsSettingsTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void initialize_adminNoTelephony_shouldNotShowPhonePreference() {
|
||||
setupSelectedUser();
|
||||
doReturn(false).when(mTelephonyManager).isVoiceCapable();
|
||||
@@ -306,7 +303,6 @@ public class UserDetailsSettingsTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void initialize_adminSelectsSecondaryUser_shouldShowRemovePreference() {
|
||||
setupSelectedUser();
|
||||
mUserManager.setIsAdminUser(true);
|
||||
@@ -319,7 +315,6 @@ public class UserDetailsSettingsTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void initialize_adminSelectsNewRestrictedUser_shouldOpenAppContentScreen() {
|
||||
setupSelectedRestrictedUser();
|
||||
mUserManager.setIsAdminUser(true);
|
||||
@@ -342,7 +337,6 @@ public class UserDetailsSettingsTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void initialize_adminSelectsRestrictedUser_shouldSetupPreferences() {
|
||||
setupSelectedRestrictedUser();
|
||||
mUserManager.setIsAdminUser(true);
|
||||
@@ -361,7 +355,6 @@ public class UserDetailsSettingsTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void initialize_adminSelectsExistingRestrictedUser_shouldNotStartAppAndContentAccess() {
|
||||
setupSelectedRestrictedUser();
|
||||
mUserManager.setIsAdminUser(true);
|
||||
@@ -373,7 +366,6 @@ public class UserDetailsSettingsTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void initialize_adminSelectsGuest_shouldShowRemovePreference() {
|
||||
setupSelectedGuest();
|
||||
mUserManager.setIsAdminUser(true);
|
||||
@@ -396,7 +388,6 @@ public class UserDetailsSettingsTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void initialize_disallowRemoveUserRestriction_shouldNotShowRemovePreference() {
|
||||
setupSelectedUser();
|
||||
mUserManager.setIsAdminUser(true);
|
||||
@@ -408,7 +399,6 @@ public class UserDetailsSettingsTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void initialize_userHasCallRestriction_shouldSetPhoneSwitchUnChecked() {
|
||||
setupSelectedUser();
|
||||
mUserManager.setIsAdminUser(true);
|
||||
@@ -421,7 +411,6 @@ public class UserDetailsSettingsTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void initialize_noCallRestriction_shouldSetPhoneSwitchChecked() {
|
||||
setupSelectedUser();
|
||||
mUserManager.setIsAdminUser(true);
|
||||
@@ -432,7 +421,6 @@ public class UserDetailsSettingsTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void initialize_guestSelected_noCallRestriction_shouldSetPhonePreference() {
|
||||
setupSelectedGuest();
|
||||
mUserManager.setIsAdminUser(true);
|
||||
@@ -444,7 +432,6 @@ public class UserDetailsSettingsTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void initialize_guestSelected_callRestriction_shouldSetPhonePreference() {
|
||||
setupSelectedGuest();
|
||||
mUserManager.setIsAdminUser(true);
|
||||
@@ -457,7 +444,6 @@ public class UserDetailsSettingsTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void initialize_switchUserDisallowed_shouldSetAdminDisabledOnSwitchPreference() {
|
||||
setupSelectedUser();
|
||||
mUserCapabilities.mDisallowSwitchUser = true;
|
||||
@@ -473,7 +459,6 @@ public class UserDetailsSettingsTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void initialize_switchUserAllowed_shouldSetSwitchPreferenceEnabled() {
|
||||
setupSelectedUser();
|
||||
mUserCapabilities.mDisallowSwitchUser = false;
|
||||
|
||||
@@ -27,6 +27,8 @@ import static com.android.settings.core.BasePreferenceController.AVAILABLE;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.anyInt;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.spy;
|
||||
@@ -43,6 +45,7 @@ import androidx.preference.SwitchPreference;
|
||||
import androidx.test.core.app.ApplicationProvider;
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@@ -68,6 +71,7 @@ public class HibernationSwitchPreferenceControllerTest {
|
||||
|
||||
private HibernationSwitchPreferenceController mController;
|
||||
private Context mContext;
|
||||
private String mOriginalPreSFlagValue;
|
||||
|
||||
@Before
|
||||
public void setUp() throws PackageManager.NameNotFoundException {
|
||||
@@ -89,6 +93,16 @@ public class HibernationSwitchPreferenceControllerTest {
|
||||
"true", true /* makeDefault */);
|
||||
mController = new HibernationSwitchPreferenceController(mContext, KEY);
|
||||
when(mPreference.getKey()).thenReturn(mController.getPreferenceKey());
|
||||
|
||||
mOriginalPreSFlagValue = DeviceConfig.getProperty(NAMESPACE_APP_HIBERNATION,
|
||||
PROPERTY_HIBERNATION_TARGETS_PRE_S_APPS);
|
||||
}
|
||||
|
||||
@After
|
||||
public void cleanUp() {
|
||||
// Restore original device config values.
|
||||
DeviceConfig.setProperty(NAMESPACE_APP_HIBERNATION, PROPERTY_HIBERNATION_TARGETS_PRE_S_APPS,
|
||||
mOriginalPreSFlagValue, true /* makeDefault */);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -124,43 +138,37 @@ public class HibernationSwitchPreferenceControllerTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateState_exemptedByDefaultPackage_shouldNotCheck() {
|
||||
public void isPackageHibernationExemptByUser_preSAppShouldBeExemptByDefault() {
|
||||
when(mAppOpsManager.unsafeCheckOpNoThrow(
|
||||
eq(OPSTR_AUTO_REVOKE_PERMISSIONS_IF_UNUSED), anyInt(), eq(EXEMPTED_PACKAGE_NAME)))
|
||||
.thenReturn(MODE_DEFAULT);
|
||||
mController.setPackage(EXEMPTED_PACKAGE_NAME);
|
||||
|
||||
mController.updateState(mPreference);
|
||||
|
||||
verify(mPreference).setChecked(false);
|
||||
assertTrue(mController.isPackageHibernationExemptByUser());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateState_exemptedPackageOverrideByUser_shouldCheck() {
|
||||
public void isPackageHibernationExemptByUser_preSAppShouldNotBeExemptWithUserSetting() {
|
||||
when(mAppOpsManager.unsafeCheckOpNoThrow(
|
||||
eq(OPSTR_AUTO_REVOKE_PERMISSIONS_IF_UNUSED), anyInt(), eq(EXEMPTED_PACKAGE_NAME)))
|
||||
.thenReturn(MODE_ALLOWED);
|
||||
mController.setPackage(EXEMPTED_PACKAGE_NAME);
|
||||
|
||||
mController.updateState(mPreference);
|
||||
|
||||
verify(mPreference).setChecked(true);
|
||||
assertFalse(mController.isPackageHibernationExemptByUser());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateState_unexemptedPackageOverrideByUser_shouldNotCheck() {
|
||||
public void isPackageHibernationExemptByUser_SAppShouldBeExemptWithUserSetting() {
|
||||
when(mAppOpsManager.unsafeCheckOpNoThrow(
|
||||
eq(OPSTR_AUTO_REVOKE_PERMISSIONS_IF_UNUSED), anyInt(), eq(UNEXEMPTED_PACKAGE_NAME)))
|
||||
.thenReturn(MODE_IGNORED);
|
||||
mController.setPackage(UNEXEMPTED_PACKAGE_NAME);
|
||||
|
||||
mController.updateState(mPreference);
|
||||
|
||||
verify(mPreference).setChecked(false);
|
||||
assertTrue(mController.isPackageHibernationExemptByUser());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateState_exemptedByDefaultPackageOverriddenByPreSFlag_shouldCheck() {
|
||||
public void isPackageHibernationExemptByUser_preSAppShouldNotBeExemptByDefaultWithPreSFlag() {
|
||||
DeviceConfig.setProperty(NAMESPACE_APP_HIBERNATION, PROPERTY_HIBERNATION_TARGETS_PRE_S_APPS,
|
||||
"true", true /* makeDefault */);
|
||||
when(mAppOpsManager.unsafeCheckOpNoThrow(
|
||||
@@ -168,8 +176,6 @@ public class HibernationSwitchPreferenceControllerTest {
|
||||
.thenReturn(MODE_DEFAULT);
|
||||
mController.setPackage(EXEMPTED_PACKAGE_NAME);
|
||||
|
||||
mController.updateState(mPreference);
|
||||
|
||||
verify(mPreference).setChecked(true);
|
||||
assertFalse(mController.isPackageHibernationExemptByUser());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user