1 /*
2  * Copyright (C) 2024 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.settings.network.telephony;
18 
19 import android.content.Context;
20 import android.os.Build;
21 import android.safetycenter.SafetyCenterManager;
22 import android.telephony.TelephonyManager;
23 import android.util.Log;
24 
25 import androidx.annotation.NonNull;
26 import androidx.annotation.VisibleForTesting;
27 
28 import com.android.internal.telephony.flags.Flags;
29 import com.android.settings.core.BasePreferenceController;
30 
31 /**
32  * {@link BasePreferenceController} for visibility of Notifications divider on Cellular Security
33  * settings page.
34  */
35 public class CellularSecurityNotificationsDividerController extends
36                 BasePreferenceController {
37 
38     private static final String LOG_TAG = "CellularSecurityNotificationsDividerController";
39 
40     private TelephonyManager mTelephonyManager;
41     @VisibleForTesting
42     protected SafetyCenterManager mSafetyCenterManager;
43 
44     /**
45      * Class constructor of "Cellular Security" preference.
46      *
47      * @param context of settings
48      * @param prefKey assigned within UI entry of XML file
49      */
CellularSecurityNotificationsDividerController( @onNull Context context, @NonNull String prefKey)50     public CellularSecurityNotificationsDividerController(
51             @NonNull Context context, @NonNull String prefKey) {
52         super(context, prefKey);
53         mTelephonyManager = mContext.getSystemService(TelephonyManager.class);
54         mSafetyCenterManager = mContext.getSystemService(SafetyCenterManager.class);
55     }
56 
57     @Override
getAvailabilityStatus()58     public int getAvailabilityStatus() {
59         if (!Flags.enableIdentifierDisclosureTransparencyUnsolEvents()
60                 || !Flags.enableModemCipherTransparencyUnsolEvents()
61                 || !Flags.enableIdentifierDisclosureTransparency()
62                 || !Flags.enableModemCipherTransparency()) {
63             return UNSUPPORTED_ON_DEVICE;
64         }
65         if (!isSafetyCenterSupported()) {
66             return UNSUPPORTED_ON_DEVICE;
67         }
68         if (mTelephonyManager == null) {
69             Log.w(LOG_TAG, "Telephony manager not yet initialized");
70             mTelephonyManager = mContext.getSystemService(TelephonyManager.class);
71         }
72         // Checking for hardware support, i.e. IRadio AIDL version must be >= 2.2
73         try {
74             // Must call both APIs, as we can't use the combined toggle if both aren't available
75             areNotificationsEnabled();
76         } catch (UnsupportedOperationException e) {
77             Log.i(LOG_TAG, "Cellular security notifications are unsupported, hiding divider: "
78                     + e.getMessage());
79             return UNSUPPORTED_ON_DEVICE;
80         }
81 
82         return AVAILABLE;
83     }
84 
85     @VisibleForTesting
areNotificationsEnabled()86     protected boolean areNotificationsEnabled() {
87         return mTelephonyManager.isNullCipherNotificationsEnabled()
88             && mTelephonyManager.isCellularIdentifierDisclosureNotificationsEnabled();
89     }
90 
isSafetyCenterSupported()91     protected boolean isSafetyCenterSupported() {
92         if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
93             return false;
94         }
95         mSafetyCenterManager = mContext.getSystemService(
96                 SafetyCenterManager.class);
97         if (mSafetyCenterManager == null) {
98             return false;
99         }
100         return mSafetyCenterManager.isSafetyCenterEnabled();
101     }
102 }
103