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