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

Commit 0a052cd5 authored by Yuri Lin's avatar Yuri Lin
Browse files

Delete WrappedSysUiStatsEvent in favor of new StatsEventTestUtils

The custom WrappedSysUiStatsEvent was used to enable testing of pulled atoms, which otherwise were only available in a previously-uninspectable StatsEvent format. Now that there is an official test utility for converting StatsEvent objects to the associated Atom (which we can read), we can inspect the contents of generated StatsEvent directly, removing the need for wrapping the builder and maintaining separate custom testing code.

Also suppresses lint warning on ZenModeHelperTest so we don't have to lock the locks to set up mConfig.

Bug: 161127189
Test: atest ZenModeHelperTest, PreferencesHelperTest

Change-Id: Ibb647adc635fcebf628c12aa0c47392a7cdc3424
parent de0b4db9
Loading
Loading
Loading
Loading
+1 −3
Original line number Diff line number Diff line
@@ -2328,8 +2328,7 @@ public class NotificationManagerService extends SystemService {
        mRankingHandler = rankingHandler;
        mConditionProviders = conditionProviders;
        mZenModeHelper = new ZenModeHelper(getContext(), mHandler.getLooper(), mConditionProviders,
                new SysUiStatsEvent.BuilderFactory(), flagResolver,
                new ZenModeEventLogger(mPackageManagerClient));
                flagResolver, new ZenModeEventLogger(mPackageManagerClient));
        mZenModeHelper.addCallback(new ZenModeHelper.Callback() {
            @Override
            public void onConfigChanged() {
@@ -2390,7 +2389,6 @@ public class NotificationManagerService extends SystemService {
                mNotificationChannelLogger,
                mAppOps,
                mUserProfiles,
                new SysUiStatsEvent.BuilderFactory(),
                mShowReviewPermissionsNotification);
        mRankingHelper = new RankingHelper(getContext(),
                mRankingHandler,
+47 −59
Original line number Diff line number Diff line
@@ -25,7 +25,6 @@ import static android.app.NotificationManager.IMPORTANCE_DEFAULT;
import static android.app.NotificationManager.IMPORTANCE_MAX;
import static android.app.NotificationManager.IMPORTANCE_NONE;
import static android.app.NotificationManager.IMPORTANCE_UNSPECIFIED;
import static android.util.StatsLog.ANNOTATION_ID_IS_UID;

import static com.android.internal.util.FrameworkStatsLog.PACKAGE_NOTIFICATION_CHANNEL_GROUP_PREFERENCES;
import static com.android.internal.util.FrameworkStatsLog.PACKAGE_NOTIFICATION_CHANNEL_PREFERENCES;
@@ -78,6 +77,7 @@ import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.config.sysui.SystemUiSystemPropertiesFlags;
import com.android.internal.config.sysui.SystemUiSystemPropertiesFlags.NotificationFlags;
import com.android.internal.logging.MetricsLogger;
import com.android.internal.util.FrameworkStatsLog;
import com.android.internal.util.Preconditions;
import com.android.internal.util.XmlUtils;
import com.android.modules.utils.TypedXmlPullParser;
@@ -169,7 +169,6 @@ public class PreferencesHelper implements RankingConfig {
     * fields.
     */
    private static final int DEFAULT_LOCKED_APP_FIELDS = 0;
    private final SysUiStatsEvent.BuilderFactory mStatsEventBuilderFactory;

    /**
     * All user-lockable fields for a given application.
@@ -208,7 +207,6 @@ public class PreferencesHelper implements RankingConfig {
            ZenModeHelper zenHelper, PermissionHelper permHelper, PermissionManager permManager,
            NotificationChannelLogger notificationChannelLogger,
            AppOpsManager appOpsManager, ManagedServices.UserProfiles userProfiles,
            SysUiStatsEvent.BuilderFactory statsEventBuilderFactory,
            boolean showReviewPermissionsNotification) {
        mContext = context;
        mZenModeHelper = zenHelper;
@@ -219,7 +217,6 @@ public class PreferencesHelper implements RankingConfig {
        mNotificationChannelLogger = notificationChannelLogger;
        mAppOps = appOpsManager;
        mUserProfiles = userProfiles;
        mStatsEventBuilderFactory = statsEventBuilderFactory;
        mShowReviewPermissionsNotification = showReviewPermissionsNotification;

        XML_VERSION = 4;
@@ -2190,11 +2187,7 @@ public class PreferencesHelper implements RankingConfig {
                    break;
                }
                pulledEvents++;
                SysUiStatsEvent.Builder event = mStatsEventBuilderFactory.newBuilder()
                        .setAtomId(PACKAGE_NOTIFICATION_PREFERENCES);
                final PackagePreferences r = mPackagePreferences.valueAt(i);
                event.writeInt(r.uid);
                event.addBooleanAnnotation(ANNOTATION_ID_IS_UID, true);

                // collect whether this package's importance info was user-set for later, if needed
                // before the migration is enabled, this will simply default to false in all cases.
@@ -2214,15 +2207,7 @@ public class PreferencesHelper implements RankingConfig {

                    pkgsWithPermissionsToHandle.remove(key);
                }
                event.writeInt(importance);

                event.writeInt(r.visibility);
                event.writeInt(r.lockedAppFields);

                // optional bool user_set_importance = 5;
                event.writeBoolean(importanceIsUserSet);

                // optional FsiState fsi_state = 6;
                final boolean isStickyHunFlagEnabled = SystemUiSystemPropertiesFlags.getResolver()
                        .isEnabled(NotificationFlags.SHOW_STICKY_HUN_FOR_DENIED_FSI);

@@ -2232,20 +2217,23 @@ public class PreferencesHelper implements RankingConfig {
                final int fsiState = getFsiState(r.pkg, r.uid, requestedFSIPermission,
                        isStickyHunFlagEnabled);

                event.writeInt(fsiState);

                // optional bool is_fsi_permission_user_set = 7;
                final int currentPermissionFlags = mPm.getPermissionFlags(
                        android.Manifest.permission.USE_FULL_SCREEN_INTENT, r.pkg,
                        UserHandle.getUserHandleForUid(r.uid));

                final boolean isUserSet =
                final boolean fsiIsUserSet =
                        isFsiPermissionUserSet(r.pkg, r.uid, fsiState, currentPermissionFlags,
                                isStickyHunFlagEnabled);

                event.writeBoolean(isUserSet);

                events.add(event.build());
                events.add(FrameworkStatsLog.buildStatsEvent(
                        PACKAGE_NOTIFICATION_PREFERENCES,
                        /* optional int32 uid = 1 [(is_uid) = true] */ r.uid,
                        /* optional int32 importance = 2 */ importance,
                        /* optional int32 visibility = 3 */ r.visibility,
                        /* optional int32 user_locked_fields = 4 */ r.lockedAppFields,
                        /* optional bool user_set_importance = 5 */ importanceIsUserSet,
                        /* optional FsiState fsi_state = 6 */ fsiState,
                        /* optional bool is_fsi_permission_user_set = 7 */ fsiIsUserSet));
            }
        }

@@ -2256,18 +2244,18 @@ public class PreferencesHelper implements RankingConfig {
                    break;
                }
                pulledEvents++;
                SysUiStatsEvent.Builder event = mStatsEventBuilderFactory.newBuilder()
                        .setAtomId(PACKAGE_NOTIFICATION_PREFERENCES);
                event.writeInt(p.first);
                event.addBooleanAnnotation(ANNOTATION_ID_IS_UID, true);
                event.writeInt(pkgPermissions.get(p).first ? IMPORTANCE_DEFAULT : IMPORTANCE_NONE);

                // fill out the rest of the fields with default values so as not to confuse the
                // builder
                event.writeInt(DEFAULT_VISIBILITY);
                event.writeInt(DEFAULT_LOCKED_APP_FIELDS);
                event.writeBoolean(pkgPermissions.get(p).second); // user_set_importance field
                events.add(event.build());
                // Because all fields are required in FrameworkStatsLog.buildStatsEvent, we have
                // to fill in default values for all the unspecified fields.
                events.add(FrameworkStatsLog.buildStatsEvent(
                        PACKAGE_NOTIFICATION_PREFERENCES,
                        /* optional int32 uid = 1 [(is_uid) = true] */ p.first,
                        /* optional int32 importance = 2 */ pkgPermissions.get(p).first
                                ? IMPORTANCE_DEFAULT : IMPORTANCE_NONE,
                        /* optional int32 visibility = 3 */ DEFAULT_VISIBILITY,
                        /* optional int32 user_locked_fields = 4 */ DEFAULT_LOCKED_APP_FIELDS,
                        /* optional bool user_set_importance = 5 */ pkgPermissions.get(p).second,
                        /* optional FsiState fsi_state = 6 */ 0,
                        /* optional bool is_fsi_permission_user_set = 7 */ false));
            }
        }
    }
