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

Commit 21d19cb6 authored by Amit Mahajan's avatar Amit Mahajan Committed by android-build-merger
Browse files

Merge "Add class IntentBroadcaster to rebroadcast intents on USER_UNLOCKED." am: 8afeb7b0

am: 88fb6c2c

Change-Id: I8d97f8a6407cf6d92a20992c16308c0975939a16
parents 5c2b0336 88fb6c2c
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
@@ -74,6 +74,7 @@ public class PhoneFactory {

    static private ProxyController sProxyController;
    static private UiccController sUiccController;
    private static IntentBroadcaster sIntentBroadcaster;

    static private CommandsInterface sCommandsInterface = null;
    static private SubscriptionInfoUpdater sSubInfoRecordUpdater = null;
@@ -238,6 +239,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
@@ -32,8 +32,6 @@ import android.os.IRemoteCallback;
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.telephony.CarrierConfigManager;
@@ -51,10 +49,7 @@ import com.android.internal.telephony.uicc.IccUtils;

import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 *@hide
@@ -105,8 +100,6 @@ public class SubscriptionInfoUpdater extends Handler {
    private static int[] mInsertSimState = new int[PROJECT_SIM_NUM];
    private SubscriptionManager mSubscriptionManager = null;
    private IPackageManager mPackageManager;
    private UserManager mUserManager;
    private Map<Integer, Intent> rebroadcastIntentsOnUnlock = new HashMap<>();

    // The current foreground user ID.
    private int mCurrentlyActiveUserId;
@@ -119,11 +112,9 @@ public class SubscriptionInfoUpdater extends Handler {
        mPhone = phone;
        mSubscriptionManager = SubscriptionManager.from(mContext);
        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);
@@ -170,22 +161,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;
@@ -203,7 +178,6 @@ public class SubscriptionInfoUpdater extends Handler {
            logd("simStatus: " + simStatus);

            if (action.equals(TelephonyIntents.ACTION_SIM_STATE_CHANGED)) {
                rebroadcastIntentsOnUnlock.put(slotIndex, intent);
                if (IccCardConstants.INTENT_VALUE_ICC_ABSENT.equals(simStatus)) {
                    sendMessage(obtainMessage(EVENT_SIM_ABSENT, slotIndex, -1));
                } else if (IccCardConstants.INTENT_VALUE_ICC_UNKNOWN.equals(simStatus)) {
@@ -685,8 +659,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;
@@ -506,7 +507,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
@@ -410,7 +410,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