• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.internaldata;
18 
19 import static android.os.Build.VERSION_CODES.TIRAMISU;
20 import static android.os.Build.VERSION_CODES.UPSIDE_DOWN_CAKE;
21 
22 import android.os.Bundle;
23 import android.safetycenter.SafetyCenterData;
24 import android.safetycenter.SafetyCenterEntryGroup;
25 import android.safetycenter.SafetyCenterIssue;
26 import android.safetycenter.SafetyCenterStaticEntry;
27 
28 import androidx.annotation.Nullable;
29 import androidx.annotation.RequiresApi;
30 
31 import com.android.modules.utils.build.SdkLevel;
32 
33 /** A class to facilitate working with Safety Center {@link Bundle}s. */
34 @RequiresApi(TIRAMISU)
35 public final class SafetyCenterBundles {
36 
SafetyCenterBundles()37     private SafetyCenterBundles() {}
38 
39     /**
40      * A key used in {@link SafetyCenterData#getExtras()} that returns a {@link Bundle} mapping
41      * {@link SafetyCenterIssue} ids to their associated {@link SafetyCenterEntryGroup} ids.
42      */
43     public static final String ISSUES_TO_GROUPS_BUNDLE_KEY = "IssuesToGroups";
44 
45     /**
46      * A key used in {@link SafetyCenterData#getExtras()} that returns a {@link Bundle} mapping
47      * {@link SafetyCenterStaticEntry} to their associated {@link SafetyCenterEntryId}.
48      *
49      * @see #getStaticEntryId(SafetyCenterData, SafetyCenterStaticEntry)
50      * @see #toBundleKey(SafetyCenterStaticEntry)
51      */
52     public static final String STATIC_ENTRIES_TO_IDS_BUNDLE_KEY = "StaticEntriesToIds";
53 
54     /**
55      * Returns the {@link SafetyCenterEntryId} associated with a {@link SafetyCenterStaticEntry}
56      * using the {@link SafetyCenterData#getExtras()} {@link Bundle}.
57      *
58      * <p>Returns {@code null} if the {@link SafetyCenterEntryId} couldn't be retrieved.
59      *
60      * <p>This is a hack to workaround the fact that {@link SafetyCenterStaticEntry} doesn't expose
61      * an associated ID.
62      */
63     @Nullable
getStaticEntryId( SafetyCenterData safetyCenterData, SafetyCenterStaticEntry safetyCenterStaticEntry)64     public static SafetyCenterEntryId getStaticEntryId(
65             SafetyCenterData safetyCenterData, SafetyCenterStaticEntry safetyCenterStaticEntry) {
66         if (!SdkLevel.isAtLeastU()) {
67             return null;
68         }
69         Bundle staticEntriesToIds =
70                 safetyCenterData.getExtras().getBundle(STATIC_ENTRIES_TO_IDS_BUNDLE_KEY);
71         if (staticEntriesToIds == null) {
72             return null;
73         }
74         String entryIdString = staticEntriesToIds.getString(toBundleKey(safetyCenterStaticEntry));
75         if (entryIdString == null) {
76             return null;
77         }
78         return SafetyCenterIds.entryIdFromString(entryIdString);
79     }
80 
81     /**
82      * Returns a {@code String} that uniquely identifies the {@link SafetyCenterStaticEntry} in the
83      * {@link SafetyCenterData#getExtras()} {@link Bundle}.
84      *
85      * <p>This key is generated based on the {@link SafetyCenterStaticEntry} content. This comes
86      * with the restriction that two separate {@link SafetyCenterStaticEntry} returned by Safety
87      * Center cannot be equal, as they would otherwise collide in the {@link Bundle}.
88      */
89     @RequiresApi(UPSIDE_DOWN_CAKE)
toBundleKey(SafetyCenterStaticEntry safetyCenterStaticEntry)90     public static String toBundleKey(SafetyCenterStaticEntry safetyCenterStaticEntry) {
91         SafetyCenterStaticEntryBundleKey.Builder keyBuilder =
92                 SafetyCenterStaticEntryBundleKey.newBuilder()
93                         .setTitle(safetyCenterStaticEntry.getTitle().toString());
94         CharSequence summary = safetyCenterStaticEntry.getSummary();
95         if (summary != null) {
96             keyBuilder.setSummary(summary.toString());
97         }
98         return SafetyCenterIds.encodeToString(keyBuilder.build());
99     }
100 }
101