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

Commit 47c1a77d authored by Amit Mahajan's avatar Amit Mahajan Committed by Android (Google) Code Review
Browse files

Merge "Add class IntentBroadcaster to rebroadcast intents on USER_UNLOCKED." into oc-dr1-dev

parents db8e3904 d1d2aa15
Loading
Loading
Loading
Loading
+102 −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.app.ActivityManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.UserHandle;
import android.util.Log;

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

/**
 * This class is used to broadcast intents that need to be rebroadcast after the device is unlocked.
 * NOTE: Currently this is used only for SIM_STATE_CHANGED so logic is hardcoded for that;
 * for example broadcasts are always sticky, only the last intent for the slotId is rebroadcast,
 * etc.
 */
public class IntentBroadcaster {
    private static final String TAG = "IntentBroadcaster";

    private Map<Integer, Intent> mRebroadcastIntents = new HashMap<>();
    private static IntentBroadcaster sIntentBroadcaster;

    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(Intent.ACTION_USER_UNLOCKED)) {
                synchronized (mRebroadcastIntents) {
                    // rebroadcast intents
                    Iterator iterator = mRebroadcastIntents.entrySet().iterator();
                    while (iterator.hasNext()) {
                        Map.Entry pair = (Map.Entry) iterator.next();
                        Intent i = (Intent) pair.getValue();
                        i.putExtra(TelephonyIntents.EXTRA_REBROADCAST_ON_UNLOCK, true);
                        iterator.remove();
                        logd("Rebroadcasting intent " + i.getAction() + " "
                                + i.getStringExtra(IccCardConstants.INTENT_KEY_ICC_STATE)
                                + " for slotId " + pair.getKey());
                        ActivityManager.broadcastStickyIntent(i, UserHandle.USER_ALL);
                    }
                }
            }
        }
    };

    private IntentBroadcaster(Context context) {
        context.registerReceiver(mReceiver, new IntentFilter(Intent.ACTION_USER_UNLOCKED));
    }

    /**
     * Method to get an instance of IntentBroadcaster after creating one if needed.
     * @return IntentBroadcaster instance
     */
    public static IntentBroadcaster getInstance(Context context) {
        if (sIntentBroadcaster == null) {
            sIntentBroadcaster = new IntentBroadcaster(context);
        }
        return sIntentBroadcaster;
    }

    public static IntentBroadcaster getInstance() {
        return sIntentBroadcaster;
    }

    /**
     * Wrapper for ActivityManager.broadcastStickyIntent() that also stores intent to be rebroadcast
     * on USER_UNLOCKED
     */
    public void broadcastStickyIntent(Intent intent, int slotId) {
        logd("Broadcasting and adding intent for rebroadcast: " + intent.getAction() + " "
                + intent.getStringExtra(IccCardConstants.INTENT_KEY_ICC_STATE)
                + " for slotId " + slotId);
        synchronized (mRebroadcastIntents) {
            ActivityManager.broadcastStickyIntent(intent, UserHandle.USER_ALL);
            mRebroadcastIntents.put(slotId, intent);
        }
    }

    private void logd(String s) {
        Log.d(TAG, s);
    }
}
+3 −0
Original line number Diff line number Diff line
@@ -71,6 +71,7 @@ public class PhoneFactory {

    static private ProxyController sProxyController;
    static private UiccController sUiccController;
    private static IntentBroadcaster sIntentBroadcaster;
    private static @Nullable EuiccController sEuiccController;

    static private CommandsInterface sCommandsInterface = null;
@@ -240,6 +241,8 @@ public class PhoneFactory {
                sProxyController = ProxyController.getInstance(context, sPhones,
                        sUiccController, sCommandsInterfaces, sPhoneSwitcher);

                sIntentBroadcaster = IntentBroadcaster.getInstance(context);

                sNotificationChannelController = new NotificationChannelController(context);

                sTelephonyNetworkFactories = new TelephonyNetworkFactory[numPhones];
+1 −28
Original line number Diff line number Diff line
@@ -34,8 +34,6 @@ import android.os.Looper;
import android.os.Message;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.os.UserHandle;
import android.os.UserManager;
import android.preference.PreferenceManager;
import android.provider.Settings;
import android.service.euicc.EuiccProfileInfo;
@@ -61,10 +59,7 @@ import com.android.internal.telephony.uicc.IccUtils;
import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 *@hide
@@ -117,8 +112,6 @@ public class SubscriptionInfoUpdater extends Handler {
    private SubscriptionManager mSubscriptionManager = null;
    private EuiccManager mEuiccManager;
    private IPackageManager mPackageManager;
    private UserManager mUserManager;
    private Map<Integer, Intent> rebroadcastIntentsOnUnlock = new HashMap<>();

    // The current foreground user ID.
    private int mCurrentlyActiveUserId;
@@ -134,11 +127,9 @@ public class SubscriptionInfoUpdater extends Handler {
        mSubscriptionManager = SubscriptionManager.from(mContext);
        mEuiccManager = (EuiccManager) mContext.getSystemService(Context.EUICC_SERVICE);
        mPackageManager = IPackageManager.Stub.asInterface(ServiceManager.getService("package"));
        mUserManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE);

        IntentFilter intentFilter = new IntentFilter(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
        intentFilter.addAction(IccCardProxy.ACTION_INTERNAL_SIM_STATE_CHANGED);
        intentFilter.addAction(Intent.ACTION_USER_UNLOCKED);
        mContext.registerReceiver(sReceiver, intentFilter);

        mCarrierServiceBindHelper = new CarrierServiceBindHelper(mContext);
@@ -185,22 +176,6 @@ public class SubscriptionInfoUpdater extends Handler {
            String action = intent.getAction();
            logd("Action: " + action);

            if (action.equals(Intent.ACTION_USER_UNLOCKED)) {
                // broadcast pending intents
                Iterator iterator = rebroadcastIntentsOnUnlock.entrySet().iterator();
                while (iterator.hasNext()) {
                    Map.Entry pair = (Map.Entry) iterator.next();
                    Intent i = (Intent)pair.getValue();
                    i.putExtra(TelephonyIntents.EXTRA_REBROADCAST_ON_UNLOCK, true);
                    iterator.remove();
                    logd("Broadcasting intent ACTION_SIM_STATE_CHANGED for mCardIndex: " +
                            pair.getKey());
                    ActivityManager.broadcastStickyIntent(i, UserHandle.USER_ALL);
                }
                logd("[Receiver]-");
                return;
            }

            if (!action.equals(TelephonyIntents.ACTION_SIM_STATE_CHANGED) &&
                    !action.equals(IccCardProxy.ACTION_INTERNAL_SIM_STATE_CHANGED)) {
                return;
@@ -220,7 +195,6 @@ public class SubscriptionInfoUpdater extends Handler {
            // TODO: All of the below should be converted to ACTION_INTERNAL_SIM_STATE_CHANGED to
            // ensure that the SubscriptionInfo is updated before the broadcasts are sent out.
            if (action.equals(TelephonyIntents.ACTION_SIM_STATE_CHANGED)) {
                rebroadcastIntentsOnUnlock.put(slotIndex, intent);
                if (IccCardConstants.INTENT_VALUE_ICC_ABSENT.equals(simStatus)
                        || IccCardConstants.INTENT_VALUE_ICC_NOT_READY.equals(simStatus)) {
                    sendMessage(obtainMessage(EVENT_SIM_ABSENT_OR_NOT_READY, slotIndex, -1,
@@ -805,8 +779,7 @@ public class SubscriptionInfoUpdater extends Handler {
        SubscriptionManager.putPhoneIdAndSubIdExtra(i, slotId);
        logd("Broadcasting intent ACTION_SIM_STATE_CHANGED " + state + " reason " + reason +
             " for mCardIndex: " + slotId);
        ActivityManager.broadcastStickyIntent(i, UserHandle.USER_ALL);
        rebroadcastIntentsOnUnlock.put(slotId, i);
        IntentBroadcaster.getInstance().broadcastStickyIntent(i, slotId);
    }

    public void dispose() {
+2 −1
Original line number Diff line number Diff line
@@ -36,6 +36,7 @@ import com.android.internal.telephony.CommandsInterface.RadioState;
import com.android.internal.telephony.IccCard;
import com.android.internal.telephony.IccCardConstants;
import com.android.internal.telephony.IccCardConstants.State;
import com.android.internal.telephony.IntentBroadcaster;
import com.android.internal.telephony.MccTable;
import com.android.internal.telephony.Phone;
import com.android.internal.telephony.PhoneConstants;
@@ -507,7 +508,7 @@ public class IccCardProxy extends Handler implements IccCard {
            SubscriptionManager.putPhoneIdAndSubIdExtra(intent, mPhoneId);
            log("broadcastIccStateChangedIntent intent ACTION_SIM_STATE_CHANGED value=" + value
                + " reason=" + reason + " for mPhoneId=" + mPhoneId);
            ActivityManager.broadcastStickyIntent(intent, UserHandle.USER_ALL);
            IntentBroadcaster.getInstance().broadcastStickyIntent(intent, mPhoneId);
        }
    }

+1 −1
Original line number Diff line number Diff line
@@ -457,7 +457,7 @@ public class SubscriptionInfoUpdaterTest extends TelephonyTest {

    @Test
    @SmallTest
    public void testSimLockWIthIccId() throws Exception {
    public void testSimLockWithIccId() throws Exception {
        /* no need for IccId query */

        replaceInstance(SubscriptionInfoUpdater.class, "mIccId", null,
Loading