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

Commit 00c318dc authored by Xiangyu/Malcolm Chen's avatar Xiangyu/Malcolm Chen Committed by android-build-merger
Browse files

Merge "Implement SubscriptionController#setPreferredData." am: 59f6f295

am: 05408762

Change-Id: Ie57193129d7bbff0f4f0eb0997b5e065ae14089c
parents 1f226c6e 05408762
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -246,7 +246,7 @@ public class PhoneFactory {
                int maxActivePhones = sPhoneConfigurationManager
                        .getNumberOfModemsWithSimultaneousDataConnections();

                sPhoneSwitcher = new PhoneSwitcher(maxActivePhones, numPhones,
                sPhoneSwitcher = PhoneSwitcher.make(maxActivePhones, numPhones,
                        sContext, sc, Looper.myLooper(), tr, sCommandsInterfaces,
                        sPhones);

+119 −16
Original line number Diff line number Diff line
@@ -39,6 +39,7 @@ import android.os.RemoteException;
import android.telephony.PhoneCapability;
import android.telephony.PhoneStateListener;
import android.telephony.Rlog;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import android.util.LocalLog;

@@ -77,17 +78,53 @@ public class PhoneSwitcher extends Handler {
    private final PhoneStateListener mPhoneStateListener;

    private int mMaxActivePhones;
    private int mDefaultDataSubscription;
    private static PhoneSwitcher sPhoneSwitcher = null;

    private final static int EVENT_DEFAULT_SUBSCRIPTION_CHANGED = 101;
    private final static int EVENT_SUBSCRIPTION_CHANGED         = 102;
    private final static int EVENT_REQUEST_NETWORK              = 103;
    private final static int EVENT_RELEASE_NETWORK              = 104;
    private final static int EVENT_EMERGENCY_TOGGLE             = 105;
    private final static int EVENT_RESEND_DATA_ALLOWED          = 106;
    // Default subscription ID from user setting.
    private int mDefaultDataSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;

    // If mPreferredDataSubId is an active subscription, it overrides
    // mDefaultDataSubId and decides:
    // 1. In modem layer, which subscription is preferred to have data traffic on.
    // 2. In TelephonyNetworkFactory, which subscription will apply default network requets, which
    //    are requests without specifying a subId.
    private int mPreferredDataSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;

    @VisibleForTesting
    // Corresponding phoneId after considerting mPreferredDataSubId and mDefaultDataSubId above.
    protected int mPreferredDataPhoneId = SubscriptionManager.INVALID_PHONE_INDEX;

    private static final int EVENT_DEFAULT_SUBSCRIPTION_CHANGED   = 101;
    private static final int EVENT_SUBSCRIPTION_CHANGED           = 102;
    private static final int EVENT_REQUEST_NETWORK                = 103;
    private static final int EVENT_RELEASE_NETWORK                = 104;
    private static final int EVENT_EMERGENCY_TOGGLE               = 105;
    private static final int EVENT_RESEND_DATA_ALLOWED            = 106;
    private static final int EVENT_PREFERRED_SUBSCRIPTION_CHANGED = 107;

    private final static int MAX_LOCAL_LOG_LINES = 30;

    /**
     * Method to get singleton instance.
     */
    public static PhoneSwitcher getInstance() {
        return sPhoneSwitcher;
    }

    /**
     * Method to create singleton instance.
     */
    public static PhoneSwitcher make(int maxActivePhones, int numPhones, Context context,
            SubscriptionController subscriptionController, Looper looper, ITelephonyRegistry tr,
            CommandsInterface[] cis, Phone[] phones) {
        if (sPhoneSwitcher == null) {
            sPhoneSwitcher = new PhoneSwitcher(maxActivePhones, numPhones, context,
                    subscriptionController, looper, tr, cis, phones);
        }

        return sPhoneSwitcher;
    }

    @VisibleForTesting
    public PhoneSwitcher(Looper looper) {
        super(looper);
@@ -108,6 +145,7 @@ public class PhoneSwitcher extends Handler {
        };
    }

    @VisibleForTesting
    public PhoneSwitcher(int maxActivePhones, int numPhones, Context context,
            SubscriptionController subscriptionController, Looper looper, ITelephonyRegistry tr,
            CommandsInterface[] cis, Phone[] phones) {
@@ -221,6 +259,10 @@ public class PhoneSwitcher extends Handler {
                onResendDataAllowed(msg);
                break;
            }
            case EVENT_PREFERRED_SUBSCRIPTION_CHANGED: {
                onEvaluate(REQUESTS_UNCHANGED, "preferredDataSubIdChanged");
                break;
            }
        }
    }

@@ -292,14 +334,17 @@ public class PhoneSwitcher extends Handler {
            return;
        }

        // Check if preferred slotId is changed.
        boolean diffDetected = requestsChanged;

        // Check if user setting of default data sub is changed.
        final int dataSub = mSubscriptionController.getDefaultDataSubId();
        if (dataSub != mDefaultDataSubscription) {
            sb.append(" default ").append(mDefaultDataSubscription).append("->").append(dataSub);
            mDefaultDataSubscription = dataSub;
            diffDetected = true;
        if (dataSub != mDefaultDataSubId) {
            sb.append(" default ").append(mDefaultDataSubId).append("->").append(dataSub);
            mDefaultDataSubId = dataSub;
        }

        // Check if phoneId to subId mapping is changed.
        for (int i = 0; i < mNumPhones; i++) {
            int sub = mSubscriptionController.getSubIdUsingPhoneId(i);
            if (sub != mPhoneSubscriptions[i]) {
@@ -310,6 +355,15 @@ public class PhoneSwitcher extends Handler {
            }
        }

        // Check if phoneId for preferred data is changed.
        int oldPreferredDataPhoneId = mPreferredDataPhoneId;
        updatePhoneIdForDefaultNetworkRequests();
        if (oldPreferredDataPhoneId != mPreferredDataPhoneId) {
            sb.append(" preferred phoneId ").append(oldPreferredDataPhoneId)
                    .append("->").append(mPreferredDataPhoneId);
            diffDetected = true;
        }

        if (diffDetected) {
            log("evaluating due to " + sb.toString());

@@ -324,7 +378,8 @@ public class PhoneSwitcher extends Handler {
            }

            if (VDBG) {
                log("default subId = " + mDefaultDataSubscription);
                log("default subId = " + mDefaultDataSubId);
                log("preferred subId = " + mPreferredDataSubId);
                for (int i = 0; i < mNumPhones; i++) {
                    log(" phone[" + i + "] using sub[" + mPhoneSubscriptions[i] + "]");
                }
@@ -399,17 +454,19 @@ public class PhoneSwitcher extends Handler {
        if (mMaxActivePhones != newMaxActivePhones) {
            mMaxActivePhones = newMaxActivePhones;
            log("Max active phones changed to " + mMaxActivePhones);
            onEvaluate(true, "phoneCfgChanged");
            onEvaluate(REQUESTS_UNCHANGED, "phoneCfgChanged");
        }
    }

    private int phoneIdForRequest(NetworkRequest netRequest) {
        NetworkSpecifier specifier = netRequest.networkCapabilities.getNetworkSpecifier();
        if (specifier == null) {
            return mPreferredDataPhoneId;
        }

        int subId;

        if (specifier == null) {
            subId = mDefaultDataSubscription;
        } else if (specifier instanceof StringNetworkSpecifier) {
        if (specifier instanceof StringNetworkSpecifier) {
            try {
                subId = Integer.parseInt(((StringNetworkSpecifier) specifier).specifier);
            } catch (NumberFormatException e) {
@@ -433,6 +490,39 @@ public class PhoneSwitcher extends Handler {
        return phoneId;
    }

    private int getSubIdForDefaultNetworkRequests() {
        if (mSubscriptionController.isActiveSubId(mPreferredDataSubId)) {
            return mPreferredDataSubId;
        } else {
            return mDefaultDataSubId;
        }
    }

    // This updates mPreferredDataPhoneId which decides which phone should
    // handle default network requests.
    private void updatePhoneIdForDefaultNetworkRequests() {
        int subId = getSubIdForDefaultNetworkRequests();
        int phoneId = SubscriptionManager.INVALID_PHONE_INDEX;

        if (SubscriptionManager.isUsableSubIdValue(subId)) {
            for (int i = 0; i < mNumPhones; i++) {
                if (mPhoneSubscriptions[i] == subId) {
                    phoneId = i;
                    break;
                }
            }
        }

        mPreferredDataPhoneId = phoneId;
    }

    /**
     * Returns whether phone should handle default network requests.
     */
    public boolean isActiveForDefaultRequests(int phoneId) {
        return isPhoneActive(phoneId) && phoneId == mPreferredDataPhoneId;
    }

    public boolean isPhoneActive(int phoneId) {
        validatePhoneId(phoneId);
        return mPhoneStates[phoneId].active;
@@ -456,6 +546,19 @@ public class PhoneSwitcher extends Handler {
        }
    }

    /**
     * Set a subscription as preferred data subscription.
     * See {@link SubscriptionManager#setPreferredData(int)} for more details.
     */
    public void setPreferredData(int subId) {
        if (mPreferredDataSubId != subId) {
            log("setPreferredData subId changed to " + subId);
            mPreferredDataSubId = subId;
            Message msg = PhoneSwitcher.this.obtainMessage(EVENT_PREFERRED_SUBSCRIPTION_CHANGED);
            msg.sendToTarget();
        }
    }

    private void log(String l) {
        Rlog.d(LOG_TAG, l);
        mLocalLog.log(l);
+18 −2
Original line number Diff line number Diff line
@@ -168,6 +168,7 @@ public class SubscriptionController extends ISub.Stub {

    private int[] colorArr;
    private long mLastISubServiceRegTime;
    private int mPreferredDataSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;

    public static SubscriptionController init(Phone phone) {
        synchronized (SubscriptionController.class) {
@@ -2301,8 +2302,23 @@ public class SubscriptionController extends ISub.Stub {

    @Override
    public int setPreferredData(int slotId) {
        // TODO: send to phone switcher.
        enforceModifyPhoneState("setPreferredData");
        final long token = Binder.clearCallingIdentity();

        try {
            // TODO: make this API takes in subId directly.
            int subId = getSubIdUsingPhoneId(slotId);

            if (mPreferredDataSubId != subId) {
                mPreferredDataSubId = subId;
                PhoneSwitcher.getInstance().setPreferredData(subId);
                //TODO: notifyPreferredDataSubIdChanged();
            }

            return 0;
        } finally {
            Binder.restoreCallingIdentity(token);
        }
    }

    @Override
+49 −51
Original line number Diff line number Diff line
@@ -52,9 +52,12 @@ public class TelephonyNetworkFactory extends NetworkFactory {
    private final HashMap<NetworkRequest, LocalLog> mSpecificRequests =
            new HashMap<NetworkRequest, LocalLog>();

    private int mPhoneId;
    private final int mPhoneId;
    // Only when this network factory is active, it will apply any network requests.
    private boolean mIsActive;
    private boolean mIsDefault;
    // Whether this network factory is active and should handle default network requests.
    // Default network requests are those that don't specify subscription ID.
    private boolean mIsActiveForDefault;
    private int mSubscriptionId;

    private final static int TELEPHONY_NETWORK_SCORE = 50;
@@ -62,9 +65,8 @@ public class TelephonyNetworkFactory extends NetworkFactory {
    private final Handler mInternalHandler;
    private static final int EVENT_ACTIVE_PHONE_SWITCH          = 1;
    private static final int EVENT_SUBSCRIPTION_CHANGED         = 2;
    private static final int EVENT_DEFAULT_SUBSCRIPTION_CHANGED = 3;
    private static final int EVENT_NETWORK_REQUEST              = 4;
    private static final int EVENT_NETWORK_RELEASE              = 5;
    private static final int EVENT_NETWORK_REQUEST              = 3;
    private static final int EVENT_NETWORK_RELEASE              = 4;

    public TelephonyNetworkFactory(PhoneSwitcher phoneSwitcher,
            SubscriptionController subscriptionController, SubscriptionMonitor subscriptionMonitor,
@@ -90,9 +92,7 @@ public class TelephonyNetworkFactory extends NetworkFactory {
        mSubscriptionMonitor.registerForSubscriptionChanged(mPhoneId, mInternalHandler,
                EVENT_SUBSCRIPTION_CHANGED, null);

        mIsDefault = false;
        mSubscriptionMonitor.registerForDefaultDataSubscriptionChanged(mPhoneId, mInternalHandler,
                EVENT_DEFAULT_SUBSCRIPTION_CHANGED, null);
        mIsActiveForDefault = false;

        register();
    }
@@ -138,10 +138,6 @@ public class TelephonyNetworkFactory extends NetworkFactory {
                    onSubIdChange();
                    break;
                }
                case EVENT_DEFAULT_SUBSCRIPTION_CHANGED: {
                    onDefaultChange();
                    break;
                }
                case EVENT_NETWORK_REQUEST: {
                    onNeedNetworkFor(msg);
                    break;
@@ -155,34 +151,51 @@ public class TelephonyNetworkFactory extends NetworkFactory {
    }

    private static final int REQUEST_LOG_SIZE = 40;
    private static final boolean REQUEST = true;
    private static final boolean RELEASE = false;

    private void applyRequests(HashMap<NetworkRequest, LocalLog> requestMap, boolean action,
            String logStr) {
    private static final int ACTION_NO_OP   = 0;
    private static final int ACTION_REQUEST = 1;
    private static final int ACTION_RELEASE = 2;

    private void applyRequests(HashMap<NetworkRequest, LocalLog> requestMap,
            int action, String logStr) {
        if (action == ACTION_NO_OP) return;

        for (NetworkRequest networkRequest : requestMap.keySet()) {
            LocalLog localLog = requestMap.get(networkRequest);
            localLog.log(logStr);
            if (action == REQUEST) {
            if (action == ACTION_REQUEST) {
                mDcTracker.requestNetwork(networkRequest, localLog);
            } else {
            } else if (action == ACTION_RELEASE) {
                mDcTracker.releaseNetwork(networkRequest, localLog);
            }
        }
    }

    private static int getAction(boolean wasActive, boolean isActive) {
        if (!wasActive && isActive) {
            return ACTION_REQUEST;
        } else if (wasActive && !isActive) {
            return ACTION_RELEASE;
        } else {
            return ACTION_NO_OP;
        }
    }

    // apply or revoke requests if our active-ness changes
    private void onActivePhoneSwitch() {
        final boolean newIsActive = mPhoneSwitcher.isPhoneActive(mPhoneId);
        if (mIsActive != newIsActive) {
            mIsActive = newIsActive;
            String logString = "onActivePhoneSwitch(" + mIsActive + ", " + mIsDefault + ")";
        final boolean newIsActiveForDefault = mPhoneSwitcher.isActiveForDefaultRequests(mPhoneId);

        String logString = "onActivePhoneSwitch(newIsActive " + newIsActive + ", "
                + "newIsActive " + newIsActiveForDefault + ")";
        if (DBG) log(logString);
            if (mIsDefault) {
                applyRequests(mDefaultRequests, (mIsActive ? REQUEST : RELEASE), logString);
            }
            applyRequests(mSpecificRequests, (mIsActive ? REQUEST : RELEASE), logString);
        }

        applyRequests(mSpecificRequests, getAction(mIsActive, newIsActive), logString);
        applyRequests(mDefaultRequests, getAction(mIsActiveForDefault, newIsActiveForDefault),
                logString);

        mIsActive = newIsActive;
        mIsActiveForDefault = newIsActiveForDefault;
    }

    // watch for phone->subId changes, reapply new filter and let
@@ -196,21 +209,6 @@ public class TelephonyNetworkFactory extends NetworkFactory {
        }
    }

    // watch for default-data changes (could be side effect of
    // phoneId->subId map change or direct change of default subId)
    // and apply/revoke default-only requests.
    private void onDefaultChange() {
        final int newDefaultSubscriptionId = mSubscriptionController.getDefaultDataSubId();
        final boolean newIsDefault = (newDefaultSubscriptionId == mSubscriptionId);
        if (newIsDefault != mIsDefault) {
            mIsDefault = newIsDefault;
            String logString = "onDefaultChange(" + mIsActive + "," + mIsDefault + ")";
            if (DBG) log(logString);
            if (mIsActive == false) return;
            applyRequests(mDefaultRequests, (mIsDefault ? REQUEST : RELEASE), logString);
        }
    }

    @Override
    public void needNetworkFor(NetworkRequest networkRequest, int score) {
        Message msg = mInternalHandler.obtainMessage(EVENT_NETWORK_REQUEST);
@@ -229,23 +227,23 @@ public class TelephonyNetworkFactory extends NetworkFactory {
                localLog = new LocalLog(REQUEST_LOG_SIZE);
                localLog.log("created for " + networkRequest);
                mDefaultRequests.put(networkRequest, localLog);
                isApplicable = mIsDefault;
                isApplicable = mIsActiveForDefault;
            }
        } else {
            localLog = mSpecificRequests.get(networkRequest);
            if (localLog == null) {
                localLog = new LocalLog(REQUEST_LOG_SIZE);
                mSpecificRequests.put(networkRequest, localLog);
                isApplicable = true;
                isApplicable = mIsActive;
            }
        }
        if (mIsActive && isApplicable) {
        if (isApplicable) {
            String s = "onNeedNetworkFor";
            localLog.log(s);
            log(s + " " + networkRequest);
            mDcTracker.requestNetwork(networkRequest, localLog);
        } else {
            String s = "not acting - isApp=" + isApplicable + ", isAct=" + mIsActive;
            String s = "not acting - isApplicable=" + isApplicable + ", mIsActive=" + mIsActive;
            localLog.log(s);
            log(s + " " + networkRequest);
        }
@@ -264,19 +262,19 @@ public class TelephonyNetworkFactory extends NetworkFactory {
        boolean isApplicable = false;
        if (networkRequest.networkCapabilities.getNetworkSpecifier() == null) {
            // request only for the default network
            isApplicable = mDefaultRequests.containsKey(networkRequest) && mIsActiveForDefault;
            localLog = mDefaultRequests.remove(networkRequest);
            isApplicable = (localLog != null) && mIsDefault;
        } else {
            isApplicable = mSpecificRequests.containsKey(networkRequest) && mIsActive;
            localLog = mSpecificRequests.remove(networkRequest);
            isApplicable = (localLog != null);
        }
        if (mIsActive && isApplicable) {
        if (isApplicable) {
            String s = "onReleaseNetworkFor";
            localLog.log(s);
            log(s + " " + networkRequest);
            mDcTracker.releaseNetwork(networkRequest, localLog);
        } else {
            String s = "not releasing - isApp=" + isApplicable + ", isAct=" + mIsActive;
            String s = "not releasing - isApplicable=" + isApplicable + ", mIsActive=" + mIsActive;
            localLog.log(s);
            log(s + " " + networkRequest);
        }
@@ -289,7 +287,7 @@ public class TelephonyNetworkFactory extends NetworkFactory {
    public void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
        final IndentingPrintWriter pw = new IndentingPrintWriter(writer, "  ");
        pw.println(LOG_TAG + " mSubId=" + mSubscriptionId + " mIsActive=" +
                mIsActive + " mIsDefault=" + mIsDefault);
                mIsActive + " mIsActiveForDefault=" + mIsActiveForDefault);
        pw.println("Default Requests:");
        pw.increaseIndent();
        for (NetworkRequest nr : mDefaultRequests.keySet()) {
+101 −11

File changed.

Preview size limit exceeded, changes collapsed.

Loading