blob: cb661ed9d57f88694da11bc6038394469946f3f9 [file] [log] [blame]
Pavel Zhamaitsiak4ab32562015-02-23 15:34:07 -08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.settings;
18
Pavel Zhamaitsiak897b4922015-03-06 16:02:53 -080019import android.app.Activity;
20import android.app.AlertDialog;
21import android.content.BroadcastReceiver;
Meng Wang90e8e472016-05-25 17:12:27 -070022import android.content.ComponentName;
Pavel Zhamaitsiak4ab32562015-02-23 15:34:07 -080023import android.content.Context;
Pavel Zhamaitsiak897b4922015-03-06 16:02:53 -080024import android.content.Intent;
25import android.content.IntentFilter;
Pavel Zhamaitsiak4ab32562015-02-23 15:34:07 -080026import android.os.Bundle;
Sungmin Choi577add82015-12-23 14:24:52 +090027import android.os.PersistableBundle;
Jason Monk39b46742015-09-10 15:52:51 -040028import android.support.v7.preference.ListPreference;
29import android.support.v7.preference.Preference;
Meng Wang90e8e472016-05-25 17:12:27 -070030import android.support.v7.preference.Preference.OnPreferenceClickListener;
Jason Monk39b46742015-09-10 15:52:51 -040031import android.support.v7.preference.PreferenceScreen;
Sungmin Choi577add82015-12-23 14:24:52 +090032import android.telephony.CarrierConfigManager;
Pavel Zhamaitsiak4ab32562015-02-23 15:34:07 -080033import android.telephony.PhoneStateListener;
34import android.telephony.TelephonyManager;
Meng Wang90e8e472016-05-25 17:12:27 -070035import android.text.TextUtils;
Pavel Zhamaitsiak4ab32562015-02-23 15:34:07 -080036import android.util.Log;
37import android.widget.Switch;
Pavel Zhamaitsiakee5f27d2015-03-23 14:43:24 -070038import android.widget.TextView;
Pavel Zhamaitsiak4ab32562015-02-23 15:34:07 -080039
40import com.android.ims.ImsConfig;
41import com.android.ims.ImsManager;
Tamas Berghammer265d3c22016-06-22 15:34:45 +010042import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Amit Mahajanb9221862015-12-14 13:23:29 -080043import com.android.internal.telephony.Phone;
Pavel Zhamaitsiak4ab32562015-02-23 15:34:07 -080044import com.android.settings.widget.SwitchBar;
45
46/**
47 * "Wi-Fi Calling settings" screen. This preference screen lets you
Pavel Zhamaitsiak1e034fe2015-03-12 12:36:17 -070048 * enable/disable Wi-Fi Calling and change Wi-Fi Calling mode.
Pavel Zhamaitsiak4ab32562015-02-23 15:34:07 -080049 */
50public class WifiCallingSettings extends SettingsPreferenceFragment
51 implements SwitchBar.OnSwitchChangeListener,
Pavel Zhamaitsiak71099322015-03-02 15:01:03 -080052 Preference.OnPreferenceChangeListener {
Pavel Zhamaitsiak4ab32562015-02-23 15:34:07 -080053
54 private static final String TAG = "WifiCallingSettings";
55
56 //String keys for preference lookup
57 private static final String BUTTON_WFC_MODE = "wifi_calling_mode";
Meng Wangfe3749d2016-09-19 14:53:24 -070058 private static final String BUTTON_WFC_ROAMING_MODE = "wifi_calling_roaming_mode";
Meng Wang90e8e472016-05-25 17:12:27 -070059 private static final String PREFERENCE_EMERGENCY_ADDRESS = "emergency_address_key";
60
61 private static final int REQUEST_CHECK_WFC_EMERGENCY_ADDRESS = 1;
Pavel Zhamaitsiak4ab32562015-02-23 15:34:07 -080062
Meng Wang0dc43112016-06-02 17:06:22 -070063 public static final String EXTRA_LAUNCH_CARRIER_APP = "EXTRA_LAUNCH_CARRIER_APP";
64
65 public static final int LAUCH_APP_ACTIVATE = 0;
66 public static final int LAUCH_APP_UPDATE = 1;
67
Pavel Zhamaitsiak4ab32562015-02-23 15:34:07 -080068 //UI objects
69 private SwitchBar mSwitchBar;
70 private Switch mSwitch;
71 private ListPreference mButtonWfcMode;
Meng Wangfe3749d2016-09-19 14:53:24 -070072 private ListPreference mButtonWfcRoamingMode;
Meng Wang0dc43112016-06-02 17:06:22 -070073 private Preference mUpdateAddress;
Pavel Zhamaitsiakee5f27d2015-03-23 14:43:24 -070074 private TextView mEmptyView;
Pavel Zhamaitsiak4ab32562015-02-23 15:34:07 -080075
Pavel Zhamaitsiak2fe4b3c2015-08-07 12:02:38 -070076 private boolean mValidListener = false;
Sungmin Choi577add82015-12-23 14:24:52 +090077 private boolean mEditableWfcMode = true;
Meng Wangfe3749d2016-09-19 14:53:24 -070078 private boolean mEditableWfcRoamingMode = true;
Pavel Zhamaitsiak2fe4b3c2015-08-07 12:02:38 -070079
Pavel Zhamaitsiak4ab32562015-02-23 15:34:07 -080080 private final PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
81 /*
82 * Enable/disable controls when in/out of a call and depending on
83 * TTY mode and TTY support over VoLTE.
84 * @see android.telephony.PhoneStateListener#onCallStateChanged(int,
85 * java.lang.String)
86 */
87 @Override
88 public void onCallStateChanged(int state, String incomingNumber) {
89 final SettingsActivity activity = (SettingsActivity) getActivity();
90 boolean isNonTtyOrTtyOnVolteEnabled = ImsManager
91 .isNonTtyOrTtyOnVolteEnabled(activity);
92 final SwitchBar switchBar = activity.getSwitchBar();
93 boolean isWfcEnabled = switchBar.getSwitch().isChecked()
94 && isNonTtyOrTtyOnVolteEnabled;
95
96 switchBar.setEnabled((state == TelephonyManager.CALL_STATE_IDLE)
97 && isNonTtyOrTtyOnVolteEnabled);
98
Meng Wangfe3749d2016-09-19 14:53:24 -070099 boolean isWfcModeEditable = true;
100 boolean isWfcRoamingModeEditable = false;
101 final CarrierConfigManager configManager = (CarrierConfigManager)
102 activity.getSystemService(Context.CARRIER_CONFIG_SERVICE);
103 if (configManager != null) {
104 PersistableBundle b = configManager.getConfig();
105 if (b != null) {
106 isWfcModeEditable = b.getBoolean(
107 CarrierConfigManager.KEY_EDITABLE_WFC_MODE_BOOL);
108 isWfcRoamingModeEditable = b.getBoolean(
109 CarrierConfigManager.KEY_EDITABLE_WFC_ROAMING_MODE_BOOL);
110 }
111 }
112
Pavel Zhamaitsiak4ab32562015-02-23 15:34:07 -0800113 Preference pref = getPreferenceScreen().findPreference(BUTTON_WFC_MODE);
Pavel Zhamaitsiak4ab32562015-02-23 15:34:07 -0800114 if (pref != null) {
Meng Wangfe3749d2016-09-19 14:53:24 -0700115 pref.setEnabled(isWfcEnabled && isWfcModeEditable
116 && (state == TelephonyManager.CALL_STATE_IDLE));
117 }
118 Preference pref_roam = getPreferenceScreen().findPreference(BUTTON_WFC_ROAMING_MODE);
119 if (pref_roam != null) {
120 pref_roam.setEnabled(isWfcEnabled && isWfcRoamingModeEditable
Pavel Zhamaitsiak4ab32562015-02-23 15:34:07 -0800121 && (state == TelephonyManager.CALL_STATE_IDLE));
Pavel Zhamaitsiak4ab32562015-02-23 15:34:07 -0800122 }
123 }
124 };
125
Meng Wang90e8e472016-05-25 17:12:27 -0700126 private final OnPreferenceClickListener mUpdateAddressListener =
127 new OnPreferenceClickListener() {
128 /*
129 * Launch carrier emergency address managemnent activity
130 */
131 @Override
132 public boolean onPreferenceClick(Preference preference) {
133 final Context context = getActivity();
134 Intent carrierAppIntent = getCarrierActivityIntent(context);
135 if (carrierAppIntent != null) {
Meng Wang0dc43112016-06-02 17:06:22 -0700136 carrierAppIntent.putExtra(EXTRA_LAUNCH_CARRIER_APP, LAUCH_APP_UPDATE);
Meng Wang90e8e472016-05-25 17:12:27 -0700137 startActivity(carrierAppIntent);
138 }
139 return true;
140 }
141 };
142
Pavel Zhamaitsiak4ab32562015-02-23 15:34:07 -0800143 @Override
144 public void onActivityCreated(Bundle savedInstanceState) {
145 super.onActivityCreated(savedInstanceState);
146
147 final SettingsActivity activity = (SettingsActivity) getActivity();
148
149 mSwitchBar = activity.getSwitchBar();
150 mSwitch = mSwitchBar.getSwitch();
151 mSwitchBar.show();
Pavel Zhamaitsiakee5f27d2015-03-23 14:43:24 -0700152
153 mEmptyView = (TextView) getView().findViewById(android.R.id.empty);
Jason Monk39b46742015-09-10 15:52:51 -0400154 setEmptyView(mEmptyView);
Meng Wang7cecf2d2017-05-16 11:50:20 -0700155 String emptyViewText = activity.getString(R.string.wifi_calling_off_explanation)
156 + activity.getString(R.string.wifi_calling_off_explanation_2);
157 mEmptyView.setText(emptyViewText);
Pavel Zhamaitsiak4ab32562015-02-23 15:34:07 -0800158 }
159
160 @Override
161 public void onDestroyView() {
162 super.onDestroyView();
163 mSwitchBar.hide();
164 }
165
Pavel Zhamaitsiak897b4922015-03-06 16:02:53 -0800166 private void showAlert(Intent intent) {
167 Context context = getActivity();
168
Amit Mahajanb9221862015-12-14 13:23:29 -0800169 CharSequence title = intent.getCharSequenceExtra(Phone.EXTRA_KEY_ALERT_TITLE);
170 CharSequence message = intent.getCharSequenceExtra(Phone.EXTRA_KEY_ALERT_MESSAGE);
Pavel Zhamaitsiak897b4922015-03-06 16:02:53 -0800171
172 AlertDialog.Builder builder = new AlertDialog.Builder(context);
173 builder.setMessage(message)
174 .setTitle(title)
Pavel Zhamaitsiakee5f27d2015-03-23 14:43:24 -0700175 .setIcon(android.R.drawable.ic_dialog_alert)
Pavel Zhamaitsiak897b4922015-03-06 16:02:53 -0800176 .setPositiveButton(android.R.string.ok, null);
177 AlertDialog dialog = builder.create();
178 dialog.show();
179 }
180
181 private IntentFilter mIntentFilter;
182
183 private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
184 @Override
185 public void onReceive(Context context, Intent intent) {
186 String action = intent.getAction();
Pavel Zhamaitsiake8616912015-03-12 15:29:11 -0700187 if (action.equals(ImsManager.ACTION_IMS_REGISTRATION_ERROR)) {
Pavel Zhamaitsiak897b4922015-03-06 16:02:53 -0800188 // If this fragment is active then we are immediately
189 // showing alert on screen. There is no need to add
190 // notification in this case.
191 //
192 // In order to communicate to ImsPhone that it should
193 // not show notification, we are changing result code here.
194 setResultCode(Activity.RESULT_CANCELED);
195
Pavel Zhamaitsiake8616912015-03-12 15:29:11 -0700196 // UX requirement is to disable WFC in case of "permanent" registration failures.
197 mSwitch.setChecked(false);
198
Pavel Zhamaitsiak897b4922015-03-06 16:02:53 -0800199 showAlert(intent);
200 }
201 }
202 };
203
Pavel Zhamaitsiak4ab32562015-02-23 15:34:07 -0800204 @Override
Fan Zhang65076132016-08-08 10:25:13 -0700205 public int getMetricsCategory() {
Chris Wren9d1bfd12016-01-26 18:04:01 -0500206 return MetricsEvent.WIFI_CALLING;
Chris Wren8a963ba2015-03-20 10:29:14 -0400207 }
208
209 @Override
Pavel Zhamaitsiak4ab32562015-02-23 15:34:07 -0800210 public void onCreate(Bundle savedInstanceState) {
211 super.onCreate(savedInstanceState);
212
213 addPreferencesFromResource(R.xml.wifi_calling_settings);
214
215 mButtonWfcMode = (ListPreference) findPreference(BUTTON_WFC_MODE);
216 mButtonWfcMode.setOnPreferenceChangeListener(this);
217
Meng Wangfe3749d2016-09-19 14:53:24 -0700218 mButtonWfcRoamingMode = (ListPreference) findPreference(BUTTON_WFC_ROAMING_MODE);
219 mButtonWfcRoamingMode.setOnPreferenceChangeListener(this);
220
Meng Wang0dc43112016-06-02 17:06:22 -0700221 mUpdateAddress = (Preference) findPreference(PREFERENCE_EMERGENCY_ADDRESS);
Meng Wang90e8e472016-05-25 17:12:27 -0700222 mUpdateAddress.setOnPreferenceClickListener(mUpdateAddressListener);
223
Pavel Zhamaitsiak897b4922015-03-06 16:02:53 -0800224 mIntentFilter = new IntentFilter();
Pavel Zhamaitsiake8616912015-03-12 15:29:11 -0700225 mIntentFilter.addAction(ImsManager.ACTION_IMS_REGISTRATION_ERROR);
Sungmin Choi577add82015-12-23 14:24:52 +0900226
227 CarrierConfigManager configManager = (CarrierConfigManager)
228 getSystemService(Context.CARRIER_CONFIG_SERVICE);
Tyler Gunn2013ba82016-04-01 08:41:18 -0700229 boolean isWifiOnlySupported = true;
Sungmin Choi577add82015-12-23 14:24:52 +0900230 if (configManager != null) {
231 PersistableBundle b = configManager.getConfig();
232 if (b != null) {
233 mEditableWfcMode = b.getBoolean(CarrierConfigManager.KEY_EDITABLE_WFC_MODE_BOOL);
Meng Wangfe3749d2016-09-19 14:53:24 -0700234 mEditableWfcRoamingMode = b.getBoolean(
235 CarrierConfigManager.KEY_EDITABLE_WFC_ROAMING_MODE_BOOL);
Tyler Gunn2013ba82016-04-01 08:41:18 -0700236 isWifiOnlySupported = b.getBoolean(
237 CarrierConfigManager.KEY_CARRIER_WFC_SUPPORTS_WIFI_ONLY_BOOL, true);
Sungmin Choi577add82015-12-23 14:24:52 +0900238 }
239 }
Tyler Gunn2013ba82016-04-01 08:41:18 -0700240
241 if (!isWifiOnlySupported) {
242 mButtonWfcMode.setEntries(R.array.wifi_calling_mode_choices_without_wifi_only);
243 mButtonWfcMode.setEntryValues(R.array.wifi_calling_mode_values_without_wifi_only);
Meng Wangfe3749d2016-09-19 14:53:24 -0700244 mButtonWfcRoamingMode.setEntries(
245 R.array.wifi_calling_mode_choices_v2_without_wifi_only);
246 mButtonWfcRoamingMode.setEntryValues(
247 R.array.wifi_calling_mode_values_without_wifi_only);
Tyler Gunn2013ba82016-04-01 08:41:18 -0700248 }
Pavel Zhamaitsiak4ab32562015-02-23 15:34:07 -0800249 }
250
251 @Override
252 public void onResume() {
253 super.onResume();
254
255 final Context context = getActivity();
256
Pavel Zhamaitsiak4ab32562015-02-23 15:34:07 -0800257 // NOTE: Buttons will be enabled/disabled in mPhoneStateListener
258 boolean wfcEnabled = ImsManager.isWfcEnabledByUser(context)
259 && ImsManager.isNonTtyOrTtyOnVolteEnabled(context);
260 mSwitch.setChecked(wfcEnabled);
Meng Wangfe3749d2016-09-19 14:53:24 -0700261 int wfcMode = ImsManager.getWfcMode(context, false);
262 int wfcRoamingMode = ImsManager.getWfcMode(context, true);
Pavel Zhamaitsiak4ab32562015-02-23 15:34:07 -0800263 mButtonWfcMode.setValue(Integer.toString(wfcMode));
Meng Wangfe3749d2016-09-19 14:53:24 -0700264 mButtonWfcRoamingMode.setValue(Integer.toString(wfcRoamingMode));
265 updateButtonWfcMode(context, wfcEnabled, wfcMode, wfcRoamingMode);
Pavel Zhamaitsiak4ab32562015-02-23 15:34:07 -0800266
Meng Wangd3b0c372017-02-23 14:33:49 -0800267 if (ImsManager.isWfcEnabledByPlatform(context)) {
268 TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
269 tm.listen(mPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
270
271 mSwitchBar.addOnSwitchChangeListener(this);
272
273 mValidListener = true;
274 }
275
Pavel Zhamaitsiak897b4922015-03-06 16:02:53 -0800276 context.registerReceiver(mIntentReceiver, mIntentFilter);
277
278 Intent intent = getActivity().getIntent();
Amit Mahajanb9221862015-12-14 13:23:29 -0800279 if (intent.getBooleanExtra(Phone.EXTRA_KEY_ALERT_SHOW, false)) {
Pavel Zhamaitsiak897b4922015-03-06 16:02:53 -0800280 showAlert(intent);
281 }
Pavel Zhamaitsiak4ab32562015-02-23 15:34:07 -0800282 }
283
284 @Override
285 public void onPause() {
286 super.onPause();
287
Pavel Zhamaitsiak897b4922015-03-06 16:02:53 -0800288 final Context context = getActivity();
289
Pavel Zhamaitsiak2fe4b3c2015-08-07 12:02:38 -0700290 if (mValidListener) {
291 mValidListener = false;
292
Pavel Zhamaitsiak4ab32562015-02-23 15:34:07 -0800293 TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
294 tm.listen(mPhoneStateListener, PhoneStateListener.LISTEN_NONE);
295
296 mSwitchBar.removeOnSwitchChangeListener(this);
297 }
Pavel Zhamaitsiak897b4922015-03-06 16:02:53 -0800298
299 context.unregisterReceiver(mIntentReceiver);
Pavel Zhamaitsiak4ab32562015-02-23 15:34:07 -0800300 }
301
302 /**
303 * Listens to the state change of the switch.
304 */
305 @Override
306 public void onSwitchChanged(Switch switchView, boolean isChecked) {
307 final Context context = getActivity();
Meng Wang90e8e472016-05-25 17:12:27 -0700308 Log.d(TAG, "onSwitchChanged(" + isChecked + ")");
Pavel Zhamaitsiak4ab32562015-02-23 15:34:07 -0800309
Meng Wang90e8e472016-05-25 17:12:27 -0700310 if (!isChecked) {
311 updateWfcMode(context, false);
312 return;
313 }
314
315 // Call address management activity before turning on WFC
316 Intent carrierAppIntent = getCarrierActivityIntent(context);
317 if (carrierAppIntent != null) {
Meng Wang0dc43112016-06-02 17:06:22 -0700318 carrierAppIntent.putExtra(EXTRA_LAUNCH_CARRIER_APP, LAUCH_APP_ACTIVATE);
Meng Wang90e8e472016-05-25 17:12:27 -0700319 startActivityForResult(carrierAppIntent, REQUEST_CHECK_WFC_EMERGENCY_ADDRESS);
320 } else {
321 updateWfcMode(context, true);
322 }
323 }
324
325 /*
326 * Get the Intent to launch carrier emergency address management activity.
327 * Return null when no activity found.
328 */
329 private static Intent getCarrierActivityIntent(Context context) {
330 // Retrive component name from carrirt config
331 CarrierConfigManager configManager = context.getSystemService(CarrierConfigManager.class);
332 if (configManager == null) return null;
333
334 PersistableBundle bundle = configManager.getConfig();
335 if (bundle == null) return null;
336
337 String carrierApp = bundle.getString(
338 CarrierConfigManager.KEY_WFC_EMERGENCY_ADDRESS_CARRIER_APP_STRING);
339 if (TextUtils.isEmpty(carrierApp)) return null;
340
341 ComponentName componentName = ComponentName.unflattenFromString(carrierApp);
342 if (componentName == null) return null;
343
344 // Build and return intent
345 Intent intent = new Intent();
346 intent.setComponent(componentName);
347 return intent;
348 }
349
350 /*
351 * Turn on/off WFC mode with ImsManager and update UI accordingly
352 */
353 private void updateWfcMode(Context context, boolean wfcEnabled) {
354 Log.i(TAG, "updateWfcMode(" + wfcEnabled + ")");
355 ImsManager.setWfcSetting(context, wfcEnabled);
Pavel Zhamaitsiak4ab32562015-02-23 15:34:07 -0800356
Meng Wangfe3749d2016-09-19 14:53:24 -0700357 int wfcMode = ImsManager.getWfcMode(context, false);
358 int wfcRoamingMode = ImsManager.getWfcMode(context, true);
359 updateButtonWfcMode(context, wfcEnabled, wfcMode, wfcRoamingMode);
Meng Wang90e8e472016-05-25 17:12:27 -0700360 if (wfcEnabled) {
Fan Zhangaa71afe2016-09-22 10:43:12 -0700361 mMetricsFeatureProvider.action(getActivity(), getMetricsCategory(), wfcMode);
Pavel Zhamaitsiak5a6d17c2015-06-20 15:48:00 -0700362 } else {
Fan Zhangaa71afe2016-09-22 10:43:12 -0700363 mMetricsFeatureProvider.action(getActivity(), getMetricsCategory(), -1);
Pavel Zhamaitsiak5a6d17c2015-06-20 15:48:00 -0700364 }
Pavel Zhamaitsiaka4314a52015-04-15 16:12:55 -0700365 }
366
Meng Wang90e8e472016-05-25 17:12:27 -0700367 @Override
368 public void onActivityResult(int requestCode, int resultCode, Intent data) {
369 super.onActivityResult(requestCode, resultCode, data);
370
371 final Context context = getActivity();
372
373 if (requestCode == REQUEST_CHECK_WFC_EMERGENCY_ADDRESS) {
374 Log.d(TAG, "WFC emergency address activity result = " + resultCode);
375
376 if (resultCode == Activity.RESULT_OK) {
377 updateWfcMode(context, true);
378 }
379 }
380 }
381
Meng Wangfe3749d2016-09-19 14:53:24 -0700382 private void updateButtonWfcMode(Context context, boolean wfcEnabled,
383 int wfcMode, int wfcRoamingMode) {
Pavel Zhamaitsiak248b8fd2015-03-09 16:58:39 -0700384 mButtonWfcMode.setSummary(getWfcModeSummary(context, wfcMode));
Meng Wangb97b16d2016-06-27 16:26:33 -0700385 mButtonWfcMode.setEnabled(wfcEnabled && mEditableWfcMode);
Meng Wangfe3749d2016-09-19 14:53:24 -0700386 // mButtonWfcRoamingMode.setSummary is not needed; summary is just selected value.
387 mButtonWfcRoamingMode.setEnabled(wfcEnabled && mEditableWfcRoamingMode);
Pavel Zhamaitsiakee5f27d2015-03-23 14:43:24 -0700388
389 final PreferenceScreen preferenceScreen = getPreferenceScreen();
Meng Wang184753f2016-06-15 18:17:06 -0700390 boolean updateAddressEnabled = (getCarrierActivityIntent(context) != null);
Pavel Zhamaitsiaka4314a52015-04-15 16:12:55 -0700391 if (wfcEnabled) {
Meng Wang4436d6f2016-08-18 12:19:01 -0700392 if (mEditableWfcMode) {
393 preferenceScreen.addPreference(mButtonWfcMode);
394 } else {
Meng Wangfe3749d2016-09-19 14:53:24 -0700395 // Don't show WFC (home) preference if it's not editable.
Meng Wang4436d6f2016-08-18 12:19:01 -0700396 preferenceScreen.removePreference(mButtonWfcMode);
397 }
Meng Wangfe3749d2016-09-19 14:53:24 -0700398 if (mEditableWfcRoamingMode) {
399 preferenceScreen.addPreference(mButtonWfcRoamingMode);
400 } else {
401 // Don't show WFC roaming preference if it's not editable.
402 preferenceScreen.removePreference(mButtonWfcRoamingMode);
403 }
Meng Wang184753f2016-06-15 18:17:06 -0700404 if (updateAddressEnabled) {
405 preferenceScreen.addPreference(mUpdateAddress);
Meng Wang438b2912016-06-22 10:26:40 -0700406 } else {
407 preferenceScreen.removePreference(mUpdateAddress);
Meng Wang184753f2016-06-15 18:17:06 -0700408 }
Pavel Zhamaitsiakee5f27d2015-03-23 14:43:24 -0700409 } else {
410 preferenceScreen.removePreference(mButtonWfcMode);
Meng Wangfe3749d2016-09-19 14:53:24 -0700411 preferenceScreen.removePreference(mButtonWfcRoamingMode);
Meng Wang0dc43112016-06-02 17:06:22 -0700412 preferenceScreen.removePreference(mUpdateAddress);
Pavel Zhamaitsiakee5f27d2015-03-23 14:43:24 -0700413 }
Pavel Zhamaitsiak4ab32562015-02-23 15:34:07 -0800414 }
415
416 @Override
417 public boolean onPreferenceChange(Preference preference, Object newValue) {
418 final Context context = getActivity();
419 if (preference == mButtonWfcMode) {
420 mButtonWfcMode.setValue((String) newValue);
421 int buttonMode = Integer.valueOf((String) newValue);
Meng Wangfe3749d2016-09-19 14:53:24 -0700422 int currentWfcMode = ImsManager.getWfcMode(context, false);
423 if (buttonMode != currentWfcMode) {
424 ImsManager.setWfcMode(context, buttonMode, false);
Pavel Zhamaitsiak4ab32562015-02-23 15:34:07 -0800425 mButtonWfcMode.setSummary(getWfcModeSummary(context, buttonMode));
Fan Zhangaa71afe2016-09-22 10:43:12 -0700426 mMetricsFeatureProvider.action(getActivity(), getMetricsCategory(), buttonMode);
Pavel Zhamaitsiak4ab32562015-02-23 15:34:07 -0800427 }
Meng Wangfe3749d2016-09-19 14:53:24 -0700428 if (!mEditableWfcRoamingMode) {
429 int currentWfcRoamingMode = ImsManager.getWfcMode(context, true);
430 if (buttonMode != currentWfcRoamingMode) {
431 ImsManager.setWfcMode(context, buttonMode, true);
432 // mButtonWfcRoamingMode.setSummary is not needed; summary is selected value
433 }
434 }
435 } else if (preference == mButtonWfcRoamingMode) {
436 mButtonWfcRoamingMode.setValue((String) newValue);
437 int buttonMode = Integer.valueOf((String) newValue);
438 int currentMode = ImsManager.getWfcMode(context, true);
439 if (buttonMode != currentMode) {
440 ImsManager.setWfcMode(context, buttonMode, true);
441 // mButtonWfcRoamingMode.setSummary is not needed; summary is just selected value.
Meng Wangee54eb82016-09-29 11:36:11 -0700442 mMetricsFeatureProvider.action(getActivity(), getMetricsCategory(), buttonMode);
Meng Wangfe3749d2016-09-19 14:53:24 -0700443 }
Pavel Zhamaitsiak4ab32562015-02-23 15:34:07 -0800444 }
445 return true;
446 }
447
Fan Zhangf1acdd32016-10-13 09:53:38 -0700448 public static int getWfcModeSummary(Context context, int wfcMode) {
Pavel Zhamaitsiakee5f27d2015-03-23 14:43:24 -0700449 int resId = com.android.internal.R.string.wifi_calling_off_summary;
Pavel Zhamaitsiak4ab32562015-02-23 15:34:07 -0800450 if (ImsManager.isWfcEnabledByUser(context)) {
451 switch (wfcMode) {
452 case ImsConfig.WfcModeFeatureValueConstants.WIFI_ONLY:
Pavel Zhamaitsiakee5f27d2015-03-23 14:43:24 -0700453 resId = com.android.internal.R.string.wfc_mode_wifi_only_summary;
Pavel Zhamaitsiak4ab32562015-02-23 15:34:07 -0800454 break;
455 case ImsConfig.WfcModeFeatureValueConstants.CELLULAR_PREFERRED:
Pavel Zhamaitsiakee5f27d2015-03-23 14:43:24 -0700456 resId = com.android.internal.R.string.wfc_mode_cellular_preferred_summary;
Pavel Zhamaitsiak4ab32562015-02-23 15:34:07 -0800457 break;
458 case ImsConfig.WfcModeFeatureValueConstants.WIFI_PREFERRED:
Pavel Zhamaitsiakee5f27d2015-03-23 14:43:24 -0700459 resId = com.android.internal.R.string.wfc_mode_wifi_preferred_summary;
Pavel Zhamaitsiak4ab32562015-02-23 15:34:07 -0800460 break;
461 default:
462 Log.e(TAG, "Unexpected WFC mode value: " + wfcMode);
463 }
464 }
465 return resId;
466 }
467}