1 /*
2  * Copyright (C) 2022 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 static android.os.Build.VERSION_CODES.UPSIDE_DOWN_CAKE;
20 import static android.safetycenter.SafetyCenterManager.EXTRA_REFRESH_REQUEST_TYPE_FETCH_FRESH_DATA;
21 import static android.safetycenter.SafetyCenterManager.EXTRA_REFRESH_REQUEST_TYPE_GET_DATA;
22 import static android.safetycenter.SafetyCenterManager.REFRESH_REASON_DEVICE_LOCALE_CHANGE;
23 import static android.safetycenter.SafetyCenterManager.REFRESH_REASON_DEVICE_REBOOT;
24 import static android.safetycenter.SafetyCenterManager.REFRESH_REASON_OTHER;
25 import static android.safetycenter.SafetyCenterManager.REFRESH_REASON_PAGE_OPEN;
26 import static android.safetycenter.SafetyCenterManager.REFRESH_REASON_PERIODIC;
27 import static android.safetycenter.SafetyCenterManager.REFRESH_REASON_RESCAN_BUTTON_CLICK;
28 import static android.safetycenter.SafetyCenterManager.REFRESH_REASON_SAFETY_CENTER_ENABLED;
29 
30 import android.annotation.TargetApi;
31 import android.safetycenter.SafetyCenterManager.RefreshReason;
32 import android.safetycenter.SafetyCenterManager.RefreshRequestType;
33 import android.util.Log;
34 
35 /** Helpers to do with {@link RefreshReason}. */
36 final class RefreshReasons {
37 
38     private static final String TAG = "RefreshReasons";
39 
RefreshReasons()40     private RefreshReasons() {}
41 
42     /**
43      * Validates the given {@link RefreshReason}, and throws an {@link IllegalArgumentException} in
44      * case of unexpected value.
45      */
46     @TargetApi(UPSIDE_DOWN_CAKE)
validate(@efreshReason int refreshReason)47     static void validate(@RefreshReason int refreshReason) {
48         switch (refreshReason) {
49             case REFRESH_REASON_RESCAN_BUTTON_CLICK:
50             case REFRESH_REASON_PAGE_OPEN:
51             case REFRESH_REASON_DEVICE_REBOOT:
52             case REFRESH_REASON_DEVICE_LOCALE_CHANGE:
53             case REFRESH_REASON_SAFETY_CENTER_ENABLED:
54             case REFRESH_REASON_OTHER:
55             case REFRESH_REASON_PERIODIC:
56                 return;
57         }
58         throw new IllegalArgumentException("Unexpected refresh reason: " + refreshReason);
59     }
60 
61     /** Converts the given {@link RefreshReason} to a {@link RefreshRequestType}. */
62     @TargetApi(UPSIDE_DOWN_CAKE)
63     @RefreshRequestType
toRefreshRequestType(@efreshReason int refreshReason)64     static int toRefreshRequestType(@RefreshReason int refreshReason) {
65         switch (refreshReason) {
66             case REFRESH_REASON_RESCAN_BUTTON_CLICK:
67                 return EXTRA_REFRESH_REQUEST_TYPE_FETCH_FRESH_DATA;
68             case REFRESH_REASON_PAGE_OPEN:
69             case REFRESH_REASON_DEVICE_REBOOT:
70             case REFRESH_REASON_DEVICE_LOCALE_CHANGE:
71             case REFRESH_REASON_SAFETY_CENTER_ENABLED:
72             case REFRESH_REASON_OTHER:
73             case REFRESH_REASON_PERIODIC:
74                 return EXTRA_REFRESH_REQUEST_TYPE_GET_DATA;
75         }
76         Log.w(TAG, "Unexpected refresh reason: " + refreshReason);
77         return EXTRA_REFRESH_REQUEST_TYPE_GET_DATA;
78     }
79 
80     /**
81      * Returns {@code true} if the given {@link RefreshReason} corresponds to a background refresh.
82      */
83     @TargetApi(UPSIDE_DOWN_CAKE)
isBackgroundRefresh(@efreshReason int refreshReason)84     static boolean isBackgroundRefresh(@RefreshReason int refreshReason) {
85         switch (refreshReason) {
86             case REFRESH_REASON_DEVICE_REBOOT:
87             case REFRESH_REASON_DEVICE_LOCALE_CHANGE:
88             case REFRESH_REASON_SAFETY_CENTER_ENABLED:
89             case REFRESH_REASON_OTHER:
90             case REFRESH_REASON_PERIODIC:
91                 return true;
92             case REFRESH_REASON_PAGE_OPEN:
93             case REFRESH_REASON_RESCAN_BUTTON_CLICK:
94                 return false;
95         }
96         Log.w(TAG, "Unexpected refresh reason: " + refreshReason);
97         return false;
98     }
99 }
100