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.telephony.SubscriptionManager;
44 import android.util.Log;
45 import android.view.LayoutInflater;
46 import android.view.Menu;
47 import android.view.MenuInflater;
48 import android.view.MenuItem;
49 import android.view.View;
50 import android.widget.TextView;
51 import android.widget.Toast;
53 import com.android.internal.telephony.Phone;
54 import com.android.internal.telephony.PhoneConstants;
55 import com.android.internal.telephony.TelephonyIntents;
56 import com.android.internal.telephony.TelephonyProperties;
57 import android.telephony.TelephonyManager;
59 import java.util.ArrayList;
61 public class ApnSettings extends SettingsPreferenceFragment implements
62 Preference.OnPreferenceChangeListener {
63 static final String TAG = "ApnSettings";
65 public static final String EXTRA_POSITION = "position";
66 public static final String RESTORE_CARRIERS_URI =
67 "content://telephony/carriers/restore";
68 public static final String PREFERRED_APN_URI =
69 "content://telephony/carriers/preferapn";
71 public static final String APN_ID = "apn_id";
73 private static final int ID_INDEX = 0;
74 private static final int NAME_INDEX = 1;
75 private static final int APN_INDEX = 2;
76 private static final int TYPES_INDEX = 3;
78 private static final int MENU_NEW = Menu.FIRST;
79 private static final int MENU_RESTORE = Menu.FIRST + 1;
81 private static final int EVENT_RESTORE_DEFAULTAPN_START = 1;
82 private static final int EVENT_RESTORE_DEFAULTAPN_COMPLETE = 2;
84 private static final int DIALOG_RESTORE_DEFAULTAPN = 1001;
86 private static final Uri DEFAULTAPN_URI = Uri.parse(RESTORE_CARRIERS_URI);
87 private static final Uri PREFERAPN_URI = Uri.parse(PREFERRED_APN_URI);
89 private static boolean mRestoreDefaultApnMode;
91 private RestoreApnUiHandler mRestoreApnUiHandler;
92 private RestoreApnProcessHandler mRestoreApnProcessHandler;
93 private HandlerThread mRestoreDefaultApnThread;
94 private SubscriptionInfo mSubscriptionInfo;
96 private UserManager mUm;
98 private String mSelectedKey;
100 private IntentFilter mMobileStateFilter;
102 private boolean mUnavailable;
104 private final BroadcastReceiver mMobileStateReceiver = new BroadcastReceiver() {
106 public void onReceive(Context context, Intent intent) {
107 if (intent.getAction().equals(
108 TelephonyIntents.ACTION_ANY_DATA_CONNECTION_STATE_CHANGED)) {
109 PhoneConstants.DataState state = getMobileDataState(intent);
112 if (!mRestoreDefaultApnMode) {
115 showDialog(DIALOG_RESTORE_DEFAULTAPN);
123 private static PhoneConstants.DataState getMobileDataState(Intent intent) {
124 String str = intent.getStringExtra(PhoneConstants.STATE_KEY);
126 return Enum.valueOf(PhoneConstants.DataState.class, str);
128 return PhoneConstants.DataState.DISCONNECTED;
133 public void onCreate(Bundle icicle) {
134 super.onCreate(icicle);
135 final Activity activity = getActivity();
136 final int subId = activity.getIntent().getIntExtra("sub_id",
137 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
139 mUm = (UserManager) getSystemService(Context.USER_SERVICE);
141 mMobileStateFilter = new IntentFilter(
142 TelephonyIntents.ACTION_ANY_DATA_CONNECTION_STATE_CHANGED);
144 if (!mUm.hasUserRestriction(UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
145 setHasOptionsMenu(true);
148 mSubscriptionInfo = Utils.findRecordBySubId(activity, subId);
152 public void onActivityCreated(Bundle savedInstanceState) {
153 super.onActivityCreated(savedInstanceState);
155 TextView empty = (TextView) getView().findViewById(android.R.id.empty);
157 empty.setText(R.string.apn_settings_not_available);
158 getListView().setEmptyView(empty);
161 if (mUm.hasUserRestriction(UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
163 setPreferenceScreen(new PreferenceScreen(getActivity(), null));
167 addPreferencesFromResource(R.xml.apn_settings);
169 getListView().setItemsCanFocus(true);
173 public void onResume() {
180 getActivity().registerReceiver(mMobileStateReceiver, mMobileStateFilter);
182 if (!mRestoreDefaultApnMode) {
188 public void onPause() {
195 getActivity().unregisterReceiver(mMobileStateReceiver);
199 public void onDestroy() {
202 if (mRestoreDefaultApnThread != null) {
203 mRestoreDefaultApnThread.quit();
207 private void fillList() {
208 final TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
209 final String mccmnc = mSubscriptionInfo == null ? ""
210 : tm.getSimOperator(mSubscriptionInfo.getSubscriptionId());
211 Log.d(TAG, "mccmnc = " + mccmnc);
212 final String where = "numeric=\""
214 + "\" AND NOT (type='ia' AND (apn=\"\" OR apn IS NULL))";
216 Cursor cursor = getContentResolver().query(Telephony.Carriers.CONTENT_URI, new String[] {
217 "_id", "name", "apn", "type"}, where, null,
218 Telephony.Carriers.DEFAULT_SORT_ORDER);
220 if (cursor != null) {
221 PreferenceGroup apnList = (PreferenceGroup) findPreference("apn_list");
224 ArrayList<Preference> mmsApnList = new ArrayList<Preference>();
226 mSelectedKey = getSelectedApnKey();
227 cursor.moveToFirst();
228 while (!cursor.isAfterLast()) {
229 String name = cursor.getString(NAME_INDEX);
230 String apn = cursor.getString(APN_INDEX);
231 String key = cursor.getString(ID_INDEX);
232 String type = cursor.getString(TYPES_INDEX);
234 ApnPreference pref = new ApnPreference(getActivity());
238 pref.setSummary(apn);
239 pref.setPersistent(false);
240 pref.setOnPreferenceChangeListener(this);
242 boolean selectable = ((type == null) || !type.equals("mms"));
243 pref.setSelectable(selectable);
245 if ((mSelectedKey != null) && mSelectedKey.equals(key)) {
248 apnList.addPreference(pref);
250 mmsApnList.add(pref);
256 for (Preference preference : mmsApnList) {
257 apnList.addPreference(preference);
263 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
265 menu.add(0, MENU_NEW, 0,
266 getResources().getString(R.string.menu_new))
267 .setIcon(android.R.drawable.ic_menu_add)
268 .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
269 menu.add(0, MENU_RESTORE, 0,
270 getResources().getString(R.string.menu_restore))
271 .setIcon(android.R.drawable.ic_menu_upload);
274 super.onCreateOptionsMenu(menu, inflater);
278 public boolean onOptionsItemSelected(MenuItem item) {
279 switch (item.getItemId()) {
288 return super.onOptionsItemSelected(item);
291 private void addNewApn() {
292 Intent intent = new Intent(Intent.ACTION_INSERT, Telephony.Carriers.CONTENT_URI);
293 int subId = mSubscriptionInfo != null ? mSubscriptionInfo.getSubscriptionId()
294 : SubscriptionManager.INVALID_SUBSCRIPTION_ID;
295 intent.putExtra("sub_id", subId);
296 startActivity(intent);
300 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
301 int pos = Integer.parseInt(preference.getKey());
302 Uri url = ContentUris.withAppendedId(Telephony.Carriers.CONTENT_URI, pos);
303 startActivity(new Intent(Intent.ACTION_EDIT, url));
307 public boolean onPreferenceChange(Preference preference, Object newValue) {
308 Log.d(TAG, "onPreferenceChange(): Preference - " + preference
309 + ", newValue - " + newValue + ", newValue type - "
310 + newValue.getClass());
311 if (newValue instanceof String) {
312 setSelectedApnKey((String) newValue);
318 private void setSelectedApnKey(String key) {
320 ContentResolver resolver = getContentResolver();
322 ContentValues values = new ContentValues();
323 values.put(APN_ID, mSelectedKey);
324 resolver.update(PREFERAPN_URI, values, null, null);
327 private String getSelectedApnKey() {
330 Cursor cursor = getContentResolver().query(PREFERAPN_URI, new String[] {"_id"},
331 null, null, Telephony.Carriers.DEFAULT_SORT_ORDER);
332 if (cursor.getCount() > 0) {
333 cursor.moveToFirst();
334 key = cursor.getString(ID_INDEX);
340 private boolean restoreDefaultApn() {
341 showDialog(DIALOG_RESTORE_DEFAULTAPN);
342 mRestoreDefaultApnMode = true;
344 if (mRestoreApnUiHandler == null) {
345 mRestoreApnUiHandler = new RestoreApnUiHandler();
348 if (mRestoreApnProcessHandler == null ||
349 mRestoreDefaultApnThread == null) {
350 mRestoreDefaultApnThread = new HandlerThread(
351 "Restore default APN Handler: Process Thread");
352 mRestoreDefaultApnThread.start();
353 mRestoreApnProcessHandler = new RestoreApnProcessHandler(
354 mRestoreDefaultApnThread.getLooper(), mRestoreApnUiHandler);
357 mRestoreApnProcessHandler
358 .sendEmptyMessage(EVENT_RESTORE_DEFAULTAPN_START);
362 private class RestoreApnUiHandler extends Handler {
364 public void handleMessage(Message msg) {
366 case EVENT_RESTORE_DEFAULTAPN_COMPLETE:
367 Activity activity = getActivity();
368 if (activity == null) {
369 mRestoreDefaultApnMode = false;
373 getPreferenceScreen().setEnabled(true);
374 mRestoreDefaultApnMode = false;
375 removeDialog(DIALOG_RESTORE_DEFAULTAPN);
378 getResources().getString(
379 R.string.restore_default_apn_completed),
380 Toast.LENGTH_LONG).show();
386 private class RestoreApnProcessHandler extends Handler {
387 private Handler mRestoreApnUiHandler;
389 public RestoreApnProcessHandler(Looper looper, Handler restoreApnUiHandler) {
391 this.mRestoreApnUiHandler = restoreApnUiHandler;
395 public void handleMessage(Message msg) {
397 case EVENT_RESTORE_DEFAULTAPN_START:
398 ContentResolver resolver = getContentResolver();
399 resolver.delete(DEFAULTAPN_URI, null, null);
401 .sendEmptyMessage(EVENT_RESTORE_DEFAULTAPN_COMPLETE);
408 public Dialog onCreateDialog(int id) {
409 if (id == DIALOG_RESTORE_DEFAULTAPN) {
410 ProgressDialog dialog = new ProgressDialog(getActivity());
411 dialog.setMessage(getResources().getString(R.string.restore_default_apn));
412 dialog.setCancelable(false);