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

Commit 2e6b585c authored by Mady Mellor's avatar Mady Mellor
Browse files

If android version is different & app has SAW, auto-grant bubbles

We've had developer feedback around this not applying to all versions
of android.

To address this feedback, this CL adds an attribute to save which
android version was last checked for the bubbles auto-grant. If
the current android version doesn't match whats saved, perform the
check for SAW again.

Additionally checks if the user has locked the field / changed the
bubble preference before, if so, don't modify it.

Adds a test that verifies we don't call app-ops if the version is the
same and adds a test for the versions being different.

Existing tests verify that the pref isn't overridden if it's locked.

Flag: EXEMPT bug fix
Test: atest PreferencesHelperTest
Bug: 372294507
Change-Id: Ic6c879ee4401b122c2bf9680be9dfe22d6baf15f
parent 5e560296
Loading
Loading
Loading
Loading
+18 −10
Original line number Diff line number Diff line
@@ -42,6 +42,7 @@ import static com.android.internal.util.FrameworkStatsLog.PACKAGE_NOTIFICATION_P
import static com.android.internal.util.FrameworkStatsLog.PACKAGE_NOTIFICATION_PREFERENCES__FSI_STATE__DENIED;
import static com.android.internal.util.FrameworkStatsLog.PACKAGE_NOTIFICATION_PREFERENCES__FSI_STATE__GRANTED;
import static com.android.internal.util.FrameworkStatsLog.PACKAGE_NOTIFICATION_PREFERENCES__FSI_STATE__NOT_REQUESTED;
import static com.android.server.notification.PreferencesHelper.LockableAppFields.USER_LOCKED_BUBBLE;
import static com.android.server.notification.PreferencesHelper.LockableAppFields.USER_LOCKED_PROMOTABLE;

