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.content.Intent;
21 import android.os.PersistableBundle;
22 import android.provider.Settings;
23 import android.telephony.CarrierConfigManager;
24 import android.telephony.satellite.SatelliteManager;
25 import android.util.Log;
26 
27 import androidx.annotation.NonNull;
28 import androidx.annotation.Nullable;
29 import androidx.preference.Preference;
30 import androidx.preference.PreferenceScreen;
31 
32 import com.android.internal.telephony.flags.Flags;
33 import com.android.settings.R;
34 import com.android.settings.SettingsActivity;
35 import com.android.settings.network.CarrierConfigCache;
36 import com.android.settingslib.core.lifecycle.LifecycleObserver;
37 import com.android.settingslib.core.lifecycle.events.OnStart;
38 import com.android.settingslib.core.lifecycle.events.OnStop;
39 
40 import java.util.Set;
41 
42 /**
43  * Preference controller for "Satellite Setting"
44  */
45 public class SatelliteSettingPreferenceController extends
46         TelephonyBasePreferenceController implements LifecycleObserver, OnStart, OnStop {
47 
48     private static final String TAG = "SatelliteSettingPreferenceController";
49 
50     CarrierConfigCache mCarrierConfigCache;
51     SatelliteManager mSatelliteManager;
52     @Nullable private Boolean mIsSatelliteEligible = null;
53 
SatelliteSettingPreferenceController(@onNull Context context, @NonNull String key)54     public SatelliteSettingPreferenceController(@NonNull Context context, @NonNull String key) {
55         super(context, key);
56         mCarrierConfigCache = CarrierConfigCache.getInstance(context);
57         mSatelliteManager = context.getSystemService(SatelliteManager.class);
58     }
59 
60     @Override
getAvailabilityStatus(int subId)61     public int getAvailabilityStatus(int subId) {
62         if (!Flags.carrierEnabledSatelliteFlag()) {
63             logd("getAvailabilityStatus() : carrierEnabledSatelliteFlag is disabled");
64             return UNSUPPORTED_ON_DEVICE;
65         }
66 
67         final PersistableBundle carrierConfig = mCarrierConfigCache.getConfigForSubId(subId);
68         final boolean isSatelliteAttachSupported = carrierConfig.getBoolean(
69                 CarrierConfigManager.KEY_SATELLITE_ATTACH_SUPPORTED_BOOL);
70 
71         return isSatelliteAttachSupported ? AVAILABLE : CONDITIONALLY_UNAVAILABLE;
72     }
73 
74     @Override
onStart()75     public void onStart() {
76     }
77 
78     @Override
onStop()79     public void onStop() {
80     }
81 
82     @Override
displayPreference(@onNull PreferenceScreen screen)83     public void displayPreference(@NonNull PreferenceScreen screen) {
84         super.displayPreference(screen);
85     }
86 
87     @Override
updateState(@ullable Preference preference)88     public void updateState(@Nullable Preference preference) {
89         super.updateState(preference);
90         if (preference != null) {
91             updateSummary(preference);
92         }
93     }
94 
95     @Override
handlePreferenceTreeClick(@onNull Preference preference)96     public boolean handlePreferenceTreeClick(@NonNull Preference preference) {
97         if (getPreferenceKey().equals(preference.getKey())) {
98             // This activity runs in phone process, we must use intent to start
99             final Intent intent = new Intent(Settings.ACTION_SATELLITE_SETTING)
100                     .setPackage(mContext.getPackageName());
101             // This will setup the Home and Search affordance
102             intent.putExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT_AS_SUBSETTING, true);
103             intent.putExtra(SatelliteSetting.SUB_ID, mSubId);
104             mContext.startActivity(intent);
105             return true;
106         }
107 
108         return false;
109     }
110 
111     /**
112      * Set subId for Satellite Settings page.
113      * @param subId subscription ID.
114      */
init(int subId)115     public void init(int subId) {
116         logd("init(), subId=" + subId);
117         mSubId = subId;
118     }
119 
updateSummary(Preference preference)120     private void updateSummary(Preference preference) {
121         try {
122             Set<Integer> restrictionReason =
123                     mSatelliteManager.getAttachRestrictionReasonsForCarrier(mSubId);
124             boolean isSatelliteEligible = !restrictionReason.contains(
125                     SatelliteManager.SATELLITE_COMMUNICATION_RESTRICTION_REASON_ENTITLEMENT);
126             if (mIsSatelliteEligible == null || mIsSatelliteEligible != isSatelliteEligible) {
127                 mIsSatelliteEligible = isSatelliteEligible;
128                 String summary = mContext.getString(
129                         mIsSatelliteEligible ? R.string.satellite_setting_enabled_summary
130                                 : R.string.satellite_setting_disabled_summary);
131                 preference.setSummary(summary);
132             }
133         } catch (SecurityException | IllegalStateException | IllegalArgumentException ex) {
134             loge(ex.toString());
135             preference.setSummary(R.string.satellite_setting_disabled_summary);
136         }
137     }
138 
logd(String message)139     private static void logd(String message) {
140         Log.d(TAG, message);
141     }
142 
loge(String message)143     private static void loge(String message) {
144         Log.e(TAG, message);
145     }
146 }
147