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.data; 18 19 import android.app.PendingIntent; 20 import android.safetycenter.SafetySourceData; 21 import android.safetycenter.SafetySourceIssue; 22 import android.safetycenter.SafetySourceStatus; 23 24 import com.android.modules.utils.build.SdkLevel; 25 26 final class SafetySourceDataOverrides { SafetySourceDataOverrides()27 private SafetySourceDataOverrides() {} 28 copyDataToBuilderWithoutIssues(SafetySourceData data)29 static SafetySourceData.Builder copyDataToBuilderWithoutIssues(SafetySourceData data) { 30 if (SdkLevel.isAtLeastU()) { 31 return new SafetySourceData.Builder(data).clearIssues(); 32 } 33 34 // Copy T-only fields 35 return new SafetySourceData.Builder().setStatus(data.getStatus()); 36 } 37 copyStatusToBuilder(SafetySourceStatus status)38 static SafetySourceStatus.Builder copyStatusToBuilder(SafetySourceStatus status) { 39 if (SdkLevel.isAtLeastU()) { 40 return new SafetySourceStatus.Builder(status); 41 } 42 43 // Copy T-only fields 44 return new SafetySourceStatus.Builder( 45 status.getTitle(), status.getSummary(), status.getSeverityLevel()) 46 .setPendingIntent(status.getPendingIntent()) 47 .setEnabled(status.isEnabled()) 48 .setIconAction(status.getIconAction()); 49 } 50 copyIssueToBuilderWithoutActions(SafetySourceIssue issue)51 static SafetySourceIssue.Builder copyIssueToBuilderWithoutActions(SafetySourceIssue issue) { 52 if (SdkLevel.isAtLeastU()) { 53 return new SafetySourceIssue.Builder(issue).clearActions(); 54 } 55 56 // Copy T-only fields 57 return new SafetySourceIssue.Builder( 58 issue.getId(), 59 issue.getTitle(), 60 issue.getSummary(), 61 issue.getSeverityLevel(), 62 issue.getIssueTypeId()) 63 .setIssueCategory(issue.getIssueCategory()) 64 .setSubtitle(issue.getSubtitle()) 65 .setOnDismissPendingIntent(issue.getOnDismissPendingIntent()); 66 } 67 68 /** 69 * Returns an new {@link SafetySourceIssue.Action} object, replacing its {@link PendingIntent} 70 * with the one supplied. 71 */ overrideActionPendingIntent( SafetySourceIssue.Action action, PendingIntent pendingIntent)72 static SafetySourceIssue.Action overrideActionPendingIntent( 73 SafetySourceIssue.Action action, PendingIntent pendingIntent) { 74 // TODO(b/303443020): Add setter for pendingIntent so this method can use the copy builder. 75 return new SafetySourceIssue.Action.Builder( 76 action.getId(), action.getLabel(), pendingIntent) 77 .setWillResolve(action.willResolve()) 78 .setSuccessMessage(action.getSuccessMessage()) 79 .build(); 80 } 81 } 82