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

Commit 9466793b authored by Malcolm Chen's avatar Malcolm Chen
Browse files

Bind or unbind Network and Data service upon multi-SIM config change.

Bug: 142514392
Test: manual and sanity
Change-Id: Ib4268fc2e000f7ea5a25a35c300b25c9f0e17868
Merged-In: Ib4268fc2e000f7ea5a25a35c300b25c9f0e17868
parent de110840
Loading
Loading
Loading
Loading
+45 −21
Original line number Diff line number Diff line
@@ -38,6 +38,7 @@ import android.telephony.INetworkServiceCallback;
import android.telephony.NetworkRegistrationInfo;
import android.telephony.NetworkService;
import android.telephony.Rlog;
import android.telephony.SubscriptionManager;
import android.text.TextUtils;

import java.util.Hashtable;
@@ -100,6 +101,9 @@ public class NetworkRegistrationManager extends Handler {
        intentFilter.addAction(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED);
        phone.getContext().registerReceiverAsUser(mBroadcastReceiver, UserHandle.ALL,
                intentFilter, null, null);
        PhoneConfigurationManager.registerForMultiSimConfigChange(
                this, EVENT_BIND_NETWORK_SERVICE, null);

        sendEmptyMessage(EVENT_BIND_NETWORK_SERVICE);
    }

