summaryrefslogtreecommitdiff
path: root/service/java/com/android/safetycenter/data/SafetyCenterIssueRepository.java
blob: 566c28c1edba8850c15373a3af834e8d1f199315 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
 * Copyright (C) 2023 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.safetycenter.data;

import static android.os.Build.VERSION_CODES.TIRAMISU;

import static com.android.safetycenter.data.SafetyCenterIssueDeduplicator.DeduplicationInfo;

import static java.util.Collections.emptyList;
import static java.util.Collections.emptyMap;
import static java.util.Collections.emptySet;

import android.annotation.UserIdInt;
import android.content.Context;
import android.safetycenter.SafetySourceData;
import android.safetycenter.SafetySourceIssue;
import android.safetycenter.config.SafetySource;
import android.safetycenter.config.SafetySourcesGroup;
import android.util.SparseArray;

import androidx.annotation.RequiresApi;

import com.android.modules.utils.build.SdkLevel;
import com.android.permission.util.UserUtils;
import com.android.safetycenter.SafetyCenterConfigReader;
import com.android.safetycenter.SafetySourceIssueInfo;
import com.android.safetycenter.SafetySourceKey;
import com.android.safetycenter.SafetySources;
import com.android.safetycenter.UserProfileGroup;
import com.android.safetycenter.internaldata.SafetyCenterIssueKey;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Set;

import javax.annotation.concurrent.NotThreadSafe;

/**
 * Contains issue related data.
 *
 * <p>Responsible for generating lists of issues and deduplication of issues.
 */
@RequiresApi(TIRAMISU)
@NotThreadSafe
final class SafetyCenterIssueRepository {

    private static final SafetySourceIssuesInfoBySeverityDescending
            SAFETY_SOURCE_ISSUES_INFO_BY_SEVERITY_DESCENDING =
                    new SafetySourceIssuesInfoBySeverityDescending();

    private static final DeduplicationInfo EMPTY_DEDUP_INFO =
            new DeduplicationInfo(emptyList(), emptyList(), emptyMap());

    private final Context mContext;
    private final SafetySourceDataRepository mSafetySourceDataRepository;
    private final SafetyCenterConfigReader mSafetyCenterConfigReader;
    private final SafetyCenterIssueDismissalRepository mSafetyCenterIssueDismissalRepository;
    private final SafetyCenterIssueDeduplicator mSafetyCenterIssueDeduplicator;

    private final SparseArray<DeduplicationInfo> mUserIdToDedupInfo = new SparseArray<>();

    SafetyCenterIssueRepository(
            Context context,
            SafetySourceDataRepository safetySourceDataRepository,
            SafetyCenterConfigReader safetyCenterConfigReader,
            SafetyCenterIssueDismissalRepository safetyCenterIssueDismissalRepository,
            SafetyCenterIssueDeduplicator safetyCenterIssueDeduplicator) {
        mContext = context;
        mSafetySourceDataRepository = safetySourceDataRepository;
        mSafetyCenterConfigReader = safetyCenterConfigReader;
        mSafetyCenterIssueDismissalRepository = safetyCenterIssueDismissalRepository;
        mSafetyCenterIssueDeduplicator = safetyCenterIssueDeduplicator;
    }

    /**
     * Updates the class as per the current state of issues. Should be called after any state update
     * that can affect issues.
     */
    void updateIssues(UserProfileGroup userProfileGroup) {
        updateIssues(userProfileGroup.getProfileParentUserId(), /* isManagedProfile= */ false);

        int[] managedProfileUserIds = userProfileGroup.getManagedProfilesUserIds();
        for (int i = 0; i < managedProfileUserIds.length; i++) {
            updateIssues(managedProfileUserIds[i], /* isManagedProfile= */ true);
        }
    }

    /**
     * Updates the class as per the current state of issues. Should be called after any state update
     * that can affect issues.
     */
    void updateIssues(@UserIdInt int userId) {
        updateIssues(userId, UserUtils.isManagedProfile(userId, mContext));
    }

