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

Commit 458a1b29 authored by Nathan Harold's avatar Nathan Harold
Browse files

Update Usage Setting From SubInfo to Modem

Update the usage setting to the modem under either of
the following conditions:
1) The usage setting in the Subscription DB has changed; or
2) The phone refreshes its setting to the modem
   (which happens as radioCapabilityUpdated)

Bug: 210023167
Test: manual
Change-Id: I3df7d97850ef3e174abbe528b0471ee3aac6e1db
parent 5a2e8293
Loading
Loading
Loading
Loading
+20 −0
Original line number Diff line number Diff line
@@ -45,6 +45,7 @@ import android.os.AsyncResult;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerExecutor;
import android.os.Looper;
import android.os.Message;
import android.os.PersistableBundle;
@@ -274,6 +275,8 @@ public class GsmCdmaPhone extends Phone {
    private final ImsManagerFactory mImsManagerFactory;
    private final CarrierPrivilegesTracker mCarrierPrivilegesTracker;

    private final SubscriptionManager.OnSubscriptionsChangedListener mSubscriptionsChangedListener;

    // Constructors

    public GsmCdmaPhone(Context context, CommandsInterface ci, PhoneNotifier notifier, int phoneId,
@@ -376,6 +379,19 @@ public class GsmCdmaPhone extends Phone {
        loadTtyMode();

        CallManager.getInstance().registerPhone(this);

        mSubscriptionsChangedListener =
                new SubscriptionManager.OnSubscriptionsChangedListener() {
            @Override
            public void onSubscriptionsChanged() {
                sendEmptyMessage(EVENT_SUBSCRIPTIONS_CHANGED);
            }
        };

        SubscriptionManager subMan = context.getSystemService(SubscriptionManager.class);
        subMan.addOnSubscriptionsChangedListener(
                new HandlerExecutor(this), mSubscriptionsChangedListener);

        logd("GsmCdmaPhone: constructor: sub = " + mPhoneId);
    }

@@ -3307,6 +3323,10 @@ public class GsmCdmaPhone extends Phone {
            case EVENT_SET_VONR_ENABLED_DONE:
                logd("EVENT_SET_VONR_ENABLED_DONE is done");
                break;
            case EVENT_SUBSCRIPTIONS_CHANGED:
                logd("EVENT_SUBSCRIPTIONS_CHANGED");
                updateUsageSetting();
                break;

            default:
                super.handleMessage(msg);
+102 −1
Original line number Diff line number Diff line
@@ -236,8 +236,11 @@ public abstract class Phone extends Handler implements PhoneInternalInterface {
    protected static final int EVENT_LINK_CAPACITY_CHANGED = 59;
    protected static final int EVENT_RESET_CARRIER_KEY_IMSI_ENCRYPTION = 60;
    protected static final int EVENT_SET_VONR_ENABLED_DONE = 61;
    protected static final int EVENT_SUBSCRIPTIONS_CHANGED = 62;
    protected static final int EVENT_GET_USAGE_SETTING_DONE = 63;
    protected static final int EVENT_SET_USAGE_SETTING_DONE = 64;

    protected static final int EVENT_LAST = EVENT_SET_VONR_ENABLED_DONE;
    protected static final int EVENT_LAST = EVENT_SET_USAGE_SETTING_DONE;

    // For shared prefs.
    private static final String GSM_ROAMING_LIST_OVERRIDE_PREFIX = "gsm_roaming_list_";
@@ -364,6 +367,10 @@ public abstract class Phone extends Handler implements PhoneInternalInterface {
    private int mLceStatus = RILConstants.LCE_NOT_AVAILABLE;
    protected TelephonyComponentFactory mTelephonyComponentFactory;

    private int mPreferredUsageSetting = SubscriptionManager.USAGE_SETTING_UNKNOWN;
    private int mUsageSettingFromModem = SubscriptionManager.USAGE_SETTING_UNKNOWN;
    private boolean mIsUsageSettingSupported = true;

    //IMS
    /**
     * {@link CallStateException} message text used to indicate that an IMS call has failed because
@@ -812,6 +819,48 @@ public abstract class Phone extends Handler implements PhoneInternalInterface {
                    mAllDataDisconnectedRegistrants.notifyRegistrants();
                }
                break;
            case EVENT_GET_USAGE_SETTING_DONE:
                ar = (AsyncResult) msg.obj;
                if (ar.exception == null) {
                    try {
                        int mUsageSettingFromModem = ((int[]) ar.result)[0];
                    } catch (NullPointerException | ClassCastException e) {
                        Rlog.e(LOG_TAG, "Invalid response for usage setting " + ar.result);
                        break;
                    }

                    if (mUsageSettingFromModem != mPreferredUsageSetting) {
                        mCi.setUsageSetting(obtainMessage(EVENT_SET_USAGE_SETTING_DONE),
                                mPreferredUsageSetting);
                    }
                } else {
                    try {
                        CommandException ce = (CommandException) ar.exception;
                        if (ce.getCommandError() == CommandException.Error.REQUEST_NOT_SUPPORTED) {
                            mIsUsageSettingSupported = false;
                        }
                        Rlog.w(LOG_TAG, "Unexpected failure to retrieve usage setting " + ce);
                    } catch (ClassCastException unused) {
                        Rlog.e(LOG_TAG, "Invalid Exception for usage setting " + ar.exception);
                        break; // technically extraneous, but good hygiene
                    }
                }
                break;
            case EVENT_SET_USAGE_SETTING_DONE:
                ar = (AsyncResult) msg.obj;
                if (ar.exception != null) {
                    try {
                        CommandException ce = (CommandException) ar.exception;
                        if (ce.getCommandError() == CommandException.Error.REQUEST_NOT_SUPPORTED) {
                            mIsUsageSettingSupported = false;
                        }
                        Rlog.w(LOG_TAG, "Unexpected failure to set usage setting " + ce);
                    } catch (ClassCastException unused) {
                        Rlog.e(LOG_TAG, "Invalid Exception for usage setting " + ar.exception);
                        break; // technically extraneous, but good hygiene
                    }
                }
                break;
            default:
                throw new RuntimeException("unexpected event not handled");
        }
@@ -4435,8 +4484,60 @@ public abstract class Phone extends Handler implements PhoneInternalInterface {
        if (restoreNetworkSelection) {
            restoreSavedNetworkSelection(null);
        }

        updateUsageSetting();
    }

    // Need a magic little helper function to avoid a static call via SubscriptionManager
    private int getPreferredUsageSetting() {
        String result = SubscriptionController.getInstance().getSubscriptionProperty(
                getSubId(), SubscriptionManager.USAGE_SETTING);
        try {
            return Integer.parseInt(result);
        } catch (NumberFormatException nfe) {
        }
        return SubscriptionManager.USAGE_SETTING_UNKNOWN;
    }

    /**
     * Attempt to update the usage setting.
     *
     * @return whether the usage setting will be updated (used for test)
     */
    public boolean updateUsageSetting() {
        if (!mIsUsageSettingSupported) return false;

        final int subId = getSubId();
        if (!SubscriptionManager.isValidSubscriptionId(subId)) return false;

        int lastPreferredUsageSetting = mPreferredUsageSetting;

        int mPreferredUsageSetting = getPreferredUsageSetting();

        // We might get a lot of requests to update, so definitely we don't want to hammer
        // the modem with multiple duplicate requests for usage setting updates
        if (mPreferredUsageSetting == lastPreferredUsageSetting) return false;

        // If the user prefers the default setting, we now need to resolve that into a concrete
        // value, since the modem will have a "concrete" value.
        if (mPreferredUsageSetting == SubscriptionManager.USAGE_SETTING_DEFAULT) {
            mPreferredUsageSetting = mContext.getResources().getInteger(
                    com.android.internal.R.integer.config_default_cellular_usage_setting);
        }

        // If the modem value hasn't been updated, request it.
        if (mUsageSettingFromModem == SubscriptionManager.USAGE_SETTING_UNKNOWN) {
            mCi.getUsageSetting(obtainMessage(EVENT_GET_USAGE_SETTING_DONE));
            // If the modem value is already known, and the value has changed, proceed to update.
        } else if (mPreferredUsageSetting != mUsageSettingFromModem) {
            mCi.setUsageSetting(obtainMessage(EVENT_SET_USAGE_SETTING_DONE),
                    mPreferredUsageSetting);
        }
        return true;
    }



    /**
     * Registers the handler when phone radio  capability is changed.
     *
+6 −0
Original line number Diff line number Diff line
@@ -1096,4 +1096,10 @@ public interface PhoneInternalInterface {
     *  their mobile plan.
     */
    String getMobileProvisioningUrl();

    /**
     * Update the cellular usage setting if applicable.
     */
    boolean updateUsageSetting();

}