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