@@ -2288,20 +2276,21 @@ public class PreferencesHelper implements RankingConfig {
                    if (++totalChannelsPulled > NOTIFICATION_CHANNEL_PULL_LIMIT) {
                        break;
                    }
                    SysUiStatsEvent.Builder event = mStatsEventBuilderFactory.newBuilder()
                            .setAtomId(PACKAGE_NOTIFICATION_CHANNEL_PREFERENCES);
                    event.writeInt(r.uid);
                    event.addBooleanAnnotation(ANNOTATION_ID_IS_UID, true);
                    event.writeString(channel.getId());
                    event.writeString(channel.getName().toString());
                    event.writeString(channel.getDescription());
                    event.writeInt(channel.getImportance());
                    event.writeInt(channel.getUserLockedFields());
                    event.writeBoolean(channel.isDeleted());
                    event.writeBoolean(channel.getConversationId() != null);
                    event.writeBoolean(channel.isDemoted());
                    event.writeBoolean(channel.isImportantConversation());
                    events.add(event.build());
                    events.add(FrameworkStatsLog.buildStatsEvent(
                            PACKAGE_NOTIFICATION_CHANNEL_PREFERENCES,
                            /* optional int32 uid = 1 [(is_uid) = true] */ r.uid,
                            /* optional string channel_id = 2 */ channel.getId(),
                            /* optional string channel_name = 3 */ channel.getName().toString(),
                            /* optional string description = 4 */ channel.getDescription(),
                            /* optional int32 importance = 5 */ channel.getImportance(),
                            /* optional int32 user_locked_fields = 6 */
                            channel.getUserLockedFields(),
                            /* optional bool is_deleted = 7 */ channel.isDeleted(),
                            /* optional bool is_conversation = 8 */
                            channel.getConversationId() != null,
                            /* optional bool is_demoted_conversation = 9 */ channel.isDemoted(),
                            /* optional bool is_important_conversation = 10 */
                            channel.isImportantConversation()));
                }
            }
        }
