Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f0367c98 authored by Matías Hernández's avatar Matías Hernández
Browse files

Settings: don't try to allow NLSes with too-long component names

* NotificationAccessConfirmationActivity (triggered through CompanionDeviceManager) -> Don't show the dialog, bail out early similarly to other invalid inputs.
* NotificationAccessSettings (from Special App Access) -> No changes, but use the canonical constant now.
* ApprovalPreferenceController (used in NotificationAccessDetails) -> Disable the toggle, unless the NLS was previously approved (in which case it can still be removed).

Fixes: 260570119
Fixes: 286043036
Test: atest + manually
Change-Id: Ifc048311746c027e3683cdcf65f1079d04cf7c56
Merged-In: Ifc048311746c027e3683cdcf65f1079d04cf7c56
parent 78a4048e
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -81,6 +81,9 @@ public class ApprovalPreferenceController extends BasePreferenceController {
        final SwitchPreference preference = (SwitchPreference) pref;
        final CharSequence label = mPkgInfo.applicationInfo.loadLabel(mPm);
        preference.setChecked(isServiceEnabled(mCn));
        final boolean isAllowedCn = mCn.flattenToShortString().length()
                <= NotificationManager.MAX_SERVICE_COMPONENT_NAME_LENGTH;
        preference.setEnabled(preference.isChecked() || isAllowedCn);
        preference.setOnPreferenceChangeListener((p, newValue) -> {
            final boolean access = (Boolean) newValue;
            if (!access) {
+3 −1
Original line number Diff line number Diff line
@@ -67,7 +67,9 @@ public class NotificationAccessConfirmationActivity extends Activity
        mUserId = getIntent().getIntExtra(EXTRA_USER_ID, UserHandle.USER_NULL);
        CharSequence mAppLabel;

        if (mComponentName == null || mComponentName.getPackageName() == null) {
        if (mComponentName == null || mComponentName.getPackageName() == null
                || mComponentName.flattenToString().length()
                > NotificationManager.MAX_SERVICE_COMPONENT_NAME_LENGTH) {
            finish();
            return;
        }
+2 −2
Original line number Diff line number Diff line
@@ -63,7 +63,6 @@ public class NotificationAccessSettings extends EmptyTextSettings {
    private static final String TAG = "NotifAccessSettings";
    static final String ALLOWED_KEY = "allowed";
    static final String NOT_ALLOWED_KEY = "not_allowed";
    private static final int MAX_CN_LENGTH = 500;

    private static final ManagedServiceSettings.Config CONFIG =
            new ManagedServiceSettings.Config.Builder()
@@ -145,7 +144,8 @@ public class NotificationAccessSettings extends EmptyTextSettings {
        for (ServiceInfo service : services) {
            final ComponentName cn = new ComponentName(service.packageName, service.name);
            boolean isAllowed = mNm.isNotificationListenerAccessGranted(cn);
            if (!isAllowed && cn.flattenToString().length() > MAX_CN_LENGTH) {
            if (!isAllowed && cn.flattenToString().length()
                    > NotificationManager.MAX_SERVICE_COMPONENT_NAME_LENGTH) {
                continue;
            }

+19 −0
Original line number Diff line number Diff line
@@ -77,6 +77,25 @@ public class ApprovalPreferenceControllerTest {
        mController.setPkgInfo(mPkgInfo);
    }

    @Test
    public void updateState_enabled() {
        SwitchPreference pref = new SwitchPreference(mContext);
        mController.updateState(pref);
        assertThat(pref.isEnabled()).isTrue();
    }

    @Test
    public void updateState_invalidCn_disabled() {
        ComponentName longCn = new ComponentName("com.example.package",
                com.google.common.base.Strings.repeat("Blah", 150));
        mController.setCn(longCn);
        SwitchPreference pref = new SwitchPreference(mContext);

        mController.updateState(pref);

        assertThat(pref.isEnabled()).isFalse();
    }

    @Test
    public void updateState_checked() {
        when(mNm.isNotificationListenerAccessGranted(mCn)).thenReturn(true);