Show or hide call forwarding,additional settings according to carrier config.

Show or hide call forwarding,additional settings,clir and call waiting according to
KEY_CALL_FORWARDING_VISIBILITY_BOOL, KEY_ADDITIONAL_SETTINGS_CALLER_ID_VISIBILITY_BOOL
and KEY_ADDITIONAL_SETTINGS_CALL_WAITING_VISIBILITY_BOOL.

Bug: 110121209
Test: manual - test case as below :
1.flag clir on and flag cw on ,do CLIR show/hide ,
    and make call to other device.(PASS)
2.flag clir on and flag cw on ,do CW on/off,and CW function work.(PASS)
3.flag clir off and flag cw on ,no CLIR in additional settings page(PASS)
4.flag clir off and flag cw on ,do CW on/off,and CW function work.(PASS)
5.flag clir on and flag cw off ,do CLIR show/hide ,
    and make call to other device.(PASS)
6.flag clir on and flag cw off ,no CW in additional settings page(PASS)
7.flag clir off and flag cw off,
    no Additional Settings in call feature settings(PASS)
8.flag cf off, no CF in call feature settings (PASS)

Change-Id: I4f57438e9776f182812614d66a40ed25d3d73e9d
diff --git a/src/com/android/phone/GsmUmtsAdditionalCallOptions.java b/src/com/android/phone/GsmUmtsAdditionalCallOptions.java
index 082eaa9..b79cdd8 100644
--- a/src/com/android/phone/GsmUmtsAdditionalCallOptions.java
+++ b/src/com/android/phone/GsmUmtsAdditionalCallOptions.java
@@ -2,8 +2,10 @@
 
 import android.app.ActionBar;
 import android.os.Bundle;
+import android.os.PersistableBundle;
 import android.preference.Preference;
 import android.preference.PreferenceScreen;
+import android.telephony.CarrierConfigManager;
 import android.util.Log;
 import android.view.MenuItem;
 
@@ -26,6 +28,9 @@
     private Phone mPhone;
     private SubscriptionInfoHelper mSubscriptionInfoHelper;
 