@@ -2323,16 +2312,15 @@ public class PreferencesHelper implements RankingConfig {
                    if (++totalGroupsPulled > NOTIFICATION_CHANNEL_GROUP_PULL_LIMIT) {
                        break;
                    }
                    SysUiStatsEvent.Builder event = mStatsEventBuilderFactory.newBuilder()
                            .setAtomId(PACKAGE_NOTIFICATION_CHANNEL_GROUP_PREFERENCES);
                    event.writeInt(r.uid);
                    event.addBooleanAnnotation(ANNOTATION_ID_IS_UID, true);
                    event.writeString(groupChannel.getId());
                    event.writeString(groupChannel.getName().toString());
                    event.writeString(groupChannel.getDescription());
                    event.writeBoolean(groupChannel.isBlocked());
                    event.writeInt(groupChannel.getUserLockedFields());
                    events.add(event.build());
                    events.add(FrameworkStatsLog.buildStatsEvent(
                            PACKAGE_NOTIFICATION_CHANNEL_GROUP_PREFERENCES,
                            /* optional int32 uid = 1 [(is_uid) = true] */ r.uid,
                            /* optional string group_id = 2 */ groupChannel.getId(),
                            /* optional string group_name = 3 */ groupChannel.getName().toString(),
                            /* optional string description = 4 */ groupChannel.getDescription(),
                            /* optional bool is_blocked = 5 */ groupChannel.isBlocked(),
                            /* optional int32 user_locked_fields = 6 */
                            groupChannel.getUserLockedFields()));
                }
            }
        }
+17 −26
Original line number Diff line number Diff line
@@ -21,7 +21,6 @@ import static android.app.NotificationManager.AUTOMATIC_RULE_STATUS_ENABLED;
import static android.app.NotificationManager.AUTOMATIC_RULE_STATUS_REMOVED;
import static android.app.NotificationManager.Policy.PRIORITY_SENDERS_ANY;
import static android.service.notification.NotificationServiceProto.ROOT_CONFIG;
import static android.util.StatsLog.ANNOTATION_ID_IS_UID;

import static com.android.internal.util.FrameworkStatsLog.DND_MODE_RULE;

