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.telephony.TelephonyManager;
21 import android.util.Log;
22 
23 import androidx.annotation.NonNull;
24 
25 import com.android.internal.telephony.flags.FeatureFlags;
26 import com.android.internal.telephony.flags.FeatureFlagsImpl;
27 import com.android.settings.core.BasePreferenceController;
28 
29 /**
30  * {@link BasePreferenceController} for visibility of Encryption divider on Cellular Security
31  * settings page.
32  */
33 public class CellularSecurityEncryptionDividerController extends
34                 BasePreferenceController {
35 
36     private static final String LOG_TAG = "CellularSecurityEncryptionDividerController";
37 
38     private TelephonyManager mTelephonyManager;
39 
40     protected final FeatureFlags mFeatureFlags = new FeatureFlagsImpl();
41 
42     /**
43      * Class constructor of "Cellular Security" preference.
44      *
45      * @param context of settings
46      * @param prefKey assigned within UI entry of XML file
47      */
CellularSecurityEncryptionDividerController( @onNull Context context, @NonNull String prefKey)48     public CellularSecurityEncryptionDividerController(
49             @NonNull Context context, @NonNull String prefKey) {
50         super(context, prefKey);
51         mTelephonyManager = mContext.getSystemService(TelephonyManager.class);
52     }
53 
54     @Override
getAvailabilityStatus()55     public int getAvailabilityStatus() {
56         if (mTelephonyManager == null) {
57             Log.w(LOG_TAG, "Telephony manager not yet initialized.");
58             mTelephonyManager = mContext.getSystemService(TelephonyManager.class);
59         }
60 
61         try {
62             mTelephonyManager.isNullCipherAndIntegrityPreferenceEnabled();
63         } catch (UnsupportedOperationException e) {
64             Log.i(LOG_TAG, "Null cipher enablement is unsupported, hiding divider: "
65                     + e.getMessage());
66             return UNSUPPORTED_ON_DEVICE;
67         } catch (Exception e) {
68             Log.e(LOG_TAG,
69                     "Failed isNullCipherAndIntegrityEnabled. Setting availability to "
70                             + "CONDITIONALLY_UNAVAILABLE. Exception: "
71                             + e.getMessage());
72             return CONDITIONALLY_UNAVAILABLE;
73         }
74         return AVAILABLE;
75     }
76 }
77