Avoid the cast for getSystemService

Bug: 227027117
Test: atest NfcNciInstrumentationTests
Test: Checkstyle hook
Change-Id: Ie89a0a11b343978963c0e9b8397081ff03c80dff
diff --git a/src/com/android/nfc/ConfirmConnectToWifiNetworkActivity.java b/src/com/android/nfc/ConfirmConnectToWifiNetworkActivity.java
index 8752dbb..2b8d8cf 100644
--- a/src/com/android/nfc/ConfirmConnectToWifiNetworkActivity.java
+++ b/src/com/android/nfc/ConfirmConnectToWifiNetworkActivity.java
@@ -60,7 +60,7 @@
 
     @Override
     public void onClick(View v) {
-        WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
+        WifiManager wifiManager = getSystemService(WifiManager.class);
 
         if (!isChangeWifiStateGranted()) {
             showFailToast();
@@ -86,7 +86,7 @@
     }
 
     private boolean isChangeWifiStateGranted() {
-        AppOpsManager appOps = (AppOpsManager) getSystemService(Context.APP_OPS_SERVICE);
+        AppOpsManager appOps = getSystemService(AppOpsManager.class);
         int modeChangeWifiState = appOps.checkOpNoThrow(AppOpsManager.OP_CHANGE_WIFI_STATE,
                                                         Binder.getCallingUid(), getPackageName());
         return modeChangeWifiState == AppOpsManager.MODE_ALLOWED;
@@ -148,8 +148,8 @@
                         && wifiState == WifiManager.WIFI_STATE_ENABLED) {
                     if (getAndClearEnableWifiInProgress()) {
                         doConnect(
-                                (WifiManager) ConfirmConnectToWifiNetworkActivity.this
-                                        .getSystemService(Context.WIFI_SERVICE));
+                                ConfirmConnectToWifiNetworkActivity.this
+                                        .getSystemService(WifiManager.class));
                     }
                 }
             }
diff --git a/src/com/android/nfc/NfcApplication.java b/src/com/android/nfc/NfcApplication.java
index 1ae4537..11c56ba 100644
--- a/src/com/android/nfc/NfcApplication.java
+++ b/src/com/android/nfc/NfcApplication.java
@@ -52,7 +52,7 @@
         // object in those cases, hence check the name of the process
         // to determine whether we're the main NFC service, or the
         // handover process
-        ActivityManager am = (ActivityManager)this.getSystemService(ACTIVITY_SERVICE);
+        ActivityManager am = this.getSystemService(ActivityManager.class);
         List processes = am.getRunningAppProcesses();
         Iterator i = processes.iterator();
         while (i.hasNext()) {
diff --git a/src/com/android/nfc/NfcService.java b/src/com/android/nfc/NfcService.java
index 1ad6f5a..c6e94c4 100755
--- a/src/com/android/nfc/NfcService.java
+++ b/src/com/android/nfc/NfcService.java
@@ -523,7 +523,7 @@
 
         mIsDebugBuild = "userdebug".equals(Build.TYPE) || "eng".equals(Build.TYPE);
 
-        mPowerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
+        mPowerManager = mContext.getSystemService(PowerManager.class);
 
         mRoutingWakeLock = mPowerManager.newWakeLock(
                 PowerManager.PARTIAL_WAKE_LOCK, "NfcService:mRoutingWakeLock");
@@ -532,9 +532,9 @@
                         | PowerManager.ACQUIRE_CAUSES_WAKEUP
                         | PowerManager.ON_AFTER_RELEASE, "NfcService:mRequireUnlockWakeLock");
 
-        mKeyguard = (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
-        mUserManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
-        mVibrator = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE);
+        mKeyguard = mContext.getSystemService(KeyguardManager.class);
+        mUserManager = mContext.getSystemService(UserManager.class);
+        mVibrator = mContext.getSystemService(Vibrator.class);
         mVibrationEffect = VibrationEffect.createOneShot(200, VibrationEffect.DEFAULT_AMPLITUDE);
 
         mScreenState = mScreenStateHelper.checkScreenState();
