From 25264ecfdacee1ef141c7456df348290e21036ac Mon Sep 17 00:00:00 2001 From: Remi NGUYEN VAN Date: Tue, 15 Mar 2022 12:03:01 +0000 Subject: [PATCH] Use TelephonyManagerShim factory method Constructors should not be used with shims as they do not return the right instance when running on an older device build. Use the added factory method for TelephonyManagerShim, which will return a shim instance that depends on the device SDK. Bug: 194332512 Test: atest CarrierPrivilegeAuthenticatorTest Change-Id: I3e0857b4ced007dc4c0367623105dba1a6140800 --- .../CarrierPrivilegeAuthenticator.java | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/service/src/com/android/server/connectivity/CarrierPrivilegeAuthenticator.java b/service/src/com/android/server/connectivity/CarrierPrivilegeAuthenticator.java index b7617626d3..7123236859 100644 --- a/service/src/com/android/server/connectivity/CarrierPrivilegeAuthenticator.java +++ b/service/src/com/android/server/connectivity/CarrierPrivilegeAuthenticator.java @@ -31,7 +31,6 @@ import android.content.pm.PackageManager; import android.net.NetworkCapabilities; import android.net.NetworkSpecifier; import android.net.TelephonyNetworkSpecifier; -import android.os.Build; import android.os.Handler; import android.os.HandlerThread; import android.os.Process; @@ -96,11 +95,7 @@ public class CarrierPrivilegeAuthenticator extends BroadcastReceiver { @NonNull final TelephonyManager t) { mContext = c; mTelephonyManager = t; - if (Build.VERSION.SDK_INT > Build.VERSION_CODES.S) { - mTelephonyManagerShim = new TelephonyManagerShimImpl(mTelephonyManager); - } else { - mTelephonyManagerShim = null; - } + mTelephonyManagerShim = TelephonyManagerShimImpl.newInstance(mTelephonyManager); mThread = new HandlerThread(TAG); mThread.start(); mHandler = new Handler(mThread.getLooper()) {}; @@ -194,36 +189,30 @@ public class CarrierPrivilegeAuthenticator extends BroadcastReceiver { private void addCarrierPrivilegesListener(int logicalSlotIndex, Executor executor, CarrierPrivilegesListenerShim listener) { - if (mTelephonyManagerShim == null) { - return; - } try { mTelephonyManagerShim.addCarrierPrivilegesListener( logicalSlotIndex, executor, listener); } catch (UnsupportedApiLevelException unsupportedApiLevelException) { + // Should not happen since CarrierPrivilegeAuthenticator is only used on T+ Log.e(TAG, "addCarrierPrivilegesListener API is not available"); } } private void removeCarrierPrivilegesListener(CarrierPrivilegesListenerShim listener) { - if (mTelephonyManagerShim == null) { - return; - } try { mTelephonyManagerShim.removeCarrierPrivilegesListener(listener); } catch (UnsupportedApiLevelException unsupportedApiLevelException) { + // Should not happen since CarrierPrivilegeAuthenticator is only used on T+ Log.e(TAG, "removeCarrierPrivilegesListener API is not available"); } } private String getCarrierServicePackageNameForLogicalSlot(int logicalSlotIndex) { - if (mTelephonyManagerShim == null) { - return null; - } try { return mTelephonyManagerShim.getCarrierServicePackageNameForLogicalSlot( logicalSlotIndex); } catch (UnsupportedApiLevelException unsupportedApiLevelException) { + // Should not happen since CarrierPrivilegeAuthenticator is only used on T+ Log.e(TAG, "getCarrierServicePackageNameForLogicalSlot API is not available"); } return null;