| The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 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 | |
| Fabrice Di Meglio | 3e825fe | 2014-10-13 18:59:34 -0700 | [diff] [blame] | 19 | import android.app.Activity; |
| The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 20 | import android.app.Dialog; |
| 21 | import android.app.ProgressDialog; |
| John Huang | 6829688 | 2009-03-24 18:26:28 -0700 | [diff] [blame] | 22 | import android.content.BroadcastReceiver; |
| The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 23 | import android.content.ContentResolver; |
| 24 | import android.content.ContentUris; |
| John Huang | 6829688 | 2009-03-24 18:26:28 -0700 | [diff] [blame] | 25 | import android.content.ContentValues; |
| The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 26 | import android.content.Context; |
| 27 | import android.content.Intent; |
| John Huang | 6829688 | 2009-03-24 18:26:28 -0700 | [diff] [blame] | 28 | import android.content.IntentFilter; |
| The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 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; |
| Julia Reynolds | ee27b9d | 2014-05-09 13:36:20 -0400 | [diff] [blame] | 36 | import android.os.UserManager; |
| The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 37 | import android.preference.Preference; |
| The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 38 | import android.preference.PreferenceGroup; |
| 39 | import android.preference.PreferenceScreen; |
| 40 | import android.provider.Telephony; |
| PauloftheWest | 6f341b2 | 2014-12-02 08:45:00 -0800 | [diff] [blame] | 41 | import android.telephony.SubscriptionInfo; |
| Mick Lin | 09c9e5e | 2015-01-22 17:28:43 +0800 | [diff] [blame] | 42 | import android.telephony.SubscriptionManager; |
| John Huang | 6829688 | 2009-03-24 18:26:28 -0700 | [diff] [blame] | 43 | import android.util.Log; |
| The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 44 | import android.view.Menu; |
| Fabrice Di Meglio | 61a1fec | 2014-08-13 16:22:38 -0700 | [diff] [blame] | 45 | import android.view.MenuInflater; |
| The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 46 | import android.view.MenuItem; |
| Fabrice Di Meglio | 61a1fec | 2014-08-13 16:22:38 -0700 | [diff] [blame] | 47 | import android.widget.TextView; |
| The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 48 | import android.widget.Toast; |
| 49 | |
| Chris Wren | 8a963ba | 2015-03-20 10:29:14 -0400 | [diff] [blame^] | 50 | import com.android.internal.logging.MetricsLogger; |
| Wink Saville | 9ad4a89 | 2012-06-14 12:33:43 -0700 | [diff] [blame] | 51 | import com.android.internal.telephony.PhoneConstants; |
| John Huang | 6829688 | 2009-03-24 18:26:28 -0700 | [diff] [blame] | 52 | import com.android.internal.telephony.TelephonyIntents; |
| Sanket Padawe | 19627ca | 2014-12-18 10:29:16 -0800 | [diff] [blame] | 53 | import android.telephony.TelephonyManager; |
| John Huang | 6829688 | 2009-03-24 18:26:28 -0700 | [diff] [blame] | 54 | |
| 55 | import java.util.ArrayList; |
| 56 | |
| Fabrice Di Meglio | 61a1fec | 2014-08-13 16:22:38 -0700 | [diff] [blame] | 57 | public class ApnSettings extends SettingsPreferenceFragment implements |
| John Huang | 6829688 | 2009-03-24 18:26:28 -0700 | [diff] [blame] | 58 | Preference.OnPreferenceChangeListener { |
| 59 | static final String TAG = "ApnSettings"; |
| The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 60 | |
| 61 | public static final String EXTRA_POSITION = "position"; |
| 62 | public static final String RESTORE_CARRIERS_URI = |
| 63 | "content://telephony/carriers/restore"; |
| John Huang | 6829688 | 2009-03-24 18:26:28 -0700 | [diff] [blame] | 64 | public static final String PREFERRED_APN_URI = |
| 65 | "content://telephony/carriers/preferapn"; |
| 66 | |
| 67 | public static final String APN_ID = "apn_id"; |
| The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 68 | |
| 69 | private static final int ID_INDEX = 0; |
| 70 | private static final int NAME_INDEX = 1; |
| 71 | private static final int APN_INDEX = 2; |
| John Huang | 6829688 | 2009-03-24 18:26:28 -0700 | [diff] [blame] | 72 | private static final int TYPES_INDEX = 3; |
| The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 73 | |
| 74 | private static final int MENU_NEW = Menu.FIRST; |
| 75 | private static final int MENU_RESTORE = Menu.FIRST + 1; |
| 76 | |
| 77 | private static final int EVENT_RESTORE_DEFAULTAPN_START = 1; |
| 78 | private static final int EVENT_RESTORE_DEFAULTAPN_COMPLETE = 2; |
| 79 | |
| 80 | private static final int DIALOG_RESTORE_DEFAULTAPN = 1001; |
| 81 | |
| 82 | private static final Uri DEFAULTAPN_URI = Uri.parse(RESTORE_CARRIERS_URI); |
| John Huang | 6829688 | 2009-03-24 18:26:28 -0700 | [diff] [blame] | 83 | private static final Uri PREFERAPN_URI = Uri.parse(PREFERRED_APN_URI); |
| The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 84 | |
| 85 | private static boolean mRestoreDefaultApnMode; |
| 86 | |
| 87 | private RestoreApnUiHandler mRestoreApnUiHandler; |
| 88 | private RestoreApnProcessHandler mRestoreApnProcessHandler; |
| Jack Wang | 481582f | 2011-01-27 14:25:22 +0800 | [diff] [blame] | 89 | private HandlerThread mRestoreDefaultApnThread; |
| PauloftheWest | 6f341b2 | 2014-12-02 08:45:00 -0800 | [diff] [blame] | 90 | private SubscriptionInfo mSubscriptionInfo; |
| The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 91 | |
| Julia Reynolds | ee27b9d | 2014-05-09 13:36:20 -0400 | [diff] [blame] | 92 | private UserManager mUm; |
| 93 | |
| John Huang | 6829688 | 2009-03-24 18:26:28 -0700 | [diff] [blame] | 94 | private String mSelectedKey; |
| 95 | |
| 96 | private IntentFilter mMobileStateFilter; |
| 97 | |
| Julia Reynolds | ee27b9d | 2014-05-09 13:36:20 -0400 | [diff] [blame] | 98 | private boolean mUnavailable; |
| 99 | |
| John Huang | 6829688 | 2009-03-24 18:26:28 -0700 | [diff] [blame] | 100 | private final BroadcastReceiver mMobileStateReceiver = new BroadcastReceiver() { |
| 101 | @Override |
| 102 | public void onReceive(Context context, Intent intent) { |
| 103 | if (intent.getAction().equals( |
| 104 | TelephonyIntents.ACTION_ANY_DATA_CONNECTION_STATE_CHANGED)) { |
| Wink Saville | 9ad4a89 | 2012-06-14 12:33:43 -0700 | [diff] [blame] | 105 | PhoneConstants.DataState state = getMobileDataState(intent); |
| John Huang | 6829688 | 2009-03-24 18:26:28 -0700 | [diff] [blame] | 106 | switch (state) { |
| 107 | case CONNECTED: |
| 108 | if (!mRestoreDefaultApnMode) { |
| 109 | fillList(); |
| 110 | } else { |
| 111 | showDialog(DIALOG_RESTORE_DEFAULTAPN); |
| 112 | } |
| 113 | break; |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | }; |
| 118 | |
| Wink Saville | 9ad4a89 | 2012-06-14 12:33:43 -0700 | [diff] [blame] | 119 | private static PhoneConstants.DataState getMobileDataState(Intent intent) { |
| 120 | String str = intent.getStringExtra(PhoneConstants.STATE_KEY); |
| John Huang | 6829688 | 2009-03-24 18:26:28 -0700 | [diff] [blame] | 121 | if (str != null) { |
| Wink Saville | 9ad4a89 | 2012-06-14 12:33:43 -0700 | [diff] [blame] | 122 | return Enum.valueOf(PhoneConstants.DataState.class, str); |
| John Huang | 6829688 | 2009-03-24 18:26:28 -0700 | [diff] [blame] | 123 | } else { |
| Wink Saville | 9ad4a89 | 2012-06-14 12:33:43 -0700 | [diff] [blame] | 124 | return PhoneConstants.DataState.DISCONNECTED; |
| John Huang | 6829688 | 2009-03-24 18:26:28 -0700 | [diff] [blame] | 125 | } |
| 126 | } |
| The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 127 | |
| 128 | @Override |
| Chris Wren | 8a963ba | 2015-03-20 10:29:14 -0400 | [diff] [blame^] | 129 | protected int getMetricsCategory() { |
| 130 | return MetricsLogger.APN; |
| 131 | } |
| 132 | |
| 133 | @Override |
| Fabrice Di Meglio | 61a1fec | 2014-08-13 16:22:38 -0700 | [diff] [blame] | 134 | public void onCreate(Bundle icicle) { |
| The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 135 | super.onCreate(icicle); |
| PauloftheWest | 6f341b2 | 2014-12-02 08:45:00 -0800 | [diff] [blame] | 136 | final Activity activity = getActivity(); |
| Mick Lin | 09c9e5e | 2015-01-22 17:28:43 +0800 | [diff] [blame] | 137 | final int subId = activity.getIntent().getIntExtra("sub_id", |
| 138 | SubscriptionManager.INVALID_SUBSCRIPTION_ID); |
| The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 139 | |
| Julia Reynolds | ee27b9d | 2014-05-09 13:36:20 -0400 | [diff] [blame] | 140 | mUm = (UserManager) getSystemService(Context.USER_SERVICE); |
| 141 | |
| Fabrice Di Meglio | 61a1fec | 2014-08-13 16:22:38 -0700 | [diff] [blame] | 142 | mMobileStateFilter = new IntentFilter( |
| 143 | TelephonyIntents.ACTION_ANY_DATA_CONNECTION_STATE_CHANGED); |
| 144 | |
| Julia Reynolds | 6cc7ccc | 2014-08-18 16:04:48 -0400 | [diff] [blame] | 145 | if (!mUm.hasUserRestriction(UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) { |
| 146 | setHasOptionsMenu(true); |
| 147 | } |
| PauloftheWest | 6f341b2 | 2014-12-02 08:45:00 -0800 | [diff] [blame] | 148 | |
| Sanket Padawe | 4be06d9 | 2014-12-18 06:24:36 +0000 | [diff] [blame] | 149 | mSubscriptionInfo = Utils.findRecordBySubId(activity, subId); |
| Fabrice Di Meglio | 61a1fec | 2014-08-13 16:22:38 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | @Override |
| 153 | public void onActivityCreated(Bundle savedInstanceState) { |
| 154 | super.onActivityCreated(savedInstanceState); |
| 155 | |
| 156 | TextView empty = (TextView) getView().findViewById(android.R.id.empty); |
| 157 | if (empty != null) { |
| 158 | empty.setText(R.string.apn_settings_not_available); |
| 159 | getListView().setEmptyView(empty); |
| 160 | } |
| 161 | |
| Julia Reynolds | ee27b9d | 2014-05-09 13:36:20 -0400 | [diff] [blame] | 162 | if (mUm.hasUserRestriction(UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) { |
| 163 | mUnavailable = true; |
| Fabrice Di Meglio | 61a1fec | 2014-08-13 16:22:38 -0700 | [diff] [blame] | 164 | setPreferenceScreen(new PreferenceScreen(getActivity(), null)); |
| Julia Reynolds | ee27b9d | 2014-05-09 13:36:20 -0400 | [diff] [blame] | 165 | return; |
| 166 | } |
| 167 | |
| The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 168 | addPreferencesFromResource(R.xml.apn_settings); |
| John Huang | 6829688 | 2009-03-24 18:26:28 -0700 | [diff] [blame] | 169 | |
| Fabrice Di Meglio | 61a1fec | 2014-08-13 16:22:38 -0700 | [diff] [blame] | 170 | getListView().setItemsCanFocus(true); |
| The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | @Override |
| Fabrice Di Meglio | 61a1fec | 2014-08-13 16:22:38 -0700 | [diff] [blame] | 174 | public void onResume() { |
| The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 175 | super.onResume(); |
| 176 | |
| Julia Reynolds | ee27b9d | 2014-05-09 13:36:20 -0400 | [diff] [blame] | 177 | if (mUnavailable) { |
| 178 | return; |
| 179 | } |
| 180 | |
| Fabrice Di Meglio | 61a1fec | 2014-08-13 16:22:38 -0700 | [diff] [blame] | 181 | getActivity().registerReceiver(mMobileStateReceiver, mMobileStateFilter); |
| John Huang | 6829688 | 2009-03-24 18:26:28 -0700 | [diff] [blame] | 182 | |
| The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 183 | if (!mRestoreDefaultApnMode) { |
| 184 | fillList(); |
| The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | |
| John Huang | 6829688 | 2009-03-24 18:26:28 -0700 | [diff] [blame] | 188 | @Override |
| Fabrice Di Meglio | 61a1fec | 2014-08-13 16:22:38 -0700 | [diff] [blame] | 189 | public void onPause() { |
| John Huang | 6829688 | 2009-03-24 18:26:28 -0700 | [diff] [blame] | 190 | super.onPause(); |
| Jack Wang | 481582f | 2011-01-27 14:25:22 +0800 | [diff] [blame] | 191 | |
| Julia Reynolds | ee27b9d | 2014-05-09 13:36:20 -0400 | [diff] [blame] | 192 | if (mUnavailable) { |
| 193 | return; |
| 194 | } |
| 195 | |
| Fabrice Di Meglio | 61a1fec | 2014-08-13 16:22:38 -0700 | [diff] [blame] | 196 | getActivity().unregisterReceiver(mMobileStateReceiver); |
| John Huang | 6829688 | 2009-03-24 18:26:28 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| Jack Wang | 481582f | 2011-01-27 14:25:22 +0800 | [diff] [blame] | 199 | @Override |
| Fabrice Di Meglio | 61a1fec | 2014-08-13 16:22:38 -0700 | [diff] [blame] | 200 | public void onDestroy() { |
| Jack Wang | 481582f | 2011-01-27 14:25:22 +0800 | [diff] [blame] | 201 | super.onDestroy(); |
| 202 | |
| 203 | if (mRestoreDefaultApnThread != null) { |
| 204 | mRestoreDefaultApnThread.quit(); |
| 205 | } |
| 206 | } |
| 207 | |
| The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 208 | private void fillList() { |
| Sanket Padawe | 19627ca | 2014-12-18 10:29:16 -0800 | [diff] [blame] | 209 | final TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); |
| PauloftheWest | 6f341b2 | 2014-12-02 08:45:00 -0800 | [diff] [blame] | 210 | final String mccmnc = mSubscriptionInfo == null ? "" |
| Sanket Padawe | 19627ca | 2014-12-18 10:29:16 -0800 | [diff] [blame] | 211 | : tm.getSimOperator(mSubscriptionInfo.getSubscriptionId()); |
| 212 | Log.d(TAG, "mccmnc = " + mccmnc); |
| PauloftheWest | 6f341b2 | 2014-12-02 08:45:00 -0800 | [diff] [blame] | 213 | final String where = "numeric=\"" |
| 214 | + mccmnc |
| Sanket Padawe | a7f21a3 | 2015-01-23 09:53:48 -0800 | [diff] [blame] | 215 | + "\" AND NOT (type='ia' AND (apn=\"\" OR apn IS NULL))"; |
| John Huang | 6829688 | 2009-03-24 18:26:28 -0700 | [diff] [blame] | 216 | |
| Jeff Brown | b4ea1e4 | 2011-09-12 14:59:51 -0700 | [diff] [blame] | 217 | Cursor cursor = getContentResolver().query(Telephony.Carriers.CONTENT_URI, new String[] { |
| 218 | "_id", "name", "apn", "type"}, where, null, |
| John Huang | 6829688 | 2009-03-24 18:26:28 -0700 | [diff] [blame] | 219 | Telephony.Carriers.DEFAULT_SORT_ORDER); |
| The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 220 | |
| Hyejin Kim | 88c80b1 | 2013-02-14 15:49:19 +0900 | [diff] [blame] | 221 | if (cursor != null) { |
| 222 | PreferenceGroup apnList = (PreferenceGroup) findPreference("apn_list"); |
| 223 | apnList.removeAll(); |
| The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 224 | |
| Hyejin Kim | 88c80b1 | 2013-02-14 15:49:19 +0900 | [diff] [blame] | 225 | ArrayList<Preference> mmsApnList = new ArrayList<Preference>(); |
| The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 226 | |
| Hyejin Kim | 88c80b1 | 2013-02-14 15:49:19 +0900 | [diff] [blame] | 227 | mSelectedKey = getSelectedApnKey(); |
| 228 | cursor.moveToFirst(); |
| 229 | while (!cursor.isAfterLast()) { |
| 230 | String name = cursor.getString(NAME_INDEX); |
| 231 | String apn = cursor.getString(APN_INDEX); |
| 232 | String key = cursor.getString(ID_INDEX); |
| 233 | String type = cursor.getString(TYPES_INDEX); |
| John Huang | 6829688 | 2009-03-24 18:26:28 -0700 | [diff] [blame] | 234 | |
| Fabrice Di Meglio | 61a1fec | 2014-08-13 16:22:38 -0700 | [diff] [blame] | 235 | ApnPreference pref = new ApnPreference(getActivity()); |
| John Huang | 6829688 | 2009-03-24 18:26:28 -0700 | [diff] [blame] | 236 | |
| Hyejin Kim | 88c80b1 | 2013-02-14 15:49:19 +0900 | [diff] [blame] | 237 | pref.setKey(key); |
| 238 | pref.setTitle(name); |
| 239 | pref.setSummary(apn); |
| 240 | pref.setPersistent(false); |
| 241 | pref.setOnPreferenceChangeListener(this); |
| John Huang | 6829688 | 2009-03-24 18:26:28 -0700 | [diff] [blame] | 242 | |
| Hyejin Kim | 88c80b1 | 2013-02-14 15:49:19 +0900 | [diff] [blame] | 243 | boolean selectable = ((type == null) || !type.equals("mms")); |
| 244 | pref.setSelectable(selectable); |
| 245 | if (selectable) { |
| 246 | if ((mSelectedKey != null) && mSelectedKey.equals(key)) { |
| 247 | pref.setChecked(); |
| 248 | } |
| 249 | apnList.addPreference(pref); |
| 250 | } else { |
| 251 | mmsApnList.add(pref); |
| John Huang | 6829688 | 2009-03-24 18:26:28 -0700 | [diff] [blame] | 252 | } |
| Hyejin Kim | 88c80b1 | 2013-02-14 15:49:19 +0900 | [diff] [blame] | 253 | cursor.moveToNext(); |
| The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 254 | } |
| Hyejin Kim | 88c80b1 | 2013-02-14 15:49:19 +0900 | [diff] [blame] | 255 | cursor.close(); |
| The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 256 | |
| Hyejin Kim | 88c80b1 | 2013-02-14 15:49:19 +0900 | [diff] [blame] | 257 | for (Preference preference : mmsApnList) { |
| 258 | apnList.addPreference(preference); |
| 259 | } |
| The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 260 | } |
| 261 | } |
| 262 | |
| 263 | @Override |
| Fabrice Di Meglio | 61a1fec | 2014-08-13 16:22:38 -0700 | [diff] [blame] | 264 | public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { |
| 265 | if (!mUnavailable) { |
| 266 | menu.add(0, MENU_NEW, 0, |
| 267 | getResources().getString(R.string.menu_new)) |
| 268 | .setIcon(android.R.drawable.ic_menu_add) |
| 269 | .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); |
| 270 | menu.add(0, MENU_RESTORE, 0, |
| 271 | getResources().getString(R.string.menu_restore)) |
| 272 | .setIcon(android.R.drawable.ic_menu_upload); |
| 273 | } |
| 274 | |
| 275 | super.onCreateOptionsMenu(menu, inflater); |
| The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | @Override |
| 279 | public boolean onOptionsItemSelected(MenuItem item) { |
| 280 | switch (item.getItemId()) { |
| 281 | case MENU_NEW: |
| 282 | addNewApn(); |
| 283 | return true; |
| 284 | |
| 285 | case MENU_RESTORE: |
| 286 | restoreDefaultApn(); |
| 287 | return true; |
| 288 | } |
| 289 | return super.onOptionsItemSelected(item); |
| 290 | } |
| 291 | |
| 292 | private void addNewApn() { |
| Mick Lin | 09c9e5e | 2015-01-22 17:28:43 +0800 | [diff] [blame] | 293 | Intent intent = new Intent(Intent.ACTION_INSERT, Telephony.Carriers.CONTENT_URI); |
| 294 | int subId = mSubscriptionInfo != null ? mSubscriptionInfo.getSubscriptionId() |
| 295 | : SubscriptionManager.INVALID_SUBSCRIPTION_ID; |
| 296 | intent.putExtra("sub_id", subId); |
| 297 | startActivity(intent); |
| The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | @Override |
| 301 | public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) { |
| 302 | int pos = Integer.parseInt(preference.getKey()); |
| 303 | Uri url = ContentUris.withAppendedId(Telephony.Carriers.CONTENT_URI, pos); |
| 304 | startActivity(new Intent(Intent.ACTION_EDIT, url)); |
| 305 | return true; |
| 306 | } |
| 307 | |
| John Huang | 6829688 | 2009-03-24 18:26:28 -0700 | [diff] [blame] | 308 | public boolean onPreferenceChange(Preference preference, Object newValue) { |
| 309 | Log.d(TAG, "onPreferenceChange(): Preference - " + preference |
| 310 | + ", newValue - " + newValue + ", newValue type - " |
| 311 | + newValue.getClass()); |
| 312 | if (newValue instanceof String) { |
| 313 | setSelectedApnKey((String) newValue); |
| 314 | } |
| 315 | |
| 316 | return true; |
| 317 | } |
| 318 | |
| 319 | private void setSelectedApnKey(String key) { |
| 320 | mSelectedKey = key; |
| 321 | ContentResolver resolver = getContentResolver(); |
| 322 | |
| 323 | ContentValues values = new ContentValues(); |
| 324 | values.put(APN_ID, mSelectedKey); |
| 325 | resolver.update(PREFERAPN_URI, values, null, null); |
| 326 | } |
| 327 | |
| 328 | private String getSelectedApnKey() { |
| 329 | String key = null; |
| 330 | |
| Jeff Brown | b4ea1e4 | 2011-09-12 14:59:51 -0700 | [diff] [blame] | 331 | Cursor cursor = getContentResolver().query(PREFERAPN_URI, new String[] {"_id"}, |
| 332 | null, null, Telephony.Carriers.DEFAULT_SORT_ORDER); |
| John Huang | 6829688 | 2009-03-24 18:26:28 -0700 | [diff] [blame] | 333 | if (cursor.getCount() > 0) { |
| 334 | cursor.moveToFirst(); |
| 335 | key = cursor.getString(ID_INDEX); |
| 336 | } |
| 337 | cursor.close(); |
| 338 | return key; |
| 339 | } |
| 340 | |
| The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 341 | private boolean restoreDefaultApn() { |
| 342 | showDialog(DIALOG_RESTORE_DEFAULTAPN); |
| 343 | mRestoreDefaultApnMode = true; |
| 344 | |
| 345 | if (mRestoreApnUiHandler == null) { |
| 346 | mRestoreApnUiHandler = new RestoreApnUiHandler(); |
| 347 | } |
| 348 | |
| Jack Wang | 481582f | 2011-01-27 14:25:22 +0800 | [diff] [blame] | 349 | if (mRestoreApnProcessHandler == null || |
| 350 | mRestoreDefaultApnThread == null) { |
| 351 | mRestoreDefaultApnThread = new HandlerThread( |
| The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 352 | "Restore default APN Handler: Process Thread"); |
| Jack Wang | 481582f | 2011-01-27 14:25:22 +0800 | [diff] [blame] | 353 | mRestoreDefaultApnThread.start(); |
| The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 354 | mRestoreApnProcessHandler = new RestoreApnProcessHandler( |
| Jack Wang | 481582f | 2011-01-27 14:25:22 +0800 | [diff] [blame] | 355 | mRestoreDefaultApnThread.getLooper(), mRestoreApnUiHandler); |
| The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | mRestoreApnProcessHandler |
| 359 | .sendEmptyMessage(EVENT_RESTORE_DEFAULTAPN_START); |
| 360 | return true; |
| 361 | } |
| 362 | |
| 363 | private class RestoreApnUiHandler extends Handler { |
| 364 | @Override |
| 365 | public void handleMessage(Message msg) { |
| 366 | switch (msg.what) { |
| 367 | case EVENT_RESTORE_DEFAULTAPN_COMPLETE: |
| Fabrice Di Meglio | 3e825fe | 2014-10-13 18:59:34 -0700 | [diff] [blame] | 368 | Activity activity = getActivity(); |
| 369 | if (activity == null) { |
| 370 | mRestoreDefaultApnMode = false; |
| 371 | return; |
| 372 | } |
| The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 373 | fillList(); |
| 374 | getPreferenceScreen().setEnabled(true); |
| 375 | mRestoreDefaultApnMode = false; |
| Fabrice Di Meglio | 61a1fec | 2014-08-13 16:22:38 -0700 | [diff] [blame] | 376 | removeDialog(DIALOG_RESTORE_DEFAULTAPN); |
| The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 377 | Toast.makeText( |
| Fabrice Di Meglio | 3e825fe | 2014-10-13 18:59:34 -0700 | [diff] [blame] | 378 | activity, |
| The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 379 | getResources().getString( |
| 380 | R.string.restore_default_apn_completed), |
| 381 | Toast.LENGTH_LONG).show(); |
| 382 | break; |
| 383 | } |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | private class RestoreApnProcessHandler extends Handler { |
| 388 | private Handler mRestoreApnUiHandler; |
| 389 | |
| 390 | public RestoreApnProcessHandler(Looper looper, Handler restoreApnUiHandler) { |
| 391 | super(looper); |
| 392 | this.mRestoreApnUiHandler = restoreApnUiHandler; |
| 393 | } |
| 394 | |
| 395 | @Override |
| 396 | public void handleMessage(Message msg) { |
| 397 | switch (msg.what) { |
| 398 | case EVENT_RESTORE_DEFAULTAPN_START: |
| 399 | ContentResolver resolver = getContentResolver(); |
| Hyejin Kim | 88c80b1 | 2013-02-14 15:49:19 +0900 | [diff] [blame] | 400 | resolver.delete(DEFAULTAPN_URI, null, null); |
| The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 401 | mRestoreApnUiHandler |
| 402 | .sendEmptyMessage(EVENT_RESTORE_DEFAULTAPN_COMPLETE); |
| 403 | break; |
| 404 | } |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | @Override |
| Fabrice Di Meglio | 61a1fec | 2014-08-13 16:22:38 -0700 | [diff] [blame] | 409 | public Dialog onCreateDialog(int id) { |
| The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 410 | if (id == DIALOG_RESTORE_DEFAULTAPN) { |
| Fabrice Di Meglio | 61a1fec | 2014-08-13 16:22:38 -0700 | [diff] [blame] | 411 | ProgressDialog dialog = new ProgressDialog(getActivity()); |
| The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 412 | dialog.setMessage(getResources().getString(R.string.restore_default_apn)); |
| 413 | dialog.setCancelable(false); |
| 414 | return dialog; |
| 415 | } |
| 416 | return null; |
| 417 | } |
| The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 418 | } |