1 package com.android.settings; 2 3 import android.app.Activity; 4 import android.app.AlertDialog; 5 import android.content.DialogInterface; 6 import android.os.AsyncResult; 7 import android.os.Bundle; 8 import android.os.Handler; 9 import android.os.Message; 10 import android.util.Log; 11 import android.view.View; 12 import android.view.Window; 13 import android.view.WindowManager; 14 import android.widget.AdapterView; 15 import android.widget.ArrayAdapter; 16 import android.widget.ListView; 17 18 import com.android.internal.telephony.Phone; 19 import com.android.internal.telephony.PhoneFactory; 20 21 22 /** 23 * Radio Band Mode Selection Class 24 * 25 * It will query baseband about all available band modes and display them 26 * in screen. It will display all six band modes if the query failed. 27 * 28 * After user select one band, it will send the selection to baseband. 29 * 30 * It will alter user the result of select operation and exit, no matter success 31 * or not. 32 * 33 */ 34 public class BandMode extends Activity { 35 private static final String LOG_TAG = "phone"; 36 private static final boolean DBG = false; 37 38 private static final int EVENT_BAND_SCAN_COMPLETED = 100; 39 private static final int EVENT_BAND_SELECTION_DONE = 200; 40 41 //Directly maps to RIL_RadioBandMode from ril.h 42 private static final String[] BAND_NAMES = new String[] { 43 "Automatic", 44 "Europe", 45 "United States", 46 "Japan", 47 "Australia", 48 "Australia 2", 49 "Cellular 800", 50 "PCS", 51 "Class 3 (JTACS)", 52 "Class 4 (Korea-PCS)", 53 "Class 5", 54 "Class 6 (IMT2000)", 55 "Class 7 (700Mhz-Upper)", 56 "Class 8 (1800Mhz-Upper)", 57 "Class 9 (900Mhz)", 58 "Class 10 (800Mhz-Secondary)", 59 "Class 11 (Europe PAMR 400Mhz)", 60 "Class 15 (US-AWS)", 61 "Class 16 (US-2500Mhz)" 62 }; 63 64 private ListView mBandList; 65 private ArrayAdapter mBandListAdapter; 66 private BandListItem mTargetBand = null; 67 private DialogInterface mProgressPanel; 68 69 private Phone mPhone = null; 70 71 @Override 72 protected void onCreate(Bundle icicle) { 73 super.onCreate(icicle); 74 75 requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); 76 77 setContentView(R.layout.band_mode); 78 79 setTitle(getString(R.string.band_mode_title)); 80 getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, 81 WindowManager.LayoutParams.WRAP_CONTENT); 82 83 mPhone = PhoneFactory.getDefaultPhone(); 84 85 mBandList = (ListView) findViewById(R.id.band); 86 mBandListAdapter = new ArrayAdapter<BandListItem>(this, 87 android.R.layout.simple_list_item_1); 88 mBandList.setAdapter(mBandListAdapter); 89 mBandList.setOnItemClickListener(mBandSelectionHandler); 90 91 loadBandList(); 92 } 93 94 private AdapterView.OnItemClickListener mBandSelectionHandler = 95 new AdapterView.OnItemClickListener () { 96 public void onItemClick(AdapterView parent, View v, 97 int position, long id) { 98 99 getWindow().setFeatureInt( 100 Window.FEATURE_INDETERMINATE_PROGRESS, 101 Window.PROGRESS_VISIBILITY_ON); 102 103 mTargetBand = (BandListItem) parent.getAdapter().getItem(position); 104 105 if (DBG) log("Select band : " + mTargetBand.toString()); 106 107 Message msg = 108 mHandler.obtainMessage(EVENT_BAND_SELECTION_DONE); 109 mPhone.setBandMode(mTargetBand.getBand(), msg); 110 } 111 }; 112 113 static private class BandListItem { 114 private int mBandMode = Phone.BM_UNSPECIFIED; 115 116 public BandListItem(int bm) { 117 mBandMode = bm; 118 } 119 120 public int getBand() { 121 return mBandMode; 122 } 123 124 public String toString() { 125 if (mBandMode >= BAND_NAMES.length) return "Band mode " + mBandMode; 126 return BAND_NAMES[mBandMode]; 127 } 128 } 129 130 private void loadBandList() { 131 String str = getString(R.string.band_mode_loading); 132 133 if (DBG) log(str); 134 135 136 //ProgressDialog.show(this, null, str, true, true, null); 137 mProgressPanel = new AlertDialog.Builder(this) 138 .setMessage(str) 139 .show(); 140 141 Message msg = mHandler.obtainMessage(EVENT_BAND_SCAN_COMPLETED); 142 mPhone.queryAvailableBandMode(msg); 143 144 } 145 146 private void bandListLoaded(AsyncResult result) { 147 if (DBG) log("network list loaded"); 148 149 if (mProgressPanel != null) mProgressPanel.dismiss(); 150 151 clearList(); 152 153 boolean addBandSuccess = false; 154 BandListItem item; 155 156 if (result.result != null) { 157 int bands[] = (int[])result.result; 158 159 if(bands.length == 0) { 160 Log.wtf(LOG_TAG, "No Supported Band Modes"); 161 return; 162 } 163 164 int size = bands[0]; 165 166 if (size > 0) { 167 for (int i=1; i<=size; i++) { 168 item = new BandListItem(bands[i]); 169 mBandListAdapter.add(item); 170 if (DBG) log("Add " + item.toString()); 171 } 172 addBandSuccess = true; 173 } 174 } 175 176 if (addBandSuccess == false) { 177 if (DBG) log("Error in query, add default list"); 178 for (int i=0; i<Phone.BM_NUM_BAND_MODES; i++) { 179 item = new BandListItem(i); 180 mBandListAdapter.add(item); 181 if (DBG) log("Add default " + item.toString()); 182 } 183 } 184 mBandList.requestFocus(); 185 } 186 187 private void displayBandSelectionResult(Throwable ex) { 188 String status = getString(R.string.band_mode_set) 189 +" [" + mTargetBand.toString() + "] "; 190 191 if (ex != null) { 192 status = status + getString(R.string.band_mode_failed); 193 } else { 194 status = status + getString(R.string.band_mode_succeeded); 195 } 196 197 mProgressPanel = new AlertDialog.Builder(this) 198 .setMessage(status) 199 .setPositiveButton(android.R.string.ok, null).show(); 200 } 201 202 private void clearList() { 203 while(mBandListAdapter.getCount() > 0) { 204 mBandListAdapter.remove( 205 mBandListAdapter.getItem(0)); 206 } 207 } 208 209 private void log(String msg) { 210 Log.d(LOG_TAG, "[BandsList] " + msg); 211 } 212 213 private Handler mHandler = new Handler() { 214 public void handleMessage(Message msg) { 215 AsyncResult ar; 216 switch (msg.what) { 217 case EVENT_BAND_SCAN_COMPLETED: 218 ar = (AsyncResult) msg.obj; 219 220 bandListLoaded(ar); 221 break; 222 223 case EVENT_BAND_SELECTION_DONE: 224 ar = (AsyncResult) msg.obj; 225 226 getWindow().setFeatureInt( 227 Window.FEATURE_INDETERMINATE_PROGRESS, 228 Window.PROGRESS_VISIBILITY_OFF); 229 230 if (!isFinishing()) { 231 displayBandSelectionResult(ar.exception); 232 } 233 break; 234 } 235 } 236 }; 237 238 239 } 240