    private void updateIssues(@UserIdInt int userId, boolean isManagedProfile) {
        List<SafetySourceIssueInfo> issues =
                getAllStoredIssuesFromRawSourceData(userId, isManagedProfile);

        issues.sort(SAFETY_SOURCE_ISSUES_INFO_BY_SEVERITY_DESCENDING);

        mUserIdToDedupInfo.put(userId, produceDedupInfo(issues));
    }

    private DeduplicationInfo produceDedupInfo(List<SafetySourceIssueInfo> issues) {
        if (SdkLevel.isAtLeastU()) {
            return mSafetyCenterIssueDeduplicator.deduplicateIssues(issues);
        }
        return new DeduplicationInfo(issues, emptyList(), emptyMap());
    }

    /**
     * Fetches a list of issues related to the given {@link UserProfileGroup}.
     *
     * <p>Issues in the list are sorted in descending order and deduplicated (if applicable, only on
     * Android U+).
     *
     * <p>Only includes issues related to active/running {@code userId}s in the given {@link
     * UserProfileGroup}.
     */
    List<SafetySourceIssueInfo> getIssuesDedupedSortedDescFor(UserProfileGroup userProfileGroup) {
        List<SafetySourceIssueInfo> issuesInfo = getIssuesFor(userProfileGroup);
        issuesInfo.sort(SAFETY_SOURCE_ISSUES_INFO_BY_SEVERITY_DESCENDING);
        return issuesInfo;
    }

    /**
     * Counts the total number of issues from loggable sources, in the given {@link
     * UserProfileGroup}.
     *
     * <p>Only includes issues related to active/running {@code userId}s in the given {@link
     * UserProfileGroup}.
     */
    int countLoggableIssuesFor(UserProfileGroup userProfileGroup) {
        List<SafetySourceIssueInfo> relevantIssues = getIssuesFor(userProfileGroup);
        int issueCount = 0;
        for (int i = 0; i < relevantIssues.size(); i++) {
            SafetySourceIssueInfo safetySourceIssueInfo = relevantIssues.get(i);
            if (SafetySources.isLoggable(safetySourceIssueInfo.getSafetySource())) {
                issueCount++;
            }
        }
        return issueCount;
    }

    /** Gets a list of all issues for the given {@code userId}. */
    List<SafetySourceIssueInfo> getIssuesForUser(@UserIdInt int userId) {
        return filterOutHiddenIssues(
                mUserIdToDedupInfo.get(userId, EMPTY_DEDUP_INFO).getDeduplicatedIssues());
    }

    /**
     * Returns a set of {@link SafetySourcesGroup} IDs that the given {@link SafetyCenterIssueKey}
     * is mapped to, or an empty list if no such mapping is configured.
     */
    Set<String> getGroupMappingFor(SafetyCenterIssueKey issueKey) {
        return mUserIdToDedupInfo
                .get(issueKey.getUserId(), EMPTY_DEDUP_INFO)
                .getIssueToGroupMapping()
                .getOrDefault(issueKey, emptySet());
    }

    /**
     * Returns the list of issues for the given {@code userId} which were removed from the given
     * list of issues in the most recent {@link SafetyCenterIssueDeduplicator#deduplicateIssues}
     * call. These issues were removed because they were duplicates of other issues.
     *
     * <p>If this method is called before any calls to {@link
     * SafetyCenterIssueDeduplicator#deduplicateIssues} then an empty list is returned.
     */
    List<SafetySourceIssueInfo> getLatestDuplicates(@UserIdInt int userId) {
        return mUserIdToDedupInfo.get(userId, EMPTY_DEDUP_INFO).getFilteredOutDuplicateIssues();
    }

    private List<SafetySourceIssueInfo> filterOutHiddenIssues(List<SafetySourceIssueInfo> issues) {
        List<SafetySourceIssueInfo> result = new ArrayList<>();
        for (int i = 0; i < issues.size(); i++) {
            SafetySourceIssueInfo issueInfo = issues.get(i);
            if (!mSafetyCenterIssueDismissalRepository.isIssueHidden(
                    issueInfo.getSafetyCenterIssueKey())) {
                result.add(issueInfo);
            }
        }
        return result;
    }

