Clean hidden api - getSystemService

Remove hidden api for mainline project.
This commit is for misc android/os apis.
Changes alls call to getSystemService to make lint an happy robot

Bug: 190440540
Test: atest packages/apps/Bluetooth/tests/unit
Tag: #refactor
Change-Id: Ia5ea35a6d92f6e475d3a8c686e9bbe0b0ee9c9d6
diff --git a/android/app/src/com/android/bluetooth/Utils.java b/android/app/src/com/android/bluetooth/Utils.java
index b9b156c..da6034b 100644
--- a/android/app/src/com/android/bluetooth/Utils.java
+++ b/android/app/src/com/android/bluetooth/Utils.java
@@ -625,7 +625,7 @@
         // Use the Bluetooth process identity when making call to get parent user
         final long ident = Binder.clearCallingIdentity();
         try {
-            UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
+            UserManager um = context.getSystemService(UserManager.class);
             UserHandle uh = um.getProfileParent(callingUser);
             int parentUser = (uh != null) ? uh.getIdentifier() : UserHandle.USER_NULL;
 
diff --git a/android/app/src/com/android/bluetooth/a2dp/A2dpCodecConfig.java b/android/app/src/com/android/bluetooth/a2dp/A2dpCodecConfig.java
index 6e0b1c0..54e2afd 100644
--- a/android/app/src/com/android/bluetooth/a2dp/A2dpCodecConfig.java
+++ b/android/app/src/com/android/bluetooth/a2dp/A2dpCodecConfig.java
@@ -59,7 +59,7 @@
         mA2dpNativeInterface = a2dpNativeInterface;
         mCodecConfigPriorities = assignCodecConfigPriorities();
 
-        AudioManager audioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);
+        AudioManager audioManager = mContext.getSystemService(AudioManager.class);
         if (audioManager == null) {
           Log.w(TAG, "Can't obtain the codec offloading prefernece from null AudioManager");
           return;
diff --git a/android/app/src/com/android/bluetooth/a2dp/A2dpService.java b/android/app/src/com/android/bluetooth/a2dp/A2dpService.java
index c6b1759..93aeee4 100644
--- a/android/app/src/com/android/bluetooth/a2dp/A2dpService.java
+++ b/android/app/src/com/android/bluetooth/a2dp/A2dpService.java
@@ -123,7 +123,7 @@
                 "A2dpNativeInterface cannot be null when A2dpService starts");
         mDatabaseManager = Objects.requireNonNull(mAdapterService.getDatabase(),
                 "DatabaseManager cannot be null when A2dpService starts");
-        mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
+        mAudioManager = getSystemService(AudioManager.class);
         Objects.requireNonNull(mAudioManager,
                                "AudioManager cannot be null when A2dpService starts");
 
diff --git a/android/app/src/com/android/bluetooth/a2dpsink/A2dpSinkStreamHandler.java b/android/app/src/com/android/bluetooth/a2dpsink/A2dpSinkStreamHandler.java
index a6ce1f3..84a6fcd 100644
--- a/android/app/src/com/android/bluetooth/a2dpsink/A2dpSinkStreamHandler.java
+++ b/android/app/src/com/android/bluetooth/a2dpsink/A2dpSinkStreamHandler.java
@@ -16,7 +16,6 @@
 
 package com.android.bluetooth.a2dpsink;
 
-import android.annotation.RequiresPermission;
 import android.bluetooth.BluetoothDevice;
 import android.bluetooth.BluetoothHeadsetClientCall;
 import android.content.Context;
