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

Commit 64ed2316 authored by Chen Xu's avatar Chen Xu Committed by Android (Google) Code Review
Browse files

Merge "support carrier action reset on mobile data off" into oc-dr1-dev

parents dfd823bb 8a8637aa
Loading
Loading
Loading
Loading
+28 −20
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@ import android.os.Registrant;
import android.os.RegistrantList;
import android.provider.Settings;
import android.telephony.Rlog;
import android.telephony.TelephonyManager;
import android.util.LocalLog;
import android.util.Log;

@@ -56,6 +57,8 @@ public class CarrierActionAgent extends Handler {
    public static final int CARRIER_ACTION_SET_METERED_APNS_ENABLED      = 0;
    public static final int CARRIER_ACTION_SET_RADIO_ENABLED             = 1;
    public static final int CARRIER_ACTION_RESET                         = 2;
    public static final int EVENT_APM_SETTINGS_CHANGED                   = 3;
    public static final int EVENT_MOBILE_DATA_SETTINGS_CHANGED           = 4;

    /** Member variables */
    private final Phone mPhone;
@@ -81,37 +84,31 @@ public class CarrierActionAgent extends Handler {
                    // ignore rebroadcast since carrier apps are direct boot aware.
                    return;
                }
                if (IccCardConstants.INTENT_VALUE_ICC_LOADED.equals(iccState) ||
                        IccCardConstants.INTENT_VALUE_ICC_ABSENT.equals(iccState)) {
                if (IccCardConstants.INTENT_VALUE_ICC_LOADED.equals(iccState)) {
                    sendEmptyMessage(CARRIER_ACTION_RESET);
                    String mobileData = Settings.Global.MOBILE_DATA;
                    if (TelephonyManager.getDefault().getSimCount() != 1) {
                        mobileData = mobileData + mPhone.getSubId();
                    }
            }
        }
    };

    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) {
                    mSettingsObserver.observe(Settings.Global.getUriFor(mobileData),
                            EVENT_MOBILE_DATA_SETTINGS_CHANGED);
                    mSettingsObserver.observe(
                            Settings.Global.getUriFor(Settings.Global.AIRPLANE_MODE_ON),
                            EVENT_APM_SETTINGS_CHANGED);
                } else if (IccCardConstants.INTENT_VALUE_ICC_ABSENT.equals(iccState)) {
                    sendEmptyMessage(CARRIER_ACTION_RESET);
                    mSettingsObserver.unobserve();
                }
            }
        }
    };

    /** 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);
        mSettingsObserver = new SettingsObserver(mPhone.getContext(), this);
        if (DBG) log("Creating CarrierActionAgent");
    }

@@ -141,6 +138,17 @@ public class CarrierActionAgent extends Handler {
                mPhone.getCarrierSignalAgent().notifyCarrierSignalReceivers(
                        new Intent(TelephonyIntents.ACTION_CARRIER_SIGNAL_RESET));
                break;
            case EVENT_APM_SETTINGS_CHANGED:
                log("EVENT_APM_SETTINGS_CHANGED");
                if ((Settings.Global.getInt(mPhone.getContext().getContentResolver(),
                        Settings.Global.AIRPLANE_MODE_ON, 0) != 0)) {
                    sendEmptyMessage(CARRIER_ACTION_RESET);
                }
                break;
            case EVENT_MOBILE_DATA_SETTINGS_CHANGED:
                log("EVENT_MOBILE_DATA_SETTINGS_CHANGED");
                if (!mPhone.getDataEnabled()) sendEmptyMessage(CARRIER_ACTION_RESET);
                break;
            default:
                loge("Unknown carrier action: " + msg.what);
        }
+78 −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.content.ContentResolver;
import android.content.Context;
import android.database.ContentObserver;
import android.net.Uri;
import android.os.Handler;
import android.telephony.Rlog;

import java.util.HashMap;
import java.util.Map;

/**
 * The class to describe settings observer
 */
public class SettingsObserver extends ContentObserver {
    private final Map<Uri, Integer> mUriEventMap;
    private final Context mContext;
    private final Handler mHandler;
    private static final String TAG = "SettingsObserver";

    public SettingsObserver(Context context, Handler handler) {
        super(null);
        mUriEventMap = new HashMap<>();
        mContext = context;
        mHandler = handler;
    }

    /**
     * Start observing a content.
     * @param uri Content URI
     * @param what The event to fire if the content changes
     */
    public void observe(Uri uri, int what) {
        mUriEventMap.put(uri, what);
        final ContentResolver resolver = mContext.getContentResolver();
        resolver.registerContentObserver(uri, false, this);
    }

    /**
     * Stop observing a content.
     */
    public void unobserve() {
        final ContentResolver resolver = mContext.getContentResolver();
        resolver.unregisterContentObserver(this);
    }

    @Override
    public void onChange(boolean selfChange) {
        Rlog.e(TAG, "Should never be reached.");
    }

    @Override
    public void onChange(boolean selfChange, Uri uri) {
        final Integer what = mUriEventMap.get(uri);
        if (what != null) {
            mHandler.obtainMessage(what.intValue()).sendToTarget();
        } else {
            Rlog.e(TAG, "No matching event to send for URI=" + uri);
        }
    }
}
+23 −3
Original line number Diff line number Diff line
@@ -20,14 +20,18 @@ import static com.android.internal.telephony.TelephonyTestUtils.waitForMs;
import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.anyLong;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

import android.content.Intent;
import android.database.ContentObserver;
import android.net.Uri;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.provider.Settings;
import android.provider.Telephony;
import android.telephony.CarrierConfigManager;
import android.test.mock.MockContentResolver;
import android.test.suitebuilder.annotation.SmallTest;

@@ -95,16 +99,32 @@ public class CarrierActionAgentTest extends TelephonyTest {
    @Test
    @SmallTest
    public void testCarrierActionResetOnAPM() {
        // setting observer register at sim loading
        final Intent intent = new Intent(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
        intent.putExtra(IccCardConstants.INTENT_KEY_ICC_STATE,
                IccCardConstants.INTENT_VALUE_ICC_LOADED);
        mContext.sendBroadcast(intent);
        waitForMs(100);

        // carrier actions triggered from sim loading
        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);

        // simulate APM change from off -> on
        Settings.Global.putInt(mFakeContentResolver, Settings.Global.AIRPLANE_MODE_ON, 1);
        mFakeContentResolver.notifyChange(
                Settings.Global.getUriFor(Settings.Global.AIRPLANE_MODE_ON), null);
        waitForMs(200);
        ArgumentCaptor<Message> message = ArgumentCaptor.forClass(Message.class);

        verify(mDataActionHandler).sendMessageAtTime(message.capture(), anyLong());
        // carrier actions triggered from APM
        verify(mDataActionHandler, times(2)).sendMessageAtTime(message.capture(), anyLong());
        assertEquals(DATA_CARRIER_ACTION_EVENT, message.getValue().what);

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