    private List<SafetySourceIssueInfo> getAllStoredIssuesFromRawSourceData(
            @UserIdInt int userId, boolean isManagedProfile) {
        List<SafetySourceIssueInfo> allIssuesInfo = new ArrayList<>();

        List<SafetySourcesGroup> safetySourcesGroups =
                mSafetyCenterConfigReader.getSafetySourcesGroups();
        for (int j = 0; j < safetySourcesGroups.size(); j++) {
            addSafetySourceIssuesInfo(
                    allIssuesInfo, safetySourcesGroups.get(j), userId, isManagedProfile);
        }

        return allIssuesInfo;
    }

    private void addSafetySourceIssuesInfo(
            List<SafetySourceIssueInfo> issuesInfo,
            SafetySourcesGroup safetySourcesGroup,
            @UserIdInt int userId,
            boolean isManagedProfile) {
        List<SafetySource> safetySources = safetySourcesGroup.getSafetySources();
        for (int i = 0; i < safetySources.size(); i++) {
            SafetySource safetySource = safetySources.get(i);

            if (!SafetySources.isExternal(safetySource)) {
                continue;
            }
            if (isManagedProfile && !SafetySources.supportsManagedProfiles(safetySource)) {
                continue;
            }

            addSafetySourceIssuesInfo(issuesInfo, safetySource, safetySourcesGroup, userId);
        }
    }

    private void addSafetySourceIssuesInfo(
            List<SafetySourceIssueInfo> issuesInfo,
            SafetySource safetySource,
            SafetySourcesGroup safetySourcesGroup,
            @UserIdInt int userId) {
        SafetySourceKey key = SafetySourceKey.of(safetySource.getId(), userId);
        SafetySourceData safetySourceData = mSafetySourceDataRepository.getSafetySourceData(key);

        if (safetySourceData == null) {
            return;
        }

        List<SafetySourceIssue> safetySourceIssues = safetySourceData.getIssues();
        for (int i = 0; i < safetySourceIssues.size(); i++) {
            SafetySourceIssue safetySourceIssue = safetySourceIssues.get(i);

            SafetySourceIssueInfo safetySourceIssueInfo =
                    new SafetySourceIssueInfo(
                            safetySourceIssue, safetySource, safetySourcesGroup, userId);
            issuesInfo.add(safetySourceIssueInfo);
        }
    }

    /**
     * Only includes issues related to active/running {@code userId}s in the given {@link
     * UserProfileGroup}.
     */
    private List<SafetySourceIssueInfo> getIssuesFor(UserProfileGroup userProfileGroup) {
        List<SafetySourceIssueInfo> issues =
                new ArrayList<>(getIssuesForUser(userProfileGroup.getProfileParentUserId()));

        int[] managedRunningProfileUserIds = userProfileGroup.getManagedRunningProfilesUserIds();
        for (int i = 0; i < managedRunningProfileUserIds.length; i++) {
            issues.addAll(getIssuesForUser(managedRunningProfileUserIds[i]));
        }

        return issues;
    }

    /** A comparator to order {@link SafetySourceIssueInfo} by severity level descending. */
    private static final class SafetySourceIssuesInfoBySeverityDescending
            implements Comparator<SafetySourceIssueInfo> {

        private SafetySourceIssuesInfoBySeverityDescending() {}

        @Override
        public int compare(SafetySourceIssueInfo left, SafetySourceIssueInfo right) {
            return Integer.compare(
                    right.getSafetySourceIssue().getSeverityLevel(),
                    left.getSafetySourceIssue().getSeverityLevel());
        }
    }

    /** Dumps state for debugging purposes. */
    void dump(PrintWriter fout) {
        fout.println("ISSUE REPOSITORY");
        for (int i = 0; i < mUserIdToDedupInfo.size(); i++) {
            fout.println();
            fout.println("\tUSER ID=" + mUserIdToDedupInfo.keyAt(i));
            fout.println("\tDEDUPLICATION INFO=" + mUserIdToDedupInfo.valueAt(i));
        }
        fout.println();
    }

    /** Clears all the data from the repository. */
    void clear() {
        mUserIdToDedupInfo.clear();
    }

    /** Clears all data related to the given {@code userId}. */
    void clearForUser(@UserIdInt int userId) {
        mUserIdToDedupInfo.delete(userId);
    }
}