Prevent disabling default phone/sms apps in battery setting

- In handleDisableable(), call getKeepEnabledPackages() in the same way
  as AppActionButtonPreferenceController.
- Update getKeepEnabledPackages() to dynamically query default phone/sms
  app packages.

Change-Id: I8dc7d6248cf440dcc053f6acba9d5548d5670c41
Bug: 80328396
Bug: 80312809
Test: robotests
This commit is contained in:
Fan Zhang
2018-05-25 14:59:40 -07:00
parent 58019a84ed
commit b70b11e02b
5 changed files with 141 additions and 15 deletions

View File

@@ -20,6 +20,7 @@ import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.when;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
@@ -34,17 +35,23 @@ import android.os.UserManager;
import com.android.settings.testutils.ApplicationTestUtils;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.testutils.shadow.ShadowDefaultDialerManager;
import com.android.settings.testutils.shadow.ShadowSmsApplication;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowApplication;
import org.robolectric.util.ReflectionHelpers;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
/**
* Tests for {@link ApplicationFeatureProviderImpl}.
@@ -246,8 +253,18 @@ public final class ApplicationFeatureProviderImplTest {
}
@Test
public void getKeepEnabledPackages_shouldContainNothing() {
assertThat(mProvider.getKeepEnabledPackages()).isEmpty();
@Config(shadows = {ShadowSmsApplication.class, ShadowDefaultDialerManager.class})
public void getKeepEnabledPackages_shouldContainDefaultPhoneAndSms() {
final String testDialer = "com.android.test.defaultdialer";
final String testSms = "com.android.test.defaultsms";
ShadowSmsApplication.setDefaultSmsApplication(new ComponentName(testSms, "receiver"));
ShadowDefaultDialerManager.setDefaultDialerApplication(testDialer);
ReflectionHelpers.setField(mProvider, "mContext", RuntimeEnvironment.application);
final Set<String> keepEnabledPackages = mProvider.getKeepEnabledPackages();
final List<String> expectedPackages = Arrays.asList(testDialer, testSms);
assertThat(keepEnabledPackages).containsExactlyElementsIn(expectedPackages);
}
private void setUpUsersAndInstalledApps() {

View File

@@ -0,0 +1,44 @@
/*
* Copyright (C) 2018 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.content.Context;
import android.telecom.DefaultDialerManager;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.robolectric.annotation.Resetter;
@Implements(DefaultDialerManager.class)
public class ShadowDefaultDialerManager {
private static String sDefaultDailer;
@Resetter
public void reset() {
sDefaultDailer = null;
}
@Implementation
public static String getDefaultDialerApplication(Context context) {
return sDefaultDailer;
}
public static void setDefaultDialerApplication(String dialer) {
sDefaultDailer = dialer;
}
}

View File

@@ -0,0 +1,46 @@
/*
* Copyright (C) 2018 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.content.ComponentName;
import android.content.Context;
import com.android.internal.telephony.SmsApplication;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.robolectric.annotation.Resetter;
@Implements(SmsApplication.class)
public class ShadowSmsApplication {
private static ComponentName sDefaultSmsApplication;
@Resetter
public void reset() {
sDefaultSmsApplication = null;
}
@Implementation
public static ComponentName getDefaultSmsApplication(Context context, boolean updateIfNeeded) {
return sDefaultSmsApplication;
}
public static void setDefaultSmsApplication(ComponentName cn) {
sDefaultSmsApplication = cn;
}
}