Improve variable naming and coding styles

Bug: 151708408
Bug: 149273649
Test: manual
Merged-In: I035ba012deab32cb9273579847794b959e93a2c1
Change-Id: I035ba012deab32cb9273579847794b959e93a2c1
diff --git a/res/values/config.xml b/res/values/config.xml
index 98c4fb9..50ad038 100644
--- a/res/values/config.xml
+++ b/res/values/config.xml
@@ -4,12 +4,12 @@
     <bool name="enable_nfc_url_open_dialog">false</bool>
     <bool name="enable_auto_play">true</bool>
     <bool name="enable_notify_dispatch_failed">false</bool>
-    <bool name="enable_nfc_blocking_alert">false</bool>
-    <integer name="nfc_blocking_count">10</integer>
+    <bool name="enable_antenna_blocked_alert">false</bool>
+    <integer name="max_antenna_blocked_failure_count">10</integer>
     <integer name="unknown_tag_polling_delay">-1</integer>
 
     <!-- List of SKUs where Secure NFC functionality is supported -->
     <string-array name="config_skuSupportsSecureNfc" translatable="false" />
     <!-- NFC blocking alert notification link uri string -->
-    <string name="nfc_blocking_alert_link" translatable="false" />
+    <string name="antenna_blocked_alert_link" translatable="false" />
 </resources>
diff --git a/src/com/android/nfc/NfcBlockedNotification.java b/src/com/android/nfc/NfcBlockedNotification.java
index 111f82c..5dd28ee 100644
--- a/src/com/android/nfc/NfcBlockedNotification.java
+++ b/src/com/android/nfc/NfcBlockedNotification.java
@@ -40,13 +40,13 @@
         super.onCreate(savedInstanceState);
         PendingIntent pIntent;
         Intent infoIntent;
-        if (TextUtils.isEmpty(getString(R.string.nfc_blocking_alert_link))) {
-            // Do nothing after user click the notification if nfc_blocking_alert_link is empty
+        if (TextUtils.isEmpty(getString(R.string.antenna_blocked_alert_link))) {
+            // Do nothing after user click the notification if antenna_blocked_alert_link is empty
             infoIntent = new Intent();
         } else {
             // Open the link after user click the notification
             infoIntent = new Intent(Intent.ACTION_VIEW);
-            infoIntent.setData(Uri.parse(getString(R.string.nfc_blocking_alert_link)));
+            infoIntent.setData(Uri.parse(getString(R.string.antenna_blocked_alert_link)));
         }
         Notification.Builder builder = new Notification.Builder(this, NFC_NOTIFICATION_CHANNEL);
         builder.setContentTitle(getString(R.string.nfc_blocking_alert_title))
@@ -58,7 +58,7 @@
         mNotificationChannel = new NotificationChannel(NFC_NOTIFICATION_CHANNEL,
             getString(R.string.nfcUserLabel), NotificationManager.IMPORTANCE_DEFAULT);
         NotificationManager notificationManager =
-            (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
+            getApplicationContext().getSystemService(NotificationManager.class);
         notificationManager.createNotificationChannel(mNotificationChannel);
         notificationManager.notify(NOTIFICATION_ID_NFC, builder.build());
     }
diff --git a/src/com/android/nfc/NfcService.java b/src/com/android/nfc/NfcService.java
index c708fe4..f1ba3be 100644
--- a/src/com/android/nfc/NfcService.java
+++ b/src/com/android/nfc/NfcService.java
@@ -131,8 +131,8 @@
     static final String PREF_FIRST_BEAM = "first_beam";
     static final String PREF_FIRST_BOOT = "first_boot";
 
-    static final String PREF_NFC_MESSAGE = "nfc_message";
-    static final boolean NFC_MESSAGE_DEFAULT = true;
+    static final String PREF_ANTENNA_BLOCKED_MESSAGE_SHOWN = "antenna_blocked_message_shown";
+    static final boolean ANTENNA_BLOCKED_MESSAGE_SHOWN_DEFAULT = false;
 
     static final String TRON_NFC_CE = "nfc_ce";
     static final String TRON_NFC_P2P = "nfc_p2p";
@@ -159,6 +159,9 @@
     static final int MSG_APPLY_SCREEN_STATE = 16;
     static final int MSG_TRANSACTION_EVENT = 17;
 
+    // Negative value for NO polling delay
+    static final int NO_POLL_DELAY = -1;
+
     // Update stats every 4 hours
     static final long STATS_UPDATE_INTERVAL_MS = 4 * 60 * 60 * 1000;
     static final long MAX_POLLING_PAUSE_TIMEOUT = 40000;
@@ -248,9 +251,9 @@
     private int mUserId;
     boolean mPollingPaused;
 
-    // Nfc notification message
-    boolean mNfcMessageEnabled;
-    private static int mDispatchFailedCounts;
+    // True if nfc notification message already shown
+    boolean mAntennaBlockedMessageShown;
+    private static int mDispatchFailedCount;
     private static int mDispatchFailedMax;
 
     static final int INVALID_NATIVE_HANDLE = -1;
@@ -292,6 +295,9 @@
     boolean mIsBeamCapable;
     boolean mIsSecureNfcCapable;
 
+    int mPollDelay;
+    boolean mNotifyDispatchFailed;
+
     private NfcDispatcher mNfcDispatcher;
     private PowerManager mPowerManager;
     private KeyguardManager mKeyguard;
@@ -513,6 +519,22 @@
             mIsSecureNfcCapable;
         mDeviceHost.setNfcSecure(mIsSecureNfcEnabled);
 
+
+        // Notification message variables
+        mDispatchFailedCount = 0;
+        if (mContext.getResources().getBoolean(R.bool.enable_antenna_blocked_alert) &&
+            !mPrefs.getBoolean(PREF_ANTENNA_BLOCKED_MESSAGE_SHOWN, ANTENNA_BLOCKED_MESSAGE_SHOWN_DEFAULT)) {
+            mAntennaBlockedMessageShown = false;
+            mDispatchFailedMax =
+                mContext.getResources().getInteger(R.integer.max_antenna_blocked_failure_count);
+        } else {
+            mAntennaBlockedMessageShown = true;
+        }
+
+        // Polling delay variables
+        mPollDelay = mContext.getResources().getInteger(R.integer.unknown_tag_polling_delay);
+        mNotifyDispatchFailed = mContext.getResources().getBoolean(R.bool.enable_notify_dispatch_failed);
+
         // Make sure this is only called when object construction is complete.
         ServiceManager.addService(SERVICE_NAME, mNfcAdapter);
 
@@ -654,20 +676,6 @@
                         initialized = mDeviceHost.checkFirmware();
                     }
 