@@ -115,7 +114,7 @@
     public A2dpSinkStreamHandler(A2dpSinkService a2dpSinkService, Context context) {
         mA2dpSinkService = a2dpSinkService;
         mContext = context;
-        mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
+        mAudioManager = context.getSystemService(AudioManager.class);
     }
 
     /**
diff --git a/android/app/src/com/android/bluetooth/audio_util/MediaPlayerList.java b/android/app/src/com/android/bluetooth/audio_util/MediaPlayerList.java
index cdb85d2..cb99acd 100644
--- a/android/app/src/com/android/bluetooth/audio_util/MediaPlayerList.java
+++ b/android/app/src/com/android/bluetooth/audio_util/MediaPlayerList.java
@@ -136,11 +136,10 @@
         pkgFilter.addDataScheme(PACKAGE_SCHEME);
         context.registerReceiver(mPackageChangedBroadcastReceiver, pkgFilter);
 
-        mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
+        mAudioManager = context.getSystemService(AudioManager.class);
         mAudioManager.registerAudioPlaybackCallback(mAudioPlaybackCallback, new Handler(mLooper));
 
-        mMediaSessionManager =
-                (MediaSessionManager) context.getSystemService(Context.MEDIA_SESSION_SERVICE);
+        mMediaSessionManager = context.getSystemService(MediaSessionManager.class);
         mMediaSessionManager.addOnActiveSessionsChangedListener(
                 mActiveSessionsChangedListener, null, new Handler(looper));
         mMediaSessionManager.addOnMediaKeyEventSessionChangedListener(
diff --git a/android/app/src/com/android/bluetooth/avrcp/AvrcpTargetService.java b/android/app/src/com/android/bluetooth/avrcp/AvrcpTargetService.java
index 644ef34..cbee693 100644
--- a/android/app/src/com/android/bluetooth/avrcp/AvrcpTargetService.java
+++ b/android/app/src/com/android/bluetooth/avrcp/AvrcpTargetService.java
@@ -16,7 +16,6 @@
 
 package com.android.bluetooth.avrcp;
 
-import android.annotation.RequiresPermission;
 import android.bluetooth.BluetoothA2dp;
 import android.bluetooth.BluetoothDevice;
 import android.bluetooth.BluetoothProfile;
@@ -206,7 +205,7 @@
             return true;
         }
 
-        mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
+        mAudioManager = getSystemService(AudioManager.class);
         sDeviceMaxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
 
         mMediaPlayerList = new MediaPlayerList(Looper.myLooper(), this);
@@ -218,7 +217,7 @@
 
         mVolumeManager = new AvrcpVolumeManager(this, mAudioManager, mNativeInterface);
 
-        UserManager userManager = UserManager.get(getApplicationContext());
+        UserManager userManager = getApplicationContext().getSystemService(UserManager.class);
         if (userManager.isUserUnlocked()) {
             mMediaPlayerList.init(new ListCallback());
         }
diff --git a/android/app/src/com/android/bluetooth/avrcpcontroller/AvrcpControllerStateMachine.java b/android/app/src/com/android/bluetooth/avrcpcontroller/AvrcpControllerStateMachine.java
index 96f4f82..75eca47 100755
--- a/android/app/src/com/android/bluetooth/avrcpcontroller/AvrcpControllerStateMachine.java
+++ b/android/app/src/com/android/bluetooth/avrcpcontroller/AvrcpControllerStateMachine.java
@@ -21,7 +21,6 @@
 import android.bluetooth.BluetoothAvrcpController;
 import android.bluetooth.BluetoothDevice;
 import android.bluetooth.BluetoothProfile;
-import android.content.Context;
 import android.content.Intent;
 import android.media.AudioManager;
 import android.net.Uri;
@@ -179,7 +178,7 @@
 
         mGetFolderList = new GetFolderList();
         addState(mGetFolderList, mConnected);
-        mAudioManager = (AudioManager) service.getSystemService(Context.AUDIO_SERVICE);
+        mAudioManager = service.getSystemService(AudioManager.class);
         mIsVolumeFixed = mAudioManager.isVolumeFixed();
 
         setInitialState(mDisconnected);
diff --git a/android/app/src/com/android/bluetooth/btservice/ActiveDeviceManager.java b/android/app/src/com/android/bluetooth/btservice/ActiveDeviceManager.java
index 74300e7..c60e977 100644
--- a/android/app/src/com/android/bluetooth/btservice/ActiveDeviceManager.java
+++ b/android/app/src/com/android/bluetooth/btservice/ActiveDeviceManager.java
@@ -370,7 +370,7 @@
     ActiveDeviceManager(AdapterService service, ServiceFactory factory) {
         mAdapterService = service;
         mFactory = factory;
-        mAudioManager = (AudioManager) service.getSystemService(Context.AUDIO_SERVICE);
+        mAudioManager = service.getSystemService(AudioManager.class);
         mAudioManagerAudioDeviceCallback = new AudioManagerAudioDeviceCallback();
     }
 
diff --git a/android/app/src/com/android/bluetooth/btservice/AdapterService.java b/android/app/src/com/android/bluetooth/btservice/AdapterService.java
index f467925..8a7e58c 100644
--- a/android/app/src/com/android/bluetooth/btservice/AdapterService.java
+++ b/android/app/src/com/android/bluetooth/btservice/AdapterService.java
@@ -520,8 +520,9 @@
         // Android TV doesn't show consent dialogs for just works and encryption only le pairing
         boolean isAtvDevice = getApplicationContext().getPackageManager().hasSystemFeature(
                 PackageManager.FEATURE_LEANBACK_ONLY);
-        initNative(isGuest(), isCommonCriteriaMode(), configCompareResult, getInitFlags(),
-                isAtvDevice);
+        mUserManager = getSystemService(UserManager.class);
+        initNative(mUserManager.isGuestUser(), isCommonCriteriaMode(), configCompareResult,
+                getInitFlags(), isAtvDevice);
         mNativeAvailable = true;
         mCallbacks = new RemoteCallbackList<IBluetoothCallback>();
         mAppOps = getSystemService(AppOpsManager.class);
@@ -529,9 +530,8 @@
         getAdapterPropertyNative(AbstractionLayer.BT_PROPERTY_BDADDR);
         getAdapterPropertyNative(AbstractionLayer.BT_PROPERTY_BDNAME);
         getAdapterPropertyNative(AbstractionLayer.BT_PROPERTY_CLASS_OF_DEVICE);
-        mAlarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
-        mPowerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
-        mUserManager = (UserManager) getSystemService(Context.USER_SERVICE);
+        mAlarmManager = getSystemService(AlarmManager.class);
+        mPowerManager = getSystemService(PowerManager.class);
         mBatteryStatsManager = getSystemService(BatteryStatsManager.class);
         mCompanionDeviceManager = getSystemService(CompanionDeviceManager.class);
 
@@ -3588,13 +3588,8 @@
         }
     };
 
-    private boolean isGuest() {
-        return UserManager.get(this).isGuestUser();
-    }
-
     private boolean isCommonCriteriaMode() {
-        return ((DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE))
-                .isCommonCriteriaModeEnabled(null);
+        return getSystemService(DevicePolicyManager.class).isCommonCriteriaModeEnabled(null);
     }
 
     @SuppressLint("AndroidFrameworkRequiresPermission")
diff --git a/android/app/src/com/android/bluetooth/btservice/ProfileService.java b/android/app/src/com/android/bluetooth/btservice/ProfileService.java
index 3be69f7..d9b3f7d 100644
--- a/android/app/src/com/android/bluetooth/btservice/ProfileService.java
+++ b/android/app/src/com/android/bluetooth/btservice/ProfileService.java
@@ -301,7 +301,7 @@
         getApplicationContext().registerReceiver(mUserSwitchedReceiver, filter);
         int currentUserId = ActivityManager.getCurrentUser();
         setCurrentUser(currentUserId);
-        UserManager userManager = UserManager.get(getApplicationContext());
+        UserManager userManager = getApplicationContext().getSystemService(UserManager.class);
         if (userManager.isUserUnlocked(UserHandle.of(currentUserId))) {
             setUserUnlocked(currentUserId);
         }
diff --git a/android/app/src/com/android/bluetooth/gatt/ScanManager.java b/android/app/src/com/android/bluetooth/gatt/ScanManager.java
index 6688184..bc88596 100644
--- a/android/app/src/com/android/bluetooth/gatt/ScanManager.java
+++ b/android/app/src/com/android/bluetooth/gatt/ScanManager.java
@@ -128,9 +128,9 @@
         mService = service;
         mScanNative = new ScanNative();
         mCurUsedTrackableAdvertisements = 0;
-        mDm = (DisplayManager) mService.getSystemService(Context.DISPLAY_SERVICE);
-        mActivityManager = (ActivityManager) mService.getSystemService(Context.ACTIVITY_SERVICE);
-        mLocationManager = (LocationManager) mService.getSystemService(Context.LOCATION_SERVICE);
+        mDm = mService.getSystemService(DisplayManager.class);
+        mActivityManager = mService.getSystemService(ActivityManager.class);
+        mLocationManager = mService.getSystemService(LocationManager.class);
 
         mPriorityMap.put(ScanSettings.SCAN_MODE_OPPORTUNISTIC, 0);
         mPriorityMap.put(ScanSettings.SCAN_MODE_LOW_POWER, 1);
@@ -560,7 +560,7 @@
             mFilterIndexStack = new ArrayDeque<Integer>();
             mClientFilterIndexMap = new HashMap<Integer, Deque<Integer>>();
 
-            mAlarmManager = (AlarmManager) mService.getSystemService(Context.ALARM_SERVICE);
+            mAlarmManager = mService.getSystemService(AlarmManager.class);
             Intent batchIntent = new Intent(ACTION_REFRESH_BATCHED_SCAN, null);
             mBatchScanIntervalIntent = PendingIntent.getBroadcast(mService, 0, batchIntent,
                     PendingIntent.FLAG_IMMUTABLE);
diff --git a/android/app/src/com/android/bluetooth/hearingaid/HearingAidService.java b/android/app/src/com/android/bluetooth/hearingaid/HearingAidService.java
index c712ddb..4659d62 100644
--- a/android/app/src/com/android/bluetooth/hearingaid/HearingAidService.java
+++ b/android/app/src/com/android/bluetooth/hearingaid/HearingAidService.java
@@ -114,7 +114,7 @@
                 "HearingAidNativeInterface cannot be null when HearingAidService starts");
         mDatabaseManager = Objects.requireNonNull(mAdapterService.getDatabase(),
                 "DatabaseManager cannot be null when HearingAidService starts");
-        mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
+        mAudioManager = getSystemService(AudioManager.class);
         Objects.requireNonNull(mAudioManager,
                 "AudioManager cannot be null when HearingAidService starts");
 
diff --git a/android/app/src/com/android/bluetooth/hfp/HeadsetPhoneState.java b/android/app/src/com/android/bluetooth/hfp/HeadsetPhoneState.java
index 9bcc534..50094cf 100644
--- a/android/app/src/com/android/bluetooth/hfp/HeadsetPhoneState.java
+++ b/android/app/src/com/android/bluetooth/hfp/HeadsetPhoneState.java
@@ -16,10 +16,8 @@
 
 package com.android.bluetooth.hfp;
 
-import android.annotation.RequiresPermission;
 import android.annotation.SuppressLint;
 import android.bluetooth.BluetoothDevice;
-import android.content.Context;
 import android.os.Handler;
 import android.telephony.PhoneStateListener;
 import android.telephony.ServiceState;
@@ -76,8 +74,7 @@
     HeadsetPhoneState(HeadsetService headsetService) {
         Objects.requireNonNull(headsetService, "headsetService is null");
         mHeadsetService = headsetService;
-        mTelephonyManager =
-                (TelephonyManager) mHeadsetService.getSystemService(Context.TELEPHONY_SERVICE);
+        mTelephonyManager = mHeadsetService.getSystemService(TelephonyManager.class);
         Objects.requireNonNull(mTelephonyManager, "TELEPHONY_SERVICE is null");
         // Register for SubscriptionInfo list changes which is guaranteed to invoke
         // onSubscriptionInfoChanged and which in turns calls loadInBackgroud.
diff --git a/android/app/src/com/android/bluetooth/hfp/HeadsetSystemInterface.java b/android/app/src/com/android/bluetooth/hfp/HeadsetSystemInterface.java
index 141a43a..100180a 100644
--- a/android/app/src/com/android/bluetooth/hfp/HeadsetSystemInterface.java
+++ b/android/app/src/com/android/bluetooth/hfp/HeadsetSystemInterface.java
@@ -21,10 +21,8 @@
 import android.annotation.RequiresPermission;
 import android.bluetooth.BluetoothDevice;
 import android.bluetooth.BluetoothHeadset;
-import com.android.bluetooth.telephony.BluetoothInCallService;
 import android.content.ActivityNotFoundException;
 import android.content.ComponentName;
-import android.content.Context;
 import android.content.Intent;
 import android.content.pm.ApplicationInfo;
 import android.content.pm.PackageManager;
@@ -33,6 +31,7 @@
 import android.os.PowerManager;
 import android.util.Log;
 
+import com.android.bluetooth.telephony.BluetoothInCallService;
 import com.android.internal.annotations.VisibleForTesting;
 
 import java.util.List;
@@ -56,9 +55,8 @@
             Log.wtf(TAG, "HeadsetService parameter is null");
         }
         mHeadsetService = headsetService;
-        mAudioManager = (AudioManager) mHeadsetService.getSystemService(Context.AUDIO_SERVICE);
-        PowerManager powerManager =
-                (PowerManager) mHeadsetService.getSystemService(Context.POWER_SERVICE);
+        mAudioManager = mHeadsetService.getSystemService(AudioManager.class);
+        PowerManager powerManager = mHeadsetService.getSystemService(PowerManager.class);
         mVoiceRecognitionWakeLock =
                 powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG + ":VoiceRecognition");
         mVoiceRecognitionWakeLock.setReferenceCounted(false);
diff --git a/android/app/src/com/android/bluetooth/hfpclient/HeadsetClientService.java b/android/app/src/com/android/bluetooth/hfpclient/HeadsetClientService.java
index dfb9f41..666f3a0 100644
--- a/android/app/src/com/android/bluetooth/hfpclient/HeadsetClientService.java
+++ b/android/app/src/com/android/bluetooth/hfpclient/HeadsetClientService.java
@@ -98,7 +98,7 @@
 
         mBatteryManager = getSystemService(BatteryManager.class);
 
-        mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
+        mAudioManager = getSystemService(AudioManager.class);
         if (mAudioManager == null) {
             Log.e(TAG, "AudioManager service doesn't exist?");
         } else {
diff --git a/android/app/src/com/android/bluetooth/hfpclient/connserv/HfpClientConnectionService.java b/android/app/src/com/android/bluetooth/hfpclient/connserv/HfpClientConnectionService.java
index 8ee2b67..386bba4 100644
--- a/android/app/src/com/android/bluetooth/hfpclient/connserv/HfpClientConnectionService.java
+++ b/android/app/src/com/android/bluetooth/hfpclient/connserv/HfpClientConnectionService.java
@@ -134,7 +134,7 @@
             Log.d(TAG, "onCreate");
         }
         mAdapter = BluetoothAdapter.getDefaultAdapter();
-        mTelecomManager = (TelecomManager) getSystemService(Context.TELECOM_SERVICE);
+        mTelecomManager = getSystemService(TelecomManager.class);
         if (mTelecomManager != null) mTelecomManager.clearPhoneAccounts();
         mAdapter.getProfileProxy(this, mServiceListener, BluetoothProfile.HEADSET_CLIENT);
     }
diff --git a/android/app/src/com/android/bluetooth/hfpclient/connserv/HfpClientDeviceBlock.java b/android/app/src/com/android/bluetooth/hfpclient/connserv/HfpClientDeviceBlock.java
index ca38778..e6fe3eb 100644
--- a/android/app/src/com/android/bluetooth/hfpclient/connserv/HfpClientDeviceBlock.java
+++ b/android/app/src/com/android/bluetooth/hfpclient/connserv/HfpClientDeviceBlock.java
@@ -61,7 +61,7 @@
         mDevice = device;
         mTAG = "HfpClientDeviceBlock." + mDevice.getAddress();
         mPhoneAccount = HfpClientConnectionService.createAccount(mContext, device);
-        mTelecomManager = (TelecomManager) mContext.getSystemService(Context.TELECOM_SERVICE);
+        mTelecomManager = mContext.getSystemService(TelecomManager.class);
 
         // Register the phone account since block is created only when devices are connected
         mTelecomManager.registerPhoneAccount(mPhoneAccount);
diff --git a/android/app/src/com/android/bluetooth/hid/HidDeviceService.java b/android/app/src/com/android/bluetooth/hid/HidDeviceService.java
index b99fbc0..3ddaeab 100644
--- a/android/app/src/com/android/bluetooth/hid/HidDeviceService.java
+++ b/android/app/src/com/android/bluetooth/hid/HidDeviceService.java
@@ -30,7 +30,6 @@
 import android.bluetooth.IBluetoothHidDeviceCallback;
 import android.content.Attributable;
 import android.content.AttributionSource;
-import android.content.Context;
 import android.content.Intent;
 import android.os.Binder;
 import android.os.Handler;
@@ -746,7 +745,7 @@
         mHidDeviceNativeInterface = HidDeviceNativeInterface.getInstance();
         mHidDeviceNativeInterface.init();
         mNativeAvailable = true;
-        mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
+        mActivityManager = getSystemService(ActivityManager.class);
         mActivityManager.addOnUidImportanceListener(mUidImportanceListener,
                 FOREGROUND_IMPORTANCE_CUTOFF);
         setHidDeviceService(this);
diff --git a/android/app/src/com/android/bluetooth/map/BluetoothMapContent.java b/android/app/src/com/android/bluetooth/map/BluetoothMapContent.java
index 3b525d1..5324ec2 100644
--- a/android/app/src/com/android/bluetooth/map/BluetoothMapContent.java
+++ b/android/app/src/com/android/bluetooth/map/BluetoothMapContent.java
@@ -2185,8 +2185,7 @@
     }
 
     private void setFilterInfo(FilterInfo fi) {
-        TelephonyManager tm =
-                (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
+        TelephonyManager tm = mContext.getSystemService(TelephonyManager.class);
         if (tm != null) {
             fi.mPhoneType = tm.getPhoneType();
             fi.mPhoneNum = tm.getLine1Number();
@@ -3659,8 +3658,7 @@
         long time = -1;
         String msgBody;
         BluetoothMapbMessageSms message = new BluetoothMapbMessageSms();
-        TelephonyManager tm =
-                (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
+        TelephonyManager tm = mContext.getSystemService(TelephonyManager.class);
 
         Cursor c = mResolver.query(Sms.CONTENT_URI, SMS_PROJECTION, "_ID = " + id, null, null);
         if (c == null || !c.moveToFirst()) {
diff --git a/android/app/src/com/android/bluetooth/map/BluetoothMapContentObserver.java b/android/app/src/com/android/bluetooth/map/BluetoothMapContentObserver.java
index ad36b73..f7a2dde 100644
--- a/android/app/src/com/android/bluetooth/map/BluetoothMapContentObserver.java
+++ b/android/app/src/com/android/bluetooth/map/BluetoothMapContentObserver.java
@@ -26,7 +26,6 @@
 import android.content.Intent;
 import android.content.IntentFilter;
 import android.content.IntentFilter.MalformedMimeTypeException;
-import android.content.pm.PackageManager;
 import android.database.ContentObserver;
 import android.database.Cursor;
 import android.database.sqlite.SQLiteException;
@@ -447,8 +446,7 @@
 
     private TYPE getSmsType() {
         TYPE smsType = null;
-        TelephonyManager tm =
-                (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
+        TelephonyManager tm = mContext.getSystemService(TelephonyManager.class);
 
         if (tm.getPhoneType() == TelephonyManager.PHONE_TYPE_CDMA) {
             smsType = TYPE.SMS_CDMA;
@@ -1242,7 +1240,7 @@
         if (V) {
             Log.d(TAG, "initMsgList");
         }
-        UserManager manager = UserManager.get(mContext);
+        UserManager manager = mContext.getSystemService(UserManager.class);
         if (manager == null || !manager.isUserUnlocked()) {
             return;
         }
@@ -1454,9 +1452,8 @@
                                         name = phone;
                                     }
                                 } else {
-                                    TelephonyManager tm =
-                                            (TelephonyManager) mContext.getSystemService(
-                                                    Context.TELEPHONY_SERVICE);
+                                    TelephonyManager tm = mContext.getSystemService(
+                                            TelephonyManager.class);
                                     if (tm != null) {
                                         phone = tm.getLine1Number();
                                         name = phone;
@@ -3425,7 +3422,7 @@
 
     private class CeBroadcastReceiver extends BroadcastReceiver {
         public void register() {
-            UserManager manager = UserManager.get(mContext);
+            UserManager manager = mContext.getSystemService(UserManager.class);
             if (manager == null || manager.isUserUnlocked()) {
                 mStorageUnlocked = true;
                 return;
@@ -3599,21 +3596,19 @@
     }
 
     private void registerPhoneServiceStateListener() {
-        TelephonyManager tm =
-                (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
+        TelephonyManager tm = mContext.getSystemService(TelephonyManager.class);
         tm.listen(mPhoneListener, PhoneStateListener.LISTEN_SERVICE_STATE);
     }
 
     private void unRegisterPhoneServiceStateListener() {
-        TelephonyManager tm =
-                (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
+        TelephonyManager tm = mContext.getSystemService(TelephonyManager.class);
         tm.listen(mPhoneListener, PhoneStateListener.LISTEN_NONE);
     }
 
     private void resendPendingMessages() {
         /* Send pending messages in outbox */
         String where = "type = " + Sms.MESSAGE_TYPE_OUTBOX;
