1package com.android.settings;
2
3import android.app.Activity;
4import android.app.AlertDialog;
5import 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
18import com.android.internal.telephony.Phone;
19import 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 */
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    //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                mBandListAdapter.add(
168                        new BandListItem(Phone.BM_UNSPECIFIED)); //Always include AUTOMATIC
169                for (int i=1; i<=size; i++) {
170                    if (bands[i] == Phone.BM_UNSPECIFIED) {
171                        continue;
172                    }
173                    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");
183            for (int i=0; i<Phone.BM_NUM_BAND_MODES; i++) {
184                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
235                    if (!isFinishing()) {
236                        displayBandSelectionResult(ar.exception);
237                    }
238                    break;
239            }
240        }
241    };
242
243
244}
245