-                    mDispatchFailedCounts = 0;
-                    try {
-                        if (mContext.getResources().getBoolean(R.bool.enable_nfc_blocking_alert) &&
-                            mPrefs.getBoolean(PREF_NFC_MESSAGE, NFC_MESSAGE_DEFAULT)) {
-                            mNfcMessageEnabled = true;
-                            mDispatchFailedMax =
-                                mContext.getResources().getInteger(R.integer.nfc_blocking_count);
-                        } else  {
-                            mNfcMessageEnabled = false;
-                        }
-                    } catch (NotFoundException e) {
-                        mNfcMessageEnabled = false;
-                    }
-
                     if (initialized) {
                         SystemProperties.set("nfc.initialized", "true");
                     }
@@ -2587,15 +2595,11 @@
                 if (dispatchResult == NfcDispatcher.DISPATCH_FAIL && !mInProvisionMode) {
                     if (DBG) Log.d(TAG, "Tag dispatch failed");
                     unregisterObject(tagEndpoint.getHandle());
-                    int pollDelay = -1;
-                    try {
-                        pollDelay = mContext.getResources().getInteger(R.integer.unknown_tag_polling_delay);
-                    } catch (NotFoundException e) {
-                        Log.e(TAG, "Keep presence checking.", e);
-                    }
-                    if (pollDelay >= 0) {
+                    if (mPollDelay > NO_POLL_DELAY) {
                         tagEndpoint.stopPresenceChecking();
-                        mNfcAdapter.pausePolling(pollDelay);
+                        mNfcAdapter.pausePolling(mPollDelay);
+                    } else {
+                        Log.e(TAG, "Keep presence checking.");
                     }
                     if (mScreenState == ScreenStateHelper.SCREEN_STATE_ON_UNLOCKED &&
                         mContext.getResources().getBoolean(R.bool.enable_notify_dispatch_failed)) {
@@ -2607,20 +2611,20 @@
                         mToast.show();
                         playSound(SOUND_ERROR);
                     }
-                    if (mNfcMessageEnabled && mDispatchFailedCounts++ > mDispatchFailedMax) {
+                    if (!mAntennaBlockedMessageShown && mDispatchFailedCount++ > mDispatchFailedMax) {
                         Intent dialogIntent = new Intent(mContext, NfcBlockedNotification.class);
                         dialogIntent.setFlags(
                             Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                         mContext.startActivity(dialogIntent);
-                        mPrefsEditor.putBoolean(PREF_NFC_MESSAGE, false);
+                        mPrefsEditor.putBoolean(PREF_ANTENNA_BLOCKED_MESSAGE_SHOWN, true);
                         mPrefsEditor.apply();
                         mBackupManager.dataChanged();
-                        mNfcMessageEnabled = false;
-                        mDispatchFailedCounts = 0;
+                        mAntennaBlockedMessageShown = true;
+                        mDispatchFailedCount = 0;
                         if (DBG) Log.d(TAG, "Tag dispatch failed notification");
                     }
                 } else if (dispatchResult == NfcDispatcher.DISPATCH_SUCCESS) {
-                    mDispatchFailedCounts = 0;
+                    mDispatchFailedCount = 0;
                     mVibrator.vibrate(mVibrationEffect);
                     playSound(SOUND_END);
                 }