+    private boolean mShowCLIRButton;
+    private boolean mShowCWButton;
+
     @Override
     protected void onCreate(Bundle icicle) {
         super.onCreate(icicle);
@@ -41,24 +46,60 @@
         mCLIRButton = (CLIRListPreference) prefSet.findPreference(BUTTON_CLIR_KEY);
         mCWButton = (CallWaitingSwitchPreference) prefSet.findPreference(BUTTON_CW_KEY);
 
-        mPreferences.add(mCLIRButton);
-        mPreferences.add(mCWButton);
-
-        if (icicle == null) {
-            if (DBG) Log.d(LOG_TAG, "start to init ");
-            mCLIRButton.init(this, false, mPhone);
+        PersistableBundle b = null;
+        if (mSubscriptionInfoHelper.hasSubId()) {
+            b = PhoneGlobals.getInstance().getCarrierConfigForSubId(
+                    mSubscriptionInfoHelper.getSubId());
         } else {
-            if (DBG) Log.d(LOG_TAG, "restore stored states");
-            mInitIndex = mPreferences.size();
-            mCLIRButton.init(this, true, mPhone);
-            mCWButton.init(this, true, mPhone);
-            int[] clirArray = icicle.getIntArray(mCLIRButton.getKey());
-            if (clirArray != null) {
-                if (DBG) Log.d(LOG_TAG, "onCreate:  clirArray[0]="
-                        + clirArray[0] + ", clirArray[1]=" + clirArray[1]);
-                mCLIRButton.handleGetCLIRResult(clirArray);
+            b = PhoneGlobals.getInstance().getCarrierConfig();
+        }
+
+        if (b != null) {
+            mShowCLIRButton = b.getBoolean(
+                    CarrierConfigManager.KEY_ADDITIONAL_SETTINGS_CALLER_ID_VISIBILITY_BOOL);
+            mShowCWButton = b.getBoolean(
+                    CarrierConfigManager.KEY_ADDITIONAL_SETTINGS_CALL_WAITING_VISIBILITY_BOOL);
+        }
+
+        if (mCLIRButton != null) {
+            if (mShowCLIRButton) {
+                mPreferences.add(mCLIRButton);
             } else {
-                mCLIRButton.init(this, false, mPhone);
+                prefSet.removePreference(mCLIRButton);
+            }
+        }
+
+        if (mCWButton != null) {
+            if (mShowCWButton) {
+                mPreferences.add(mCWButton);
+            } else {
+                prefSet.removePreference(mCWButton);
+            }
+        }
+
+        if (mPreferences.size() != 0) {
+            if (icicle == null) {
+                if (DBG) Log.d(LOG_TAG, "start to init ");
+                doPreferenceInit(mInitIndex);
+            } else {
+                if (DBG) Log.d(LOG_TAG, "restore stored states");
+                mInitIndex = mPreferences.size();
+                if (mShowCWButton) {
+                    mCWButton.init(this, true, mPhone);
+                }
+                if (mShowCLIRButton) {
+                    mCLIRButton.init(this, true, mPhone);
+                    int[] clirArray = icicle.getIntArray(mCLIRButton.getKey());
+                    if (clirArray != null) {
+                        if (DBG) {
+                            Log.d(LOG_TAG, "onCreate:  clirArray[0]="
+                                    + clirArray[0] + ", clirArray[1]=" + clirArray[1]);
+                        }
+                        mCLIRButton.handleGetCLIRResult(clirArray);
+                    } else {
+                        mCLIRButton.init(this, false, mPhone);
+                    }
+                }
             }
         }
 
@@ -73,7 +114,7 @@
     protected void onSaveInstanceState(Bundle outState) {
         super.onSaveInstanceState(outState);
 
-        if (mCLIRButton.clirArray != null) {
+        if (mShowCLIRButton && mCLIRButton.clirArray != null) {
             outState.putIntArray(mCLIRButton.getKey(), mCLIRButton.clirArray);
         }
     }
@@ -82,10 +123,7 @@
     public void onFinished(Preference preference, boolean reading) {
         if (mInitIndex < mPreferences.size()-1 && !isFinishing()) {
             mInitIndex++;
-            Preference pref = mPreferences.get(mInitIndex);
-            if (pref instanceof CallWaitingSwitchPreference) {
-                ((CallWaitingSwitchPreference) pref).init(this, false, mPhone);
-            }
+            doPreferenceInit(mInitIndex);
         }
         super.onFinished(preference, reading);
     }
@@ -99,4 +137,15 @@
         }
         return super.onOptionsItemSelected(item);
     }
+
+    private void doPreferenceInit(int index) {
+        if (mPreferences.size() != 0) {
+            Preference pref = mPreferences.get(index);
+            if (pref instanceof CallWaitingSwitchPreference) {
+                ((CallWaitingSwitchPreference) pref).init(this, false, mPhone);
+            } else if (pref instanceof CLIRListPreference) {
+                ((CLIRListPreference) pref).init(this, false, mPhone);
+            }
+        }
+    }
 }
diff --git a/src/com/android/phone/GsmUmtsCallOptions.java b/src/com/android/phone/GsmUmtsCallOptions.java
index 3b27d28..ab44b54 100644
--- a/src/com/android/phone/GsmUmtsCallOptions.java
+++ b/src/com/android/phone/GsmUmtsCallOptions.java
@@ -18,14 +18,12 @@
 
 import android.os.Bundle;
 import android.os.PersistableBundle;
-import android.preference.CheckBoxPreference;
 import android.preference.Preference;
 import android.preference.PreferenceActivity;
 import android.preference.PreferenceScreen;
 import android.telephony.CarrierConfigManager;
 import android.view.MenuItem;
 
-import com.android.internal.telephony.Phone;
 import com.android.internal.telephony.PhoneConstants;
 
 public class GsmUmtsCallOptions extends PreferenceActivity {
@@ -64,25 +62,45 @@
     }
 
     public static void init(PreferenceScreen prefScreen, SubscriptionInfoHelper subInfoHelper) {
-        Preference callForwardingPref = prefScreen.findPreference(CALL_FORWARDING_KEY);
-        callForwardingPref.setIntent(subInfoHelper.getIntent(GsmUmtsCallForwardOptions.class));
-
-        Preference additionalGsmSettingsPref =
-                prefScreen.findPreference(ADDITIONAL_GSM_SETTINGS_KEY);
-        additionalGsmSettingsPref.setIntent(
-                subInfoHelper.getIntent(GsmUmtsAdditionalCallOptions.class));
-
-        Preference callBarringPref = prefScreen.findPreference(CALL_BARRING_KEY);
         PersistableBundle b = null;
         if (subInfoHelper.hasSubId()) {
             b = PhoneGlobals.getInstance().getCarrierConfigForSubId(subInfoHelper.getSubId());
         } else {
             b = PhoneGlobals.getInstance().getCarrierConfig();
         }
-        if (b != null && b.getBoolean(CarrierConfigManager.KEY_CALL_BARRING_VISIBILITY_BOOL)) {
-            callBarringPref.setIntent(subInfoHelper.getIntent(GsmUmtsCallBarringOptions.class));
-        } else {
-            prefScreen.removePreference(callBarringPref);
+
+        Preference callForwardingPref = prefScreen.findPreference(CALL_FORWARDING_KEY);
+        if (callForwardingPref != null) {
+            if (b != null && b.getBoolean(
+                    CarrierConfigManager.KEY_CALL_FORWARDING_VISIBILITY_BOOL)) {
+                callForwardingPref.setIntent(
+                        subInfoHelper.getIntent(GsmUmtsCallForwardOptions.class));
+            } else {
+                prefScreen.removePreference(callForwardingPref);
+            }
+        }
+
+        Preference additionalGsmSettingsPref =
+                prefScreen.findPreference(ADDITIONAL_GSM_SETTINGS_KEY);
+        if (additionalGsmSettingsPref != null) {
+            if (b != null && (b.getBoolean(
+                    CarrierConfigManager.KEY_ADDITIONAL_SETTINGS_CALL_WAITING_VISIBILITY_BOOL)
+                    || b.getBoolean(
+                    CarrierConfigManager.KEY_ADDITIONAL_SETTINGS_CALLER_ID_VISIBILITY_BOOL))) {
+                additionalGsmSettingsPref.setIntent(
+                        subInfoHelper.getIntent(GsmUmtsAdditionalCallOptions.class));
+            } else {
+                prefScreen.removePreference(additionalGsmSettingsPref);
+            }
+        }
+
+        Preference callBarringPref = prefScreen.findPreference(CALL_BARRING_KEY);
+        if (callBarringPref != null) {
+            if (b != null && b.getBoolean(CarrierConfigManager.KEY_CALL_BARRING_VISIBILITY_BOOL)) {
+                callBarringPref.setIntent(subInfoHelper.getIntent(GsmUmtsCallBarringOptions.class));
+            } else {
+                prefScreen.removePreference(callBarringPref);
+            }
         }
     }
 }