• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.android.settings;
2 
3 import android.app.Activity;
4 import android.content.DialogInterface;
5 import android.os.AsyncResult;
6 import android.os.Bundle;
7 import android.os.Handler;
8 import android.os.Message;
9 import android.util.Log;
10 import android.view.View;
11 import android.view.Window;
12 import android.widget.AdapterView;
13 import android.widget.ArrayAdapter;
14 import android.widget.ListView;
15 
16 import androidx.appcompat.app.AlertDialog;
17 
18 import com.android.internal.telephony.Phone;
19 import com.android.internal.telephony.PhoneFactory;
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  */
33 public 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     //Directly maps to RIL_RadioBandMode from ril.h
41     private static final String[] BAND_NAMES = new String[] {
42             "Automatic",
43             "Europe",
44             "United States",
45             "Japan",
46             "Australia",
47             "Australia 2",
48             "Cellular 800",
49             "PCS",
50             "Class 3 (JTACS)",
51             "Class 4 (Korea-PCS)",
52             "Class 5",
53             "Class 6 (IMT2000)",
54             "Class 7 (700Mhz-Upper)",
55             "Class 8 (1800Mhz-Upper)",
56             "Class 9 (900Mhz)",
57             "Class 10 (800Mhz-Secondary)",
58             "Class 11 (Europe PAMR 400Mhz)",
59             "Class 15 (US-AWS)",
60             "Class 16 (US-2500Mhz)"
61     };
62 
63     private ListView mBandList;
64     private ArrayAdapter mBandListAdapter;
65     private BandListItem mTargetBand = null;
66     private DialogInterface mProgressPanel;
67 
68     private Phone mPhone = null;
69 
70     @Override
onCreate(Bundle icicle)71     protected void onCreate(Bundle icicle) {
72         super.onCreate(icicle);
73 
74         requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
75         setContentView(R.layout.band_mode);
76 
77         mPhone = PhoneFactory.getDefaultPhone();
78 
79         mBandList = (ListView) findViewById(R.id.band);
80         mBandListAdapter = new ArrayAdapter<BandListItem>(this,
81                 android.R.layout.simple_list_item_1);
82         mBandList.setAdapter(mBandListAdapter);
83         mBandList.setOnItemClickListener(mBandSelectionHandler);
84 
85         loadBandList();
86     }
87 
88     private AdapterView.OnItemClickListener mBandSelectionHandler =
89             new AdapterView.OnItemClickListener () {
90                 public void onItemClick(AdapterView parent, View v,
91                         int position, long id) {
92 
93                     getWindow().setFeatureInt(
94                             Window.FEATURE_INDETERMINATE_PROGRESS,
95                             Window.PROGRESS_VISIBILITY_ON);
96 
97                     mTargetBand = (BandListItem) parent.getAdapter().getItem(position);
98 
99                     if (DBG) log("Select band : " + mTargetBand.toString());
100 
101                     Message msg =
102                             mHandler.obtainMessage(EVENT_BAND_SELECTION_DONE);
103                     mPhone.setBandMode(mTargetBand.getBand(), msg);
104                 }
105             };
106 
107     static private class BandListItem {
108         private int mBandMode = Phone.BM_UNSPECIFIED;
109 
BandListItem(int bm)110         public BandListItem(int bm) {
111             mBandMode = bm;
112         }
113 
getBand()114         public int getBand() {
115             return mBandMode;
116         }
117 
toString()118         public String toString() {
119             if (mBandMode >= BAND_NAMES.length) return "Band mode " + mBandMode;
120             return BAND_NAMES[mBandMode];
121         }
122     }
123 
loadBandList()124     private void loadBandList() {
125         String str = getString(R.string.band_mode_loading);
126 
127         if (DBG) log(str);
128 
129 
130         //ProgressDialog.show(this, null, str, true, true, null);
131         mProgressPanel = new AlertDialog.Builder(this)
132             .setMessage(str)
133             .show();
134 
135         Message msg = mHandler.obtainMessage(EVENT_BAND_SCAN_COMPLETED);
136         mPhone.queryAvailableBandMode(msg);
137 
138     }
139 
bandListLoaded(AsyncResult result)140     private void bandListLoaded(AsyncResult result) {
141         if (DBG) log("network list loaded");
142 
143         if (mProgressPanel != null) mProgressPanel.dismiss();
144 
145         clearList();
146 
147         boolean addBandSuccess = false;
148         BandListItem item;
149 
150         if (result.result != null) {
151             int bands[] = (int[])result.result;
152 
153             if(bands.length == 0) {
154                 Log.wtf(LOG_TAG, "No Supported Band Modes");
155                 return;
156             }
157 
158             int size = bands[0];
159 
160             if (size > 0) {
161                 mBandListAdapter.add(
162                         new BandListItem(Phone.BM_UNSPECIFIED)); //Always include AUTOMATIC
163                 for (int i=1; i<=size; i++) {
164                     if (bands[i] == Phone.BM_UNSPECIFIED) {
165                         continue;
166                     }
167                     item = new BandListItem(bands[i]);
168                     mBandListAdapter.add(item);
169                     if (DBG) log("Add " + item.toString());
170                 }
171                 addBandSuccess = true;
172             }
173         }
174 
175         if (addBandSuccess == false) {
176             if (DBG) log("Error in query, add default list");
177             for (int i=0; i<Phone.BM_NUM_BAND_MODES; i++) {
178                 item = new BandListItem(i);
179                 mBandListAdapter.add(item);
180                 if (DBG) log("Add default " + item.toString());
181             }
182         }
183         mBandList.requestFocus();
184     }
185 
displayBandSelectionResult(Throwable ex)186     private void displayBandSelectionResult(Throwable ex) {
187         String status = getString(R.string.band_mode_set)
188                 +" [" + mTargetBand.toString() + "] ";
189 
190         if (ex != null) {
191             status = status + getString(R.string.band_mode_failed);
192         } else {
193             status = status + getString(R.string.band_mode_succeeded);
194         }
195 
196         mProgressPanel = new AlertDialog.Builder(this)
197             .setMessage(status)
198             .setPositiveButton(android.R.string.ok, null).show();
199     }
200 
clearList()201     private void clearList() {
202         while(mBandListAdapter.getCount() > 0) {
203             mBandListAdapter.remove(
204                     mBandListAdapter.getItem(0));
205         }
206     }
207 
log(String msg)208     private void log(String msg) {
209         Log.d(LOG_TAG, "[BandsList] " + msg);
210     }
211 
212     private Handler mHandler = new Handler() {
213         public void handleMessage(Message msg) {
214             AsyncResult ar;
215             switch (msg.what) {
216                 case EVENT_BAND_SCAN_COMPLETED:
217                     ar = (AsyncResult) msg.obj;
218 
219                     bandListLoaded(ar);
220                     break;
221 
222                 case EVENT_BAND_SELECTION_DONE:
223                     ar = (AsyncResult) msg.obj;
224 
225                     getWindow().setFeatureInt(
226                             Window.FEATURE_INDETERMINATE_PROGRESS,
227                             Window.PROGRESS_VISIBILITY_OFF);
228 
229                     if (!isFinishing()) {
230                         displayBandSelectionResult(ar.exception);
231                     }
232                     break;
233             }
234         }
235     };
236 
237 
238 }
239