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

Commit 047610af authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "Add metrics for data switch"

parents 41645ac5 8180b55a
Loading
Loading
Loading
Loading
+12 −6
Original line number Diff line number Diff line
@@ -707,26 +707,32 @@ message TelephonyEvent {
  }

  enum NetworkValidationState {
      /** The network validation state is unknown. */
      UNKOWN = 0;

      /** The network under validation is initial established. */
      AVAILABLE = 0;
      AVAILABLE = 1;

      /** The validation is failed. */
      FAILED = 1;
      FAILED = 2;

      /** The validation is passed. */
      PASSED = 2;
      PASSED = 3;
  }

  message DataSwitch {
      enum Reason {
          /** Data switch caused by unkown reason. */
          UNKONWN = 0;

          /** Data switch caused by user's manual switch. */
          MANUAL = 0;
          MANUAL = 1;

          /** Data switch caused by incoming/outgoing call. */
          IN_CALL = 1;
          IN_CALL = 2;

          /** Data switch caused by CBRS switch. */
          CBRS = 2;
          CBRS = 3;
      }

      /** The reason for data switch. */
+11 −0
Original line number Diff line number Diff line
@@ -25,6 +25,9 @@ import android.os.Handler;
import android.telephony.SubscriptionManager;
import android.util.Log;

import com.android.internal.telephony.metrics.TelephonyMetrics;
import com.android.internal.telephony.nano.TelephonyProto.TelephonyEvent;

/**
 * This class will validate whether cellular network verified by Connectivity's
 * validation process. It listens request on a specific subId, sends a network request
@@ -186,6 +189,10 @@ public class CellularNetworkValidator {
                mConnectivityManager.unregisterNetworkCallback(mNetworkCallback);
                mState = STATE_IDLE;
            }

            TelephonyMetrics.getInstance().writeNetworkValidate(
                    passed ? TelephonyEvent.NetworkValidationState.PASSED
                            : TelephonyEvent.NetworkValidationState.FAILED);
        }

        mSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
@@ -203,6 +210,10 @@ public class CellularNetworkValidator {
        @Override
        public void onAvailable(Network network) {
            logd("network onAvailable " + network);
            if (ConnectivityNetworkCallback.this.mSubId == CellularNetworkValidator.this.mSubId) {
                TelephonyMetrics.getInstance().writeNetworkValidate(
                        TelephonyEvent.NetworkValidationState.AVAILABLE);
            }
        }

        @Override
+103 −4
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package com.android.internal.telephony;

import static android.net.NetworkCapabilities.TRANSPORT_CELLULAR;
import static android.telephony.PhoneStateListener.LISTEN_PHONE_CAPABILITY_CHANGE;
import static android.telephony.PhoneStateListener.LISTEN_PRECISE_CALL_STATE;
import static android.telephony.SubscriptionManager.DEFAULT_SUBSCRIPTION_ID;
@@ -26,7 +27,10 @@ import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.ConnectivityManager.NetworkCallback;
import android.net.MatchAllNetworkSpecifier;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.net.NetworkFactory;
import android.net.NetworkRequest;
@@ -48,6 +52,10 @@ import android.util.LocalLog;

import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.telephony.dataconnection.DcRequest;
import com.android.internal.telephony.metrics.TelephonyMetrics;
import com.android.internal.telephony.nano.TelephonyProto.TelephonyEvent;
import com.android.internal.telephony.nano.TelephonyProto.TelephonyEvent.DataSwitch;
import com.android.internal.telephony.nano.TelephonyProto.TelephonyEvent.OnDemandDataSwitch;
import com.android.internal.util.IndentingPrintWriter;

import java.io.FileDescriptor;
@@ -65,8 +73,10 @@ import java.util.List;
 * the active phones.  Note we don't wait for data attach (which may not happen anyway).
 */
public class PhoneSwitcher extends Handler {
    private final static String LOG_TAG = "PhoneSwitcher";
    private final static boolean VDBG = false;
    private static final String LOG_TAG = "PhoneSwitcher";
    private static final boolean VDBG = false;

    private static final int DEFAULT_NETWORK_CHANGE_TIMEOUT_MS = 5000;

    private final List<DcRequest> mPrioritizedDcRequests = new ArrayList<DcRequest>();
    private final RegistrantList mActivePhoneRegistrants;
@@ -113,6 +123,7 @@ public class PhoneSwitcher extends Handler {
    private static final int EVENT_RADIO_AVAILABLE                = 108;
    private static final int EVENT_PHONE_IN_CALL_CHANGED          = 109;
    private static final int EVENT_NETWORK_VALIDATION_DONE        = 110;
    private static final int EVENT_REMOVE_DEFAULT_NETWORK_CHANGE_CALLBACK = 111;

    // Depending on version of IRadioConfig, we need to send either RIL_REQUEST_ALLOW_DATA if it's
    // 1.0, or RIL_REQUEST_SET_PREFERRED_DATA if it's 1.1 or later. So internally mHalCommandToUse
@@ -129,6 +140,24 @@ public class PhoneSwitcher extends Handler {
    // Default timeout value of network validation in millisecond.
    private final static int DEFAULT_VALIDATION_EXPIRATION_TIME = 2000;

    private Boolean mHasRegisteredDefaultNetworkChangeCallback = false;

    private ConnectivityManager mConnectivityManager;

    private final ConnectivityManager.NetworkCallback mDefaultNetworkCallback =
            new NetworkCallback() {
                @Override
                public void onAvailable(Network network) {
                    if (mConnectivityManager.getNetworkCapabilities(network)
                            .hasTransport(TRANSPORT_CELLULAR)) {
                        logDataSwitchEvent(
                                TelephonyEvent.EventState.END,
                                TelephonyEvent.DataSwitch.Reason.UNKONWN);
                    }
                    removeDefaultNetworkChangeCallback();
                }
            };

    /**
     * Method to get singleton instance.
     */
@@ -150,6 +179,7 @@ public class PhoneSwitcher extends Handler {
        return sPhoneSwitcher;
    }

    /** This constructor is only used for testing purpose. */
    @VisibleForTesting
    public PhoneSwitcher(int numPhones, Looper looper) {
        super(looper);
@@ -241,11 +271,14 @@ public class PhoneSwitcher extends Handler {
        } catch (RemoteException e) {
        }

        mConnectivityManager =
            (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

        mContext.registerReceiver(mDefaultDataChangedReceiver,
                new IntentFilter(TelephonyIntents.ACTION_DEFAULT_DATA_SUBSCRIPTION_CHANGED));

        NetworkCapabilities netCap = new NetworkCapabilities();
        netCap.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR);
        netCap.addTransportType(TRANSPORT_CELLULAR);
        netCap.addCapability(NetworkCapabilities.NET_CAPABILITY_MMS);
        netCap.addCapability(NetworkCapabilities.NET_CAPABILITY_SUPL);
        netCap.addCapability(NetworkCapabilities.NET_CAPABILITY_DUN);
@@ -295,7 +328,9 @@ public class PhoneSwitcher extends Handler {
                break;
            }
            case EVENT_DEFAULT_SUBSCRIPTION_CHANGED: {
                logDataSwitchEvent(TelephonyEvent.EventState.START, DataSwitch.Reason.MANUAL);
                onEvaluate(REQUESTS_UNCHANGED, "defaultChanged");
                registerDefaultNetworkChangeCallback();
                break;
            }
            case EVENT_REQUEST_NETWORK: {
@@ -316,6 +351,7 @@ public class PhoneSwitcher extends Handler {
            }
            case EVENT_PREFERRED_SUBSCRIPTION_CHANGED: {
                onEvaluate(REQUESTS_UNCHANGED, "preferredDataSubscriptionIdChanged");
                registerDefaultNetworkChangeCallback();
                break;
            }
            case EVENT_RADIO_AVAILABLE: {
@@ -324,7 +360,9 @@ public class PhoneSwitcher extends Handler {
                break;
            }
            case EVENT_PHONE_IN_CALL_CHANGED: {
                logDataSwitchEvent(TelephonyEvent.EventState.START, DataSwitch.Reason.IN_CALL);
                onEvaluate(REQUESTS_UNCHANGED, "EVENT_PHONE_IN_CALL_CHANGED");
                registerDefaultNetworkChangeCallback();
                break;
            }
            case EVENT_NETWORK_VALIDATION_DONE: {
@@ -333,6 +371,10 @@ public class PhoneSwitcher extends Handler {
                onValidationDone(subId, passed);
                break;
            }
            case EVENT_REMOVE_DEFAULT_NETWORK_CHANGE_CALLBACK: {
                removeDefaultNetworkChangeCallback();
                break;
            }
        }
    }

@@ -371,7 +413,8 @@ public class PhoneSwitcher extends Handler {

    private void onRequestNetwork(NetworkRequest networkRequest) {
        final DcRequest dcRequest = new DcRequest(networkRequest, mContext);
        if (mPrioritizedDcRequests.contains(dcRequest) == false) {
        if (!mPrioritizedDcRequests.contains(dcRequest)) {
            collectRequestNetworkMetrics(networkRequest);
            mPrioritizedDcRequests.add(dcRequest);
            Collections.sort(mPrioritizedDcRequests);
            onEvaluate(REQUESTS_CHANGED, "netRequest");
@@ -383,6 +426,53 @@ public class PhoneSwitcher extends Handler {

        if (mPrioritizedDcRequests.remove(dcRequest)) {
            onEvaluate(REQUESTS_CHANGED, "netReleased");
            collectReleaseNetworkMetrics(networkRequest);
        }
    }

    private void removeDefaultNetworkChangeCallback() {
        synchronized (mHasRegisteredDefaultNetworkChangeCallback) {
            if (mHasRegisteredDefaultNetworkChangeCallback) {
                mHasRegisteredDefaultNetworkChangeCallback = false;
                removeMessages(EVENT_REMOVE_DEFAULT_NETWORK_CHANGE_CALLBACK);
                mConnectivityManager.unregisterNetworkCallback(mDefaultNetworkCallback);
            }
        }
    }

    private void registerDefaultNetworkChangeCallback() {
        removeDefaultNetworkChangeCallback();

        synchronized (mHasRegisteredDefaultNetworkChangeCallback) {
            mHasRegisteredDefaultNetworkChangeCallback = true;
            mConnectivityManager.registerDefaultNetworkCallback(mDefaultNetworkCallback);
            sendMessageDelayed(
                    obtainMessage(EVENT_REMOVE_DEFAULT_NETWORK_CHANGE_CALLBACK),
                    DEFAULT_NETWORK_CHANGE_TIMEOUT_MS);
        }
    }

    private void collectRequestNetworkMetrics(NetworkRequest networkRequest) {
        // Request network for MMS will temporary disable the network on default data subscription,
        // this only happen on multi-sim device.
        if (mNumPhones > 1 && networkRequest.networkCapabilities.hasCapability(
                NetworkCapabilities.NET_CAPABILITY_MMS)) {
            OnDemandDataSwitch onDemandDataSwitch = new OnDemandDataSwitch();
            onDemandDataSwitch.apn = TelephonyEvent.ApnType.MMS;
            onDemandDataSwitch.state = TelephonyEvent.EventState.START;
            TelephonyMetrics.getInstance().writeOnDemandDataSwitch(onDemandDataSwitch);
        }
    }

    private void collectReleaseNetworkMetrics(NetworkRequest networkRequest) {
        // Release network for MMS will recover the network on default data subscription, this only
        // happen on multi-sim device.
        if (mNumPhones > 1 && networkRequest.networkCapabilities.hasCapability(
                NetworkCapabilities.NET_CAPABILITY_MMS)) {
            OnDemandDataSwitch onDemandDataSwitch = new OnDemandDataSwitch();
            onDemandDataSwitch.apn = TelephonyEvent.ApnType.MMS;
            onDemandDataSwitch.state = TelephonyEvent.EventState.END;
            TelephonyMetrics.getInstance().writeOnDemandDataSwitch(onDemandDataSwitch);
        }
    }

@@ -695,6 +785,7 @@ public class PhoneSwitcher extends Handler {
            return;
        }

        logDataSwitchEvent(TelephonyEvent.EventState.START, DataSwitch.Reason.CBRS);
        // If validation feature is not supported, set it directly. Otherwise,
        // start validation on the subscription first.
        if (!CellularNetworkValidator.isValidationFeatureSupported()) {
@@ -710,6 +801,7 @@ public class PhoneSwitcher extends Handler {
     * from opportunistic subscription to primary subscription.
     */
    public void unsetOpportunisticDataSubscription() {
        logDataSwitchEvent(TelephonyEvent.EventState.START, DataSwitch.Reason.CBRS);
        if (CellularNetworkValidator.isValidationFeatureSupported()
                && mValidator.isValidating()) {
            mValidator.stopValidation();
@@ -756,6 +848,13 @@ public class PhoneSwitcher extends Handler {
        mLocalLog.log(l);
    }

    private void logDataSwitchEvent(int state, int reason) {
        DataSwitch dataSwitch = new DataSwitch();
        dataSwitch.state = state;
        dataSwitch.reason = reason;
        TelephonyMetrics.getInstance().writeDataSwitch(dataSwitch);
    }

    public void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
        final IndentingPrintWriter pw = new IndentingPrintWriter(writer, "  ");
        pw.println("PhoneSwitcher:");
+28 −0
Original line number Diff line number Diff line
@@ -31,6 +31,8 @@ import static com.android.internal.telephony.nano.TelephonyProto.TelephonySettin

import android.os.SystemClock;

import com.android.internal.telephony.nano.TelephonyProto.TelephonyEvent.DataSwitch;
import com.android.internal.telephony.nano.TelephonyProto.TelephonyEvent.OnDemandDataSwitch;
import com.android.internal.telephony.nano.TelephonyProto.TelephonyEvent.PhoneStatus;
import com.android.internal.telephony.nano.TelephonyProto.TelephonyEvent.Type;

@@ -41,6 +43,11 @@ public class TelephonyEventBuilder {
        return mEvent;
    }

    /** The event is not related to any phone id. */
    public TelephonyEventBuilder() {
        this(-1 /* phoneId */);
    }

    public TelephonyEventBuilder(int phoneId) {
        this(SystemClock.elapsedRealtime(), phoneId);
    }
@@ -145,4 +152,25 @@ public class TelephonyEventBuilder {
        mEvent.phoneStatus = phoneStatus;
        return this;
    }

    /** Set and build data switch event. */
    public TelephonyEventBuilder setDataSwitch(DataSwitch dataSwitch) {
        mEvent.type = TelephonyEvent.Type.DATA_SWITCH;
        mEvent.dataSwitch = dataSwitch;
        return this;
    }

    /** Set and build network validation event. */
    public TelephonyEventBuilder setNetworkValidate(int networkValidationState) {
        mEvent.type = TelephonyEvent.Type.NETWORK_VALIDATE;
        mEvent.networkValidationState = networkValidationState;
        return this;
    }

    /** Set and build on demand data switch event. */
    public TelephonyEventBuilder setOnDemandDataSwitch(OnDemandDataSwitch onDemandDataSwitch) {
        mEvent.type = TelephonyEvent.Type.ON_DEMAND_DATA_SWITCH;
        mEvent.onDemandDataSwitch = onDemandDataSwitch;
        return this;
    }
}
+28 −0
Original line number Diff line number Diff line
@@ -86,7 +86,9 @@ import com.android.internal.telephony.nano.TelephonyProto.TelephonyEvent;
import com.android.internal.telephony.nano.TelephonyProto.TelephonyEvent.CarrierIdMatching;
import com.android.internal.telephony.nano.TelephonyProto.TelephonyEvent.CarrierIdMatchingResult;
import com.android.internal.telephony.nano.TelephonyProto.TelephonyEvent.CarrierKeyChange;
import com.android.internal.telephony.nano.TelephonyProto.TelephonyEvent.DataSwitch;
import com.android.internal.telephony.nano.TelephonyProto.TelephonyEvent.ModemRestart;
import com.android.internal.telephony.nano.TelephonyProto.TelephonyEvent.OnDemandDataSwitch;
import com.android.internal.telephony.nano.TelephonyProto.TelephonyEvent.PhoneStatus;
import com.android.internal.telephony.nano.TelephonyProto.TelephonyEvent.RilDeactivateDataCall;
import com.android.internal.telephony.nano.TelephonyProto.TelephonyEvent.RilDeactivateDataCall.DeactivateReason;
@@ -1708,6 +1710,32 @@ public class TelephonyMetrics {
        }
    }

    /**
     * Write network validation event.
     * @param networkValidationState the network validation state.
     */
    public void writeNetworkValidate(int networkValidationState) {
        addTelephonyEvent(
                new TelephonyEventBuilder().setNetworkValidate(networkValidationState).build());
    }

    /**
     * Write data switch event.
     * @param dataSwitch the reason and state of data switch.
     */
    public void writeDataSwitch(DataSwitch dataSwitch) {
        addTelephonyEvent(new TelephonyEventBuilder().setDataSwitch(dataSwitch).build());
    }

    /**
     * Write on demand data switch event.
     * @param onDemandDataSwitch the apn and state of on demand data switch.
     */
    public void writeOnDemandDataSwitch(OnDemandDataSwitch onDemandDataSwitch) {
        addTelephonyEvent(
                new TelephonyEventBuilder().setOnDemandDataSwitch(onDemandDataSwitch).build());
    }

    /**
     * Write phone state changed event
     *