-        UserManager manager = UserManager.get(mContext);
+        UserManager manager = mContext.getSystemService(UserManager.class);
         if (manager == null || !manager.isUserUnlocked()) {
             return;
         }
@@ -3699,7 +3694,7 @@
             mSmsBroadcastReceiver.unregister();
         }
         unRegisterPhoneServiceStateListener();
-        if (UserManager.get(mContext).isUserUnlocked()) {
+        if (mContext.getSystemService(UserManager.class).isUserUnlocked()) {
             failPendingMessages();
             removeDeletedMessages();
         }
diff --git a/android/app/src/com/android/bluetooth/map/BluetoothMapObexServer.java b/android/app/src/com/android/bluetooth/map/BluetoothMapObexServer.java
index 15a7840..485b5cd 100644
--- a/android/app/src/com/android/bluetooth/map/BluetoothMapObexServer.java
+++ b/android/app/src/com/android/bluetooth/map/BluetoothMapObexServer.java
@@ -445,7 +445,7 @@
     }
 
     private boolean isUserUnlocked() {
-        UserManager manager = UserManager.get(mContext);
+        UserManager manager = mContext.getSystemService(UserManager.class);
         return (manager == null || manager.isUserUnlocked());
     }
 
@@ -689,8 +689,7 @@
             }
             if (message.getType().equals(TYPE.SMS_GSM) || message.getType().equals(TYPE.SMS_CDMA)) {
                 // Convert messages to the default network type.
-                TelephonyManager tm =
-                        (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
+                TelephonyManager tm = mContext.getSystemService(TelephonyManager.class);
                 if (tm.getPhoneType() == TelephonyManager.PHONE_TYPE_GSM) {
                     message.setType(TYPE.SMS_GSM);
                 } else if (tm.getPhoneType() == TelephonyManager.PHONE_TYPE_CDMA) {
diff --git a/android/app/src/com/android/bluetooth/map/BluetoothMapService.java b/android/app/src/com/android/bluetooth/map/BluetoothMapService.java
index a2141b4..9dc73f3 100644
--- a/android/app/src/com/android/bluetooth/map/BluetoothMapService.java
+++ b/android/app/src/com/android/bluetooth/map/BluetoothMapService.java
@@ -24,7 +24,6 @@
 import android.app.ActivityThread;
 import android.app.AlarmManager;
 import android.app.PendingIntent;
-import android.bluetooth.BluetoothAdapter;
 import android.bluetooth.BluetoothDevice;
 import android.bluetooth.BluetoothMap;
 import android.bluetooth.BluetoothProfile;
@@ -64,7 +63,6 @@
 import java.util.HashMap;
 import java.util.List;
 import java.util.Objects;
-import java.util.Set;
 
 public class BluetoothMapService extends ProfileService {
     private static final String TAG = "BluetoothMapService";
@@ -238,7 +236,7 @@
 
         // Acquire the wakeLock before starting Obex transaction thread
         if (mWakeLock == null) {
-            PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
+            PowerManager pm = getSystemService(PowerManager.class);
             mWakeLock =
                     pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "StartingObexMapTransaction");
             mWakeLock.setReferenceCounted(false);
@@ -407,7 +405,7 @@
                         Log.v(TAG, "Acquire Wake Lock request message");
                     }
                     if (mWakeLock == null) {
-                        PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
+                        PowerManager pm = getSystemService(PowerManager.class);
                         mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
                                 "StartingObexMapTransaction");
                         mWakeLock.setReferenceCounted(false);
@@ -693,7 +691,7 @@
         mAdapterService = AdapterService.getAdapterService();
         mAppObserver = new BluetoothMapAppObserver(this, this);
 
-        TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
+        TelephonyManager tm = getSystemService(TelephonyManager.class);
         mSmsCapable = tm.isSmsCapable();
 
         mEnabledAccounts = mAppObserver.getEnabledAccountItems();
@@ -958,7 +956,7 @@
             Log.d(TAG, "SetUserTimeOutAlarm()");
         }
         if (mAlarmManager == null) {
-            mAlarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
+            mAlarmManager = this.getSystemService(AlarmManager.class);
         }
         mRemoveTimeoutMsg = true;
         Intent timeoutIntent = new Intent(USER_CONFIRM_TIMEOUT_ACTION);
@@ -977,7 +975,7 @@
                 PendingIntent.FLAG_IMMUTABLE);
         pIntent.cancel();
 
-        AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
+        AlarmManager alarmManager = this.getSystemService(AlarmManager.class);
         alarmManager.cancel(pIntent);
         mRemoveTimeoutMsg = false;
     }
