blob: c145381a00a0e98f0c471b1b82ac8c2c1a94fa84 [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
41 private static final String[] BAND_NAMES = new String[] {
42 "Automatic",
43 "EURO Band",
44 "USA Band",
45 "JAPAN Band",
46 "AUS Band",
47 "AUS2 Band"
48 };
49
50 private ListView mBandList;
51 private ArrayAdapter mBandListAdapter;
52 private BandListItem mTargetBand = null;
53 private DialogInterface mProgressPanel;
54
55 private Phone mPhone = null;
56
57 @Override
58 protected void onCreate(Bundle icicle) {
59 super.onCreate(icicle);
60
61 requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
Derek Taneb4575e2014-04-25 16:47:54 -070062
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080063 setContentView(R.layout.band_mode);
64
65 setTitle(getString(R.string.band_mode_title));
Romain Guy33787152010-01-08 15:07:10 -080066 getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT,
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080067 WindowManager.LayoutParams.WRAP_CONTENT);
68
69 mPhone = PhoneFactory.getDefaultPhone();
70
71 mBandList = (ListView) findViewById(R.id.band);
72 mBandListAdapter = new ArrayAdapter<BandListItem>(this,
73 android.R.layout.simple_list_item_1);
74 mBandList.setAdapter(mBandListAdapter);
75 mBandList.setOnItemClickListener(mBandSelectionHandler);
76
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080077 loadBandList();
78 }
79
80 private AdapterView.OnItemClickListener mBandSelectionHandler =
81 new AdapterView.OnItemClickListener () {
82 public void onItemClick(AdapterView parent, View v,
83 int position, long id) {
84
85 getWindow().setFeatureInt(
86 Window.FEATURE_INDETERMINATE_PROGRESS,
87 Window.PROGRESS_VISIBILITY_ON);
88
89 mTargetBand = (BandListItem) parent.getAdapter().getItem(position);
90
91 if (DBG) log("Select band : " + mTargetBand.toString());
92
93 Message msg =
94 mHandler.obtainMessage(EVENT_BAND_SELECTION_DONE);
95 mPhone.setBandMode(mTargetBand.getBand(), msg);
96 }
97 };
98
Michael Chan87620932009-05-14 17:47:02 -070099 static private class BandListItem {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800100 private int mBandMode = Phone.BM_UNSPECIFIED;
101
102 public BandListItem(int bm) {
103 mBandMode = bm;
104 }
105
106 public int getBand() {
107 return mBandMode;
108 }
109
110 public String toString() {
Derek Taneb4575e2014-04-25 16:47:54 -0700111 if (mBandMode >= BAND_NAMES.length) return "Band mode " + mBandMode;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800112 return BAND_NAMES[mBandMode];
113 }
114 }
115
116 private void loadBandList() {
117 String str = getString(R.string.band_mode_loading);
118
119 if (DBG) log(str);
120
121
122 //ProgressDialog.show(this, null, str, true, true, null);
123 mProgressPanel = new AlertDialog.Builder(this)
124 .setMessage(str)
125 .show();
126
127 Message msg = mHandler.obtainMessage(EVENT_BAND_SCAN_COMPLETED);
128 mPhone.queryAvailableBandMode(msg);
129
130 }
131
132 private void bandListLoaded(AsyncResult result) {
133 if (DBG) log("network list loaded");
134
135 if (mProgressPanel != null) mProgressPanel.dismiss();
136
137 clearList();
138
139 boolean addBandSuccess = false;
140 BandListItem item;
141
142 if (result.result != null) {
143 int bands[] = (int[])result.result;
144 int size = bands[0];
145
146 if (size > 0) {
147 for (int i=1; i<size; i++) {
148 item = new BandListItem(bands[i]);
149 mBandListAdapter.add(item);
150 if (DBG) log("Add " + item.toString());
151 }
152 addBandSuccess = true;
153 }
154 }
155
156 if (addBandSuccess == false) {
157 if (DBG) log("Error in query, add default list");
158 for (int i=0; i<Phone.BM_BOUNDARY; i++) {
159 item = new BandListItem(i);
160 mBandListAdapter.add(item);
161 if (DBG) log("Add default " + item.toString());
162 }
163 }
164 mBandList.requestFocus();
165 }
166
167 private void displayBandSelectionResult(Throwable ex) {
168 String status = getString(R.string.band_mode_set)
169 +" [" + mTargetBand.toString() + "] ";
170
171 if (ex != null) {
172 status = status + getString(R.string.band_mode_failed);
173 } else {
174 status = status + getString(R.string.band_mode_succeeded);
175 }
176
177 mProgressPanel = new AlertDialog.Builder(this)
178 .setMessage(status)
179 .setPositiveButton(android.R.string.ok, null).show();
180 }
181
182 private void clearList() {
183 while(mBandListAdapter.getCount() > 0) {
184 mBandListAdapter.remove(
185 mBandListAdapter.getItem(0));
186 }
187 }
188
189 private void log(String msg) {
190 Log.d(LOG_TAG, "[BandsList] " + msg);
191 }
192
193 private Handler mHandler = new Handler() {
194 public void handleMessage(Message msg) {
195 AsyncResult ar;
196 switch (msg.what) {
197 case EVENT_BAND_SCAN_COMPLETED:
198 ar = (AsyncResult) msg.obj;
199
200 bandListLoaded(ar);
201 break;
202
203 case EVENT_BAND_SELECTION_DONE:
204 ar = (AsyncResult) msg.obj;
205
206 getWindow().setFeatureInt(
207 Window.FEATURE_INDETERMINATE_PROGRESS,
208 Window.PROGRESS_VISIBILITY_OFF);
209
Jeevaka Badrappan26ef2c32012-06-01 10:57:31 +0300210 if (!isFinishing()) {
211 displayBandSelectionResult(ar.exception);
212 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800213 break;
214 }
215 }
216 };
217
218
219}