blob: 81e8b4987deb5262c510447fbcb86fae8659aa29 [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;
5import com.android.internal.telephony.Phone;
6import com.android.internal.telephony.PhoneFactory;
7import android.os.Bundle;
8import android.os.Message;
9import android.os.Handler;
10import android.os.AsyncResult;
11import android.util.Log;
12import android.content.DialogInterface;
13import android.view.View;
14import android.view.WindowManager;
15import android.view.Window;
16import android.widget.ListView;
17import android.widget.ArrayAdapter;
18import android.widget.AdapterView;
19
20
21/**
22 * Radio Band Mode Selection Class
23 *
24 * It will query baseband about all available band modes and display them
25 * in screen. It will display all six band modes if the query failed.
26 *
27 * After user select one band, it will send the selection to baseband.
28 *
29 * It will alter user the result of select operation and exit, no matter success
30 * or not.
31 *
32 */
33public class BandMode extends Activity {
34 private static final String LOG_TAG = "phone";
35 private static final boolean DBG = false;
36
37 private static final int EVENT_BAND_SCAN_COMPLETED = 100;
38 private static final int EVENT_BAND_SELECTION_DONE = 200;
39
40 private static final String[] BAND_NAMES = new String[] {
41 "Automatic",
42 "EURO Band",
43 "USA Band",
44 "JAPAN Band",
45 "AUS Band",
46 "AUS2 Band"
47 };
48
49 private ListView mBandList;
50 private ArrayAdapter mBandListAdapter;
51 private BandListItem mTargetBand = null;
52 private DialogInterface mProgressPanel;
53
54 private Phone mPhone = null;
55
56 @Override
57 protected void onCreate(Bundle icicle) {
58 super.onCreate(icicle);
59
60 requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
Derek Taneb4575e2014-04-25 16:47:54 -070061
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080062 setContentView(R.layout.band_mode);
63
64 setTitle(getString(R.string.band_mode_title));
Romain Guy33787152010-01-08 15:07:10 -080065 getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT,
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080066 WindowManager.LayoutParams.WRAP_CONTENT);
67
68 mPhone = PhoneFactory.getDefaultPhone();
69
70 mBandList = (ListView) findViewById(R.id.band);
71 mBandListAdapter = new ArrayAdapter<BandListItem>(this,
72 android.R.layout.simple_list_item_1);
73 mBandList.setAdapter(mBandListAdapter);
74 mBandList.setOnItemClickListener(mBandSelectionHandler);
75
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080076 loadBandList();
77 }
78
79 private AdapterView.OnItemClickListener mBandSelectionHandler =
80 new AdapterView.OnItemClickListener () {
81 public void onItemClick(AdapterView parent, View v,
82 int position, long id) {
83
84 getWindow().setFeatureInt(
85 Window.FEATURE_INDETERMINATE_PROGRESS,
86 Window.PROGRESS_VISIBILITY_ON);
87
88 mTargetBand = (BandListItem) parent.getAdapter().getItem(position);
89
90 if (DBG) log("Select band : " + mTargetBand.toString());
91
92 Message msg =
93 mHandler.obtainMessage(EVENT_BAND_SELECTION_DONE);
94 mPhone.setBandMode(mTargetBand.getBand(), msg);
95 }
96 };
97
Michael Chan87620932009-05-14 17:47:02 -070098 static private class BandListItem {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080099 private int mBandMode = Phone.BM_UNSPECIFIED;
100
101 public BandListItem(int bm) {
102 mBandMode = bm;
103 }
104
105 public int getBand() {
106 return mBandMode;
107 }
108
109 public String toString() {
Derek Taneb4575e2014-04-25 16:47:54 -0700110 if (mBandMode >= BAND_NAMES.length) return "Band mode " + mBandMode;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800111 return BAND_NAMES[mBandMode];
112 }
113 }
114
115 private void loadBandList() {
116 String str = getString(R.string.band_mode_loading);
117
118 if (DBG) log(str);
119
120
121 //ProgressDialog.show(this, null, str, true, true, null);
122 mProgressPanel = new AlertDialog.Builder(this)
123 .setMessage(str)
124 .show();
125
126 Message msg = mHandler.obtainMessage(EVENT_BAND_SCAN_COMPLETED);
127 mPhone.queryAvailableBandMode(msg);
128
129 }
130
131 private void bandListLoaded(AsyncResult result) {
132 if (DBG) log("network list loaded");
133
134 if (mProgressPanel != null) mProgressPanel.dismiss();
135
136 clearList();
137
138 boolean addBandSuccess = false;
139 BandListItem item;
140
141 if (result.result != null) {
142 int bands[] = (int[])result.result;
143 int size = bands[0];
144
145 if (size > 0) {
146 for (int i=1; i<size; i++) {
147 item = new BandListItem(bands[i]);
148 mBandListAdapter.add(item);
149 if (DBG) log("Add " + item.toString());
150 }
151 addBandSuccess = true;
152 }
153 }
154
155 if (addBandSuccess == false) {
156 if (DBG) log("Error in query, add default list");
157 for (int i=0; i<Phone.BM_BOUNDARY; i++) {
158 item = new BandListItem(i);
159 mBandListAdapter.add(item);
160 if (DBG) log("Add default " + item.toString());
161 }
162 }
163 mBandList.requestFocus();
164 }
165
166 private void displayBandSelectionResult(Throwable ex) {
167 String status = getString(R.string.band_mode_set)
168 +" [" + mTargetBand.toString() + "] ";
169
170 if (ex != null) {
171 status = status + getString(R.string.band_mode_failed);
172 } else {
173 status = status + getString(R.string.band_mode_succeeded);
174 }
175
176 mProgressPanel = new AlertDialog.Builder(this)
177 .setMessage(status)
178 .setPositiveButton(android.R.string.ok, null).show();
179 }
180
181 private void clearList() {
182 while(mBandListAdapter.getCount() > 0) {
183 mBandListAdapter.remove(
184 mBandListAdapter.getItem(0));
185 }
186 }
187
188 private void log(String msg) {
189 Log.d(LOG_TAG, "[BandsList] " + msg);
190 }
191
192 private Handler mHandler = new Handler() {
193 public void handleMessage(Message msg) {
194 AsyncResult ar;
195 switch (msg.what) {
196 case EVENT_BAND_SCAN_COMPLETED:
197 ar = (AsyncResult) msg.obj;
198
199 bandListLoaded(ar);
200 break;
201
202 case EVENT_BAND_SELECTION_DONE:
203 ar = (AsyncResult) msg.obj;
204
205 getWindow().setFeatureInt(
206 Window.FEATURE_INDETERMINATE_PROGRESS,
207 Window.PROGRESS_VISIBILITY_OFF);
208
Jeevaka Badrappan26ef2c32012-06-01 10:57:31 +0300209 if (!isFinishing()) {
210 displayBandSelectionResult(ar.exception);
211 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800212 break;
213 }
214 }
215 };
216
217
218}