OSDN Git Service

am 80134266: am 4a2423ba: Move fingerprint enroll logic into sidecar fragment am...
[android-x86/packages-apps-Settings.git] / src / com / android / settings / BandMode.java
1 package com.android.settings;
2
3 import android.app.Activity;
4 import android.app.AlertDialog;
5 import android.content.DialogInterface;
6 import android.os.AsyncResult;
7 import android.os.Bundle;
8 import android.os.Handler;
9 import android.os.Message;
10 import android.util.Log;
11 import android.view.View;
12 import android.view.Window;
13 import android.view.WindowManager;
14 import android.widget.AdapterView;
15 import android.widget.ArrayAdapter;
16 import android.widget.ListView;
17
18 import com.android.internal.telephony.Phone;
19 import 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  */
34 public 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);
62
63         setContentView(R.layout.band_mode);
64
65         setTitle(getString(R.string.band_mode_title));
66         getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT,
67                                     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
77         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
99     static private class BandListItem {
100         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() {
111             if (mBandMode >= BAND_NAMES.length) return "Band mode " + mBandMode;
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                     if (!isFinishing()) {
211                         displayBandSelectionResult(ar.exception);
212                     }
213                     break;
214             }
215         }
216     };
217
218
219 }