@@ -81,6 +80,7 @@ import com.android.internal.config.sysui.SystemUiSystemPropertiesFlags;
import com.android.internal.logging.MetricsLogger;
import com.android.internal.messages.nano.SystemMessageProto.SystemMessage;
import com.android.internal.notification.SystemNotificationChannels;
import com.android.internal.util.FrameworkStatsLog;
import com.android.internal.util.XmlUtils;
import com.android.modules.utils.TypedXmlPullParser;
import com.android.modules.utils.TypedXmlSerializer;
@@ -116,7 +116,6 @@ public class ZenModeHelper {
    private final SettingsObserver mSettingsObserver;
    private final AppOpsManager mAppOps;
    private final NotificationManager mNotificationManager;
    private final SysUiStatsEvent.BuilderFactory mStatsEventBuilderFactory;
    private ZenModeConfig mDefaultConfig;
    private final ArrayList<Callback> mCallbacks = new ArrayList<Callback>();
    private final ZenModeFiltering mFiltering;
@@ -152,7 +151,6 @@ public class ZenModeHelper {
    private String[] mPriorityOnlyDndExemptPackages;

    public ZenModeHelper(Context context, Looper looper, ConditionProviders conditionProviders,
            SysUiStatsEvent.BuilderFactory statsEventBuilderFactory,
            SystemUiSystemPropertiesFlags.FlagResolver flagResolver,
            ZenModeEventLogger zenModeEventLogger) {
        mContext = context;
@@ -174,7 +172,6 @@ public class ZenModeHelper {
        mFiltering = new ZenModeFiltering(mContext);
        mConditions = new ZenModeConditions(this, conditionProviders);
        mServiceConfig = conditionProviders.getConfig();
        mStatsEventBuilderFactory = statsEventBuilderFactory;
        mFlagResolver = flagResolver;
        mZenModeEventLogger = zenModeEventLogger;
    }
@@ -1314,17 +1311,14 @@ public class ZenModeHelper {
            for (int i = 0; i < numConfigs; i++) {
                final int user = mConfigs.keyAt(i);
                final ZenModeConfig config = mConfigs.valueAt(i);
                SysUiStatsEvent.Builder data = mStatsEventBuilderFactory.newBuilder()
                        .setAtomId(DND_MODE_RULE)
                        .writeInt(user)
                        .writeBoolean(config.manualRule != null) // enabled
                        .writeBoolean(config.areChannelsBypassingDnd)
                        .writeInt(ROOT_CONFIG)
                        .writeString("") // name, empty for root config
                        .writeInt(Process.SYSTEM_UID) // system owns root config
                        .addBooleanAnnotation(ANNOTATION_ID_IS_UID, true)
                        .writeByteArray(config.toZenPolicy().toProto());
                events.add(data.build());
                events.add(FrameworkStatsLog.buildStatsEvent(DND_MODE_RULE,
                        /* optional int32 user = 1 */ user,
                        /* optional bool enabled = 2 */ config.manualRule != null,
                        /* optional bool channels_bypassing = 3 */ config.areChannelsBypassingDnd,
                        /* optional LoggedZenMode zen_mode = 4 */ ROOT_CONFIG,
                        /* optional string id = 5 */ "", // empty for root config
                        /* optional int32 uid = 6 */ Process.SYSTEM_UID, // system owns root config
                        /* optional DNDPolicyProto policy = 7 */ config.toZenPolicy().toProto()));
                if (config.manualRule != null) {
                    ruleToProtoLocked(user, config.manualRule, true, events);
                }
@@ -1355,21 +1349,18 @@ public class ZenModeHelper {
        }

        SysUiStatsEvent.Builder data;
        data = mStatsEventBuilderFactory.newBuilder()
                .setAtomId(DND_MODE_RULE)
                .writeInt(user)
                .writeBoolean(rule.enabled)
                .writeBoolean(false) // channels_bypassing unused for rules
                .writeInt(rule.zenMode)
                .writeString(id)
                .writeInt(getPackageUid(pkg, user))
                .addBooleanAnnotation(ANNOTATION_ID_IS_UID, true);
        byte[] policyProto = new byte[]{};
        if (rule.zenPolicy != null) {
            policyProto = rule.zenPolicy.toProto();
        }
        data.writeByteArray(policyProto);
        events.add(data.build());
        events.add(FrameworkStatsLog.buildStatsEvent(DND_MODE_RULE,
                /* optional int32 user = 1 */ user,
                /* optional bool enabled = 2 */ rule.enabled,
                /* optional bool channels_bypassing = 3 */ false, // unused for rules
                /* optional android.stats.dnd.ZenMode zen_mode = 4 */ rule.zenMode,
                /* optional string id = 5 */ id,
                /* optional int32 uid = 6 */ getPackageUid(pkg, user),
                /* optional DNDPolicyProto policy = 7 */ policyProto));
    }

    private int getPackageUid(String pkg, int user) {
+1 −0
Original line number Diff line number Diff line
@@ -34,6 +34,7 @@ android_test {
        "platform-test-annotations",
        "platformprotosnano",
        "statsdprotolite",
        "StatsdTestUtils",
        "hamcrest-library",
        "servicestests-utils",
        "testables",
+100 −89

File changed.

Preview size limit exceeded, changes collapsed.

Loading