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

Commit 6e35b2c5 authored by Mark Chien's avatar Mark Chien Committed by Gerrit Code Review
Browse files

Merge "Remove SimChangeListener from Tethering"

parents 600aeb12 1d64e03f
Loading
Loading
Loading
Loading
+0 −13
Original line number Diff line number Diff line
@@ -115,7 +115,6 @@ import com.android.server.LocalServices;
import com.android.server.connectivity.tethering.IControlsTethering;
import com.android.server.connectivity.tethering.IPv6TetheringCoordinator;
import com.android.server.connectivity.tethering.OffloadController;
import com.android.server.connectivity.tethering.SimChangeListener;
import com.android.server.connectivity.tethering.TetherInterfaceStateMachine;
import com.android.server.connectivity.tethering.TetheringConfiguration;
import com.android.server.connectivity.tethering.TetheringDependencies;
@@ -201,8 +200,6 @@ public class Tethering extends BaseNetworkObserver {
    // into a single coherent structure.
    private final HashSet<TetherInterfaceStateMachine> mForwardedDownstreams;
    private final VersionedBroadcastListener mCarrierConfigChange;
    // TODO: Delete SimChangeListener; it's obsolete.
    private final SimChangeListener mSimChange;
    private final TetheringDependencies mDeps;

    private volatile TetheringConfiguration mConfig;
@@ -252,14 +249,6 @@ public class Tethering extends BaseNetworkObserver {
                    updateConfiguration();
                    reevaluateSimCardProvisioning();
                });
        // TODO: Remove SimChangeListener altogether. For now, we retain it
        // for logging purposes in case we need to debug something that might
        // be related to changing signals from ACTION_SIM_STATE_CHANGED to
        // ACTION_CARRIER_CONFIG_CHANGED.
        mSimChange = new SimChangeListener(
                mContext, smHandler, () -> {
                    mLog.log("OBSERVED SIM card change");
                });

        mStateReceiver = new StateReceiver();

@@ -1529,7 +1518,6 @@ public class Tethering extends BaseNetworkObserver {
                    return;
                }

                mSimChange.startListening();
                mUpstreamNetworkMonitor.start(mDeps.getDefaultNetworkRequest());

                // TODO: De-duplicate with updateUpstreamWanted() below.
