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 ApnSettings()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 getMobileDataState(Intent intent)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 getMetricsCategory()152 public int getMetricsCategory() { 153 return MetricsEvent.APN; 154 } 155 156 @Override onCreate(Bundle icicle)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 mUserManager = UserManager.get(activity); 177 } 178 179 @Override onActivityCreated(Bundle savedInstanceState)180 public void onActivityCreated(Bundle savedInstanceState) { 181 super.onActivityCreated(savedInstanceState); 182 183 getEmptyTextView().setText(R.string.apn_settings_not_available); 184 mUnavailable = isUiRestricted(); 185 setHasOptionsMenu(!mUnavailable); 186 if (mUnavailable) { 187 addPreferencesFromResource(R.xml.empty_settings); 188 return; 189 } 190 191 addPreferencesFromResource(R.xml.apn_settings); 192 } 193 194 @Override onResume()195 public void onResume() { 196 super.onResume(); 197 198 if (mUnavailable) { 199 return; 200 } 201 202 getActivity().registerReceiver(mMobileStateReceiver, mMobileStateFilter); 203 204 if (!mRestoreDefaultApnMode) { 205 fillList(); 206 } 207 } 208 209 @Override onPause()210 public void onPause() { 211 super.onPause(); 212 213 if (mUnavailable) { 214 return; 215 } 216 217 getActivity().unregisterReceiver(mMobileStateReceiver); 218 } 219 220 @Override onDestroy()221 public void onDestroy() { 222 super.onDestroy(); 223 224 if (mRestoreDefaultApnThread != null) { 225 mRestoreDefaultApnThread.quit(); 226 } 227 } 228 229 @Override getRestrictionEnforcedAdmin()230 public EnforcedAdmin getRestrictionEnforcedAdmin() { 231 final UserHandle user = UserHandle.of(mUserManager.getUserHandle()); 232 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS, user) 233 && !mUserManager.hasBaseUserRestriction(UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS, 234 user)) { 235 return EnforcedAdmin.MULTIPLE_ENFORCED_ADMIN; 236 } 237 return null; 238 } 239 fillList()240 private void fillList() { 241 final TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); 242 final int subId = mSubscriptionInfo != null ? mSubscriptionInfo.getSubscriptionId() 243 : SubscriptionManager.INVALID_SUBSCRIPTION_ID; 244 final String mccmnc = mSubscriptionInfo == null ? "" : tm.getSimOperator(subId); 245 Log.d(TAG, "mccmnc = " + mccmnc); 246 StringBuilder where = new StringBuilder("numeric=\"" + mccmnc + 247 "\" AND NOT (type='ia' AND (apn=\"\" OR apn IS NULL)) AND user_visible!=0"); 248 249 if (mHideImsApn) { 250 where.append(" AND NOT (type='ims')"); 251 } 252 253 Cursor cursor = getContentResolver().query(Telephony.Carriers.CONTENT_URI, new String[] { 254 "_id", "name", "apn", "type", "mvno_type", "mvno_match_data"}, where.toString(), 255 null, Telephony.Carriers.DEFAULT_SORT_ORDER); 256 257 if (cursor != null) { 258 IccRecords r = null; 259 if (mUiccController != null && mSubscriptionInfo != null) { 260 r = mUiccController.getIccRecords( 261 SubscriptionManager.getPhoneId(subId), UiccController.APP_FAM_3GPP); 262 } 263 PreferenceGroup apnList = (PreferenceGroup) findPreference("apn_list"); 264 apnList.removeAll(); 265 266 ArrayList<ApnPreference> mnoApnList = new ArrayList<ApnPreference>(); 267 ArrayList<ApnPreference> mvnoApnList = new ArrayList<ApnPreference>(); 268 ArrayList<ApnPreference> mnoMmsApnList = new ArrayList<ApnPreference>(); 269 ArrayList<ApnPreference> mvnoMmsApnList = new ArrayList<ApnPreference>(); 270 271 mSelectedKey = getSelectedApnKey(); 272 cursor.moveToFirst(); 273 while (!cursor.isAfterLast()) { 274 String name = cursor.getString(NAME_INDEX); 275 String apn = cursor.getString(APN_INDEX); 276 String key = cursor.getString(ID_INDEX); 277 String type = cursor.getString(TYPES_INDEX); 278 String mvnoType = cursor.getString(MVNO_TYPE_INDEX); 279 String mvnoMatchData = cursor.getString(MVNO_MATCH_DATA_INDEX); 280 281 ApnPreference pref = new ApnPreference(getPrefContext()); 282 283 pref.setKey(key); 284 pref.setTitle(name); 285 pref.setSummary(apn); 286 pref.setPersistent(false); 287 pref.setOnPreferenceChangeListener(this); 288 pref.setSubId(subId); 289 290 boolean selectable = ((type == null) || !type.equals("mms")); 291 pref.setSelectable(selectable); 292 if (selectable) { 293 if ((mSelectedKey != null) && mSelectedKey.equals(key)) { 294 pref.setChecked(); 295 } 296 addApnToList(pref, mnoApnList, mvnoApnList, r, mvnoType, mvnoMatchData); 297 } else { 298 addApnToList(pref, mnoMmsApnList, mvnoMmsApnList, r, mvnoType, mvnoMatchData); 299 } 300 cursor.moveToNext(); 301 } 302 cursor.close(); 303 304 if (!mvnoApnList.isEmpty()) { 305 mnoApnList = mvnoApnList; 306 mnoMmsApnList = mvnoMmsApnList; 307 308 // Also save the mvno info 309 } 310 311 for (Preference preference : mnoApnList) { 312 apnList.addPreference(preference); 313 } 314 for (Preference preference : mnoMmsApnList) { 315 apnList.addPreference(preference); 316 } 317 } 318 } 319 addApnToList(ApnPreference pref, ArrayList<ApnPreference> mnoList, ArrayList<ApnPreference> mvnoList, IccRecords r, String mvnoType, String mvnoMatchData)320 private void addApnToList(ApnPreference pref, ArrayList<ApnPreference> mnoList, 321 ArrayList<ApnPreference> mvnoList, IccRecords r, String mvnoType, 322 String mvnoMatchData) { 323 if (r != null && !TextUtils.isEmpty(mvnoType) && !TextUtils.isEmpty(mvnoMatchData)) { 324 if (ApnSetting.mvnoMatches(r, mvnoType, mvnoMatchData)) { 325 mvnoList.add(pref); 326 // Since adding to mvno list, save mvno info 327 mMvnoType = mvnoType; 328 mMvnoMatchData = mvnoMatchData; 329 } 330 } else { 331 mnoList.add(pref); 332 } 333 } 334 335 @Override onCreateOptionsMenu(Menu menu, MenuInflater inflater)336 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 337 if (!mUnavailable) { 338 if (mAllowAddingApns) { 339 menu.add(0, MENU_NEW, 0, 340 getResources().getString(R.string.menu_new)) 341 .setIcon(R.drawable.ic_menu_add_white) 342 .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); 343 } 344 menu.add(0, MENU_RESTORE, 0, 345 getResources().getString(R.string.menu_restore)) 346 .setIcon(android.R.drawable.ic_menu_upload); 347 } 348 349 super.onCreateOptionsMenu(menu, inflater); 350 } 351 352 @Override onOptionsItemSelected(MenuItem item)353 public boolean onOptionsItemSelected(MenuItem item) { 354 switch (item.getItemId()) { 355 case MENU_NEW: 356 addNewApn(); 357 return true; 358 359 case MENU_RESTORE: 360 restoreDefaultApn(); 361 return true; 362 } 363 return super.onOptionsItemSelected(item); 364 } 365 addNewApn()366 private void addNewApn() { 367 Intent intent = new Intent(Intent.ACTION_INSERT, Telephony.Carriers.CONTENT_URI); 368 int subId = mSubscriptionInfo != null ? mSubscriptionInfo.getSubscriptionId() 369 : SubscriptionManager.INVALID_SUBSCRIPTION_ID; 370 intent.putExtra(SUB_ID, subId); 371 if (!TextUtils.isEmpty(mMvnoType) && !TextUtils.isEmpty(mMvnoMatchData)) { 372 intent.putExtra(MVNO_TYPE, mMvnoType); 373 intent.putExtra(MVNO_MATCH_DATA, mMvnoMatchData); 374 } 375 startActivity(intent); 376 } 377 378 @Override onPreferenceTreeClick(Preference preference)379 public boolean onPreferenceTreeClick(Preference preference) { 380 int pos = Integer.parseInt(preference.getKey()); 381 Uri url = ContentUris.withAppendedId(Telephony.Carriers.CONTENT_URI, pos); 382 startActivity(new Intent(Intent.ACTION_EDIT, url)); 383 return true; 384 } 385 onPreferenceChange(Preference preference, Object newValue)386 public boolean onPreferenceChange(Preference preference, Object newValue) { 387 Log.d(TAG, "onPreferenceChange(): Preference - " + preference 388 + ", newValue - " + newValue + ", newValue type - " 389 + newValue.getClass()); 390 if (newValue instanceof String) { 391 setSelectedApnKey((String) newValue); 392 } 393 394 return true; 395 } 396 setSelectedApnKey(String key)397 private void setSelectedApnKey(String key) { 398 mSelectedKey = key; 399 ContentResolver resolver = getContentResolver(); 400 401 ContentValues values = new ContentValues(); 402 values.put(APN_ID, mSelectedKey); 403 resolver.update(getUriForCurrSubId(PREFERAPN_URI), values, null, null); 404 } 405 getSelectedApnKey()406 private String getSelectedApnKey() { 407 String key = null; 408 409 Cursor cursor = getContentResolver().query(getUriForCurrSubId(PREFERAPN_URI), 410 new String[] {"_id"}, null, null, Telephony.Carriers.DEFAULT_SORT_ORDER); 411 if (cursor.getCount() > 0) { 412 cursor.moveToFirst(); 413 key = cursor.getString(ID_INDEX); 414 } 415 cursor.close(); 416 return key; 417 } 418 restoreDefaultApn()419 private boolean restoreDefaultApn() { 420 showDialog(DIALOG_RESTORE_DEFAULTAPN); 421 mRestoreDefaultApnMode = true; 422 423 if (mRestoreApnUiHandler == null) { 424 mRestoreApnUiHandler = new RestoreApnUiHandler(); 425 } 426 427 if (mRestoreApnProcessHandler == null || 428 mRestoreDefaultApnThread == null) { 429 mRestoreDefaultApnThread = new HandlerThread( 430 "Restore default APN Handler: Process Thread"); 431 mRestoreDefaultApnThread.start(); 432 mRestoreApnProcessHandler = new RestoreApnProcessHandler( 433 mRestoreDefaultApnThread.getLooper(), mRestoreApnUiHandler); 434 } 435 436 mRestoreApnProcessHandler 437 .sendEmptyMessage(EVENT_RESTORE_DEFAULTAPN_START); 438 return true; 439 } 440 441 // Append subId to the Uri getUriForCurrSubId(Uri uri)442 private Uri getUriForCurrSubId(Uri uri) { 443 int subId = mSubscriptionInfo != null ? mSubscriptionInfo.getSubscriptionId() 444 : SubscriptionManager.INVALID_SUBSCRIPTION_ID; 445 if (SubscriptionManager.isValidSubscriptionId(subId)) { 446 return Uri.withAppendedPath(uri, "subId/" + String.valueOf(subId)); 447 } else { 448 return uri; 449 } 450 } 451 452 private class RestoreApnUiHandler extends Handler { 453 @Override handleMessage(Message msg)454 public void handleMessage(Message msg) { 455 switch (msg.what) { 456 case EVENT_RESTORE_DEFAULTAPN_COMPLETE: 457 Activity activity = getActivity(); 458 if (activity == null) { 459 mRestoreDefaultApnMode = false; 460 return; 461 } 462 fillList(); 463 getPreferenceScreen().setEnabled(true); 464 mRestoreDefaultApnMode = false; 465 removeDialog(DIALOG_RESTORE_DEFAULTAPN); 466 Toast.makeText( 467 activity, 468 getResources().getString( 469 R.string.restore_default_apn_completed), 470 Toast.LENGTH_LONG).show(); 471 break; 472 } 473 } 474 } 475 476 private class RestoreApnProcessHandler extends Handler { 477 private Handler mRestoreApnUiHandler; 478 RestoreApnProcessHandler(Looper looper, Handler restoreApnUiHandler)479 public RestoreApnProcessHandler(Looper looper, Handler restoreApnUiHandler) { 480 super(looper); 481 this.mRestoreApnUiHandler = restoreApnUiHandler; 482 } 483 484 @Override handleMessage(Message msg)485 public void handleMessage(Message msg) { 486 switch (msg.what) { 487 case EVENT_RESTORE_DEFAULTAPN_START: 488 ContentResolver resolver = getContentResolver(); 489 resolver.delete(getUriForCurrSubId(DEFAULTAPN_URI), null, null); 490 mRestoreApnUiHandler 491 .sendEmptyMessage(EVENT_RESTORE_DEFAULTAPN_COMPLETE); 492 break; 493 } 494 } 495 } 496 497 @Override onCreateDialog(int id)498 public Dialog onCreateDialog(int id) { 499 if (id == DIALOG_RESTORE_DEFAULTAPN) { 500 ProgressDialog dialog = new ProgressDialog(getActivity()) { 501 public boolean onTouchEvent(MotionEvent event) { 502 return true; 503 } 504 }; 505 dialog.setMessage(getResources().getString(R.string.restore_default_apn)); 506 dialog.setCancelable(false); 507 return dialog; 508 } 509 return null; 510 } 511 512 @Override getDialogMetricsCategory(int dialogId)513 public int getDialogMetricsCategory(int dialogId) { 514 if (dialogId == DIALOG_RESTORE_DEFAULTAPN) { 515 return MetricsEvent.DIALOG_APN_RESTORE_DEFAULT; 516 } 517 return 0; 518 } 519 } 520