1 /* 2 * Copyright (C) 2023 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.safetycenter; 18 19 import android.safetycenter.SafetySourceIssue; 20 21 import androidx.annotation.Nullable; 22 23 import com.android.modules.utils.build.SdkLevel; 24 25 import java.util.List; 26 27 /** 28 * A helper class to facilitate working with {@link SafetySourceIssue} objects. 29 * 30 * @hide 31 */ 32 public final class SafetySourceIssues { 33 34 /** 35 * Returns the {@link SafetySourceIssue.Action} with the given action ID belonging to the given 36 * {@link SafetySourceIssue} or {@code null} if no such action is present. 37 * 38 * <p>The action will either belong to the issue directly from {@link 39 * SafetySourceIssue#getActions()} or via {@link SafetySourceIssue#getCustomNotification()} if 40 * the issue has a custom notification. 41 */ 42 @Nullable findAction(SafetySourceIssue issue, String actionId)43 public static SafetySourceIssue.Action findAction(SafetySourceIssue issue, String actionId) { 44 SafetySourceIssue.Action action = null; 45 if (SdkLevel.isAtLeastU() && issue.getCustomNotification() != null) { 46 action = findAction(issue.getCustomNotification().getActions(), actionId); 47 } 48 if (action == null) { 49 action = findAction(issue.getActions(), actionId); 50 } 51 return action; 52 } 53 54 @Nullable findAction( List<SafetySourceIssue.Action> actions, String actionId)55 private static SafetySourceIssue.Action findAction( 56 List<SafetySourceIssue.Action> actions, String actionId) { 57 for (int i = 0; i < actions.size(); i++) { 58 SafetySourceIssue.Action action = actions.get(i); 59 if (action.getId().equals(actionId)) { 60 return action; 61 } 62 } 63 return null; 64 } 65 66 /** 67 * Returns {@code true} if {@code actionId} corresponds to a "primary" action of the given 68 * {@code issue}, or {@code false} if the action is not the primary or if no action with the 69 * given ID is found. 70 * 71 * <p>A primary action is the first action of either the issue, or its custom notification. 72 */ isPrimaryAction(SafetySourceIssue issue, String actionId)73 public static boolean isPrimaryAction(SafetySourceIssue issue, String actionId) { 74 boolean isPrimaryNotificationAction = 75 SdkLevel.isAtLeastU() 76 && issue.getCustomNotification() != null 77 && matchesFirst(issue.getCustomNotification().getActions(), actionId); 78 boolean isPrimaryIssueAction = matchesFirst(issue.getActions(), actionId); 79 return isPrimaryNotificationAction || isPrimaryIssueAction; 80 } 81 matchesFirst(List<SafetySourceIssue.Action> actions, String actionId)82 private static boolean matchesFirst(List<SafetySourceIssue.Action> actions, String actionId) { 83 return !actions.isEmpty() && actions.get(0).getId().equals(actionId); 84 } 85 SafetySourceIssues()86 private SafetySourceIssues() {} 87 } 88