@@ -1545,7 +1533,6 @@ public class Tethering extends BaseNetworkObserver {
            public void exit() {
                mOffload.stop();
                mUpstreamNetworkMonitor.stop();
                mSimChange.stopListening();
                notifyDownstreamsOfNewUpstreamIface(null);
                handleNewUpstreamNetworkState(null);
            }
+0 −79
Original line number Diff line number Diff line
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.server.connectivity.tethering;

import static com.android.internal.telephony.IccCardConstants.INTENT_VALUE_ICC_LOADED;
import static com.android.internal.telephony.IccCardConstants.INTENT_KEY_ICC_STATE;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.util.VersionedBroadcastListener;
import android.net.util.VersionedBroadcastListener.IntentCallback;
import android.os.Handler;
import android.util.Log;

import com.android.internal.telephony.TelephonyIntents;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;


/**
 * A utility class that runs the provided callback on the provided handler when
 * observing a new SIM card having been loaded.
 *
 * @hide
 */
public class SimChangeListener extends VersionedBroadcastListener {
    private static final String TAG = SimChangeListener.class.getSimpleName();
    private static final boolean DBG = false;

    public SimChangeListener(Context ctx, Handler handler, Runnable onSimCardLoadedCallback) {
        super(TAG, ctx, handler, makeIntentFilter(), makeCallback(onSimCardLoadedCallback));
    }

    private static IntentFilter makeIntentFilter() {
        final IntentFilter filter = new IntentFilter();
        filter.addAction(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
        return filter;
    }

    private static Consumer<Intent> makeCallback(Runnable onSimCardLoadedCallback) {
        return new Consumer<Intent>() {
            private boolean mSimNotLoadedSeen = false;

            @Override
            public void accept(Intent intent) {
                final String state = intent.getStringExtra(INTENT_KEY_ICC_STATE);
                Log.d(TAG, "got Sim changed to state " + state + ", mSimNotLoadedSeen=" +
                        mSimNotLoadedSeen);

                if (!INTENT_VALUE_ICC_LOADED.equals(state)) {
                    mSimNotLoadedSeen = true;
                    return;
                }

                if (mSimNotLoadedSeen) {
                    mSimNotLoadedSeen = false;
                    onSimCardLoadedCallback.run();
                }
            }
        };
    }
}
+0 −132
Original line number Diff line number Diff line
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.server.connectivity.tethering;

import static com.android.internal.telephony.IccCardConstants.INTENT_VALUE_ICC_ABSENT;
import static com.android.internal.telephony.IccCardConstants.INTENT_VALUE_ICC_LOADED;
import static com.android.internal.telephony.IccCardConstants.INTENT_KEY_ICC_STATE;
import static com.android.internal.telephony.TelephonyIntents.ACTION_SIM_STATE_CHANGED;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.reset;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.os.UserHandle;

import android.support.test.filters.SmallTest;
import android.support.test.runner.AndroidJUnit4;

import com.android.internal.util.test.BroadcastInterceptingContext;

import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.runner.RunWith;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;


@RunWith(AndroidJUnit4.class)
@SmallTest
public class SimChangeListenerTest {
    @Mock private Context mContext;
    private BroadcastInterceptingContext mServiceContext;
    private Handler mHandler;
    private SimChangeListener mSCL;
    private int mCallbackCount;

    private void doCallback() { mCallbackCount++; }

    private class MockContext extends BroadcastInterceptingContext {
        MockContext(Context base) {
            super(base);
        }
    }

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        if (Looper.myLooper() == null) {
            Looper.prepare();
        }
    }

    @Before public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
        reset(mContext);
        mServiceContext = new MockContext(mContext);
        mHandler = new Handler(Looper.myLooper());
        mCallbackCount = 0;
        mSCL = new SimChangeListener(mServiceContext, mHandler, () -> doCallback());
    }

    @After public void tearDown() throws Exception {
        if (mSCL != null) {
            mSCL.stopListening();
            mSCL = null;
        }
    }

    private void sendSimStateChangeIntent(String state) {
        final Intent intent = new Intent(ACTION_SIM_STATE_CHANGED);
        intent.putExtra(INTENT_KEY_ICC_STATE, state);
        mServiceContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL);
    }

    @Test
    public void testNotSeenFollowedBySeenCallsCallback() {
        mSCL.startListening();

        sendSimStateChangeIntent(INTENT_VALUE_ICC_ABSENT);
        sendSimStateChangeIntent(INTENT_VALUE_ICC_LOADED);
        assertEquals(1, mCallbackCount);

        sendSimStateChangeIntent(INTENT_VALUE_ICC_ABSENT);
        sendSimStateChangeIntent(INTENT_VALUE_ICC_LOADED);
        assertEquals(2, mCallbackCount);

        mSCL.stopListening();
    }

    @Test
    public void testNotListeningDoesNotCallback() {
        sendSimStateChangeIntent(INTENT_VALUE_ICC_ABSENT);
        sendSimStateChangeIntent(INTENT_VALUE_ICC_LOADED);
        assertEquals(0, mCallbackCount);

        sendSimStateChangeIntent(INTENT_VALUE_ICC_ABSENT);
        sendSimStateChangeIntent(INTENT_VALUE_ICC_LOADED);
        assertEquals(0, mCallbackCount);
    }

    @Test
    public void testSeenOnlyDoesNotCallback() {
        mSCL.startListening();

        sendSimStateChangeIntent(INTENT_VALUE_ICC_LOADED);
        assertEquals(0, mCallbackCount);

        sendSimStateChangeIntent(INTENT_VALUE_ICC_LOADED);
        assertEquals(0, mCallbackCount);

        mSCL.stopListening();
    }
}