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

Commit 47bc53ae authored by Sooraj Sasindran's avatar Sooraj Sasindran
Browse files

Add shim apis for carrier privilge listener

Bug: 194332512
CTS-Coverage-Bug: 215722242
Test: build
Change-Id: I008040cf3e204f22bf63191d7fd3a53ca897c981
parent ea4eb78f
Loading
Loading
Loading
Loading
+35 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2021 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.networkstack.apishim.api29;

import android.os.Build;
import android.telephony.TelephonyManager;

import androidx.annotation.RequiresApi;

import com.android.networkstack.apishim.common.TelephonyManagerShim;

/**
 * Implementation of {@link TelephonyManagerShim} for API 29.
 */
@RequiresApi(Build.VERSION_CODES.Q)
public class TelephonyManagerShimImpl implements TelephonyManagerShim {
    protected final TelephonyManager mTm;
    public TelephonyManagerShimImpl(TelephonyManager tm) {
        mTm = tm;
    }
}
+35 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2021 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.networkstack.apishim.api30;

import android.os.Build;
import android.telephony.TelephonyManager;

import androidx.annotation.RequiresApi;

import com.android.networkstack.apishim.common.TelephonyManagerShim;

/**
 * Implementation of {@link TelephonyManagerShim} for API 30.
 */
@RequiresApi(Build.VERSION_CODES.R)
public class TelephonyManagerShimImpl
        extends com.android.networkstack.apishim.api29.TelephonyManagerShimImpl {
    public TelephonyManagerShimImpl(TelephonyManager telephonyManager) {
        super(telephonyManager);
    }
}
+35 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2021 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.networkstack.apishim.api31;

import android.os.Build;
import android.telephony.TelephonyManager;

import androidx.annotation.RequiresApi;

import com.android.networkstack.apishim.common.TelephonyManagerShim;

/**
 * Implementation of {@link TelephonyManagerShim} for API 31.
 */
@RequiresApi(Build.VERSION_CODES.S)
public class TelephonyManagerShimImpl
        extends com.android.networkstack.apishim.api30.TelephonyManagerShimImpl {
    public TelephonyManagerShimImpl(TelephonyManager telephonyManager) {
        super(telephonyManager);
    }
}
+73 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 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.networkstack.apishim;

import android.os.Build;
import android.telephony.TelephonyManager;
import android.telephony.TelephonyManager.CarrierPrivilegesListener;

import androidx.annotation.RequiresApi;

import com.android.networkstack.apishim.common.TelephonyManagerShim;
import com.android.networkstack.apishim.common.UnsupportedApiLevelException;

import java.util.HashMap;
import java.util.List;
import java.util.concurrent.Executor;

/**
 * Implementation of {@link TelephonyManagerShim} for API 33.
 */
@RequiresApi(Build.VERSION_CODES.TIRAMISU)
public class TelephonyManagerShimImpl extends
        com.android.networkstack.apishim.api31.TelephonyManagerShimImpl {
    private HashMap<CarrierPrivilegesListenerShim, CarrierPrivilegesListener> mListenerMap =
            new HashMap<>();
    public TelephonyManagerShimImpl(TelephonyManager telephonyManager) {
        super(telephonyManager);
    }

    /** See android.telephony.TelephonyManager#addCarrierPrivilegesListener */
    public void addCarrierPrivilegesListener(
            int logicalSlotIndex,
            Executor executor,
            CarrierPrivilegesListenerShim listener)
            throws UnsupportedApiLevelException {
        CarrierPrivilegesListener carrierPrivilegesListener = new CarrierPrivilegesListener() {
            public void onCarrierPrivilegesChanged(
                    List<String> privilegedPackageNames,
                    int[] privilegedUids) {
                listener.onCarrierPrivilegesChanged(privilegedPackageNames, privilegedUids);
            }
        };
        mTm.addCarrierPrivilegesListener(logicalSlotIndex, executor, carrierPrivilegesListener);
        mListenerMap.put(listener, carrierPrivilegesListener);
    }

    /** See android.telephony.TelephonyManager#addCarrierPrivilegesListener */
    public void removeCarrierPrivilegesListener(
            CarrierPrivilegesListenerShim listener)
            throws UnsupportedApiLevelException {
        mTm.removeCarrierPrivilegesListener(mListenerMap.get(listener));
        mListenerMap.remove(listener);
    }

    /** See android.telephony.TelephonyManager#getCarrierServicePackageNameForLogicalSlot */
    public String getCarrierServicePackageNameForLogicalSlot(int logicalSlotIndex) {
        return mTm.getCarrierServicePackageNameForLogicalSlot(logicalSlotIndex);
    }
}
+62 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2021 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.networkstack.apishim.common;

import java.util.List;
import java.util.concurrent.Executor;

/**
 * Interface used to access API methods in {@link android.telephony.TelephonyManager}, with
 * appropriate fallbacks if the methods are not yet part of the released API.
 *
 * <p>This interface makes it easier for callers to use TelephonyManagerShimImpl, as it's more
 * obvious what methods must be implemented on each API level, and it abstracts from callers the
 * need to reference classes that have different implementations (which also does not work well
 * with IDEs).
 */
public interface TelephonyManagerShim {
    /** See android.telephony.TelephonyManager.CarrierPrivilegesListener */
    public interface CarrierPrivilegesListenerShim {
        /** See android.telephony.TelephonyManager
         * .CarrierPrivilegesListener#onCarrierPrivilegesChanged */
        void onCarrierPrivilegesChanged(
                List<String> privilegedPackageNames,
                int[] privilegedUids);
    }

    /** See android.telephony.TelephonyManager#addCarrierPrivilegesListener */
    default void addCarrierPrivilegesListener(
            int logicalSlotIndex,
            Executor executor,
            CarrierPrivilegesListenerShim listener)
            throws UnsupportedApiLevelException {
        throw new UnsupportedApiLevelException("Only supported starting from API 33");
    }

    /** See android.telephony.TelephonyManager#addCarrierPrivilegesListener */
    default void removeCarrierPrivilegesListener(
            CarrierPrivilegesListenerShim listener)
            throws UnsupportedApiLevelException {
        throw new UnsupportedApiLevelException("Only supported starting from API 33");
    }

    /** See android.telephony.TelephonyManager#getCarrierServicePackageNameForLogicalSlot */
    default String getCarrierServicePackageNameForLogicalSlot(int logicalSlotIndex)
            throws UnsupportedApiLevelException {
        throw new UnsupportedApiLevelException("Only supported starting from API 33");
    }
}