diff --git a/android/app/src/com/android/bluetooth/mapclient/MapClientContent.java b/android/app/src/com/android/bluetooth/mapclient/MapClientContent.java
index 5d7ff01..2daee5d 100644
--- a/android/app/src/com/android/bluetooth/mapclient/MapClientContent.java
+++ b/android/app/src/com/android/bluetooth/mapclient/MapClientContent.java
@@ -95,8 +95,7 @@
         mCallbacks = callbacks;
         mResolver = mContext.getContentResolver();
 
-        mSubscriptionManager = (SubscriptionManager) mContext
-                .getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
+        mSubscriptionManager = mContext.getSystemService(SubscriptionManager.class);
         mSubscriptionManager
                 .addSubscriptionInfoRecord(mDevice.getAddress(), Utils.getName(mDevice), 0,
                         SubscriptionManager.SUBSCRIPTION_TYPE_REMOTE_SIM);
@@ -133,8 +132,8 @@
     }
 
     static void clearAllContent(Context context) {
-        SubscriptionManager subscriptionManager = (SubscriptionManager) context
-                .getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
+        SubscriptionManager subscriptionManager =
+                context.getSystemService(SubscriptionManager.class);
         List<SubscriptionInfo> subscriptions = subscriptionManager.getActiveSubscriptionInfoList();
         for (SubscriptionInfo info : subscriptions) {
             if (info.getSubscriptionType() == SubscriptionManager.SUBSCRIPTION_TYPE_REMOTE_SIM) {
diff --git a/android/app/src/com/android/bluetooth/opp/BluetoothOppFileProvider.java b/android/app/src/com/android/bluetooth/opp/BluetoothOppFileProvider.java
index cc24c8d..8010833 100644
--- a/android/app/src/com/android/bluetooth/opp/BluetoothOppFileProvider.java
+++ b/android/app/src/com/android/bluetooth/opp/BluetoothOppFileProvider.java
@@ -70,7 +70,7 @@
                 mContext.registerReceiver(mBroadcastReceiver, userFilter, null, null);
                 mRegisteredReceiver = true;
             }
-            UserManager userManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
+            UserManager userManager = context.getSystemService(UserManager.class);
             if (userManager.isUserUnlocked()) {
                 if (!mInitialized) {
                     if (Constants.DEBUG) {
@@ -108,7 +108,7 @@
      * the paths supported by the provider.
      */
     public static Uri getUriForFile(Context context, String authority, File file) {
-        UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
+        UserManager userManager = context.getSystemService(UserManager.class);
         if (!userManager.isUserUnlocked()) {
             return null;
         }
diff --git a/android/app/src/com/android/bluetooth/opp/BluetoothOppNotification.java b/android/app/src/com/android/bluetooth/opp/BluetoothOppNotification.java
index 98842b8..5a4524d 100644
--- a/android/app/src/com/android/bluetooth/opp/BluetoothOppNotification.java
+++ b/android/app/src/com/android/bluetooth/opp/BluetoothOppNotification.java
@@ -149,8 +149,7 @@
      */
     BluetoothOppNotification(Context ctx) {
         mContext = ctx;
-        mNotificationMgr =
-                (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
+        mNotificationMgr = mContext.getSystemService(NotificationManager.class);
         mNotificationChannel = new NotificationChannel(OPP_NOTIFICATION_CHANNEL,
                 mContext.getString(R.string.opp_notification_group),
                 NotificationManager.IMPORTANCE_HIGH);
diff --git a/android/app/src/com/android/bluetooth/opp/BluetoothOppObexClientSession.java b/android/app/src/com/android/bluetooth/opp/BluetoothOppObexClientSession.java
index 1d01646..af7e268 100644
--- a/android/app/src/com/android/bluetooth/opp/BluetoothOppObexClientSession.java
+++ b/android/app/src/com/android/bluetooth/opp/BluetoothOppObexClientSession.java
@@ -168,7 +168,7 @@
             mWaitingForShare = true;
             mWaitingForRemote = false;
             mNumShares = initialNumShares;
-            PowerManager pm = (PowerManager) mContext1.getSystemService(Context.POWER_SERVICE);
+            PowerManager pm = mContext.getSystemService(PowerManager.class);
             mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
         }
 
diff --git a/android/app/src/com/android/bluetooth/opp/BluetoothOppObexServerSession.java b/android/app/src/com/android/bluetooth/opp/BluetoothOppObexServerSession.java
index 5275dd1..c36c6ea 100644
--- a/android/app/src/com/android/bluetooth/opp/BluetoothOppObexServerSession.java
+++ b/android/app/src/com/android/bluetooth/opp/BluetoothOppObexServerSession.java
@@ -110,7 +110,7 @@
         mContext = context;
         mTransport = transport;
         mBluetoothOppService = service;
-        PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
+        PowerManager pm = mContext.getSystemService(PowerManager.class);
         mPartialWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
         mPartialWakeLock.setReferenceCounted(false);
     }
diff --git a/android/app/src/com/android/bluetooth/opp/BluetoothOppReceiver.java b/android/app/src/com/android/bluetooth/opp/BluetoothOppReceiver.java
index 970b92b..3540c4d 100644
--- a/android/app/src/com/android/bluetooth/opp/BluetoothOppReceiver.java
+++ b/android/app/src/com/android/bluetooth/opp/BluetoothOppReceiver.java
@@ -280,8 +280,7 @@
     }
 
     private void cancelNotification(Context context, int id) {
-        NotificationManager notMgr =
-                (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
+        NotificationManager notMgr = context.getSystemService(NotificationManager.class);
         if (notMgr == null) {
             return;
         }
diff --git a/android/app/src/com/android/bluetooth/opp/BluetoothOppTransfer.java b/android/app/src/com/android/bluetooth/opp/BluetoothOppTransfer.java
index 5946759..e871162 100644
--- a/android/app/src/com/android/bluetooth/opp/BluetoothOppTransfer.java
+++ b/android/app/src/com/android/bluetooth/opp/BluetoothOppTransfer.java
@@ -363,8 +363,8 @@
                          */
 
                         // Remove incoming file confirm notification
-                        NotificationManager nm = (NotificationManager) mContext.getSystemService(
-                                Context.NOTIFICATION_SERVICE);
+                        NotificationManager nm =
+                                mContext.getSystemService(NotificationManager.class);
                         nm.cancel(mCurrentShare.mId);
                         // Send intent to UI for timeout handling
                         Intent in = new Intent(BluetoothShare.USER_CONFIRMATION_TIMEOUT_ACTION);
diff --git a/android/app/src/com/android/bluetooth/opp/BluetoothOppTransferActivity.java b/android/app/src/com/android/bluetooth/opp/BluetoothOppTransferActivity.java
index 73a7a61..fef9b62 100644
--- a/android/app/src/com/android/bluetooth/opp/BluetoothOppTransferActivity.java
+++ b/android/app/src/com/android/bluetooth/opp/BluetoothOppTransferActivity.java
@@ -360,12 +360,10 @@
                     BluetoothOppUtility.updateVisibilityToHidden(this, mUri);
 
                     // clear correspondent notification item
-                    ((NotificationManager) getSystemService(NOTIFICATION_SERVICE)).cancel(
-                            mTransInfo.mID);
+                    getSystemService(NotificationManager.class).cancel(mTransInfo.mID);
                 } else if (mWhichDialog == DIALOG_SEND_COMPLETE_SUCCESS) {
                     BluetoothOppUtility.updateVisibilityToHidden(this, mUri);
-                    ((NotificationManager) getSystemService(NOTIFICATION_SERVICE)).cancel(
-                            mTransInfo.mID);
+                    getSystemService(NotificationManager.class).cancel(mTransInfo.mID);
                 }
                 break;
 
@@ -382,8 +380,7 @@
                     }
                     Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
 
-                    ((NotificationManager) getSystemService(NOTIFICATION_SERVICE)).cancel(
-                            mTransInfo.mID);
+                    getSystemService(NotificationManager.class).cancel(mTransInfo.mID);
                 } else if (mWhichDialog == DIALOG_SEND_COMPLETE_FAIL) {
 
                     BluetoothOppUtility.updateVisibilityToHidden(this, mUri);
diff --git a/android/app/src/com/android/bluetooth/opp/BluetoothOppUtility.java b/android/app/src/com/android/bluetooth/opp/BluetoothOppUtility.java
index 577928d..0b211af 100644
--- a/android/app/src/com/android/bluetooth/opp/BluetoothOppUtility.java
+++ b/android/app/src/com/android/bluetooth/opp/BluetoothOppUtility.java
@@ -478,8 +478,7 @@
     }
 
     protected static void cancelNotification(Context ctx) {
-        NotificationManager nm = (NotificationManager) ctx
-                .getSystemService(Context.NOTIFICATION_SERVICE);
+        NotificationManager nm = ctx.getSystemService(NotificationManager.class);
         nm.cancel(BluetoothOppNotification.NOTIFICATION_ID_PROGRESS);
     }
 
diff --git a/android/app/src/com/android/bluetooth/pan/PanService.java b/android/app/src/com/android/bluetooth/pan/PanService.java
index 1deafe0..a77ae2e 100644
--- a/android/app/src/com/android/bluetooth/pan/PanService.java
+++ b/android/app/src/com/android/bluetooth/pan/PanService.java
@@ -137,7 +137,7 @@
         initializeNative();
         mNativeAvailable = true;
 
-        mUserManager = (UserManager) getSystemService(Context.USER_SERVICE);
+        mUserManager = getSystemService(UserManager.class);
 
         setPanService(this);
         mStarted = true;
@@ -395,7 +395,7 @@
         enforceCallingOrSelfPermission(
                 TETHER_PRIVILEGED, "Need TETHER_PRIVILEGED permission");
 
-        UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
+        UserManager um = getSystemService(UserManager.class);
         if (um.hasUserRestriction(UserManager.DISALLOW_CONFIG_TETHERING) && value) {
             throw new SecurityException("DISALLOW_CONFIG_TETHERING is enabled for this user.");
         }
diff --git a/android/app/src/com/android/bluetooth/pbap/BluetoothPbapObexServer.java b/android/app/src/com/android/bluetooth/pbap/BluetoothPbapObexServer.java
index b7bea5e..92d6e4a 100755
--- a/android/app/src/com/android/bluetooth/pbap/BluetoothPbapObexServer.java
+++ b/android/app/src/com/android/bluetooth/pbap/BluetoothPbapObexServer.java
@@ -424,7 +424,7 @@
             return ResponseCodes.OBEX_HTTP_NOT_ACCEPTABLE;
         }
 
-        if (!UserManager.get(mContext).isUserUnlocked()) {
+        if (!mContext.getSystemService(UserManager.class).isUserUnlocked()) {
             Log.e(TAG, "Storage locked, " + type + " failed");
             return ResponseCodes.OBEX_HTTP_UNAVAILABLE;
         }
diff --git a/android/app/src/com/android/bluetooth/pbap/BluetoothPbapService.java b/android/app/src/com/android/bluetooth/pbap/BluetoothPbapService.java
index e40f008..4e65d90 100644
--- a/android/app/src/com/android/bluetooth/pbap/BluetoothPbapService.java
+++ b/android/app/src/com/android/bluetooth/pbap/BluetoothPbapService.java
@@ -377,7 +377,7 @@
                     break;
                 case MSG_ACQUIRE_WAKE_LOCK:
                     if (mWakeLock == null) {
-                        PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
+                        PowerManager pm = getSystemService(PowerManager.class);
                         mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
                                 "StartingObexPbapTransaction");
                         mWakeLock.setReferenceCounted(false);
@@ -645,7 +645,7 @@
     @Override
     protected void setCurrentUser(int userId) {
         Log.i(TAG, "setCurrentUser(" + userId + ")");
-        UserManager userManager = (UserManager) getSystemService(Context.USER_SERVICE);
+        UserManager userManager = getSystemService(UserManager.class);
         if (userManager.isUserUnlocked(UserHandle.of(userId))) {
             setUserUnlocked(userId);
         }
@@ -871,7 +871,7 @@
     }
 
     private void getLocalTelephonyDetails() {
-        TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
+        TelephonyManager tm = getSystemService(TelephonyManager.class);
         if (tm != null) {
             sLocalPhoneNum = tm.getLine1Number();
             sLocalPhoneName = this.getString(R.string.localPhoneName);
diff --git a/android/app/src/com/android/bluetooth/pbap/PbapStateMachine.java b/android/app/src/com/android/bluetooth/pbap/PbapStateMachine.java
index 35e3cff..3b6887e 100644
--- a/android/app/src/com/android/bluetooth/pbap/PbapStateMachine.java
+++ b/android/app/src/com/android/bluetooth/pbap/PbapStateMachine.java
@@ -27,7 +27,6 @@
 import android.bluetooth.BluetoothPbap;
 import android.bluetooth.BluetoothProfile;
 import android.bluetooth.BluetoothSocket;
-import android.content.Context;
 import android.content.Intent;
 import android.os.Handler;
 import android.os.Looper;
@@ -367,8 +366,7 @@
         }
 
         private void createPbapNotification() {
-            NotificationManager nm =
-                    (NotificationManager) mService.getSystemService(Context.NOTIFICATION_SERVICE);
+            NotificationManager nm = mService.getSystemService(NotificationManager.class);
             NotificationChannel notificationChannel =
                     new NotificationChannel(PBAP_OBEX_NOTIFICATION_CHANNEL,
                             mService.getString(R.string.pbap_notification_group),
@@ -416,8 +414,7 @@
         }
 
         private void removePbapNotification(int id) {
-            NotificationManager nm =
-                    (NotificationManager) mService.getSystemService(Context.NOTIFICATION_SERVICE);
+            NotificationManager nm = mService.getSystemService(NotificationManager.class);
             nm.cancel(id);
         }
 
diff --git a/android/app/src/com/android/bluetooth/pbapclient/PbapClientStateMachine.java b/android/app/src/com/android/bluetooth/pbapclient/PbapClientStateMachine.java
index f340128..e946544 100644
--- a/android/app/src/com/android/bluetooth/pbapclient/PbapClientStateMachine.java
+++ b/android/app/src/com/android/bluetooth/pbapclient/PbapClientStateMachine.java
@@ -41,6 +41,8 @@
  */
 package com.android.bluetooth.pbapclient;
 
+import static android.Manifest.permission.BLUETOOTH_CONNECT;
+
 import android.bluetooth.BluetoothDevice;
 import android.bluetooth.BluetoothPbapClient;
 import android.bluetooth.BluetoothProfile;
@@ -55,7 +57,6 @@
 import android.os.Process;
 import android.os.UserManager;
 import android.util.Log;
-import static android.Manifest.permission.BLUETOOTH_CONNECT;
 
 import com.android.bluetooth.BluetoothMetricsProto;
 import com.android.bluetooth.Utils;
@@ -111,7 +112,7 @@
         mService = svc;
         mCurrentDevice = device;
         mLock = new Object();
-        mUserManager = UserManager.get(mService);
+        mUserManager = mService.getSystemService(UserManager.class);
         mDisconnected = new Disconnected();
         mConnecting = new Connecting();
         mDisconnecting = new Disconnecting();
diff --git a/android/app/src/com/android/bluetooth/sap/SapServer.java b/android/app/src/com/android/bluetooth/sap/SapServer.java
index e1de99d..c838fa3 100644
--- a/android/app/src/com/android/bluetooth/sap/SapServer.java
+++ b/android/app/src/com/android/bluetooth/sap/SapServer.java
@@ -222,7 +222,7 @@
         String title, text, button, ticker;
         Notification notification;
         NotificationManager notificationManager =
-                (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
+                mContext.getSystemService(NotificationManager.class);
         NotificationChannel notificationChannel = new NotificationChannel(SAP_NOTIFICATION_CHANNEL,
                 mContext.getString(R.string.bluetooth_sap_notif_title),
                 NotificationManager.IMPORTANCE_HIGH);
@@ -312,7 +312,7 @@
 
     void clearNotification() {
         NotificationManager notificationManager =
-                (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
+                mContext.getSystemService(NotificationManager.class);
         notificationManager.cancel(SapServer.NOTIFICATION_ID);
     }
 
@@ -627,8 +627,7 @@
      * @return false if the phone is IDLE (can be used for SAP), true otherwise.
      */
     private boolean isCallOngoing() {
-        TelephonyManager tManager =
-                (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
+        TelephonyManager tManager = mContext.getSystemService(TelephonyManager.class);
         if (tManager.getCallState() == TelephonyManager.CALL_STATE_IDLE) {
             return false;
         }
@@ -753,8 +752,7 @@
         synchronized (this) {
             Intent sapDisconnectIntent = new Intent(SapServer.SAP_DISCONNECT_ACTION);
             sapDisconnectIntent.putExtra(SAP_DISCONNECT_TYPE_EXTRA, discType);
-            AlarmManager alarmManager =
-                    (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
+            AlarmManager alarmManager = mContext.getSystemService(AlarmManager.class);
             mPendingDiscIntent = PendingIntent.getBroadcast(mContext, discType, sapDisconnectIntent,
                     PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_IMMUTABLE);
             alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
@@ -771,8 +769,7 @@
     private void stopDisconnectTimer() {
         synchronized (this) {
             if (mPendingDiscIntent != null) {
-                AlarmManager alarmManager =
-                        (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
+                AlarmManager alarmManager = mContext.getSystemService(AlarmManager.class);
                 alarmManager.cancel(mPendingDiscIntent);
                 mPendingDiscIntent.cancel();
                 if (VERBOSE) {
diff --git a/android/app/src/com/android/bluetooth/sap/SapService.java b/android/app/src/com/android/bluetooth/sap/SapService.java
index 7aa80a6..dd93f03 100644
--- a/android/app/src/com/android/bluetooth/sap/SapService.java
+++ b/android/app/src/com/android/bluetooth/sap/SapService.java
@@ -40,7 +40,6 @@
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.List;
-import java.util.Set;
 
 @TargetApi(Build.VERSION_CODES.ECLAIR)
 public class SapService extends ProfileService {
@@ -270,7 +269,7 @@
 
         // acquire the wakeLock before start SAP transaction thread
         if (mWakeLock == null) {
-            PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
+            PowerManager pm = getSystemService(PowerManager.class);
             mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "StartingSapTransaction");
             mWakeLock.setReferenceCounted(false);
             mWakeLock.acquire();
@@ -464,7 +463,7 @@
                         Log.i(TAG, "Acquire Wake Lock request message");
                     }
                     if (mWakeLock == null) {
-                        PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
+                        PowerManager pm = getSystemService(PowerManager.class);
                         mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
                                 "StartingObexMapTransaction");
                         mWakeLock.setReferenceCounted(false);
@@ -749,7 +748,7 @@
             Log.d(TAG, "cancelUserTimeOutAlarm()");
         }
         if (mAlarmManager == null) {
-            mAlarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
+            mAlarmManager = this.getSystemService(AlarmManager.class);
         }
         if (mRemoveTimeoutMsg) {
             Intent timeoutIntent = new Intent(USER_CONFIRM_TIMEOUT_ACTION);
diff --git a/android/app/src/com/android/bluetooth/util/DevicePolicyUtils.java b/android/app/src/com/android/bluetooth/util/DevicePolicyUtils.java
index 4e42642..1c4d40e 100644
--- a/android/app/src/com/android/bluetooth/util/DevicePolicyUtils.java
+++ b/android/app/src/com/android/bluetooth/util/DevicePolicyUtils.java
@@ -27,10 +27,8 @@
 
 public final class DevicePolicyUtils {
     private static boolean isBluetoothWorkContactSharingDisabled(Context context) {
-        final DevicePolicyManager dpm =
-                (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
-        final UserManager userManager =
-                (UserManager) context.getSystemService(Context.USER_SERVICE);
+        final DevicePolicyManager dpm = context.getSystemService(DevicePolicyManager.class);
+        final UserManager userManager = context.getSystemService(UserManager.class);
         final List<UserHandle> userHandleList = userManager.getAllProfiles();
 
         // Check each user.
diff --git a/android/app/src/com/android/bluetooth/vc/VolumeControlService.java b/android/app/src/com/android/bluetooth/vc/VolumeControlService.java
index 3b84f61..ffb17a4 100644
--- a/android/app/src/com/android/bluetooth/vc/VolumeControlService.java
+++ b/android/app/src/com/android/bluetooth/vc/VolumeControlService.java
@@ -102,7 +102,7 @@
         mVolumeControlNativeInterface = Objects.requireNonNull(
                 VolumeControlNativeInterface.getInstance(),
                 "VolumeControlNativeInterface cannot be null when VolumeControlService starts");
-        mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
+        mAudioManager =  getSystemService(AudioManager.class);
         Objects.requireNonNull(mAudioManager,
                 "AudioManager cannot be null when VolumeControlService starts");
 
diff --git a/android/app/tests/unit/src/com/android/bluetooth/a2dpsink/A2dpSinkStreamHandlerTest.java b/android/app/tests/unit/src/com/android/bluetooth/a2dpsink/A2dpSinkStreamHandlerTest.java
index b8fbbb9..1a70021 100644
--- a/android/app/tests/unit/src/com/android/bluetooth/a2dpsink/A2dpSinkStreamHandlerTest.java
+++ b/android/app/tests/unit/src/com/android/bluetooth/a2dpsink/A2dpSinkStreamHandlerTest.java
@@ -71,6 +71,8 @@
         mHandlerThread.start();
 
         when(mMockContext.getSystemService(Context.AUDIO_SERVICE)).thenReturn(mMockAudioManager);
+        when(mMockContext.getSystemServiceName(AudioManager.class))
+                .thenReturn(Context.AUDIO_SERVICE);
         when(mMockContext.getResources()).thenReturn(mMockResources);
         when(mMockResources.getInteger(anyInt())).thenReturn(DUCK_PERCENT);
         when(mMockAudioManager.requestAudioFocus(any())).thenReturn(
diff --git a/android/app/tests/unit/src/com/android/bluetooth/audio_util/MediaPlayerListTest.java b/android/app/tests/unit/src/com/android/bluetooth/audio_util/MediaPlayerListTest.java
index 44054f3..9f1ddd2 100644
--- a/android/app/tests/unit/src/com/android/bluetooth/audio_util/MediaPlayerListTest.java
+++ b/android/app/tests/unit/src/com/android/bluetooth/audio_util/MediaPlayerListTest.java
@@ -20,7 +20,6 @@
 
 import android.content.Context;
 import android.content.pm.PackageManager;
-import android.content.pm.ResolveInfo;
 import android.media.AudioManager;
 import android.media.session.MediaSessionManager;
 import android.media.session.PlaybackState;
@@ -30,10 +29,6 @@
 import androidx.test.filters.SmallTest;
 import androidx.test.runner.AndroidJUnit4;
 
-import java.lang.reflect.Method;
-import java.util.ArrayList;
-import java.util.List;
-
 import org.junit.After;
 import org.junit.Assert;
 import org.junit.Before;
@@ -44,6 +39,9 @@
 import org.mockito.Mock;
 import org.mockito.MockitoAnnotations;
 
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+
 @SmallTest
 @RunWith(AndroidJUnit4.class)
 public class MediaPlayerListTest {
@@ -75,13 +73,16 @@
 
         AudioManager mockAudioManager = mock(AudioManager.class);
         when(mMockContext.getSystemService(Context.AUDIO_SERVICE)).thenReturn(mockAudioManager);
+        when(mMockContext.getSystemServiceName(AudioManager.class))
+            .thenReturn(Context.AUDIO_SERVICE);
 
-        MediaSessionManager mMediaSessionManager =
-                (MediaSessionManager) InstrumentationRegistry.getTargetContext()
-                .getSystemService(Context.MEDIA_SESSION_SERVICE);
+        MediaSessionManager mMediaSessionManager = InstrumentationRegistry.getTargetContext()
+                .getSystemService(MediaSessionManager.class);
         PackageManager mockPackageManager = mock(PackageManager.class);
         when(mMockContext.getSystemService(Context.MEDIA_SESSION_SERVICE))
             .thenReturn(mMediaSessionManager);
+        when(mMockContext.getSystemServiceName(MediaSessionManager.class))
+            .thenReturn(Context.MEDIA_SESSION_SERVICE);
 
         mMediaPlayerList =
             new MediaPlayerList(Looper.myLooper(), InstrumentationRegistry.getTargetContext());
diff --git a/android/app/tests/unit/src/com/android/bluetooth/avrcpcontroller/AvrcpControllerStateMachineTest.java b/android/app/tests/unit/src/com/android/bluetooth/avrcpcontroller/AvrcpControllerStateMachineTest.java
index 41a7696..b99c921 100644
--- a/android/app/tests/unit/src/com/android/bluetooth/avrcpcontroller/AvrcpControllerStateMachineTest.java
+++ b/android/app/tests/unit/src/com/android/bluetooth/avrcpcontroller/AvrcpControllerStateMachineTest.java
@@ -16,6 +16,7 @@
 package com.android.bluetooth.avrcpcontroller;
 
 import static android.Manifest.permission.BLUETOOTH_CONNECT;
+
 import static org.mockito.Mockito.*;
 
 import android.bluetooth.BluetoothAdapter;
@@ -125,6 +126,8 @@
         doReturn(mMockResources).when(mAvrcpControllerService).getResources();
         doReturn(mAudioManager).when(mAvrcpControllerService)
                 .getSystemService(Context.AUDIO_SERVICE);
+        doReturn(Context.AUDIO_SERVICE).when(mAvrcpControllerService)
+                .getSystemServiceName(AudioManager.class);
         doReturn(mCoverArtManager).when(mAvrcpControllerService).getCoverArtManager();
         mAvrcpControllerService.sBrowseTree = new BrowseTree(null);
 
diff --git a/android/app/tests/unit/src/com/android/bluetooth/btservice/ActiveDeviceManagerTest.java b/android/app/tests/unit/src/com/android/bluetooth/btservice/ActiveDeviceManagerTest.java
index 5580155..b2c68c6 100644
--- a/android/app/tests/unit/src/com/android/bluetooth/btservice/ActiveDeviceManagerTest.java
+++ b/android/app/tests/unit/src/com/android/bluetooth/btservice/ActiveDeviceManagerTest.java
@@ -78,6 +78,8 @@
         MockitoAnnotations.initMocks(this);
         TestUtils.setAdapterService(mAdapterService);
         when(mAdapterService.getSystemService(Context.AUDIO_SERVICE)).thenReturn(mAudioManager);
+        when(mAdapterService.getSystemServiceName(AudioManager.class))
+                .thenReturn(Context.AUDIO_SERVICE);
         when(mServiceFactory.getA2dpService()).thenReturn(mA2dpService);
         when(mServiceFactory.getHeadsetService()).thenReturn(mHeadsetService);
         when(mServiceFactory.getHearingAidService()).thenReturn(mHearingAidService);
diff --git a/android/app/tests/unit/src/com/android/bluetooth/btservice/AdapterServiceTest.java b/android/app/tests/unit/src/com/android/bluetooth/btservice/AdapterServiceTest.java
index 00afdf1..71843c5 100644
--- a/android/app/tests/unit/src/com/android/bluetooth/btservice/AdapterServiceTest.java
+++ b/android/app/tests/unit/src/com/android/bluetooth/btservice/AdapterServiceTest.java
@@ -37,7 +37,6 @@
 import android.os.BatteryStatsManager;
 import android.os.Binder;
 import android.os.Bundle;
-import android.os.Handler;
 import android.os.Looper;
 import android.os.PowerManager;
 import android.os.Process;
@@ -159,8 +158,8 @@
 
         mMockContentResolver = new MockContentResolver(mMockContext);
         MockitoAnnotations.initMocks(this);
-        mPowerManager = (PowerManager) InstrumentationRegistry.getTargetContext()
-                .getSystemService(Context.POWER_SERVICE);
+        mPowerManager = InstrumentationRegistry.getTargetContext()
+                .getSystemService(PowerManager.class);
         mPermissionCheckerManager = InstrumentationRegistry.getTargetContext()
                 .getSystemService(PermissionCheckerManager.class);
 
@@ -173,15 +172,24 @@
         when(mMockContext.getUserId()).thenReturn(Process.BLUETOOTH_UID);
         when(mMockContext.getPackageManager()).thenReturn(mMockPackageManager);
         when(mMockContext.getSystemService(Context.USER_SERVICE)).thenReturn(mMockUserManager);
+        when(mMockContext.getSystemServiceName(UserManager.class)).thenReturn(Context.USER_SERVICE);
         when(mMockContext.getSystemService(Context.DEVICE_POLICY_SERVICE)).thenReturn(
                 mMockDevicePolicyManager);
+        when(mMockContext.getSystemServiceName(DevicePolicyManager.class))
+                .thenReturn(Context.DEVICE_POLICY_SERVICE);
         when(mMockContext.getSystemService(Context.POWER_SERVICE)).thenReturn(mPowerManager);
+        when(mMockContext.getSystemServiceName(PowerManager.class))
+                .thenReturn(Context.POWER_SERVICE);
         when(mMockContext.getSystemServiceName(PermissionCheckerManager.class))
                 .thenReturn(Context.PERMISSION_CHECKER_SERVICE);
-        when(mMockContext.getSystemService(PermissionCheckerManager.class))
+        when(mMockContext.getSystemService(Context.PERMISSION_CHECKER_SERVICE))
                 .thenReturn(mPermissionCheckerManager);
         when(mMockContext.getSystemService(Context.ALARM_SERVICE)).thenReturn(mMockAlarmManager);
+        when(mMockContext.getSystemServiceName(AlarmManager.class))
+                .thenReturn(Context.ALARM_SERVICE);
         when(mMockContext.getSystemService(Context.AUDIO_SERVICE)).thenReturn(mAudioManager);
+        when(mMockContext.getSystemServiceName(AudioManager.class))
+                .thenReturn(Context.AUDIO_SERVICE);
         when(mMockContext.getSystemService(Context.BATTERY_STATS_SERVICE))
                 .thenReturn(mBatteryStatsManager);
         when(mMockContext.getSystemServiceName(BatteryStatsManager.class))
@@ -356,8 +364,14 @@
         when(mockContext.getUserId()).thenReturn(Process.BLUETOOTH_UID);
         when(mockContext.getPackageManager()).thenReturn(mMockPackageManager);
         when(mockContext.getSystemService(Context.USER_SERVICE)).thenReturn(mMockUserManager);
+        when(mockContext.getSystemServiceName(UserManager.class))
+                .thenReturn(Context.USER_SERVICE);
         when(mockContext.getSystemService(Context.POWER_SERVICE)).thenReturn(mPowerManager);
+        when(mockContext.getSystemServiceName(PowerManager.class))
+                .thenReturn(Context.POWER_SERVICE);
         when(mockContext.getSystemService(Context.ALARM_SERVICE)).thenReturn(mMockAlarmManager);
+        when(mockContext.getSystemServiceName(AlarmManager.class))
+                .thenReturn(Context.ALARM_SERVICE);
 
         when(mockResources.getBoolean(R.bool.profile_supported_gatt)).thenReturn(true);
 
diff --git a/android/app/tests/unit/src/com/android/bluetooth/hfp/HeadsetPhoneStateTest.java b/android/app/tests/unit/src/com/android/bluetooth/hfp/HeadsetPhoneStateTest.java
index 51e6a26..99a9f34 100644
--- a/android/app/tests/unit/src/com/android/bluetooth/hfp/HeadsetPhoneStateTest.java
+++ b/android/app/tests/unit/src/com/android/bluetooth/hfp/HeadsetPhoneStateTest.java
@@ -81,9 +81,13 @@
         // Stub other methods
         when(mHeadsetService.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(
                 mTelephonyManager);
+        when(mHeadsetService.getSystemServiceName(TelephonyManager.class)).thenReturn(
+                Context.TELEPHONY_SERVICE);
         when(mTelephonyManager.createForSubscriptionId(anyInt())).thenReturn(mTelephonyManager);
         when(mHeadsetService.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE)).thenReturn(
                 mSubscriptionManager);
+        when(mHeadsetService.getSystemServiceName(SubscriptionManager.class)).thenReturn(
+                Context.TELEPHONY_SUBSCRIPTION_SERVICE);
         mHandlerThread = new HandlerThread("HeadsetStateMachineTestHandlerThread");
         mHandlerThread.start();
         when(mHeadsetService.getStateMachinesThreadLooper()).thenReturn(mHandlerThread.getLooper());
diff --git a/android/app/tests/unit/src/com/android/bluetooth/hfp/HeadsetServiceAndStateMachineTest.java b/android/app/tests/unit/src/com/android/bluetooth/hfp/HeadsetServiceAndStateMachineTest.java
index a265b67..7ff5f1c 100644
--- a/android/app/tests/unit/src/com/android/bluetooth/hfp/HeadsetServiceAndStateMachineTest.java
+++ b/android/app/tests/unit/src/com/android/bluetooth/hfp/HeadsetServiceAndStateMachineTest.java
@@ -159,8 +159,7 @@
         Assume.assumeTrue("Ignore test when HeadsetService is not enabled",
                 mTargetContext.getResources().getBoolean(R.bool.profile_supported_hs_hfp));
         MockitoAnnotations.initMocks(this);
-        PowerManager powerManager =
-                (PowerManager) mTargetContext.getSystemService(Context.POWER_SERVICE);
+        PowerManager powerManager = mTargetContext.getSystemService(PowerManager.class);
         mVoiceRecognitionWakeLock =
                 powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "VoiceRecognitionTest");
         TestUtils.setAdapterService(mAdapterService);
diff --git a/android/app/tests/unit/src/com/android/bluetooth/hfpclient/connserv/HfpClientDeviceBlockTest.java b/android/app/tests/unit/src/com/android/bluetooth/hfpclient/connserv/HfpClientDeviceBlockTest.java
index 6d5386f..0ff7c33 100644
--- a/android/app/tests/unit/src/com/android/bluetooth/hfpclient/connserv/HfpClientDeviceBlockTest.java
+++ b/android/app/tests/unit/src/com/android/bluetooth/hfpclient/connserv/HfpClientDeviceBlockTest.java
@@ -81,6 +81,8 @@
         when(mConnServ.getApplicationContext()).thenReturn(mApplicationContext);
 
         when(mConnServ.getSystemService(Context.TELECOM_SERVICE)).thenReturn(mTelecomManager);
+        when(mConnServ.getSystemServiceName(TelecomManager.class))
+                .thenReturn(Context.TELECOM_SERVICE);
 
         mBluetoothDevice = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(
                 TEST_DEVICE_ADDRESS);
diff --git a/android/app/tests/unit/src/com/android/bluetooth/map/BluetoothMapContentObserverTest.java b/android/app/tests/unit/src/com/android/bluetooth/map/BluetoothMapContentObserverTest.java
index 222dc08..4c3f9ed 100644
--- a/android/app/tests/unit/src/com/android/bluetooth/map/BluetoothMapContentObserverTest.java
+++ b/android/app/tests/unit/src/com/android/bluetooth/map/BluetoothMapContentObserverTest.java
@@ -107,7 +107,10 @@
         when(mockUserService.isUserUnlocked()).thenReturn(true);
         when(mockContext.getContentResolver()).thenReturn(mockResolver);
         when(mockContext.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(mockTelephony);
+        when(mockContext.getSystemServiceName(TelephonyManager.class))
+                .thenReturn(Context.TELEPHONY_SERVICE);
         when(mockContext.getSystemService(Context.USER_SERVICE)).thenReturn(mockUserService);
+        when(mockContext.getSystemServiceName(UserManager.class)).thenReturn(Context.USER_SERVICE);
 
         BluetoothMapContentObserver observer;
         try {
@@ -140,7 +143,10 @@
         when(mockUserService.isUserUnlocked()).thenReturn(true);
         when(mockContext.getContentResolver()).thenReturn(mockResolver);
         when(mockContext.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(mockTelephony);
+        when(mockContext.getSystemServiceName(TelephonyManager.class))
+                .thenReturn(Context.TELEPHONY_SERVICE);
         when(mockContext.getSystemService(Context.USER_SERVICE)).thenReturn(mockUserService);
+        when(mockContext.getSystemServiceName(UserManager.class)).thenReturn(Context.USER_SERVICE);
 
         BluetoothMapbMessageMime message = new BluetoothMapbMessageMime();
         message.setType(BluetoothMapUtils.TYPE.MMS);
diff --git a/android/app/tests/unit/src/com/android/bluetooth/mapclient/MapClientContentTest.java b/android/app/tests/unit/src/com/android/bluetooth/mapclient/MapClientContentTest.java
index 2ddd6d1..8b3eced 100644
--- a/android/app/tests/unit/src/com/android/bluetooth/mapclient/MapClientContentTest.java
+++ b/android/app/tests/unit/src/com/android/bluetooth/mapclient/MapClientContentTest.java
@@ -132,6 +132,8 @@
         when(mMockContext.getContentResolver()).thenReturn(mMockContentResolver);
         when(mMockContext.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE))
                 .thenReturn(mMockSubscriptionManager);
+        when(mMockContext.getSystemServiceName(SubscriptionManager.class))
+                .thenReturn(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
 
         when(mMockSubscriptionManager.getActiveSubscriptionInfoList())
                 .thenReturn(Arrays.asList(mMockSubscription));
diff --git a/android/app/tests/unit/src/com/android/bluetooth/mapclient/MapClientStateMachineTest.java b/android/app/tests/unit/src/com/android/bluetooth/mapclient/MapClientStateMachineTest.java
index 531be0b..6d82e5d 100644
--- a/android/app/tests/unit/src/com/android/bluetooth/mapclient/MapClientStateMachineTest.java
+++ b/android/app/tests/unit/src/com/android/bluetooth/mapclient/MapClientStateMachineTest.java
@@ -17,6 +17,7 @@
 package com.android.bluetooth.mapclient;
 
 import static android.Manifest.permission.BLUETOOTH_CONNECT;
+
 import static org.mockito.Mockito.*;
 
 import android.bluetooth.BluetoothAdapter;
@@ -120,6 +121,8 @@
         when(mMockMapClientService.getContentResolver()).thenReturn(mMockContentResolver);
         when(mMockMapClientService.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE))
                 .thenReturn(mMockSubscriptionManager);
+        when(mMockMapClientService.getSystemServiceName(SubscriptionManager.class))
+                .thenReturn(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
 
         doReturn(mTargetContext.getResources()).when(mMockMapClientService).getResources();