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

Commit 5936ad79 authored by Andrew Scull's avatar Andrew Scull Committed by Android (Google) Code Review
Browse files

Merge "Send identifier disclosure events to safety center" into main

parents 531e1229 97bb5a29
Loading
Loading
Loading
Loading
+7 −7
Original line number Diff line number Diff line
@@ -550,7 +550,7 @@ public class GsmCdmaPhone extends Phone {
            mIdentifierDisclosureNotifier =
                    mTelephonyComponentFactory
                            .inject(CellularIdentifierDisclosureNotifier.class.getName())
                            .makeIdentifierDisclosureNotifier();
                            .makeIdentifierDisclosureNotifier(mSafetySource);
            mCi.registerForCellularIdentifierDisclosures(
                    this, EVENT_CELL_IDENTIFIER_DISCLOSURE, null);
        }
@@ -3745,7 +3745,7 @@ public class GsmCdmaPhone extends Phone {
                if (mFeatureFlags.enableIdentifierDisclosureTransparencyUnsolEvents()
                        && mIdentifierDisclosureNotifier != null
                        && disclosure != null) {
                    mIdentifierDisclosureNotifier.addDisclosure(getSubId(), disclosure);
                    mIdentifierDisclosureNotifier.addDisclosure(mContext, getSubId(), disclosure);
                }
                break;

@@ -5389,9 +5389,9 @@ public class GsmCdmaPhone extends Phone {
        // flag is enabled.
        if (mFeatureFlags.enableIdentifierDisclosureTransparencyUnsolEvents()) {
            if (prefEnabled) {
            mIdentifierDisclosureNotifier.enable();
                mIdentifierDisclosureNotifier.enable(mContext);
            } else {
            mIdentifierDisclosureNotifier.disable();
                mIdentifierDisclosureNotifier.disable(mContext);
            }
        } else {
            logi("Not toggling enable state for disclosure notifier. Feature flag "
+3 −2
Original line number Diff line number Diff line
@@ -583,8 +583,9 @@ public class TelephonyComponentFactory {
    }

    /** Create CellularIdentifierDisclosureNotifier. */
    public CellularIdentifierDisclosureNotifier makeIdentifierDisclosureNotifier() {
        return CellularIdentifierDisclosureNotifier.getInstance();
    public CellularIdentifierDisclosureNotifier makeIdentifierDisclosureNotifier(
            CellularNetworkSecuritySafetySource safetySource) {
        return CellularIdentifierDisclosureNotifier.getInstance(safetySource);
    }

    /** Create NullCipherNotifier. */
+34 −32
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package com.android.internal.telephony.security;

import android.content.Context;
import android.telephony.CellularIdentifierDisclosure;

import com.android.internal.annotations.GuardedBy;
@@ -49,6 +50,7 @@ public class CellularIdentifierDisclosureNotifier {
    private static CellularIdentifierDisclosureNotifier sInstance = null;
    private final long mWindowCloseDuration;
    private final TimeUnit mWindowCloseUnit;
    private final CellularNetworkSecuritySafetySource mSafetySource;
    private final Object mEnabledLock = new Object();

    @GuardedBy("mEnabledLock")
@@ -61,11 +63,12 @@ public class CellularIdentifierDisclosureNotifier {
    // outside of that thread would require additional synchronization.
    private Map<Integer, DisclosureWindow> mWindows;

    public CellularIdentifierDisclosureNotifier() {
    public CellularIdentifierDisclosureNotifier(CellularNetworkSecuritySafetySource safetySource) {
        this(
                Executors.newSingleThreadScheduledExecutor(),
                DEFAULT_WINDOW_CLOSE_DURATION_IN_MINUTES,
                TimeUnit.MINUTES);
                TimeUnit.MINUTES,
                safetySource);
    }

    /**
@@ -79,18 +82,20 @@ public class CellularIdentifierDisclosureNotifier {
    public CellularIdentifierDisclosureNotifier(
            ScheduledExecutorService notificationQueue,
            long windowCloseDuration,
            TimeUnit windowCloseUnit) {
            TimeUnit windowCloseUnit,
            CellularNetworkSecuritySafetySource safetySource) {
        mSerializedWorkQueue = notificationQueue;
        mWindowCloseDuration = windowCloseDuration;
        mWindowCloseUnit = windowCloseUnit;
        mWindows = new HashMap<>();
        mSafetySource = safetySource;
    }

    /**
     * Add a CellularIdentifierDisclosure to be tracked by this instance. If appropriate, this will
     * trigger a user notification.
     */
    public void addDisclosure(int subId, CellularIdentifierDisclosure disclosure) {
    public void addDisclosure(Context context, int subId, CellularIdentifierDisclosure disclosure) {
        Rlog.d(TAG, "Identifier disclosure reported: " + disclosure);

        synchronized (mEnabledLock) {
@@ -111,7 +116,7 @@ public class CellularIdentifierDisclosureNotifier {
            // because we know that any actions taken on disabled will be scheduled after this
            // incrementAndNotify call.
            try {
                mSerializedWorkQueue.execute(incrementAndNotify(subId));
                mSerializedWorkQueue.execute(incrementAndNotify(context, subId));
            } catch (RejectedExecutionException e) {
                Rlog.e(TAG, "Failed to schedule incrementAndNotify: " + e.getMessage());
            }
@@ -122,12 +127,12 @@ public class CellularIdentifierDisclosureNotifier {
     * Re-enable if previously disabled. This means that {@code addDisclsoure} will start tracking
     * disclosures again and potentially emitting notifications.
     */
    public void enable() {
    public void enable(Context context) {
        synchronized (mEnabledLock) {
            Rlog.d(TAG, "enabled");
            mEnabled = true;
            try {
                mSerializedWorkQueue.execute(onEnableNotifier());
                mSerializedWorkQueue.execute(onEnableNotifier(context));
            } catch (RejectedExecutionException e) {
                Rlog.e(TAG, "Failed to schedule onEnableNotifier: " + e.getMessage());
            }
@@ -139,12 +144,12 @@ public class CellularIdentifierDisclosureNotifier {
     * This can be used to in response to a user disabling the feature to emit notifications.
     * If {@code addDisclosure} is called while in a disabled state, disclosures will be dropped.
     */
    public void disable() {
    public void disable(Context context) {
        Rlog.d(TAG, "disabled");
        synchronized (mEnabledLock) {
            mEnabled = false;
            try {
                mSerializedWorkQueue.execute(onDisableNotifier());
                mSerializedWorkQueue.execute(onDisableNotifier(context));
            } catch (RejectedExecutionException e) {
                Rlog.e(TAG, "Failed to schedule onDisableNotifier: " + e.getMessage());
            }
@@ -158,15 +163,16 @@ public class CellularIdentifierDisclosureNotifier {
    }

    /** Get a singleton CellularIdentifierDisclosureNotifier. */
    public static synchronized CellularIdentifierDisclosureNotifier getInstance() {
    public static synchronized CellularIdentifierDisclosureNotifier getInstance(
            CellularNetworkSecuritySafetySource safetySource) {
        if (sInstance == null) {
            sInstance = new CellularIdentifierDisclosureNotifier();
            sInstance = new CellularIdentifierDisclosureNotifier(safetySource);
        }

        return sInstance;
    }

    private Runnable incrementAndNotify(int subId) {
    private Runnable incrementAndNotify(Context context, int subId) {
        return () -> {
            DisclosureWindow window = mWindows.get(subId);
            if (window == null) {
@@ -174,7 +180,7 @@ public class CellularIdentifierDisclosureNotifier {
                mWindows.put(subId, window);
            }

            window.increment(this);
            window.increment(context, this);

            int disclosureCount = window.getDisclosureCount();

@@ -185,31 +191,29 @@ public class CellularIdentifierDisclosureNotifier {
                            + ". New disclosure count "
                            + disclosureCount);

            // TODO (b/308985417) emit safety center issue
            //            mSafetySource.setIdentifierDisclosure(
            //                    subId,
            //                    disclosureCount,
            //                    window.getFirstOpen(),
            //                    window.getCurrentEnd());
            mSafetySource.setIdentifierDisclosure(
                    context,
                    subId,
                    disclosureCount,
                    window.getFirstOpen(),
                    window.getCurrentEnd());
        };
    }

    private Runnable onDisableNotifier() {
    private Runnable onDisableNotifier(Context context) {
        return () -> {
            Rlog.d(TAG, "On disable notifier");
            for (DisclosureWindow window : mWindows.values()) {
                window.close();
            }
            // TODO (b/308985417) disable safety center issues
            // mSafetySource.setIdentifierDisclosureIssueEnabled(false);
            mSafetySource.setIdentifierDisclosureIssueEnabled(context, false);
        };
    }

    private Runnable onEnableNotifier() {
    private Runnable onEnableNotifier(Context context) {
        return () -> {
            Rlog.i(TAG, "On enable notifier");
            // TODO (b/308985417) enable safety center issues
            // mSafetySource.setIdentifierDisclosureIssueEnabled(true);
            mSafetySource.setIdentifierDisclosureIssueEnabled(context, true);
        };
    }

@@ -262,7 +266,7 @@ public class CellularIdentifierDisclosureNotifier {
     * A helper class that maintains all state associated with the disclosure window for a single
     * subId. No methods are thread safe. Callers must implement all synchronization.
     */
    private static class DisclosureWindow {
    private class DisclosureWindow {
        private int mDisclosureCount;
        private Instant mWindowFirstOpen;
        private Instant mLastEvent;
@@ -278,7 +282,7 @@ public class CellularIdentifierDisclosureNotifier {
            mWhenWindowCloses = null;
        }

        void increment(CellularIdentifierDisclosureNotifier notifier) {
        void increment(Context context, CellularIdentifierDisclosureNotifier notifier) {

            mDisclosureCount++;

@@ -295,7 +299,7 @@ public class CellularIdentifierDisclosureNotifier {
            try {
                mWhenWindowCloses =
                        notifier.mSerializedWorkQueue.schedule(
                                closeWindowRunnable(),
                                closeWindowRunnable(context),
                                notifier.mWindowCloseDuration,
                                notifier.mWindowCloseUnit);
            } catch (RejectedExecutionException e) {
@@ -331,7 +335,7 @@ public class CellularIdentifierDisclosureNotifier {
            mWhenWindowCloses = null;
        }

        private Runnable closeWindowRunnable() {
        private Runnable closeWindowRunnable(Context context) {
            return () -> {
                Rlog.i(
                        TAG,
@@ -340,9 +344,7 @@ public class CellularIdentifierDisclosureNotifier {
                                + ". Disclosure count was "
                                + getDisclosureCount());
                close();

                // TODO (b/308985417) clear safety center issue
                // mSafetySource.setIdentifierDisclosure(mSubId, 0, null, null);
                mSafetySource.clearIdentifierDisclosure(context, mSubId);
            };
        }

+10 −5
Original line number Diff line number Diff line
@@ -2868,7 +2868,10 @@ public class GsmCdmaPhoneTest extends TelephonyTest {

    @Test
    public void testCellularIdentifierDisclosure_disclosureEventAddedToNotifier() {
        int phoneId = 0;
        int subId = 10;
        when(mFeatureFlags.enableIdentifierDisclosureTransparencyUnsolEvents()).thenReturn(true);
        when(mSubscriptionManagerService.getSubId(phoneId)).thenReturn(subId);

        Phone phoneUT =
                new GsmCdmaPhone(
@@ -2876,7 +2879,7 @@ public class GsmCdmaPhoneTest extends TelephonyTest {
                        mMockCi,
                        mNotifier,
                        true,
                        0,
                        phoneId,
                        PhoneConstants.PHONE_TYPE_GSM,
                        mTelephonyComponentFactory,
                        (c, p) -> mImsManager,
@@ -2895,20 +2898,22 @@ public class GsmCdmaPhoneTest extends TelephonyTest {
        processAllMessages();

        verify(mIdentifierDisclosureNotifier, times(1))
                .addDisclosure(eq(mPhoneUT.getSubId()), eq(disclosure));
                .addDisclosure(eq(mContext), eq(subId), eq(disclosure));
    }

    @Test
    public void testCellularIdentifierDisclosure_disclosureEventNull() {
        int phoneId = 4;
        int subId = 6;
        when(mFeatureFlags.enableIdentifierDisclosureTransparencyUnsolEvents()).thenReturn(true);

        when(mSubscriptionManagerService.getSubId(phoneId)).thenReturn(subId);
        Phone phoneUT =
                new GsmCdmaPhone(
                        mContext,
                        mMockCi,
                        mNotifier,
                        true,
                        0,
                        phoneId,
                        PhoneConstants.PHONE_TYPE_GSM,
                        mTelephonyComponentFactory,
                        (c, p) -> mImsManager,
@@ -2920,7 +2925,7 @@ public class GsmCdmaPhoneTest extends TelephonyTest {
        processAllMessages();

        verify(mIdentifierDisclosureNotifier, never())
                .addDisclosure(eq(mPhoneUT.getSubId()), any(CellularIdentifierDisclosure.class));
                .addDisclosure(eq(mContext), eq(subId), any(CellularIdentifierDisclosure.class));
    }

    @Test
+1 −1
Original line number Diff line number Diff line
@@ -683,7 +683,7 @@ public abstract class TelephonyTest {
                .makeCellularNetworkSecuritySafetySource(any(Context.class));
        doReturn(mIdentifierDisclosureNotifier)
                .when(mTelephonyComponentFactory)
                .makeIdentifierDisclosureNotifier();
                .makeIdentifierDisclosureNotifier(any(CellularNetworkSecuritySafetySource.class));
        doReturn(mNullCipherNotifier)
                .when(mTelephonyComponentFactory)
                .makeNullCipherNotifier();
Loading