Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e9e45b4a authored by tom hsu's avatar tom hsu Committed by Android Build Coastguard Worker
Browse files

[Satellite] Refactor API usage to Util class.

Flag: EXEMPT refactor
Fix: b/403149290
Test: atest pass
(cherry picked from https://googleplex-android-review.googlesource.com/q/commit:8916db934fb3d7dc1f94f67c7b409beeff2c6ac6)
Merged-In: I8f6375c59cbb2010ad9232b8abe71af60c6133f9
Change-Id: I8f6375c59cbb2010ad9232b8abe71af60c6133f9
parent 94029d5c
Loading
Loading
Loading
Loading
+2 −16
Original line number Diff line number Diff line
@@ -38,7 +38,6 @@ import com.android.settings.network.telephony.TelephonyBasePreferenceController;
import com.android.settingslib.Utils;

import java.util.List;
import java.util.Set;

/** A controller to show some of apps info which supported on Satellite service. */
public class SatelliteAppListCategoryController extends TelephonyBasePreferenceController {
@@ -121,20 +120,7 @@ public class SatelliteAppListCategoryController extends TelephonyBasePreferenceC
                == CARRIER_ROAMING_NTN_CONNECT_MANUAL) {
            return mIsSmsAvailable;
        }
        SatelliteManager satelliteManager = mContext.getSystemService(SatelliteManager.class);
        if (satelliteManager == null) {
            Log.d(TAG, "SatelliteManager is null.");
            return false;
        }
        try {
            Set<Integer> restrictionReason =
                    satelliteManager.getAttachRestrictionReasonsForCarrier(mSubId);
            return !restrictionReason.contains(
                    SatelliteManager.SATELLITE_COMMUNICATION_RESTRICTION_REASON_ENTITLEMENT);
        } catch (SecurityException | IllegalStateException | IllegalArgumentException ex) {
            Log.d(TAG, "Error to getAttachRestrictionReasonsForCarrier : " + ex);
            return false;
        }
        return SatelliteCarrierSettingUtils.isSatelliteAccountEligible(mContext, mSubId);
    }

    static ApplicationInfo getApplicationInfo(Context context, String packageName) {
+105 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.settings.network.telephony.satellite;

import static android.telephony.CarrierConfigManager.SATELLITE_DATA_SUPPORT_ONLY_RESTRICTED;

import android.content.Context;
import android.telephony.satellite.SatelliteManager;
import android.util.Log;

import androidx.annotation.VisibleForTesting;

import java.util.Collections;
import java.util.Set;

/** A until for carrier satellite setting. */
public class SatelliteCarrierSettingUtils {
    private static final String TAG = "SatelliteCarrierSettingUtils";

    @VisibleForTesting
    static SatelliteManagerWrapper sSatelliteManagerWrapper;

    /**
     * Checks account is eligible.
     *
     * @return true if there is no restriction reason returned.
     */
    public static boolean isSatelliteAccountEligible(Context context, int subId) {
        SatelliteManagerWrapper wrapper =
                sSatelliteManagerWrapper == null ? new SatelliteManagerWrapper(context)
                        : sSatelliteManagerWrapper;

        Set<Integer> restrictionReason = wrapper.getAttachRestrictionReasonsForCarrier(subId);
        return !restrictionReason.contains(
                SatelliteManager.SATELLITE_COMMUNICATION_RESTRICTION_REASON_ENTITLEMENT);
    }

    /**
     * Use getSatelliteDataSupportMode to check data mode is restricted.
     *
     * @return true if data mode is restricted.
     */
    public static boolean isSatelliteDataRestricted(Context context, int subId) {
        SatelliteManagerWrapper wrapper =
                sSatelliteManagerWrapper == null ? new SatelliteManagerWrapper(context)
                        : sSatelliteManagerWrapper;
        return wrapper.getSatelliteDataSupportMode(subId) <= SATELLITE_DATA_SUPPORT_ONLY_RESTRICTED;
    }


    @VisibleForTesting
    static class SatelliteManagerWrapper {
        private final SatelliteManager mSatelliteManager;

        SatelliteManagerWrapper(Context context) {
            mSatelliteManager = context.getSystemService(SatelliteManager.class);
        }

        public Set<Integer> getAttachRestrictionReasonsForCarrier(int subId) {
            if (mSatelliteManager == null) {
                Log.d(TAG, "SatelliteManager is null.");
                return Collections.emptySet();
            }
            try {
                Set<Integer> restrictionReason =
                        mSatelliteManager.getAttachRestrictionReasonsForCarrier(subId);
                Log.d(TAG, "Error to getAttachRestrictionReasonsForCarrier : " + restrictionReason);
                return restrictionReason;
            } catch (SecurityException | IllegalStateException | IllegalArgumentException e) {
                Log.d(TAG, "Error to getAttachRestrictionReasonsForCarrier : " + e);
            }
            return Collections.emptySet();
        }

        public int getSatelliteDataSupportMode(int subId) {
            if (mSatelliteManager == null) {
                Log.d(TAG, "SatelliteManager is null.");
                return SATELLITE_DATA_SUPPORT_ONLY_RESTRICTED;
            }

            var dataMode = SATELLITE_DATA_SUPPORT_ONLY_RESTRICTED;
            try {
                dataMode = mSatelliteManager.getSatelliteDataSupportMode(subId);
                Log.d(TAG, "Data mode : " + dataMode);
            } catch (IllegalStateException e) {
                Log.d(TAG, "Failed to get data mode : " + e);
            }
            return dataMode;
        }
    }
}
+5 −36
Original line number Diff line number Diff line
@@ -23,7 +23,9 @@ import static android.telephony.CarrierConfigManager.KEY_EMERGENCY_MESSAGING_SUP
import static android.telephony.CarrierConfigManager.KEY_SATELLITE_ATTACH_SUPPORTED_BOOL;
import static android.telephony.CarrierConfigManager.KEY_SATELLITE_ENTITLEMENT_SUPPORTED_BOOL;
import static android.telephony.CarrierConfigManager.KEY_SATELLITE_INFORMATION_REDIRECT_URL_STRING;
import static android.telephony.CarrierConfigManager.SATELLITE_DATA_SUPPORT_ONLY_RESTRICTED;

import static com.android.settings.network.telephony.satellite.SatelliteCarrierSettingUtils.isSatelliteAccountEligible;
import static com.android.settings.network.telephony.satellite.SatelliteCarrierSettingUtils.isSatelliteDataRestricted;

import android.app.Activity;
import android.app.settings.SettingsEnums;
@@ -45,8 +47,6 @@ import androidx.preference.PreferenceCategory;
import com.android.settings.R;
import com.android.settings.dashboard.RestrictedDashboardFragment;

import java.util.Set;

/** Handle Satellite Setting Preference Layout. */
public class SatelliteSetting extends RestrictedDashboardFragment {
    private static final String TAG = "SatelliteSetting";
@@ -108,7 +108,7 @@ public class SatelliteSetting extends RestrictedDashboardFragment {
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        boolean isSatelliteEligible = isSatelliteEligible();
        boolean isSatelliteEligible = isSatelliteAccountEligible(getContext(), mSubId);
        updateHowItWorksContent(isSatelliteEligible);
    }

@@ -141,22 +141,6 @@ public class SatelliteSetting extends RestrictedDashboardFragment {
        supportedService.setSummary(R.string.summary_supported_service_for_manual_type);
    }

    private boolean isSatelliteEligible() {
        if (isCarrierRoamingNtnConnectedTypeManual()) {
            return mIsSmsAvailableForManualType;
        }
        try {
            Set<Integer> restrictionReason =
                    mSatelliteManager.getAttachRestrictionReasonsForCarrier(mSubId);
            Log.d(TAG, "Restriction reason : " + restrictionReason);
            return !restrictionReason.contains(
                    SatelliteManager.SATELLITE_COMMUNICATION_RESTRICTION_REASON_ENTITLEMENT);
        } catch (SecurityException | IllegalStateException | IllegalArgumentException ex) {
            loge(ex.toString());
            return false;
        }
    }

    private PersistableBundle fetchCarrierConfigData(int subId) {
        CarrierConfigManager carrierConfigManager = mActivity.getSystemService(
                CarrierConfigManager.class);
@@ -189,21 +173,6 @@ public class SatelliteSetting extends RestrictedDashboardFragment {

    private boolean isDataAvailableAndNotRestricted() {
        return getIntent().getBooleanExtra(EXTRA_IS_SERVICE_DATA_TYPE, false)
                && !isDataRestricted();
    }

    private boolean isDataRestricted() {
        int dataMode = SATELLITE_DATA_SUPPORT_ONLY_RESTRICTED;
        try {
            dataMode = mSatelliteManager.getSatelliteDataSupportMode(mSubId);
            Log.d(TAG, "Data mode : " + dataMode);
        } catch (IllegalStateException e) {
            Log.d(TAG, "Failed to get data mode : " + e);
        }
        return dataMode <= SATELLITE_DATA_SUPPORT_ONLY_RESTRICTED;
    }

    private static void loge(String message) {
        Log.e(TAG, message);
                && !isSatelliteDataRestricted(getContext(), mSubId);
    }
}
+1 −18
Original line number Diff line number Diff line
@@ -28,12 +28,10 @@ import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.PersistableBundle;
import android.telephony.TelephonyManager;
import android.telephony.satellite.SatelliteManager;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.StyleSpan;
import android.text.style.UnderlineSpan;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
@@ -45,8 +43,6 @@ import com.android.settings.R;
import com.android.settings.network.telephony.TelephonyBasePreferenceController;
import com.android.settingslib.Utils;

import java.util.Set;

/** A controller to control content of "Your mobile plan". */
public class SatelliteSettingAccountInfoController extends TelephonyBasePreferenceController {
    private static final String TAG = "SatelliteSettingAccountInfoController";
@@ -164,19 +160,6 @@ public class SatelliteSettingAccountInfoController extends TelephonyBasePreferen
                == CARRIER_ROAMING_NTN_CONNECT_MANUAL) {
            return mIsSmsAvailable;
        }
        SatelliteManager satelliteManager = mContext.getSystemService(SatelliteManager.class);
        if (satelliteManager == null) {
            Log.d(TAG, "SatelliteManager is null.");
            return false;
        }
        try {
            Set<Integer> restrictionReason =
                    satelliteManager.getAttachRestrictionReasonsForCarrier(mSubId);
            return !restrictionReason.contains(
                    SatelliteManager.SATELLITE_COMMUNICATION_RESTRICTION_REASON_ENTITLEMENT);
        } catch (SecurityException | IllegalStateException | IllegalArgumentException ex) {
            Log.d(TAG, "Error to getAttachRestrictionReasonsForCarrier : " + ex.toString());
            return false;
        }
        return SatelliteCarrierSettingUtils.isSatelliteAccountEligible(mContext, mSubId);
    }
}
+3 −5
Original line number Diff line number Diff line
@@ -48,7 +48,6 @@ import com.android.settings.network.telephony.TelephonyBasePreferenceController;

import java.util.Arrays;
import java.util.List;
import java.util.Set;

/**
 * Preference controller for "Satellite Setting"
@@ -191,10 +190,9 @@ public class SatelliteSettingPreferenceController extends
            }

            try {
                Set<Integer> restrictionReason =
                        mSatelliteManager.getAttachRestrictionReasonsForCarrier(mSubId);
                boolean isSatelliteEligible = !restrictionReason.contains(
                        SatelliteManager.SATELLITE_COMMUNICATION_RESTRICTION_REASON_ENTITLEMENT);
                boolean isSatelliteEligible =
                        SatelliteCarrierSettingUtils.isSatelliteAccountEligible(
                                mContext, mSubId);
                if (mIsSatelliteEligible == null || mIsSatelliteEligible != isSatelliteEligible) {
                    mIsSatelliteEligible = isSatelliteEligible;
                    String summary = mContext.getString(
Loading