blob: 83efa3f4bba20a8bf6e2e00a7a017efa9a388580 [file] [log] [blame]
The Android Open Source Projectde2d9f52008-10-21 07:00:00 -07001/*
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
17package com.android.settings;
18
19import android.content.ContentUris;
20import android.content.Context;
21import android.content.Intent;
22import android.database.Cursor;
23import android.net.Uri;
24import android.os.Bundle;
25import android.preference.Preference;
26import android.preference.PreferenceActivity;
27import android.preference.PreferenceCategory;
28import android.preference.PreferenceScreen;
29import android.provider.Telephony;
The Android Open Source Projectabc48f82008-12-17 18:06:01 -080030import android.text.TextUtils;
The Android Open Source Projectde2d9f52008-10-21 07:00:00 -070031import android.view.Menu;
32import android.view.MenuItem;
33
34public 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 Projectabc48f82008-12-17 18:06:01 -080072 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 Projectde2d9f52008-10-21 07:00:00 -070082 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}