Refactor shortcut view relevant code.

Bug: 112168722
Bug: 127406814
Test: Manually
Change-Id: I50947f464101a96e68c3932544d00bea28760949
diff --git a/src/com/android/phone/EmergencyDialer.java b/src/com/android/phone/EmergencyDialer.java
index 468919f..cff27b1 100644
--- a/src/com/android/phone/EmergencyDialer.java
+++ b/src/com/android/phone/EmergencyDialer.java
@@ -129,7 +129,7 @@
         private MetricsLogger mMetricsLogger = new MetricsLogger();
 
         public void writeMetricsForEnter() {
-            if (!mIsShortcutViewEnabled) {
+            if (!mShortcutViewConfig.isEnabled()) {
                 return;
             }
 
@@ -142,7 +142,7 @@
         }
 
         public void writeMetricsForExit() {
-            if (!mIsShortcutViewEnabled) {
+            if (!mShortcutViewConfig.isEnabled()) {
                 return;
             }
 
@@ -157,7 +157,7 @@
 
         public void writeMetricsForMakingCall(int callSource, int phoneNumberType,
                 boolean hasShortcut) {
-            if (!mIsShortcutViewEnabled) {
+            if (!mShortcutViewConfig.isEnabled()) {
                 return;
             }
 
@@ -230,8 +230,6 @@
     private View mEmergencyShortcutView;
     private View mDialpadView;
 
-    private ShortcutViewUtils.PhoneInfo mPhoneInfo;
-
     private List<EmergencyShortcutButton> mEmergencyShortcutButtonList;
     private EccShortcutAdapter mShortcutAdapter;
     private DataSetObserver mShortcutDataSetObserver = null;
@@ -291,7 +289,7 @@
     private float mDefaultDigitsTextSize;
 
     private int mEntryType;
-    private boolean mIsShortcutViewEnabled;
+    private ShortcutViewUtils.Config mShortcutViewConfig;
 
     private MetricsWriter mMetricsWriter;
     private SensorManager mSensorManager;
@@ -361,24 +359,13 @@
         PersistableBundle carrierConfig =
                 configMgr.getConfigForSubId(SubscriptionManager.getDefaultVoiceSubscriptionId());
 
-        mIsShortcutViewEnabled = false;
-        mPhoneInfo = null;
-        if (canEnableShortcutView(carrierConfig)) {
-            if (!isAirplaneModeOn()) {
-                mPhoneInfo = ShortcutViewUtils.pickPreferredPhone(this);
-            } else {
-                Log.d(LOG_TAG, "Disables shortcut view in airplane mode");
-            }
-            if (mPhoneInfo != null) {
-                mIsShortcutViewEnabled = true;
-            }
-        }
+        mShortcutViewConfig = new ShortcutViewUtils.Config(this, carrierConfig, mEntryType);
         Log.d(LOG_TAG, "Enable emergency dialer shortcut: "
-                + mIsShortcutViewEnabled);
+                + mShortcutViewConfig.isEnabled());
 
         mColorExtractor = new ColorExtractor(this);
 
-        if (mIsShortcutViewEnabled) {
+        if (mShortcutViewConfig.isEnabled()) {
             // Shortcut view doesn't support dark text theme.
             updateTheme(false);
         } else {
@@ -403,7 +390,7 @@
         ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
                 .getDefaultDisplay().getSize(displaySize);
         mBackgroundGradient.setScreenSize(displaySize.x, displaySize.y);
-        mBackgroundGradient.setAlpha(mIsShortcutViewEnabled
+        mBackgroundGradient.setAlpha(mShortcutViewConfig.isEnabled()
                 ? BLACK_BACKGROUND_GRADIENT_ALPHA : BACKGROUND_GRADIENT_ALPHA);
         getWindow().setBackgroundDrawable(mBackgroundGradient);
 
@@ -467,7 +454,7 @@
 
         mEmergencyInfoGroup = (EmergencyInfoGroup) findViewById(R.id.emergency_info_button);
 
-        if (mIsShortcutViewEnabled) {
+        if (mShortcutViewConfig.isEnabled()) {
             setupEmergencyShortcutsView();
         }
     }
@@ -533,7 +520,7 @@
     public void onBackPressed() {
         // If shortcut view is enabled and Dialpad view is visible, pressing the back key will
         // back to display EmergencyShortcutView view. Otherwise, it would finish the activity.
-        if (mIsShortcutViewEnabled && mDialpadView != null
+        if (mShortcutViewConfig.isEnabled() && mDialpadView != null
                 && mDialpadView.getVisibility() == View.VISIBLE) {
             switchView(mEmergencyShortcutView, mDialpadView, true);
             return;
@@ -610,7 +597,7 @@
         if (!TextUtils.isEmpty(phoneNumber)) {
             if (DBG) Log.d(LOG_TAG, "dial emergency number: " + Rlog.pii(LOG_TAG, phoneNumber));
             placeCall(phoneNumber, ParcelableCallAnalytics.CALL_SOURCE_EMERGENCY_SHORTCUT,
-                    mPhoneInfo);
+                    mShortcutViewConfig.getPhoneInfo());
         } else {
             Log.d(LOG_TAG, "emergency number is empty");
         }
@@ -751,7 +738,7 @@
         mUserActions = MetricsWriter.USER_ACTION_NONE;
         mMetricsWriter.writeMetricsForEnter();
 
-        if (mIsShortcutViewEnabled) {
+        if (mShortcutViewConfig.isEnabled()) {
             // Shortcut view doesn't support dark text theme.
             mBackgroundGradient.setColors(Color.BLACK, Color.BLACK, false);
             updateTheme(false);
@@ -764,7 +751,7 @@
             updateTheme(lockScreenColors.supportsDarkText());
         }
 
-        if (mIsShortcutViewEnabled) {
+        if (mShortcutViewConfig.isEnabled()) {
             updateLocationAndEccInfo();
         }
     }
@@ -814,24 +801,6 @@
         mColorExtractor.removeOnColorsChangedListener(this);
     }
 
-    private boolean canEnableShortcutView(PersistableBundle carrierConfig) {
-        if (mEntryType != ENTRY_TYPE_POWER_MENU) {
-            Log.d(LOG_TAG, "Disables shortcut view since it's not launched from power menu");
-            return false;
-        }
-        if (!carrierConfig.getBoolean(
-                CarrierConfigManager.KEY_SUPPORT_EMERGENCY_DIALER_SHORTCUT_BOOL)) {
-            Log.d(LOG_TAG, "Disables shortcut view by carrier requirement");
-            return false;
-        }
-        return true;
-    }
-
-    private boolean isAirplaneModeOn() {
-        return Settings.Global.getInt(getContentResolver(),
-                Settings.Global.AIRPLANE_MODE_ON, 0) != 0;
-    }
-
     /**
      * Sets theme based on gradient colors
      *
@@ -875,15 +844,12 @@
         // nothing and just returns input number.
         mLastNumber = PhoneNumberUtils.convertToEmergencyNumber(this, mLastNumber);
 
-        boolean isEmergencyNumber = false;
+        boolean isEmergencyNumber;
         ShortcutViewUtils.PhoneInfo phoneToMakeCall = null;
-        if (mPhoneInfo != null) {
-            isEmergencyNumber = mPhoneInfo.hasPromotedEmergencyNumber(mLastNumber);
-            if (isEmergencyNumber) {
-                phoneToMakeCall = mPhoneInfo;
-            }
-        }
-        if (!isEmergencyNumber) {
+        if (mShortcutViewConfig.hasPromotedEmergencyNumber(mLastNumber)) {
+            isEmergencyNumber = true;
+            phoneToMakeCall = mShortcutViewConfig.getPhoneInfo();
+        } else {
             TelephonyManager tm = getSystemService(TelephonyManager.class);
             isEmergencyNumber = tm.isCurrentEmergencyNumber(mLastNumber);
         }
@@ -1163,7 +1129,7 @@
     private void setLocationInfo() {
         final View locationInfo = findViewById(R.id.location_info);
 
-        String countryIso = mPhoneInfo != null ? mPhoneInfo.getCountryIso() : null;
+        String countryIso = mShortcutViewConfig.getCountryIso();
         String countryName = null;
         if (!TextUtils.isEmpty(countryIso)) {
             Locale locale = Locale.getDefault();
@@ -1244,7 +1210,7 @@
         if (!isFinishing() && !isDestroyed()) {
             setLocationInfo();
             if (mShortcutAdapter != null) {
-                mShortcutAdapter.updateCountryEccInfo(this, mPhoneInfo);
+                mShortcutAdapter.updateCountryEccInfo(this, mShortcutViewConfig.getPhoneInfo());
             }
         }
     }
diff --git a/src/com/android/phone/ShortcutViewUtils.java b/src/com/android/phone/ShortcutViewUtils.java
index 595ea86..7e24c7a 100644
--- a/src/com/android/phone/ShortcutViewUtils.java
+++ b/src/com/android/phone/ShortcutViewUtils.java
@@ -17,9 +17,12 @@
 package com.android.phone;
 
 import android.content.Context;
+import android.os.PersistableBundle;
+import android.provider.Settings;
 import android.telecom.PhoneAccount;
 import android.telecom.PhoneAccountHandle;
 import android.telecom.TelecomManager;
+import android.telephony.CarrierConfigManager;
 import android.telephony.SubscriptionManager;
 import android.telephony.TelephonyManager;
 import android.telephony.emergency.EmergencyNumber;
@@ -54,6 +57,64 @@
         PROMOTED_CATEGORIES_BITMASK = bitmask;
     }
 
+    static class Config {
+        private final boolean mCanEnableShortcutView;
+        private PhoneInfo mPhoneInfo = null;
+
+        Config(@NonNull Context context, PersistableBundle carrierConfig, int entryType) {
+            mCanEnableShortcutView = canEnableShortcutView(carrierConfig, entryType);
+            refresh(context);
+        }
+
+        void refresh(@NonNull Context context) {
+            if (mCanEnableShortcutView && !isAirplaneModeOn(context)) {
+                mPhoneInfo = ShortcutViewUtils.pickPreferredPhone(context);
+            } else {
+                mPhoneInfo = null;
+            }
+        }
+
+        boolean isEnabled() {
+            return mPhoneInfo != null;
+        }
+
+        PhoneInfo getPhoneInfo() {
+            return mPhoneInfo;
+        }
+
+        String getCountryIso() {
+            if (mPhoneInfo == null) {
+                return null;
+            }
+            return mPhoneInfo.getCountryIso();
+        }
+
+        boolean hasPromotedEmergencyNumber(String number) {
+            if (mPhoneInfo == null) {
+                return false;
+            }
+            return mPhoneInfo.hasPromotedEmergencyNumber(number);
+        }
+
+        private boolean canEnableShortcutView(PersistableBundle carrierConfig, int entryType) {
+            if (entryType != EmergencyDialer.ENTRY_TYPE_POWER_MENU) {
+                Log.d(LOG_TAG, "Disables shortcut view since it's not launched from power menu");
+                return false;
+            }
+            if (carrierConfig == null || !carrierConfig.getBoolean(
+                    CarrierConfigManager.KEY_SUPPORT_EMERGENCY_DIALER_SHORTCUT_BOOL)) {
+                Log.d(LOG_TAG, "Disables shortcut view by carrier requirement");
+                return false;
+            }
+            return true;
+        }
+
+        private boolean isAirplaneModeOn(@NonNull Context context) {
+            return Settings.Global.getInt(context.getContentResolver(),
+                    Settings.Global.AIRPLANE_MODE_ON, 0) != 0;
+        }
+    }
+
     // Info and emergency call capability of every phone.
     static class PhoneInfo {
         private final PhoneAccountHandle mHandle;