Fix crash when there is no emergencybroadcast app on device

Some devices do not have emergencybroadcast app installed, so we should
not show the preference item for it. Add the preference controller into
AppAndNotificationFragment properly hides it.

Change-Id: Ic39ba24da9bd8f3200a5e44232a5489e35c48c0a
Fix: 37945069
Test: make RunSettingsRoboTests
This commit is contained in:
Fan Zhang
2017-05-05 15:03:19 -07:00
parent 651261c7dd
commit 0335cd1e9b
4 changed files with 36 additions and 22 deletions

View File

@@ -23,6 +23,7 @@ import com.android.internal.logging.nano.MetricsProto;
import com.android.settings.R;
import com.android.settings.core.PreferenceController;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.notification.EmergencyBroadcastPreferenceController;
import com.android.settings.search.BaseSearchIndexProvider;
import java.util.ArrayList;
@@ -61,6 +62,8 @@ public class AppAndNotificationDashboardFragment extends DashboardFragment {
private static List<PreferenceController> buildPreferenceControllers(Context context) {
final List<PreferenceController> controllers = new ArrayList<>();
controllers.add(new EmergencyBroadcastPreferenceController(context,
"app_and_notif_cell_broadcast_settings"));
controllers.add(new SpecialAppAccessPreferenceController(context));
return controllers;
}

View File

@@ -20,9 +20,9 @@ import android.content.Context;
import android.content.pm.PackageManager;
import android.os.UserHandle;
import android.os.UserManager;
import android.support.annotation.VisibleForTesting;
import android.support.v7.preference.Preference;
import com.android.internal.annotations.VisibleForTesting;
import com.android.settings.accounts.AccountRestrictionHelper;
import com.android.settings.core.PreferenceController;
import com.android.settingslib.RestrictedPreference;
@@ -33,20 +33,22 @@ import com.android.settingslib.RestrictedPreference;
*/
public class EmergencyBroadcastPreferenceController extends PreferenceController {
private static final String KEY_CELL_BROADCAST_SETTINGS = "cell_broadcast_settings";
private final String mPrefKey;
private AccountRestrictionHelper mHelper;
private UserManager mUserManager;
private PackageManager mPm;
private boolean mCellBroadcastAppLinkEnabled;
public EmergencyBroadcastPreferenceController(Context context) {
this(context, new AccountRestrictionHelper(context));
public EmergencyBroadcastPreferenceController(Context context, String prefKey) {
this(context, new AccountRestrictionHelper(context), prefKey);
}
@VisibleForTesting
EmergencyBroadcastPreferenceController(Context context, AccountRestrictionHelper helper) {
@VisibleForTesting(otherwise = VisibleForTesting.NONE)
EmergencyBroadcastPreferenceController(Context context, AccountRestrictionHelper helper,
String prefKey) {
super(context);
mPrefKey = prefKey;
mHelper = helper;
mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
mPm = mContext.getPackageManager();
@@ -60,7 +62,7 @@ public class EmergencyBroadcastPreferenceController extends PreferenceController
return;
}
((RestrictedPreference) preference).checkRestrictionAndSetDisabled(
UserManager.DISALLOW_CONFIG_CELL_BROADCASTS);
UserManager.DISALLOW_CONFIG_CELL_BROADCASTS);
}
@Override
@@ -70,23 +72,23 @@ public class EmergencyBroadcastPreferenceController extends PreferenceController
@Override
public String getPreferenceKey() {
return KEY_CELL_BROADCAST_SETTINGS;
return mPrefKey;
}
@Override
public boolean isAvailable() {
return mUserManager.isAdminUser() && mCellBroadcastAppLinkEnabled
&& !mHelper.hasBaseUserRestriction(
&& !mHelper.hasBaseUserRestriction(
UserManager.DISALLOW_CONFIG_CELL_BROADCASTS, UserHandle.myUserId());
}
private boolean isCellBroadcastAppLinkEnabled() {
boolean enabled = mContext.getResources().getBoolean(
com.android.internal.R.bool.config_cellBroadcastAppLinks);
com.android.internal.R.bool.config_cellBroadcastAppLinks);
if (enabled) {
try {
if (mPm.getApplicationEnabledSetting("com.android.cellbroadcastreceiver")
== PackageManager.COMPONENT_ENABLED_STATE_DISABLED) {
== PackageManager.COMPONENT_ENABLED_STATE_DISABLED) {
enabled = false; // CMAS app disabled
}
} catch (IllegalArgumentException ignored) {

View File

@@ -186,7 +186,8 @@ public class SoundSettings extends DashboardFragment {
Lifecycle lifecycle) {
final List<PreferenceController> controllers = new ArrayList<>();
controllers.add(new ZenModePreferenceController(context));
controllers.add(new EmergencyBroadcastPreferenceController(context));
controllers.add(new EmergencyBroadcastPreferenceController(
context, "cell_broadcast_settings"));
controllers.add(new VibrateWhenRingPreferenceController(context));
// === Volumes ===

View File

@@ -44,6 +44,8 @@ import static org.mockito.Mockito.when;
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class EmergencyBroadcastPreferenceControllerTest {
private static final String PREF_TEST_KEY = "test_key";
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private Context mContext;
@Mock
@@ -62,7 +64,8 @@ public class EmergencyBroadcastPreferenceControllerTest {
MockitoAnnotations.initMocks(this);
when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
when(mContext.getPackageManager()).thenReturn(mPackageManager);
mController = new EmergencyBroadcastPreferenceController(mContext, mAccountHelper);
mController = new EmergencyBroadcastPreferenceController(mContext, mAccountHelper,
PREF_TEST_KEY);
}
@Test
@@ -72,13 +75,18 @@ public class EmergencyBroadcastPreferenceControllerTest {
verify(mPreference).checkRestrictionAndSetDisabled(anyString());
}
@Test
public void getPreferenceKey_shouldReturnKeyDefinedInConstructor() {
assertThat(mController.getPreferenceKey()).isEqualTo(PREF_TEST_KEY);
}
@Test
public void isAvailable_notAdminUser_shouldReturnFalse() {
when(mUserManager.isAdminUser()).thenReturn(false);
when(mContext.getResources().getBoolean(
com.android.internal.R.bool.config_cellBroadcastAppLinks)).thenReturn(true);
com.android.internal.R.bool.config_cellBroadcastAppLinks)).thenReturn(true);
when(mPackageManager.getApplicationEnabledSetting(anyString()))
.thenReturn(PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
.thenReturn(PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
when(mAccountHelper.hasBaseUserRestriction(anyString(), anyInt())).thenReturn(false);
assertThat(mController.isAvailable()).isFalse();
@@ -88,11 +96,11 @@ public class EmergencyBroadcastPreferenceControllerTest {
public void isAvailable_hasConfigCellBroadcastRestriction_shouldReturnFalse() {
when(mUserManager.isAdminUser()).thenReturn(true);
when(mContext.getResources().getBoolean(
com.android.internal.R.bool.config_cellBroadcastAppLinks)).thenReturn(true);
com.android.internal.R.bool.config_cellBroadcastAppLinks)).thenReturn(true);
when(mPackageManager.getApplicationEnabledSetting(anyString()))
.thenReturn(PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
.thenReturn(PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
when(mAccountHelper.hasBaseUserRestriction(
eq(UserManager.DISALLOW_CONFIG_CELL_BROADCASTS), anyInt())).thenReturn(true);
eq(UserManager.DISALLOW_CONFIG_CELL_BROADCASTS), anyInt())).thenReturn(true);
assertThat(mController.isAvailable()).isFalse();
}
@@ -101,9 +109,9 @@ public class EmergencyBroadcastPreferenceControllerTest {
public void isAvailable_cellBroadcastAppLinkDisabled_shouldReturnFalse() {
when(mUserManager.isAdminUser()).thenReturn(true);
when(mContext.getResources().getBoolean(
com.android.internal.R.bool.config_cellBroadcastAppLinks)).thenReturn(false);
com.android.internal.R.bool.config_cellBroadcastAppLinks)).thenReturn(false);
when(mPackageManager.getApplicationEnabledSetting(anyString()))
.thenReturn(PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
.thenReturn(PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
when(mAccountHelper.hasBaseUserRestriction(anyString(), anyInt())).thenReturn(false);
assertThat(mController.isAvailable()).isFalse();
@@ -113,9 +121,9 @@ public class EmergencyBroadcastPreferenceControllerTest {
public void isAvailable_cellBroadcastReceiverDisabled_shouldReturnFalse() {
when(mUserManager.isAdminUser()).thenReturn(true);
when(mContext.getResources().getBoolean(
com.android.internal.R.bool.config_cellBroadcastAppLinks)).thenReturn(true);
com.android.internal.R.bool.config_cellBroadcastAppLinks)).thenReturn(true);
when(mPackageManager.getApplicationEnabledSetting(anyString()))
.thenReturn(PackageManager.COMPONENT_ENABLED_STATE_DISABLED);
.thenReturn(PackageManager.COMPONENT_ENABLED_STATE_DISABLED);
when(mAccountHelper.hasBaseUserRestriction(anyString(), anyInt())).thenReturn(false);
assertThat(mController.isAvailable()).isFalse();