ApnSettings.java revision afc4ab2ffbb8327ddce9907961295a32cbf49d0f
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 17package com.android.settings; 18 19import android.app.Dialog; 20import android.app.ProgressDialog; 21import android.content.ContentResolver; 22import android.content.ContentUris; 23import android.content.Context; 24import android.content.Intent; 25import android.database.Cursor; 26import android.net.Uri; 27import android.os.Bundle; 28import android.os.Handler; 29import android.os.HandlerThread; 30import android.os.Looper; 31import android.os.Message; 32import android.preference.Preference; 33import android.preference.PreferenceActivity; 34import android.preference.PreferenceGroup; 35import android.preference.PreferenceScreen; 36import android.provider.Telephony; 37import android.text.TextUtils; 38import android.view.Menu; 39import android.view.MenuItem; 40import android.widget.Toast; 41 42public class ApnSettings extends PreferenceActivity { 43 44 public static final String EXTRA_POSITION = "position"; 45 public static final String RESTORE_CARRIERS_URI = 46 "content://telephony/carriers/restore"; 47 48 private static final int ID_INDEX = 0; 49 private static final int NAME_INDEX = 1; 50 private static final int APN_INDEX = 2; 51 52 private static final int MENU_NEW = Menu.FIRST; 53 private static final int MENU_RESTORE = Menu.FIRST + 1; 54 55 private static final int EVENT_RESTORE_DEFAULTAPN_START = 1; 56 private static final int EVENT_RESTORE_DEFAULTAPN_COMPLETE = 2; 57 58 private static final int DIALOG_RESTORE_DEFAULTAPN = 1001; 59 60 private static final Uri DEFAULTAPN_URI = Uri.parse(RESTORE_CARRIERS_URI); 61 62 private static boolean mRestoreDefaultApnMode; 63 64 private RestoreApnUiHandler mRestoreApnUiHandler; 65 private RestoreApnProcessHandler mRestoreApnProcessHandler; 66 67 private Cursor mCursor; 68 69 @Override 70 protected void onCreate(Bundle icicle) { 71 super.onCreate(icicle); 72 73 addPreferencesFromResource(R.xml.apn_settings); 74 } 75 76 @Override 77 protected void onResume() { 78 super.onResume(); 79 80 if (!mRestoreDefaultApnMode) { 81 fillList(); 82 } else { 83 showDialog(DIALOG_RESTORE_DEFAULTAPN); 84 } 85 } 86 87 private void fillList() { 88 mCursor = managedQuery(Telephony.Carriers.CONTENT_URI, new String[] { 89 "_id", "name", "apn"}, null, Telephony.Carriers.DEFAULT_SORT_ORDER); 90 91 PreferenceGroup apnList = (PreferenceGroup) findPreference("apn_list"); 92 apnList.removeAll(); 93 94 mCursor.moveToFirst(); 95 while (!mCursor.isAfterLast()) { 96 String name = mCursor.getString(NAME_INDEX); 97 String apn = mCursor.getString(APN_INDEX); 98 99 if (name != null && apn != null && TextUtils.getTrimmedLength(name) > 0 100 && TextUtils.getTrimmedLength(apn) > 0) { 101 Preference pref = new Preference((Context) this); 102 pref.setKey(mCursor.getString(ID_INDEX)); 103 pref.setTitle(name); 104 pref.setSummary(apn); 105 pref.setPersistent(false); 106 apnList.addPreference(pref); 107 } 108 109 mCursor.moveToNext(); 110 } 111 } 112 113 @Override 114 public boolean onCreateOptionsMenu(Menu menu) { 115 super.onCreateOptionsMenu(menu); 116 menu.add(0, MENU_NEW, 0, 117 getResources().getString(R.string.menu_new)) 118 .setIcon(android.R.drawable.ic_menu_add); 119 menu.add(0, MENU_RESTORE, 0, 120 getResources().getString(R.string.menu_restore)) 121 .setIcon(android.R.drawable.ic_menu_upload); 122 return true; 123 } 124 125 @Override 126 public boolean onOptionsItemSelected(MenuItem item) { 127 switch (item.getItemId()) { 128 case MENU_NEW: 129 addNewApn(); 130 return true; 131 132 case MENU_RESTORE: 133 restoreDefaultApn(); 134 return true; 135 } 136 return super.onOptionsItemSelected(item); 137 } 138 139 private void addNewApn() { 140 startActivity(new Intent(Intent.ACTION_INSERT, Telephony.Carriers.CONTENT_URI)); 141 } 142 143 @Override 144 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) { 145 int pos = Integer.parseInt(preference.getKey()); 146 Uri url = ContentUris.withAppendedId(Telephony.Carriers.CONTENT_URI, pos); 147 startActivity(new Intent(Intent.ACTION_EDIT, url)); 148 return true; 149 } 150 151 private boolean restoreDefaultApn() { 152 showDialog(DIALOG_RESTORE_DEFAULTAPN); 153 mRestoreDefaultApnMode = true; 154 155 if (mRestoreApnUiHandler == null) { 156 mRestoreApnUiHandler = new RestoreApnUiHandler(); 157 } 158 159 if (mRestoreApnProcessHandler == null) { 160 HandlerThread restoreDefaultApnThread = new HandlerThread( 161 "Restore default APN Handler: Process Thread"); 162 restoreDefaultApnThread.start(); 163 mRestoreApnProcessHandler = new RestoreApnProcessHandler( 164 restoreDefaultApnThread.getLooper(), mRestoreApnUiHandler); 165 } 166 167 mRestoreApnProcessHandler 168 .sendEmptyMessage(EVENT_RESTORE_DEFAULTAPN_START); 169 return true; 170 } 171 172 private class RestoreApnUiHandler extends Handler { 173 @Override 174 public void handleMessage(Message msg) { 175 switch (msg.what) { 176 case EVENT_RESTORE_DEFAULTAPN_COMPLETE: 177 fillList(); 178 getPreferenceScreen().setEnabled(true); 179 mRestoreDefaultApnMode = false; 180 dismissDialog(DIALOG_RESTORE_DEFAULTAPN); 181 Toast.makeText( 182 ApnSettings.this, 183 getResources().getString( 184 R.string.restore_default_apn_completed), 185 Toast.LENGTH_LONG).show(); 186 break; 187 } 188 } 189 } 190 191 private class RestoreApnProcessHandler extends Handler { 192 private Handler mRestoreApnUiHandler; 193 194 public RestoreApnProcessHandler(Looper looper, Handler restoreApnUiHandler) { 195 super(looper); 196 this.mRestoreApnUiHandler = restoreApnUiHandler; 197 } 198 199 @Override 200 public void handleMessage(Message msg) { 201 switch (msg.what) { 202 case EVENT_RESTORE_DEFAULTAPN_START: 203 ContentResolver resolver = getContentResolver(); 204 resolver.delete(DEFAULTAPN_URI, null, null); 205 mRestoreApnUiHandler 206 .sendEmptyMessage(EVENT_RESTORE_DEFAULTAPN_COMPLETE); 207 break; 208 } 209 } 210 } 211 212 @Override 213 protected Dialog onCreateDialog(int id) { 214 if (id == DIALOG_RESTORE_DEFAULTAPN) { 215 ProgressDialog dialog = new ProgressDialog(this); 216 dialog.setMessage(getResources().getString(R.string.restore_default_apn)); 217 dialog.setCancelable(false); 218 return dialog; 219 } 220 return null; 221 } 222 223 @Override 224 protected void onPrepareDialog(int id, Dialog dialog) { 225 if (id == DIALOG_RESTORE_DEFAULTAPN) { 226 getPreferenceScreen().setEnabled(false); 227 } 228 } 229} 230