@@ -112,7 +116,7 @@ public class NetworkRegistrationManager extends Handler {
    public void handleMessage(Message msg) {
        switch (msg.what) {
            case EVENT_BIND_NETWORK_SERVICE:
                bindService();
                rebindService();
                break;
            default:
                loge("Unhandled event " + msg.what);
@@ -231,15 +235,38 @@ public class NetworkRegistrationManager extends Handler {
        }
    }

    private void bindService() {
        Intent intent = null;
        String packageName = getPackageName();
        String className = getClassName();
    private void unbindService() {
        if (mINetworkService != null && mINetworkService.asBinder().isBinderAlive()) {
            logd("unbinding service");
            // Remove the network availability updater and then unbind the service.
            try {
                mINetworkService.removeNetworkServiceProvider(mPhone.getPhoneId());
            } catch (RemoteException e) {
                loge("Cannot remove data service provider. " + e);
            }
        }

        if (mServiceConnection != null) {
            mPhone.getContext().unbindService(mServiceConnection);
        }
        mINetworkService = null;
        mServiceConnection = null;
        mTargetBindingPackageName = null;
    }

    private void bindService(String packageName) {
        if (mPhone == null || !SubscriptionManager.isValidPhoneId(mPhone.getPhoneId())) {
            loge("can't bindService with invalid phone or phoneId.");
            return;
        }

        if (TextUtils.isEmpty(packageName)) {
            loge("Can't find the binding package");
            return;
        }

        Intent intent = null;
        String className = getClassName();
        if (TextUtils.isEmpty(className)) {
            intent = new Intent(NetworkService.SERVICE_INTERFACE);
            intent.setPackage(packageName);
@@ -248,22 +275,6 @@ public class NetworkRegistrationManager extends Handler {
            intent = new Intent(NetworkService.SERVICE_INTERFACE).setComponent(cm);
        }

        if (TextUtils.equals(packageName, mTargetBindingPackageName)) {
            logd("Service " + packageName + " already bound or being bound.");
            return;
        }

        if (mINetworkService != null && mINetworkService.asBinder().isBinderAlive()) {
            // Remove the network availability updater and then unbind the service.
            try {
                mINetworkService.removeNetworkServiceProvider(mPhone.getPhoneId());
            } catch (RemoteException e) {
                loge("Cannot remove data service provider. " + e);
            }

            mPhone.getContext().unbindService(mServiceConnection);
        }

        try {
            // We bind this as a foreground service because it is operating directly on the SIM,
            // and we do not want it subjected to power-savings restrictions while doing so.
@@ -281,6 +292,19 @@ public class NetworkRegistrationManager extends Handler {
        }
    }

    private void rebindService() {
        String packageName = getPackageName();
        // Do nothing if no need to rebind.
        if (SubscriptionManager.isValidPhoneId(mPhone.getPhoneId())
                && TextUtils.equals(packageName, mTargetBindingPackageName)) {
            logd("Service " + packageName + " already bound or being bound.");
            return;
        }

        unbindService();
        bindService(packageName);
    }

    private String getPackageName() {
        String packageName;
        int resourceId;
+50 −26
Original line number Diff line number Diff line
@@ -44,6 +44,7 @@ import android.telephony.AccessNetworkConstants.TransportType;
import android.telephony.AnomalyReporter;
import android.telephony.CarrierConfigManager;
import android.telephony.Rlog;
import android.telephony.SubscriptionManager;
import android.telephony.data.DataCallResponse;
import android.telephony.data.DataProfile;
import android.telephony.data.DataService;
@@ -53,6 +54,7 @@ import android.telephony.data.IDataServiceCallback;
import android.text.TextUtils;

import com.android.internal.telephony.Phone;
import com.android.internal.telephony.PhoneConfigurationManager;

import java.util.HashSet;
import java.util.List;
@@ -285,8 +287,10 @@ public class DataServiceManager extends Handler {

        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED);
        phone.getContext().registerReceiverAsUser(mBroadcastReceiver, UserHandle.ALL,
                intentFilter, null, null);

        PhoneConfigurationManager.registerForMultiSimConfigChange(
                this, EVENT_BIND_DATA_SERVICE, null);

        sendEmptyMessage(EVENT_BIND_DATA_SERVICE);
    }

@@ -299,7 +303,7 @@ public class DataServiceManager extends Handler {
    public void handleMessage(Message msg) {
        switch (msg.what) {
            case EVENT_BIND_DATA_SERVICE:
                bindDataService();
                rebindDataService();
                break;
            case EVENT_WATCHDOG_TIMEOUT:
                handleRequestUnresponded((CellularDataServiceCallback) msg.obj);
@@ -320,41 +324,48 @@ public class DataServiceManager extends Handler {
                message);
    }

    private void bindDataService() {
        Intent intent = null;
        String packageName = getDataServicePackageName();
        String className = getDataServiceClassName();
        if (TextUtils.isEmpty(packageName)) {
            loge("Can't find the binding package");
            return;
        }

        if (TextUtils.isEmpty(className)) {
            intent = new Intent(DataService.SERVICE_INTERFACE);
            intent.setPackage(packageName);
        } else {
            ComponentName cm = new ComponentName(packageName, className);
            intent = new Intent(DataService.SERVICE_INTERFACE).setComponent(cm);
        }

        if (TextUtils.equals(packageName, mTargetBindingPackageName)) {
            if (DBG) log("Service " + packageName + " already bound or being bound.");
            return;
        }

    private void unbindDataService() {
        // Start by cleaning up all packages that *shouldn't* have permissions.
        revokePermissionsFromUnusedDataServices();

        if (mIDataService != null && mIDataService.asBinder().isBinderAlive()) {
            log("unbinding service");
            // Remove the network availability updater and then unbind the service.
            try {
                mIDataService.removeDataServiceProvider(mPhone.getPhoneId());
            } catch (RemoteException e) {
                loge("Cannot remove data service provider. " + e);
            }
        }

        if (mServiceConnection != null) {
            mPhone.getContext().unbindService(mServiceConnection);
        }
        mIDataService = null;
        mServiceConnection = null;
        mTargetBindingPackageName = null;
        mBound = false;
    }

    private void bindDataService(String packageName) {
        if (mPhone == null || !SubscriptionManager.isValidPhoneId(mPhone.getPhoneId())) {
            loge("can't bindDataService with invalid phone or phoneId.");
            return;
        }

        if (TextUtils.isEmpty(packageName)) {
            loge("Can't find the binding package");
            return;
        }

        Intent intent = null;
        String className = getDataServiceClassName();
        if (TextUtils.isEmpty(className)) {
            intent = new Intent(DataService.SERVICE_INTERFACE);
            intent.setPackage(packageName);
        } else {
            ComponentName cm = new ComponentName(packageName, className);
            intent = new Intent(DataService.SERVICE_INTERFACE).setComponent(cm);
        }

        // Then pre-emptively grant the permissions to the package we will bind.
        grantPermissionsToService(packageName);
@@ -372,6 +383,19 @@ public class DataServiceManager extends Handler {
        }
    }

    private void rebindDataService() {
        String packageName = getDataServicePackageName();
        // Do nothing if no need to rebind.
        if (SubscriptionManager.isValidPhoneId(mPhone.getPhoneId())
                && TextUtils.equals(packageName, mTargetBindingPackageName)) {
            if (DBG) log("Service " + packageName + " already bound or being bound.");
            return;
        }

        unbindDataService();
        bindDataService(packageName);
    }

    @NonNull
    private Set<String> getAllDataServicePackageNames() {
        // Cowardly using the public PackageManager interface here.