The Android Open Source Project | de2d9f5 | 2008-10-21 07:00:00 -0700 | [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 | |
| 19 | import android.content.ContentUris; |
| 20 | import android.content.Context; |
| 21 | import android.content.Intent; |
| 22 | import android.database.Cursor; |
| 23 | import android.net.Uri; |
| 24 | import android.os.Bundle; |
| 25 | import android.preference.Preference; |
| 26 | import android.preference.PreferenceActivity; |
| 27 | import android.preference.PreferenceCategory; |
| 28 | import android.preference.PreferenceScreen; |
| 29 | import android.provider.Telephony; |
| 30 | import android.view.Menu; |
| 31 | import android.view.MenuItem; |
| 32 | |
| 33 | public class ApnSettings extends PreferenceActivity { |
| 34 | |
| 35 | public static final String EXTRA_POSITION = "position"; |
| 36 | |
| 37 | private static final int ID_INDEX = 0; |
| 38 | private static final int NAME_INDEX = 1; |
| 39 | private static final int APN_INDEX = 2; |
| 40 | |
| 41 | private static final int MENU_NEW = Menu.FIRST; |
| 42 | |
| 43 | private Cursor mCursor; |
| 44 | |
| 45 | @Override |
| 46 | protected void onCreate(Bundle icicle) { |
| 47 | super.onCreate(icicle); |
| 48 | |
| 49 | addPreferencesFromResource(R.xml.apn_settings); |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | protected void onResume() { |
| 54 | super.onResume(); |
| 55 | |
| 56 | fillList(); |
| 57 | } |
| 58 | |
| 59 | private void fillList() { |
| 60 | mCursor = managedQuery(Telephony.Carriers.CONTENT_URI, new String[] { |
| 61 | "_id", "name", "apn"}, null, Telephony.Carriers.DEFAULT_SORT_ORDER); |
| 62 | |
| 63 | PreferenceCategory apnList = (PreferenceCategory) findPreference("apn_list"); |
| 64 | apnList.removeAll(); |
| 65 | |
| 66 | mCursor.moveToFirst(); |
| 67 | while (!mCursor.isAfterLast()) { |
| 68 | String name = mCursor.getString(NAME_INDEX); |
| 69 | String apn = mCursor.getString(APN_INDEX); |
| 70 | |
| 71 | Preference pref = new Preference((Context) this); |
| 72 | pref.setKey(mCursor.getString(ID_INDEX)); |
| 73 | pref.setTitle(name); |
| 74 | pref.setSummary(apn); |
| 75 | pref.setPersistent(false); |
| 76 | apnList.addPreference(pref); |
| 77 | mCursor.moveToNext(); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public boolean onCreateOptionsMenu(Menu menu) { |
| 83 | super.onCreateOptionsMenu(menu); |
| 84 | menu.add(0, MENU_NEW, 0, |
| 85 | getResources().getString(R.string.menu_new)) |
| 86 | .setIcon(android.R.drawable.ic_menu_add); |
| 87 | return true; |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public boolean onOptionsItemSelected(MenuItem item) { |
| 92 | switch (item.getItemId()) { |
| 93 | case MENU_NEW: |
| 94 | addNewApn(); |
| 95 | return true; |
| 96 | } |
| 97 | return super.onOptionsItemSelected(item); |
| 98 | } |
| 99 | |
| 100 | private void addNewApn() { |
| 101 | startActivity(new Intent(Intent.ACTION_INSERT, Telephony.Carriers.CONTENT_URI)); |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) { |
| 106 | int pos = Integer.parseInt(preference.getKey()); |
| 107 | Uri url = ContentUris.withAppendedId(Telephony.Carriers.CONTENT_URI, pos); |
| 108 | startActivity(new Intent(Intent.ACTION_EDIT, url)); |
| 109 | return true; |
| 110 | } |
| 111 | } |