@@ -1155,7 +1155,7 @@
     }
 
     void enforceBeamShareActivityPolicy(Context context, UserHandle uh) {
-        UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
+        UserManager um = context.getSystemService(UserManager.class);
         IPackageManager mIpm = IPackageManager.Stub.asInterface(ServiceManager.getService("package"));
         boolean isGlobalEnabled = mIsNdefPushEnabled;
         boolean isActiveForUser =
@@ -1256,7 +1256,7 @@
                 mPrefsEditor.apply();
                 mIsNdefPushEnabled = true;
                 // Propagate the state change to all user profiles
-                UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
+                UserManager um = mContext.getSystemService(UserManager.class);
                 List <UserHandle> luh = um.getUserProfiles();
                 for (UserHandle uh : luh){
                     enforceBeamShareActivityPolicy(mContext, uh);
@@ -1323,7 +1323,7 @@
                 mPrefsEditor.apply();
                 mIsNdefPushEnabled = false;
                 // Propagate the state change to all user profiles
-                UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
+                UserManager um = mContext.getSystemService(UserManager.class);
                 List <UserHandle> luh = um.getUserProfiles();
                 for (UserHandle uh : luh){
                     enforceBeamShareActivityPolicy(mContext, uh);
@@ -3317,7 +3317,7 @@
                            mIsNdefPushEnabled = true;
                         }
                         // Propagate the state change to all user profiles
-                        UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
+                        UserManager um = mContext.getSystemService(UserManager.class);
                         List <UserHandle> luh = um.getUserProfiles();
                         for (UserHandle uh : luh){
                             enforceBeamShareActivityPolicy(mContext, uh);
diff --git a/src/com/android/nfc/ScreenStateHelper.java b/src/com/android/nfc/ScreenStateHelper.java
index 22ff13d..e840a58 100644
--- a/src/com/android/nfc/ScreenStateHelper.java
+++ b/src/com/android/nfc/ScreenStateHelper.java
@@ -24,9 +24,8 @@
     private final KeyguardManager mKeyguardManager;
 
     ScreenStateHelper(Context context) {
-        mKeyguardManager = (KeyguardManager)
-                context.getSystemService(Context.KEYGUARD_SERVICE);
-        mPowerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
+        mKeyguardManager = context.getSystemService(KeyguardManager.class);
+        mPowerManager = context.getSystemService(PowerManager.class);
     }
 
     int checkScreenState() {
diff --git a/src/com/android/nfc/cardemulation/AppChooserActivity.java b/src/com/android/nfc/cardemulation/AppChooserActivity.java
index ee16aeb..256df67 100644
--- a/src/com/android/nfc/cardemulation/AppChooserActivity.java
+++ b/src/com/android/nfc/cardemulation/AppChooserActivity.java
@@ -42,12 +42,11 @@
 import android.widget.ImageView;
 import android.widget.ListView;
 import android.widget.TextView;
+
 import androidx.annotation.NonNull;
 import androidx.appcompat.app.AppCompatActivity;
 
-import com.android.internal.R;
 import com.google.android.material.bottomsheet.BottomSheetBehavior;
-import com.google.android.material.bottomsheet.BottomSheetBehavior.BottomSheetCallback;
 
 import java.util.ArrayList;
 import java.util.List;
@@ -100,7 +99,7 @@
         final NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
         mCardEmuManager = CardEmulation.getInstance(adapter);
 
-        final ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
+        final ActivityManager am = getSystemService(ActivityManager.class);
         mIconSize = am.getLauncherLargeIconSize();
 
         // Three cases:
@@ -209,7 +208,7 @@
         private List<DisplayAppInfo> mList;
 
         public ListAdapter(Context context, ArrayList<ApduServiceInfo> services) {
-            mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
+            mInflater = context.getSystemService(LayoutInflater.class);
             // For each component, get the corresponding app name and icon
             PackageManager pm = getPackageManager();
             mList = new ArrayList<DisplayAppInfo>();
diff --git a/src/com/android/nfc/cardemulation/CardEmulationManager.java b/src/com/android/nfc/cardemulation/CardEmulationManager.java
index d43f89a..23e96c2 100644
--- a/src/com/android/nfc/cardemulation/CardEmulationManager.java
+++ b/src/com/android/nfc/cardemulation/CardEmulationManager.java
@@ -110,7 +110,7 @@
                 context, mNfcFServicesCache, mT3tIdentifiersCache, this);
         mServiceCache.initialize();
         mNfcFServicesCache.initialize();
-        mPowerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
+        mPowerManager = context.getSystemService(PowerManager.class);
     }
 
     public INfcCardEmulation getNfcCardEmulationInterface() {
diff --git a/src/com/android/nfc/cardemulation/HostEmulationManager.java b/src/com/android/nfc/cardemulation/HostEmulationManager.java
index 8146202..cb93150 100644
--- a/src/com/android/nfc/cardemulation/HostEmulationManager.java
+++ b/src/com/android/nfc/cardemulation/HostEmulationManager.java
@@ -111,7 +111,7 @@
         mLock = new Object();
         mAidCache = aidCache;
         mState = STATE_IDLE;
-        mKeyguard = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
+        mKeyguard = context.getSystemService(KeyguardManager.class);
         mPowerManager = context.getSystemService(PowerManager.class);
     }
 
diff --git a/src/com/android/nfc/handover/BluetoothPeripheralHandover.java b/src/com/android/nfc/handover/BluetoothPeripheralHandover.java
index ec52256..28d9ee1 100644
--- a/src/com/android/nfc/handover/BluetoothPeripheralHandover.java
+++ b/src/com/android/nfc/handover/BluetoothPeripheralHandover.java
@@ -147,7 +147,7 @@
             mIsA2dpAvailable = true;
         }
 
-        mAudioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);
+        mAudioManager = mContext.getSystemService(AudioManager.class);
 
         mState = STATE_INIT;
     }