2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings;
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.UserManager;
37 import android.preference.Preference;
38 import android.preference.PreferenceActivity;
39 import android.preference.PreferenceGroup;
40 import android.preference.PreferenceScreen;
41 import android.provider.Telephony;
42 import android.telephony.SubscriptionInfo;
43 import android.util.Log;
44 import android.view.LayoutInflater;
45 import android.view.Menu;
46 import android.view.MenuInflater;
47 import android.view.MenuItem;
48 import android.view.View;
49 import android.widget.TextView;
50 import android.widget.Toast;
52 import com.android.internal.telephony.Phone;
53 import com.android.internal.telephony.PhoneConstants;
54 import com.android.internal.telephony.TelephonyIntents;
55 import com.android.internal.telephony.TelephonyProperties;
56 import android.telephony.TelephonyManager;
58 import java.util.ArrayList;
60 public class ApnSettings extends SettingsPreferenceFragment implements
61 Preference.OnPreferenceChangeListener {
62 static final String TAG = "ApnSettings";
64 public static final String EXTRA_POSITION = "position";
65 public static final String RESTORE_CARRIERS_URI =
66 "content://telephony/carriers/restore";
67 public static final String PREFERRED_APN_URI =
68 "content://telephony/carriers/preferapn";
70 public static final String APN_ID = "apn_id";
72 private static final int ID_INDEX = 0;
73 private static final int NAME_INDEX = 1;
74 private static final int APN_INDEX = 2;
75 private static final int TYPES_INDEX = 3;
77 private static final int MENU_NEW = Menu.FIRST;
78 private static final int MENU_RESTORE = Menu.FIRST + 1;
80 private static final int EVENT_RESTORE_DEFAULTAPN_START = 1;
81 private static final int EVENT_RESTORE_DEFAULTAPN_COMPLETE = 2;
83 private static final int DIALOG_RESTORE_DEFAULTAPN = 1001;
85 private static final Uri DEFAULTAPN_URI = Uri.parse(RESTORE_CARRIERS_URI);
86 private static final Uri PREFERAPN_URI = Uri.parse(PREFERRED_APN_URI);
88 private static boolean mRestoreDefaultApnMode;
90 private RestoreApnUiHandler mRestoreApnUiHandler;
91 private RestoreApnProcessHandler mRestoreApnProcessHandler;
92 private HandlerThread mRestoreDefaultApnThread;
93 private SubscriptionInfo mSubscriptionInfo;
95 private UserManager mUm;
97 private String mSelectedKey;
99 private IntentFilter mMobileStateFilter;
101 private boolean mUnavailable;
103 private final BroadcastReceiver mMobileStateReceiver = new BroadcastReceiver() {
105 public void onReceive(Context context, Intent intent) {
106 if (intent.getAction().equals(
107 TelephonyIntents.ACTION_ANY_DATA_CONNECTION_STATE_CHANGED)) {
108 PhoneConstants.DataState state = getMobileDataState(intent);
111 if (!mRestoreDefaultApnMode) {
114 showDialog(DIALOG_RESTORE_DEFAULTAPN);
122 private static PhoneConstants.DataState getMobileDataState(Intent intent) {
123 String str = intent.getStringExtra(PhoneConstants.STATE_KEY);
125 return Enum.valueOf(PhoneConstants.DataState.class, str);
127 return PhoneConstants.DataState.DISCONNECTED;
132 public void onCreate(Bundle icicle) {
133 super.onCreate(icicle);
134 final Activity activity = getActivity();
135 final int subId = activity.getIntent().getIntExtra("sub_id", -1);
137 mUm = (UserManager) getSystemService(Context.USER_SERVICE);
139 mMobileStateFilter = new IntentFilter(
140 TelephonyIntents.ACTION_ANY_DATA_CONNECTION_STATE_CHANGED);
142 if (!mUm.hasUserRestriction(UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
143 setHasOptionsMenu(true);
146 mSubscriptionInfo = Utils.findRecordBySubId(activity, subId);
150 public void onActivityCreated(Bundle savedInstanceState) {
151 super.onActivityCreated(savedInstanceState);
153 TextView empty = (TextView) getView().findViewById(android.R.id.empty);
155 empty.setText(R.string.apn_settings_not_available);
156 getListView().setEmptyView(empty);
159 if (mUm.hasUserRestriction(UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
161 setPreferenceScreen(new PreferenceScreen(getActivity(), null));
165 addPreferencesFromResource(R.xml.apn_settings);
167 getListView().setItemsCanFocus(true);
171 public void onResume() {
178 getActivity().registerReceiver(mMobileStateReceiver, mMobileStateFilter);
180 if (!mRestoreDefaultApnMode) {
186 public void onPause() {
193 getActivity().unregisterReceiver(mMobileStateReceiver);
197 public void onDestroy() {
200 if (mRestoreDefaultApnThread != null) {
201 mRestoreDefaultApnThread.quit();
205 private void fillList() {
206 final TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
207 final String mccmnc = mSubscriptionInfo == null ? ""
208 : tm.getSimOperator(mSubscriptionInfo.getSubscriptionId());
209 Log.d(TAG, "mccmnc = " + mccmnc);
210 final String where = "numeric=\""
212 + "\" AND NOT (type='ia' AND (apn=\"\" OR apn IS NULL))";
214 Cursor cursor = getContentResolver().query(Telephony.Carriers.CONTENT_URI, new String[] {
215 "_id", "name", "apn", "type"}, where, null,
216 Telephony.Carriers.DEFAULT_SORT_ORDER);
218 if (cursor != null) {
219 PreferenceGroup apnList = (PreferenceGroup) findPreference("apn_list");
222 ArrayList<Preference> mmsApnList = new ArrayList<Preference>();
224 mSelectedKey = getSelectedApnKey();
225 cursor.moveToFirst();
226 while (!cursor.isAfterLast()) {
227 String name = cursor.getString(NAME_INDEX);
228 String apn = cursor.getString(APN_INDEX);
229 String key = cursor.getString(ID_INDEX);
230 String type = cursor.getString(TYPES_INDEX);
232 ApnPreference pref = new ApnPreference(getActivity());
236 pref.setSummary(apn);
237 pref.setPersistent(false);
238 pref.setOnPreferenceChangeListener(this);
240 boolean selectable = ((type == null) || !type.equals("mms"));
241 pref.setSelectable(selectable);
243 if ((mSelectedKey != null) && mSelectedKey.equals(key)) {
246 apnList.addPreference(pref);
248 mmsApnList.add(pref);
254 for (Preference preference : mmsApnList) {
255 apnList.addPreference(preference);
261 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
263 menu.add(0, MENU_NEW, 0,
264 getResources().getString(R.string.menu_new))
265 .setIcon(android.R.drawable.ic_menu_add)
266 .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
267 menu.add(0, MENU_RESTORE, 0,
268 getResources().getString(R.string.menu_restore))
269 .setIcon(android.R.drawable.ic_menu_upload);
272 super.onCreateOptionsMenu(menu, inflater);
276 public boolean onOptionsItemSelected(MenuItem item) {
277 switch (item.getItemId()) {
286 return super.onOptionsItemSelected(item);
289 private void addNewApn() {
290 startActivity(new Intent(Intent.ACTION_INSERT, Telephony.Carriers.CONTENT_URI));
294 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
295 int pos = Integer.parseInt(preference.getKey());
296 Uri url = ContentUris.withAppendedId(Telephony.Carriers.CONTENT_URI, pos);
297 startActivity(new Intent(Intent.ACTION_EDIT, url));
301 public boolean onPreferenceChange(Preference preference, Object newValue) {
302 Log.d(TAG, "onPreferenceChange(): Preference - " + preference
303 + ", newValue - " + newValue + ", newValue type - "
304 + newValue.getClass());
305 if (newValue instanceof String) {
306 setSelectedApnKey((String) newValue);
312 private void setSelectedApnKey(String key) {
314 ContentResolver resolver = getContentResolver();
316 ContentValues values = new ContentValues();
317 values.put(APN_ID, mSelectedKey);
318 resolver.update(PREFERAPN_URI, values, null, null);
321 private String getSelectedApnKey() {
324 Cursor cursor = getContentResolver().query(PREFERAPN_URI, new String[] {"_id"},
325 null, null, Telephony.Carriers.DEFAULT_SORT_ORDER);
326 if (cursor.getCount() > 0) {
327 cursor.moveToFirst();
328 key = cursor.getString(ID_INDEX);
334 private boolean restoreDefaultApn() {
335 showDialog(DIALOG_RESTORE_DEFAULTAPN);
336 mRestoreDefaultApnMode = true;
338 if (mRestoreApnUiHandler == null) {
339 mRestoreApnUiHandler = new RestoreApnUiHandler();
342 if (mRestoreApnProcessHandler == null ||
343 mRestoreDefaultApnThread == null) {
344 mRestoreDefaultApnThread = new HandlerThread(
345 "Restore default APN Handler: Process Thread");
346 mRestoreDefaultApnThread.start();
347 mRestoreApnProcessHandler = new RestoreApnProcessHandler(
348 mRestoreDefaultApnThread.getLooper(), mRestoreApnUiHandler);
351 mRestoreApnProcessHandler
352 .sendEmptyMessage(EVENT_RESTORE_DEFAULTAPN_START);
356 private class RestoreApnUiHandler extends Handler {
358 public void handleMessage(Message msg) {
360 case EVENT_RESTORE_DEFAULTAPN_COMPLETE:
361 Activity activity = getActivity();
362 if (activity == null) {
363 mRestoreDefaultApnMode = false;
367 getPreferenceScreen().setEnabled(true);
368 mRestoreDefaultApnMode = false;
369 removeDialog(DIALOG_RESTORE_DEFAULTAPN);
372 getResources().getString(
373 R.string.restore_default_apn_completed),
374 Toast.LENGTH_LONG).show();
380 private class RestoreApnProcessHandler extends Handler {
381 private Handler mRestoreApnUiHandler;
383 public RestoreApnProcessHandler(Looper looper, Handler restoreApnUiHandler) {
385 this.mRestoreApnUiHandler = restoreApnUiHandler;
389 public void handleMessage(Message msg) {
391 case EVENT_RESTORE_DEFAULTAPN_START:
392 ContentResolver resolver = getContentResolver();
393 resolver.delete(DEFAULTAPN_URI, null, null);
395 .sendEmptyMessage(EVENT_RESTORE_DEFAULTAPN_COMPLETE);
402 public Dialog onCreateDialog(int id) {
403 if (id == DIALOG_RESTORE_DEFAULTAPN) {
404 ProgressDialog dialog = new ProgressDialog(getActivity());
405 dialog.setMessage(getResources().getString(R.string.restore_default_apn));
406 dialog.setCancelable(false);