blob: b1302952429e050bf1f935abf78d979f3932bbc7 [file] [log] [blame]
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -08001package com.android.settings;
2
3import android.app.Activity;
4import android.app.AlertDialog;
Jason Monk39b46742015-09-10 15:52:51 -04005import android.content.DialogInterface;
6import android.os.AsyncResult;
7import android.os.Bundle;
8import android.os.Handler;
9import android.os.Message;
10import android.util.Log;
11import android.view.View;
12import android.view.Window;
13import android.view.WindowManager;
14import android.widget.AdapterView;
15import android.widget.ArrayAdapter;
16import android.widget.ListView;
17
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080018import com.android.internal.telephony.Phone;
19import com.android.internal.telephony.PhoneFactory;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080020
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 */
34public 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
Nathan Harold575fe1a2016-02-12 10:03:36 -080041 //Directly maps to RIL_RadioBandMode from ril.h
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080042 private static final String[] BAND_NAMES = new String[] {
43 "Automatic",
Nathan Harold575fe1a2016-02-12 10:03:36 -080044 "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)"
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080062 };
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);
Derek Taneb4575e2014-04-25 16:47:54 -070076
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080077 setContentView(R.layout.band_mode);
78
79 setTitle(getString(R.string.band_mode_title));
Romain Guy33787152010-01-08 15:07:10 -080080 getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT,
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080081 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
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080091 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
Michael Chan87620932009-05-14 17:47:02 -0700113 static private class BandListItem {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800114 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() {
Derek Taneb4575e2014-04-25 16:47:54 -0700125 if (mBandMode >= BAND_NAMES.length) return "Band mode " + mBandMode;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800126 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;
Nathan Harold575fe1a2016-02-12 10:03:36 -0800158
159 if(bands.length == 0) {
160 Log.wtf(LOG_TAG, "No Supported Band Modes");
161 return;
162 }
163
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800164 int size = bands[0];
165
166 if (size > 0) {
Nathan Haroldf8555e42016-12-19 18:04:52 -0800167 mBandListAdapter.add(
168 new BandListItem(Phone.BM_UNSPECIFIED)); //Always include AUTOMATIC
Nathan Harold575fe1a2016-02-12 10:03:36 -0800169 for (int i=1; i<=size; i++) {
Nathan Haroldf8555e42016-12-19 18:04:52 -0800170 if (bands[i] == Phone.BM_UNSPECIFIED) {
171 continue;
172 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800173 item = new BandListItem(bands[i]);
174 mBandListAdapter.add(item);
175 if (DBG) log("Add " + item.toString());
176 }
177 addBandSuccess = true;
178 }
179 }
180
181 if (addBandSuccess == false) {
182 if (DBG) log("Error in query, add default list");
Nathan Harold575fe1a2016-02-12 10:03:36 -0800183 for (int i=0; i<Phone.BM_NUM_BAND_MODES; i++) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800184 item = new BandListItem(i);
185 mBandListAdapter.add(item);
186 if (DBG) log("Add default " + item.toString());
187 }
188 }
189 mBandList.requestFocus();
190 }
191
192 private void displayBandSelectionResult(Throwable ex) {
193 String status = getString(R.string.band_mode_set)
194 +" [" + mTargetBand.toString() + "] ";
195
196 if (ex != null) {
197 status = status + getString(R.string.band_mode_failed);
198 } else {
199 status = status + getString(R.string.band_mode_succeeded);
200 }
201
202 mProgressPanel = new AlertDialog.Builder(this)
203 .setMessage(status)
204 .setPositiveButton(android.R.string.ok, null).show();
205 }
206
207 private void clearList() {
208 while(mBandListAdapter.getCount() > 0) {
209 mBandListAdapter.remove(
210 mBandListAdapter.getItem(0));
211 }
212 }
213
214 private void log(String msg) {
215 Log.d(LOG_TAG, "[BandsList] " + msg);
216 }
217
218 private Handler mHandler = new Handler() {
219 public void handleMessage(Message msg) {
220 AsyncResult ar;
221 switch (msg.what) {
222 case EVENT_BAND_SCAN_COMPLETED:
223 ar = (AsyncResult) msg.obj;
224
225 bandListLoaded(ar);
226 break;
227
228 case EVENT_BAND_SELECTION_DONE:
229 ar = (AsyncResult) msg.obj;
230
231 getWindow().setFeatureInt(
232 Window.FEATURE_INDETERMINATE_PROGRESS,
233 Window.PROGRESS_VISIBILITY_OFF);
234
Jeevaka Badrappan26ef2c32012-06-01 10:57:31 +0300235 if (!isFinishing()) {
236 displayBandSelectionResult(ar.exception);
237 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800238 break;
239 }
240 }
241 };
242
243
244}