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

Commit 882243e5 authored by Aman Gupta's avatar Aman Gupta Committed by Android Build Coastguard Worker
Browse files

Revert "If the phone crash try to clean up the channel which was kept opened....

Revert "If the phone crash try to clean up the channel which was kept opened. If that fails then try to reuse the existing channel."

This reverts commit e32a4eec.

Reason for revert: b/363412105, b/369170606
(cherry picked from https://googleplex-android-review.googlesource.com/q/commit:61740541b831dd4c7561901575190ef42ddb9b2c)
Merged-In: Ie97ef69f6f9e7209e65c600ac78639a1c40e0fd5
Change-Id: Ie97ef69f6f9e7209e65c600ac78639a1c40e0fd5
parent 0de0c383
Loading
Loading
Loading
Loading
+1 −2
Original line number Original line Diff line number Diff line
@@ -133,8 +133,7 @@ public class EuiccPort extends UiccPort {
            UiccCard card, MultipleEnabledProfilesMode supportedMepMode) {
            UiccCard card, MultipleEnabledProfilesMode supportedMepMode) {
        super(c, ci, ics, phoneId, lock, card);
        super(c, ci, ics, phoneId, lock, card);
        // TODO: Set supportExtendedApdu based on ATR.
        // TODO: Set supportExtendedApdu based on ATR.
        mApduSender = new ApduSender(c, phoneId, ci, ISD_R_AID,
        mApduSender = new ApduSender(ci, ISD_R_AID, false /* supportExtendedApdu */);
                              false /* supportExtendedApdu */);
        if (TextUtils.isEmpty(ics.eid)) {
        if (TextUtils.isEmpty(ics.eid)) {
            loge("no eid given in constructor for phone " + phoneId);
            loge("no eid given in constructor for phone " + phoneId);
        } else {
        } else {
+3 −81
Original line number Original line Diff line number Diff line
@@ -17,13 +17,9 @@
package com.android.internal.telephony.uicc.euicc.apdu;
package com.android.internal.telephony.uicc.euicc.apdu;


import android.annotation.Nullable;
import android.annotation.Nullable;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Handler;
import android.os.Looper;
import android.os.Looper;
import android.preference.PreferenceManager;
import android.telephony.IccOpenLogicalChannelResponse;
import android.telephony.IccOpenLogicalChannelResponse;
import android.util.Base64;


import com.android.internal.telephony.CommandsInterface;
import com.android.internal.telephony.CommandsInterface;
import com.android.internal.telephony.uicc.IccIoResult;
import com.android.internal.telephony.uicc.IccIoResult;
@@ -34,7 +30,6 @@ import com.android.telephony.Rlog;
import java.io.ByteArrayOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.IOException;
import java.util.List;
import java.util.List;
import java.util.NoSuchElementException;


/**
/**
 * This class sends a list of APDU commands to an AID on a UICC. A logical channel will be opened
 * This class sends a list of APDU commands to an AID on a UICC. A logical channel will be opened
@@ -57,9 +52,6 @@ public class ApduSender {
    private static final int SW1_NO_ERROR = 0x91;
    private static final int SW1_NO_ERROR = 0x91;


    private static final int WAIT_TIME_MS = 2000;
    private static final int WAIT_TIME_MS = 2000;
    private static final String CHANNEL_ID_PRE = "esim-channel";
    private static final String ISD_R_AID = "A0000005591010FFFFFFFF8900000100";
    private static final String CHANNEL_RESPONSE_ID_PRE = "esim-res-id";


    private static void logv(String msg) {
    private static void logv(String msg) {
        Rlog.v(LOG_TAG, msg);
        Rlog.v(LOG_TAG, msg);
@@ -74,9 +66,6 @@ public class ApduSender {
    private final OpenLogicalChannelInvocation mOpenChannel;
    private final OpenLogicalChannelInvocation mOpenChannel;
    private final CloseLogicalChannelInvocation mCloseChannel;
    private final CloseLogicalChannelInvocation mCloseChannel;
    private final TransmitApduLogicalChannelInvocation mTransmitApdu;
    private final TransmitApduLogicalChannelInvocation mTransmitApdu;
    private final Context mContext;
    private final String mChannelKey;
    private final String mChannelResponseKey;


    // Lock for accessing mChannelOpened. We only allow to open a single logical channel at any
    // Lock for accessing mChannelOpened. We only allow to open a single logical channel at any
    // time for an AID.
    // time for an AID.
@@ -86,17 +75,12 @@ public class ApduSender {
    /**
    /**
     * @param aid The AID that will be used to open a logical channel to.
     * @param aid The AID that will be used to open a logical channel to.
     */
     */
    public ApduSender(Context context, int phoneId, CommandsInterface ci, String aid,
    public ApduSender(CommandsInterface ci, String aid, boolean supportExtendedApdu) {
            boolean supportExtendedApdu) {
        mAid = aid;
        mAid = aid;
        mContext = context;
        mSupportExtendedApdu = supportExtendedApdu;
        mSupportExtendedApdu = supportExtendedApdu;
        mOpenChannel = new OpenLogicalChannelInvocation(ci);
        mOpenChannel = new OpenLogicalChannelInvocation(ci);
        mCloseChannel = new CloseLogicalChannelInvocation(ci);
        mCloseChannel = new CloseLogicalChannelInvocation(ci);
        mTransmitApdu = new TransmitApduLogicalChannelInvocation(ci);
        mTransmitApdu = new TransmitApduLogicalChannelInvocation(ci);
        mChannelKey = CHANNEL_ID_PRE + "_" + phoneId;
        mChannelResponseKey = CHANNEL_RESPONSE_ID_PRE + "_" + phoneId;
        closeExistingChannelIfExists();
    }
    }


    /**
    /**
@@ -145,20 +129,6 @@ public class ApduSender {
            public void onResult(IccOpenLogicalChannelResponse openChannelResponse) {
            public void onResult(IccOpenLogicalChannelResponse openChannelResponse) {
                int channel = openChannelResponse.getChannel();
                int channel = openChannelResponse.getChannel();
                int status = openChannelResponse.getStatus();
                int status = openChannelResponse.getStatus();
                byte[] selectResponse = openChannelResponse.getSelectResponse();
                if (mAid.equals(ISD_R_AID)
                      && status == IccOpenLogicalChannelResponse.STATUS_NO_SUCH_ELEMENT) {
                    channel = PreferenceManager.getDefaultSharedPreferences(mContext)
                                .getInt(mChannelKey, IccOpenLogicalChannelResponse.INVALID_CHANNEL);
                    if (channel != IccOpenLogicalChannelResponse.INVALID_CHANNEL) {
                        logv("Try to use already opened channel: " + channel);
                        status = IccOpenLogicalChannelResponse.STATUS_NO_ERROR;
                        String storedResponse = PreferenceManager
                                .getDefaultSharedPreferences(mContext)
                                      .getString(mChannelResponseKey, "");
                        selectResponse = Base64.decode(storedResponse, Base64.DEFAULT);
                    }
                }
                if (channel == IccOpenLogicalChannelResponse.INVALID_CHANNEL
                if (channel == IccOpenLogicalChannelResponse.INVALID_CHANNEL
                        || status != IccOpenLogicalChannelResponse.STATUS_NO_ERROR) {
                        || status != IccOpenLogicalChannelResponse.STATUS_NO_ERROR) {
                    synchronized (mChannelLock) {
                    synchronized (mChannelLock) {
@@ -173,15 +143,8 @@ public class ApduSender {


                RequestBuilder builder = new RequestBuilder(channel, mSupportExtendedApdu);
                RequestBuilder builder = new RequestBuilder(channel, mSupportExtendedApdu);
                Throwable requestException = null;
                Throwable requestException = null;
                if (mAid.equals(ISD_R_AID)) {
                   PreferenceManager.getDefaultSharedPreferences(mContext)
                         .edit().putInt(mChannelKey, channel).apply();
                   PreferenceManager.getDefaultSharedPreferences(mContext)
                        .edit().putString(mChannelResponseKey,
                           Base64.encodeToString(selectResponse, Base64.DEFAULT)).apply();
                }
                try {
                try {
                    requestProvider.buildRequest(selectResponse, builder);
                    requestProvider.buildRequest(openChannelResponse.getSelectResponse(), builder);
                } catch (Throwable e) {
                } catch (Throwable e) {
                    requestException = e;
                    requestException = e;
                }
                }
@@ -318,12 +281,6 @@ public class ApduSender {
            @Override
            @Override
            public void onResult(Boolean aBoolean) {
            public void onResult(Boolean aBoolean) {
                synchronized (mChannelLock) {
                synchronized (mChannelLock) {
                    if (mAid.equals(ISD_R_AID)) {
                      PreferenceManager.getDefaultSharedPreferences(mContext)
                             .edit().remove(mChannelKey).apply();
                      PreferenceManager.getDefaultSharedPreferences(mContext)
                             .edit().remove(mChannelResponseKey).apply();
                    }
                    mChannelOpened = false;
                    mChannelOpened = false;
                    mChannelLock.notify();
                    mChannelLock.notify();
                }
                }
@@ -336,39 +293,4 @@ public class ApduSender {
            }
            }
        }, handler);
        }, handler);
    }
    }

    /**
     * Cleanup the existing opened channel which was remainined opened earlier due
     * to failure or crash.
     */
    private void closeExistingChannelIfExists() {
        if (mCloseChannel != null) {
            int channelId = PreferenceManager.getDefaultSharedPreferences(mContext)
                .getInt(mChannelKey, IccOpenLogicalChannelResponse.INVALID_CHANNEL);
            if (channelId != IccOpenLogicalChannelResponse.INVALID_CHANNEL) {
                logv("Trying to clean up the opened channel : " +  channelId);
                synchronized (mChannelLock) {
                    mChannelOpened = true;
                    mChannelLock.notify();
                }
                mCloseChannel.invoke(channelId, new AsyncResultCallback<Boolean>() {
                    @Override
                    public void onResult(Boolean isSuccess) {
                        if (isSuccess) {
                          logv("Channel closed successfully: " +  channelId);
                          PreferenceManager.getDefaultSharedPreferences(mContext)
                                 .edit().remove(mChannelResponseKey).apply();
                          PreferenceManager.getDefaultSharedPreferences(mContext)
                                 .edit().remove(mChannelKey).apply();
                       }

                       synchronized (mChannelLock) {
                           mChannelOpened = false;
                           mChannelLock.notify();
                      }
                    }
                }, new Handler());
            }
        }
    }
}
}
+1 −3
Original line number Original line Diff line number Diff line
@@ -37,7 +37,6 @@ import com.android.internal.telephony.CommandException;
import com.android.internal.telephony.CommandsInterface;
import com.android.internal.telephony.CommandsInterface;
import com.android.internal.telephony.uicc.IccIoResult;
import com.android.internal.telephony.uicc.IccIoResult;
import com.android.internal.telephony.uicc.IccUtils;
import com.android.internal.telephony.uicc.IccUtils;
import androidx.test.InstrumentationRegistry;


import org.junit.After;
import org.junit.After;
import org.junit.Before;
import org.junit.Before;
@@ -94,8 +93,7 @@ public class ApduSenderTest {
        mResponseCaptor = new ResponseCaptor();
        mResponseCaptor = new ResponseCaptor();
        mSelectResponse = null;
        mSelectResponse = null;


        mSender = new ApduSender(InstrumentationRegistry.getContext(), 0 /* phoneId= */,
        mSender = new ApduSender(mMockCi, AID, false /* supportExtendedApdu */);
                            mMockCi, AID, false /* supportExtendedApdu */);
        mLooper = TestableLooper.get(this);
        mLooper = TestableLooper.get(this);
    }
    }