1 /* 2 * Copyright (C) 2008 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 17 package com.android.settings; 18 19 import android.app.Activity; 20 import android.app.Dialog; 21 import android.app.ProgressDialog; 22 import android.content.BroadcastReceiver; 23 import android.content.ContentResolver; 24 import android.content.ContentUris; 25 import android.content.ContentValues; 26 import android.content.Context; 27 import android.content.Intent; 28 import android.content.IntentFilter; 29 import android.database.Cursor; 30 import android.net.Uri; 31 import android.os.Bundle; 32 import android.os.Handler; 33 import android.os.HandlerThread; 34 import android.os.Looper; 35 import android.os.Message; 36 import android.os.PersistableBundle; 37 import android.os.UserHandle; 38 import android.os.UserManager; 39 import android.provider.Telephony; 40 import android.support.v7.preference.Preference; 41 import android.support.v7.preference.PreferenceGroup; 42 import android.support.v7.preference.PreferenceScreen; 43 import android.telephony.CarrierConfigManager; 44 import android.telephony.SubscriptionInfo; 45 import android.telephony.SubscriptionManager; 46 import android.telephony.TelephonyManager; 47 import android.text.TextUtils; 48 import android.util.Log; 49 import android.view.Menu; 50 import android.view.MenuInflater; 51 import android.view.MenuItem; 52 import android.view.MotionEvent; 53 import android.widget.TextView; 54 import android.widget.Toast; 55 56 import com.android.internal.logging.nano.MetricsProto.MetricsEvent; 57 import com.android.internal.telephony.PhoneConstants; 58 import com.android.internal.telephony.TelephonyIntents; 59 import com.android.internal.telephony.dataconnection.ApnSetting; 60 import com.android.internal.telephony.uicc.IccRecords; 61 import com.android.internal.telephony.uicc.UiccController; 62 import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin; 63 64 import java.util.ArrayList; 65 66 public class ApnSettings extends RestrictedSettingsFragment implements 67 Preference.OnPreferenceChangeListener { 68 static final String TAG = "ApnSettings"; 69 70 public static final String EXTRA_POSITION = "position"; 71 public static final String RESTORE_CARRIERS_URI = 72 "content://telephony/carriers/restore"; 73 public static final String PREFERRED_APN_URI = 74 "content://telephony/carriers/preferapn"; 75 76 public static final String APN_ID = "apn_id"; 77 public static final String SUB_ID = "sub_id"; 78 public static final String MVNO_TYPE = "mvno_type"; 79 public static final String MVNO_MATCH_DATA = "mvno_match_data"; 80 81 private static final int ID_INDEX = 0; 82 private static final int NAME_INDEX = 1; 83 private static final int APN_INDEX = 2; 84 private static final int TYPES_INDEX = 3; 85 private static final int MVNO_TYPE_INDEX = 4; 86 private static final int MVNO_MATCH_DATA_INDEX = 5; 87 88 private static final int MENU_NEW = Menu.FIRST; 89 private static final int MENU_RESTORE = Menu.FIRST + 1; 90 91 private static final int EVENT_RESTORE_DEFAULTAPN_START = 1; 92 private static final int EVENT_RESTORE_DEFAULTAPN_COMPLETE = 2; 93 94 private static final int DIALOG_RESTORE_DEFAULTAPN = 1001; 95 96 private static final Uri DEFAULTAPN_URI = Uri.parse(RESTORE_CARRIERS_URI); 97 private static final Uri PREFERAPN_URI = Uri.parse(PREFERRED_APN_URI); 98 99 private static boolean mRestoreDefaultApnMode; 100 101 private UserManager mUserManager; 102 private RestoreApnUiHandler mRestoreApnUiHandler; 103 private RestoreApnProcessHandler mRestoreApnProcessHandler; 104 private HandlerThread mRestoreDefaultApnThread; 105 private SubscriptionInfo mSubscriptionInfo; 106 private UiccController mUiccController; 107 private String mMvnoType; 108 private String mMvnoMatchData; 109 110 private String mSelectedKey; 111 112 private IntentFilter mMobileStateFilter; 113 114 private boolean mUnavailable; 115 116 private boolean mHideImsApn; 117 private boolean mAllowAddingApns; 118 119 public ApnSettings() { 120 super(UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS); 121 } 122 123 private final BroadcastReceiver mMobileStateReceiver = new BroadcastReceiver() { 124 @Override 125 public void onReceive(Context context, Intent intent) { 126 if (intent.getAction().equals( 127 TelephonyIntents.ACTION_ANY_DATA_CONNECTION_STATE_CHANGED)) { 128 PhoneConstants.DataState state = getMobileDataState(intent); 129 switch (state) { 130 case CONNECTED: 131 if (!mRestoreDefaultApnMode) { 132 fillList(); 133 } else { 134 showDialog(DIALOG_RESTORE_DEFAULTAPN); 135 } 136 break; 137 } 138 } 139 } 140 }; 141 142 private static PhoneConstants.DataState getMobileDataState(Intent intent) { 143 String str = intent.getStringExtra(PhoneConstants.STATE_KEY); 144 if (str != null) { 145 return Enum.valueOf(PhoneConstants.DataState.class, str); 146 } else { 147 return PhoneConstants.DataState.DISCONNECTED; 148 } 149 } 150 151 @Override 152 public int getMetricsCategory() { 153 return MetricsEvent.APN; 154 } 155 156 @Override 157 public void onCreate(Bundle icicle) { 158 super.onCreate(icicle); 159 final Activity activity = getActivity(); 160 final int subId = activity.getIntent().getIntExtra(SUB_ID, 161 SubscriptionManager.INVALID_SUBSCRIPTION_ID); 162 163 mMobileStateFilter = new IntentFilter( 164 TelephonyIntents.ACTION_ANY_DATA_CONNECTION_STATE_CHANGED); 165 166 setIfOnlyAvailableForAdmins(true); 167 168 mSubscriptionInfo = SubscriptionManager.from(activity).getActiveSubscriptionInfo(subId); 169 mUiccController = UiccController.getInstance(); 170 171 CarrierConfigManager configManager = (CarrierConfigManager) 172 getSystemService(Context.CARRIER_CONFIG_SERVICE); 173 PersistableBundle b = configManager.getConfig(); 174 mHideImsApn = b.getBoolean(CarrierConfigManager.KEY_HIDE_IMS_APN_BOOL); 175 mAllowAddingApns = b.getBoolean(CarrierConfigManager.KEY_ALLOW_ADDING_APNS_BOOL); 176 if (mAllowAddingApns) { 177 String[] readOnlyApnTypes = b.getStringArray( 178 CarrierConfigManager.KEY_READ_ONLY_APN_TYPES_STRING_ARRAY); 179 // if no apn type can be edited, do not allow adding APNs 180 if (ApnEditor.hasAllApns(readOnlyApnTypes)) { 181 Log.d(TAG, "not allowing adding APN because all APN types are read only"); 182 mAllowAddingApns = false; 183 } 184 } 185 mUserManager = UserManager.get(activity); 186 } 187 188 @Override 189 public void onActivityCreated(Bundle savedInstanceState) { 190 super.onActivityCreated(savedInstanceState); 191 192 getEmptyTextView().setText(R.string.apn_settings_not_available); 193 mUnavailable = isUiRestricted(); 194 setHasOptionsMenu(!mUnavailable); 195 if (mUnavailable) { 196 addPreferencesFromResource(R.xml.placeholder_prefs); 197 return; 198 } 199 200 addPreferencesFromResource(R.xml.apn_settings); 201 } 202 203 @Override 204 public void onResume() { 205 super.onResume(); 206 207 if (mUnavailable) { 208 return; 209 } 210 211 getActivity().registerReceiver(mMobileStateReceiver, mMobileStateFilter); 212 213 if (!mRestoreDefaultApnMode) { 214 fillList(); 215 } 216 } 217 218 @Override 219 public void onPause() { 220 super.onPause(); 221 222 if (mUnavailable) { 223 return; 224 } 225 226 getActivity().unregisterReceiver(mMobileStateReceiver); 227 } 228 229 @Override 230 public void onDestroy() { 231 super.onDestroy(); 232 233 if (mRestoreDefaultApnThread != null) { 234 mRestoreDefaultApnThread.quit(); 235 } 236 } 237 238 @Override 239 public EnforcedAdmin getRestrictionEnforcedAdmin() { 240 final UserHandle user = UserHandle.of(mUserManager.getUserHandle()); 241 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS, user) 242 && !mUserManager.hasBaseUserRestriction(UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS, 243 user)) { 244 return EnforcedAdmin.MULTIPLE_ENFORCED_ADMIN; 245 } 246 return null; 247 } 248 249 private void fillList() { 250 final TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); 251 final int subId = mSubscriptionInfo != null ? mSubscriptionInfo.getSubscriptionId() 252 : SubscriptionManager.INVALID_SUBSCRIPTION_ID; 253 final String mccmnc = mSubscriptionInfo == null ? "" : tm.getSimOperator(subId); 254 Log.d(TAG, "mccmnc = " + mccmnc); 255 StringBuilder where = new StringBuilder("numeric=\"" + mccmnc + 256 "\" AND NOT (type='ia' AND (apn=\"\" OR apn IS NULL)) AND user_visible!=0"); 257 258 if (mHideImsApn) { 259 where.append(" AND NOT (type='ims')"); 260 } 261 262 Cursor cursor = getContentResolver().query(Telephony.Carriers.CONTENT_URI, new String[] { 263 "_id", "name", "apn", "type", "mvno_type", "mvno_match_data"}, where.toString(), 264 null, Telephony.Carriers.DEFAULT_SORT_ORDER); 265 266 if (cursor != null) { 267 IccRecords r = null; 268 if (mUiccController != null && mSubscriptionInfo != null) { 269 r = mUiccController.getIccRecords( 270 SubscriptionManager.getPhoneId(subId), UiccController.APP_FAM_3GPP); 271 } 272 PreferenceGroup apnList = (PreferenceGroup) findPreference("apn_list"); 273 apnList.removeAll(); 274 275 ArrayList<ApnPreference> mnoApnList = new ArrayList<ApnPreference>(); 276 ArrayList<ApnPreference> mvnoApnList = new ArrayList<ApnPreference>(); 277 ArrayList<ApnPreference> mnoMmsApnList = new ArrayList<ApnPreference>(); 278 ArrayList<ApnPreference> mvnoMmsApnList = new ArrayList<ApnPreference>(); 279 280 mSelectedKey = getSelectedApnKey(); 281 cursor.moveToFirst(); 282 while (!cursor.isAfterLast()) { 283 String name = cursor.getString(NAME_INDEX); 284 String apn = cursor.getString(APN_INDEX); 285 String key = cursor.getString(ID_INDEX); 286 String type = cursor.getString(TYPES_INDEX); 287 String mvnoType = cursor.getString(MVNO_TYPE_INDEX); 288 String mvnoMatchData = cursor.getString(MVNO_MATCH_DATA_INDEX); 289 290 ApnPreference pref = new ApnPreference(getPrefContext()); 291 292 pref.setKey(key); 293 pref.setTitle(name); 294 pref.setSummary(apn); 295 pref.setPersistent(false); 296 pref.setOnPreferenceChangeListener(this); 297 pref.setSubId(subId); 298 299 boolean selectable = ((type == null) || !type.equals("mms")); 300 pref.setSelectable(selectable); 301 if (selectable) { 302 if ((mSelectedKey != null) && mSelectedKey.equals(key)) { 303 pref.setChecked(); 304 } 305 addApnToList(pref, mnoApnList, mvnoApnList, r, mvnoType, mvnoMatchData); 306 } else { 307 addApnToList(pref, mnoMmsApnList, mvnoMmsApnList, r, mvnoType, mvnoMatchData); 308 } 309 cursor.moveToNext(); 310 } 311 cursor.close(); 312 313 if (!mvnoApnList.isEmpty()) { 314 mnoApnList = mvnoApnList; 315 mnoMmsApnList = mvnoMmsApnList; 316 317 // Also save the mvno info 318 } 319 320 for (Preference preference : mnoApnList) { 321 apnList.addPreference(preference); 322 } 323 for (Preference preference : mnoMmsApnList) { 324 apnList.addPreference(preference); 325 } 326 } 327 } 328 329 private void addApnToList(ApnPreference pref, ArrayList<ApnPreference> mnoList, 330 ArrayList<ApnPreference> mvnoList, IccRecords r, String mvnoType, 331 String mvnoMatchData) { 332 if (r != null && !TextUtils.isEmpty(mvnoType) && !TextUtils.isEmpty(mvnoMatchData)) { 333 if (ApnSetting.mvnoMatches(r, mvnoType, mvnoMatchData)) { 334 mvnoList.add(pref); 335 // Since adding to mvno list, save mvno info 336 mMvnoType = mvnoType; 337 mMvnoMatchData = mvnoMatchData; 338 } 339 } else { 340 mnoList.add(pref); 341 } 342 } 343 344 @Override 345 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 346 if (!mUnavailable) { 347 if (mAllowAddingApns) { 348 menu.add(0, MENU_NEW, 0, 349 getResources().getString(R.string.menu_new)) 350 .setIcon(R.drawable.ic_menu_add_white) 351 .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); 352 } 353 menu.add(0, MENU_RESTORE, 0, 354 getResources().getString(R.string.menu_restore)) 355 .setIcon(android.R.drawable.ic_menu_upload); 356 } 357 358 super.onCreateOptionsMenu(menu, inflater); 359 } 360 361 @Override 362 public boolean onOptionsItemSelected(MenuItem item) { 363 switch (item.getItemId()) { 364 case MENU_NEW: 365 addNewApn(); 366 return true; 367 368 case MENU_RESTORE: 369 restoreDefaultApn(); 370 return true; 371 } 372 return super.onOptionsItemSelected(item); 373 } 374 375 private void addNewApn() { 376 Intent intent = new Intent(Intent.ACTION_INSERT, Telephony.Carriers.CONTENT_URI); 377 int subId = mSubscriptionInfo != null ? mSubscriptionInfo.getSubscriptionId() 378 : SubscriptionManager.INVALID_SUBSCRIPTION_ID; 379 intent.putExtra(SUB_ID, subId); 380 if (!TextUtils.isEmpty(mMvnoType) && !TextUtils.isEmpty(mMvnoMatchData)) { 381 intent.putExtra(MVNO_TYPE, mMvnoType); 382 intent.putExtra(MVNO_MATCH_DATA, mMvnoMatchData); 383 } 384 startActivity(intent); 385 } 386 387 @Override 388 public boolean onPreferenceTreeClick(Preference preference) { 389 int pos = Integer.parseInt(preference.getKey()); 390 Uri url = ContentUris.withAppendedId(Telephony.Carriers.CONTENT_URI, pos); 391 startActivity(new Intent(Intent.ACTION_EDIT, url)); 392 return true; 393 } 394 395 public boolean onPreferenceChange(Preference preference, Object newValue) { 396 Log.d(TAG, "onPreferenceChange(): Preference - " + preference 397 + ", newValue - " + newValue + ", newValue type - " 398 + newValue.getClass()); 399 if (newValue instanceof String) { 400 setSelectedApnKey((String) newValue); 401 } 402 403 return true; 404 } 405 406 private void setSelectedApnKey(String key) { 407 mSelectedKey = key; 408 ContentResolver resolver = getContentResolver(); 409 410 ContentValues values = new ContentValues(); 411 values.put(APN_ID, mSelectedKey); 412 resolver.update(getUriForCurrSubId(PREFERAPN_URI), values, null, null); 413 } 414 415 private String getSelectedApnKey() { 416 String key = null; 417 418 Cursor cursor = getContentResolver().query(getUriForCurrSubId(PREFERAPN_URI), 419 new String[] {"_id"}, null, null, Telephony.Carriers.DEFAULT_SORT_ORDER); 420 if (cursor.getCount() > 0) { 421 cursor.moveToFirst(); 422 key = cursor.getString(ID_INDEX); 423 } 424 cursor.close(); 425 return key; 426 } 427 428 private boolean restoreDefaultApn() { 429 showDialog(DIALOG_RESTORE_DEFAULTAPN); 430 mRestoreDefaultApnMode = true; 431 432 if (mRestoreApnUiHandler == null) { 433 mRestoreApnUiHandler = new RestoreApnUiHandler(); 434 } 435 436 if (mRestoreApnProcessHandler == null || 437 mRestoreDefaultApnThread == null) { 438 mRestoreDefaultApnThread = new HandlerThread( 439 "Restore default APN Handler: Process Thread"); 440 mRestoreDefaultApnThread.start(); 441 mRestoreApnProcessHandler = new RestoreApnProcessHandler( 442 mRestoreDefaultApnThread.getLooper(), mRestoreApnUiHandler); 443 } 444 445 mRestoreApnProcessHandler 446 .sendEmptyMessage(EVENT_RESTORE_DEFAULTAPN_START); 447 return true; 448 } 449 450 // Append subId to the Uri 451 private Uri getUriForCurrSubId(Uri uri) { 452 int subId = mSubscriptionInfo != null ? mSubscriptionInfo.getSubscriptionId() 453 : SubscriptionManager.INVALID_SUBSCRIPTION_ID; 454 if (SubscriptionManager.isValidSubscriptionId(subId)) { 455 return Uri.withAppendedPath(uri, "subId/" + String.valueOf(subId)); 456 } else { 457 return uri; 458 } 459 } 460 461 private class RestoreApnUiHandler extends Handler { 462 @Override 463 public void handleMessage(Message msg) { 464 switch (msg.what) { 465 case EVENT_RESTORE_DEFAULTAPN_COMPLETE: 466 Activity activity = getActivity(); 467 if (activity == null) { 468 mRestoreDefaultApnMode = false; 469 return; 470 } 471 fillList(); 472 getPreferenceScreen().setEnabled(true); 473 mRestoreDefaultApnMode = false; 474 removeDialog(DIALOG_RESTORE_DEFAULTAPN); 475 Toast.makeText( 476 activity, 477 getResources().getString( 478 R.string.restore_default_apn_completed), 479 Toast.LENGTH_LONG).show(); 480 break; 481 } 482 } 483 } 484 485 private class RestoreApnProcessHandler extends Handler { 486 private Handler mRestoreApnUiHandler; 487 488 public RestoreApnProcessHandler(Looper looper, Handler restoreApnUiHandler) { 489 super(looper); 490 this.mRestoreApnUiHandler = restoreApnUiHandler; 491 } 492 493 @Override 494 public void handleMessage(Message msg) { 495 switch (msg.what) { 496 case EVENT_RESTORE_DEFAULTAPN_START: 497 ContentResolver resolver = getContentResolver(); 498 resolver.delete(getUriForCurrSubId(DEFAULTAPN_URI), null, null); 499 mRestoreApnUiHandler 500 .sendEmptyMessage(EVENT_RESTORE_DEFAULTAPN_COMPLETE); 501 break; 502 } 503 } 504 } 505 506 @Override 507 public Dialog onCreateDialog(int id) { 508 if (id == DIALOG_RESTORE_DEFAULTAPN) { 509 ProgressDialog dialog = new ProgressDialog(getActivity()) { 510 public boolean onTouchEvent(MotionEvent event) { 511 return true; 512 } 513 }; 514 dialog.setMessage(getResources().getString(R.string.restore_default_apn)); 515 dialog.setCancelable(false); 516 return dialog; 517 } 518 return null; 519 } 520 521 @Override 522 public int getDialogMetricsCategory(int dialogId) { 523 if (dialogId == DIALOG_RESTORE_DEFAULTAPN) { 524 return MetricsEvent.DIALOG_APN_RESTORE_DEFAULT; 525 } 526 return 0; 527 } 528 } 529