import android.annotation.FlaggedApi;
@@ -155,6 +156,7 @@ public class PreferencesHelper implements RankingConfig {
    private static final String ATT_VERSION = "version";
    private static final String ATT_NAME = "name";
    private static final String ATT_UID = "uid";
    private static final String ATT_LAST_BUBBLES_VERSION_UPGRADE = "last_bubbles_version_upgrade";

    private static final String ATT_USERID = "userid";
    private static final String ATT_ID = "id";
@@ -286,7 +288,8 @@ public class PreferencesHelper implements RankingConfig {
        if (!TAG_RANKING.equals(tag)) return;

        final int xmlVersion = parser.getAttributeInt(null, ATT_VERSION, -1);
        boolean upgradeForBubbles = xmlVersion == XML_VERSION_BUBBLES_UPGRADE;
        boolean upgradeForBubbles = parser.getAttributeInt(null,
                ATT_LAST_BUBBLES_VERSION_UPGRADE, -1) < Build.VERSION.SDK_INT;
        boolean migrateToPermission = (xmlVersion < XML_VERSION_NOTIF_PERMISSION);
        if (mShowReviewPermissionsNotification
                && (xmlVersion < XML_VERSION_REVIEW_PERMISSIONS_NOTIFICATION)) {
@@ -337,15 +340,19 @@ public class PreferencesHelper implements RankingConfig {
            }
            boolean skipWarningLogged = false;
            boolean skipGroupWarningLogged = false;
            boolean hasSAWPermission = false;
            if (upgradeForBubbles && uid != UNKNOWN_UID) {
                hasSAWPermission = mAppOps.noteOpNoThrow(
                        OP_SYSTEM_ALERT_WINDOW, uid, name, null,
                        "check-notif-bubble") == AppOpsManager.MODE_ALLOWED;
            }
            int bubblePref = hasSAWPermission
                    ? BUBBLE_PREFERENCE_ALL
                    : parser.getAttributeInt(null, ATT_ALLOW_BUBBLE, DEFAULT_BUBBLE_PREFERENCE);
            int bubblePref = parser.getAttributeInt(null, ATT_ALLOW_BUBBLE,
                    DEFAULT_BUBBLE_PREFERENCE);
            boolean bubbleLocked = (parser.getAttributeInt(null,
                    ATT_APP_USER_LOCKED_FIELDS, DEFAULT_LOCKED_APP_FIELDS) & USER_LOCKED_BUBBLE)
                    != 0;
            if (!bubbleLocked
                    && upgradeForBubbles
                    && uid != UNKNOWN_UID
                    && mAppOps.noteOpNoThrow(OP_SYSTEM_ALERT_WINDOW, uid, name, null,
                    "check-notif-bubble") == AppOpsManager.MODE_ALLOWED) {
                // User hasn't changed bubble pref & the app has SAW, so allow all bubbles.
                bubblePref = BUBBLE_PREFERENCE_ALL;
            }
            int appImportance = parser.getAttributeInt(null, ATT_IMPORTANCE, DEFAULT_IMPORTANCE);

            // when data is loaded from disk it's loaded as USER_ALL, but restored data that
@@ -684,6 +691,7 @@ public class PreferencesHelper implements RankingConfig {
    public void writeXml(TypedXmlSerializer out, boolean forBackup, int userId) throws IOException {
        out.startTag(null, TAG_RANKING);
        out.attributeInt(null, ATT_VERSION, XML_VERSION);
        out.attributeInt(null, ATT_LAST_BUBBLES_VERSION_UPGRADE, Build.VERSION.SDK_INT);
        if (mHideSilentStatusBarIcons != DEFAULT_HIDE_SILENT_STATUS_BAR_ICONS) {
            out.startTag(null, TAG_STATUS_ICONS);
            out.attributeBoolean(null, ATT_HIDE_SILENT, mHideSilentStatusBarIcons);
+54 −5
Original line number Diff line number Diff line
@@ -1219,7 +1219,8 @@ public class PreferencesHelperTest extends UiServiceTestCase {

        ByteArrayOutputStream baos = writeXmlAndPurge(
                PKG_N_MR1, UID_N_MR1, false, USER_SYSTEM);
        String expected = "<ranking version=\"4\">\n"
        String expected = "<ranking version=\"4\" "
                + "last_bubbles_version_upgrade=\"" + Build.VERSION.SDK_INT + "\">\n"
                + "<package name=\"com.example.o\" show_badge=\"true\" "
                + "app_user_locked_fields=\"0\" sent_invalid_msg=\"false\" "
                + "sent_valid_msg=\"false\" user_demote_msg_app=\"false\" sent_valid_bubble"
@@ -1303,7 +1304,8 @@ public class PreferencesHelperTest extends UiServiceTestCase {

        ByteArrayOutputStream baos = writeXmlAndPurge(
                PKG_N_MR1, UID_N_MR1, true, USER_SYSTEM);
        String expected = "<ranking version=\"4\">\n"
        String expected = "<ranking version=\"4\" "
                + "last_bubbles_version_upgrade=\"" + Build.VERSION.SDK_INT + "\">\n"
                // Importance 0 because off in permissionhelper
                + "<package name=\"com.example.o\" importance=\"0\" show_badge=\"true\" "
                + "app_user_locked_fields=\"0\" sent_invalid_msg=\"false\" "
@@ -1389,7 +1391,8 @@ public class PreferencesHelperTest extends UiServiceTestCase {

        ByteArrayOutputStream baos = writeXmlAndPurge(
                PKG_N_MR1, UID_N_MR1, true, USER_SYSTEM);
        String expected = "<ranking version=\"4\">\n"
        String expected = "<ranking version=\"4\" "
                + "last_bubbles_version_upgrade=\"" + Build.VERSION.SDK_INT + "\">\n"
                // Importance 0 because off in permissionhelper
                + "<package name=\"com.example.o\" importance=\"0\" show_badge=\"true\" "
                + "app_user_locked_fields=\"0\" sent_invalid_msg=\"false\" "
@@ -1440,7 +1443,8 @@ public class PreferencesHelperTest extends UiServiceTestCase {

        ByteArrayOutputStream baos = writeXmlAndPurge(
                PKG_N_MR1, UID_N_MR1, true, USER_SYSTEM);
        String expected = "<ranking version=\"4\">\n"
        String expected = "<ranking version=\"4\" "
                + "last_bubbles_version_upgrade=\"" + Build.VERSION.SDK_INT + "\">\n"
                // Packages that exist solely in permissionhelper
                + "<package name=\"" + PKG_P + "\" importance=\"3\" />\n"
                + "<package name=\"" + PKG_O + "\" importance=\"0\" />\n"
@@ -4474,7 +4478,7 @@ public class PreferencesHelperTest extends UiServiceTestCase {
    }

    @Test
    public void testBubblePreference_upgradeWithSAWPermission() throws Exception {
    public void testBubblePreference_noLastVersionWithSAWPermission() throws Exception {
        when(mAppOpsManager.noteOpNoThrow(eq(OP_SYSTEM_ALERT_WINDOW), anyInt(),
                anyString(), eq(null), anyString())).thenReturn(MODE_ALLOWED);

@@ -4494,6 +4498,51 @@ public class PreferencesHelperTest extends UiServiceTestCase {
        assertEquals(0, mHelper.getAppLockedFields(PKG_O, UID_O));
    }

    @Test
    public void testBubblePreference_differentLastVersionWithSAWPermission() throws Exception {
        when(mAppOpsManager.noteOpNoThrow(eq(OP_SYSTEM_ALERT_WINDOW), anyInt(),
                anyString(), eq(null), anyString())).thenReturn(MODE_ALLOWED);
        final String xml = "<ranking version=\"4\" last_bubbles_version_upgrade=\"34\">\n"
                + "<package name=\"" + PKG_O + "\" uid=\"" + UID_O + "\">\n"
                + "<channel id=\"someId\" name=\"hi\""
                + " importance=\"3\"/>"
                + "</package>"
                + "</ranking>";
        TypedXmlPullParser parser = Xml.newFastPullParser();
        parser.setInput(new BufferedInputStream(new ByteArrayInputStream(xml.getBytes())),
                null);
        parser.nextTag();
        mHelper.readXml(parser, false, UserHandle.USER_ALL);

        assertEquals(BUBBLE_PREFERENCE_ALL, mHelper.getBubblePreference(PKG_O, UID_O));
        assertEquals(0, mHelper.getAppLockedFields(PKG_O, UID_O));
    }

    @Test
    public void testBubblePreference_sameLastVersionWithSAWPermission() throws Exception {
        when(mAppOpsManager.noteOpNoThrow(eq(OP_SYSTEM_ALERT_WINDOW), anyInt(),
                anyString(), eq(null), anyString())).thenReturn(MODE_ALLOWED);

        final String xml = "<ranking version=\"4\" "
                + "last_bubbles_version_upgrade=\"" + Build.VERSION.SDK_INT + "\">\n"
                + "<package name=\"" + PKG_O + "\" uid=\"" + UID_O + "\">\n"
                + "<channel id=\"someId\" name=\"hi\""
                + " importance=\"3\"/>"
                + "</package>"
                + "</ranking>";
        TypedXmlPullParser parser = Xml.newFastPullParser();
        parser.setInput(new BufferedInputStream(new ByteArrayInputStream(xml.getBytes())),
                null);
        parser.nextTag();
        mHelper.readXml(parser, false, UserHandle.USER_ALL);

        assertEquals(DEFAULT_BUBBLE_PREFERENCE, mHelper.getBubblePreference(PKG_O, UID_O));
        assertEquals(0, mHelper.getAppLockedFields(PKG_O, UID_O));
        // Version was the same SAW check should not have happened
        verify(mAppOpsManager, never()).noteOpNoThrow(eq(OP_SYSTEM_ALERT_WINDOW), anyInt(),
                anyString(), eq(null), anyString());
    }

    @Test
    public void testBubblePreference_upgradeWithSAWThenUserOverride() throws Exception {
        when(mAppOpsManager.noteOpNoThrow(eq(OP_SYSTEM_ALERT_WINDOW), anyInt(),