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

Commit f6c5adcd authored by Chen Xu's avatar Chen Xu Committed by Gerrit Code Review
Browse files

Merge "reset carrier actions on APM"

parents dfe5bb2f ad1a62f4
Loading
Loading
Loading
Loading
+28 −0
Original line number Diff line number Diff line
@@ -19,15 +19,18 @@ import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.ContentObserver;
import android.os.AsyncResult;
import android.os.Handler;
import android.os.Message;
import android.os.Registrant;
import android.os.RegistrantList;
import android.provider.Settings;
import android.telephony.Rlog;
import android.util.LocalLog;
import android.util.Log;

import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.util.IndentingPrintWriter;
import java.io.FileDescriptor;
import java.io.PrintWriter;
@@ -65,6 +68,8 @@ public class CarrierActionAgent extends Handler {
    /** carrier actions, true by default */
    private Boolean mCarrierActionOnMeteredApnEnabled = true;
    private Boolean mCarrierActionOnRadioEnabled = true;
    /** content observer for APM change */
    private final SettingsObserver mSettingsObserver;

    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
@@ -80,11 +85,29 @@ public class CarrierActionAgent extends Handler {
        }
    };

    private class SettingsObserver extends ContentObserver {
        SettingsObserver() {
            super(null);
        }

        @Override
        public void onChange(boolean selfChange) {
            if (Settings.Global.getInt(mPhone.getContext().getContentResolver(),
                    Settings.Global.AIRPLANE_MODE_ON, 0) != 0) {
                sendEmptyMessage(CARRIER_ACTION_RESET);
            }
        }
    }

    /** Constructor */
    public CarrierActionAgent(Phone phone) {
        mPhone = phone;
        mPhone.getContext().registerReceiver(mReceiver,
                new IntentFilter(TelephonyIntents.ACTION_SIM_STATE_CHANGED));
        mSettingsObserver = new SettingsObserver();
        mPhone.getContext().getContentResolver().registerContentObserver(
                Settings.Global.getUriFor(Settings.Global.AIRPLANE_MODE_ON),
                false, mSettingsObserver);
        if (DBG) log("Creating CarrierActionAgent");
    }

@@ -200,6 +223,11 @@ public class CarrierActionAgent extends Handler {
        list.remove(h);
    }

    @VisibleForTesting
    public ContentObserver getContentObserver() {
        return mSettingsObserver;
    }

    private void log(String s) {
        Rlog.d(LOG_TAG, "[" + mPhone.getPhoneId() + "]" + s);
    }
+135 −0
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.internal.telephony;

import android.database.ContentObserver;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.provider.Settings;
import android.test.mock.MockContentProvider;
import android.test.mock.MockContentResolver;
import android.test.suitebuilder.annotation.SmallTest;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;

import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.anyLong;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.verify;

import static com.android.internal.telephony.TelephonyTestUtils.waitForMs;

public class CarrierActionAgentTest extends TelephonyTest {
    private CarrierActionAgent mCarrierActionAgentUT;
    private FakeContentResolver mFakeContentResolver;
    private FakeContentProvider mFakeContentProvider;
    private static int DATA_CARRIER_ACTION_EVENT = 0;
    private static int RADIO_CARRIER_ACTION_EVENT = 1;
    @Mock
    private Handler mDataActionHandler;
    @Mock
    private Handler mRadioActionHandler;

    private class FakeContentResolver extends MockContentResolver {
        @Override
        public void notifyChange(Uri uri, ContentObserver observer, boolean syncToNetwork) {
            super.notifyChange(uri, observer, syncToNetwork);
            logd("onChanged(uri=" + uri + ")" + observer);
            if (observer != null) {
                observer.dispatchChange(false, uri);
            } else {
                mCarrierActionAgentUT.getContentObserver().dispatchChange(false, uri);
            }
        }
    }

    private class FakeContentProvider extends MockContentProvider {
        private int mExpectedValue;
        public void simulateChange(Uri uri) {
            mFakeContentResolver.notifyChange(uri, null);
        }
        @Override
        public Bundle call(String method, String request, Bundle args) {
            Bundle result = new Bundle();
            if (Settings.CALL_METHOD_GET_GLOBAL.equals(method)) {
                result.putString(Settings.NameValueTable.VALUE, Integer.toString(mExpectedValue));
            } else {
                mExpectedValue = Integer.parseInt(args.getString(Settings.NameValueTable.VALUE));
            }
            return result;
        }
    }

    private class CarrierActionAgentHandler extends HandlerThread {

        private CarrierActionAgentHandler(String name) {
            super(name);
        }

        @Override
        public void onLooperPrepared() {
            mCarrierActionAgentUT = new CarrierActionAgent(mPhone);
            mCarrierActionAgentUT.registerForCarrierAction(
                    CarrierActionAgent.CARRIER_ACTION_SET_METERED_APNS_ENABLED, mDataActionHandler,
                    DATA_CARRIER_ACTION_EVENT, null, false);
            mCarrierActionAgentUT.registerForCarrierAction(
                    CarrierActionAgent.CARRIER_ACTION_SET_RADIO_ENABLED, mRadioActionHandler,
                    RADIO_CARRIER_ACTION_EVENT, null, false);
            setReady(true);
        }
    }

    @Before
    public void setUp() throws Exception {
        logd("CarrierActionAgentTest +Setup!");
        super.setUp(getClass().getSimpleName());
        mFakeContentResolver = new FakeContentResolver();
        mFakeContentProvider = new FakeContentProvider();
        mFakeContentResolver.addProvider(Settings.AUTHORITY, mFakeContentProvider);
        doReturn(mFakeContentResolver).when(mContext).getContentResolver();
        new CarrierActionAgentHandler(getClass().getSimpleName()).start();
        waitUntilReady();
        logd("CarrierActionAgentTest -Setup!");
    }

    @Test
    @SmallTest
    public void testCarrierActionResetOnAPM() {
        Settings.Global.putInt(mFakeContentResolver, Settings.Global.AIRPLANE_MODE_ON, 1);
        mFakeContentProvider.simulateChange(
                Settings.Global.getUriFor(Settings.Global.AIRPLANE_MODE_ON));
        waitForMs(50);
        ArgumentCaptor<Message> message = ArgumentCaptor.forClass(Message.class);

        verify(mDataActionHandler).sendMessageAtTime(message.capture(), anyLong());
        assertEquals(DATA_CARRIER_ACTION_EVENT, message.getValue().what);

        verify(mRadioActionHandler).sendMessageAtTime(message.capture(), anyLong());
        assertEquals(RADIO_CARRIER_ACTION_EVENT, message.getValue().what);
    }

    @After
    public void tearDown() throws Exception {
        super.tearDown();
    }
}