1package 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);
61
62        setContentView(R.layout.band_mode);
63
64        setTitle(getString(R.string.band_mode_title));
65        getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT,
66                                    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
76
77
78        loadBandList();
79    }
80
81    private AdapterView.OnItemClickListener mBandSelectionHandler =
82            new AdapterView.OnItemClickListener () {
83                public void onItemClick(AdapterView parent, View v,
84                        int position, long id) {
85
86                    getWindow().setFeatureInt(
87                            Window.FEATURE_INDETERMINATE_PROGRESS,
88                            Window.PROGRESS_VISIBILITY_ON);
89
90                    mTargetBand = (BandListItem) parent.getAdapter().getItem(position);
91
92                    if (DBG) log("Select band : " + mTargetBand.toString());
93
94                    Message msg =
95                            mHandler.obtainMessage(EVENT_BAND_SELECTION_DONE);
96                    mPhone.setBandMode(mTargetBand.getBand(), msg);
97                }
98            };
99
100    static private class BandListItem {
101        private int mBandMode = Phone.BM_UNSPECIFIED;
102
103        public BandListItem(int bm) {
104            mBandMode = bm;
105        }
106
107        public int getBand() {
108            return mBandMode;
109        }
110
111        public String toString() {
112            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
210                    displayBandSelectionResult(ar.exception);
211                    break;
212            }
213        }
214    };
215
216
217}
218