1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.phone.settings;
18 
19 import android.app.Activity;
20 import android.content.DialogInterface;
21 import android.os.AsyncResult;
22 import android.os.Bundle;
23 import android.os.Handler;
24 import android.os.Message;
25 import android.util.Log;
26 import android.view.View;
27 import android.view.Window;
28 import android.widget.AdapterView;
29 import android.widget.ArrayAdapter;
30 import android.widget.ListView;
31 
32 import androidx.appcompat.app.AlertDialog;
33 
34 import com.android.internal.telephony.Phone;
35 import com.android.internal.telephony.PhoneFactory;
36 import com.android.phone.R;
37 
38 /**
39  * Radio Band Mode Selection Class
40  *
41  * It will query baseband about all available band modes and display them
42  * in screen. It will display all six band modes if the query failed.
43  *
44  * After user select one band, it will send the selection to baseband.
45  *
46  * It will alter user the result of select operation and exit, no matter success
47  * or not.
48  *
49  */
50 public class BandMode extends Activity {
51     private static final String LOG_TAG = "phone";
52     private static final boolean DBG = false;
53 
54     private static final int EVENT_BAND_SCAN_COMPLETED = 100;
55     private static final int EVENT_BAND_SELECTION_DONE = 200;
56 
57     //Directly maps to RIL_RadioBandMode from ril.h
58     private static final String[] BAND_NAMES = new String[] {
59             "Automatic",
60             "Europe",
61             "United States",
62             "Japan",
63             "Australia",
64             "Australia 2",
65             "Cellular 800",
66             "PCS",
67             "Class 3 (JTACS)",
68             "Class 4 (Korea-PCS)",
69             "Class 5",
70             "Class 6 (IMT2000)",
71             "Class 7 (700Mhz-Upper)",
72             "Class 8 (1800Mhz-Upper)",
73             "Class 9 (900Mhz)",
74             "Class 10 (800Mhz-Secondary)",
75             "Class 11 (Europe PAMR 400Mhz)",
76             "Class 15 (US-AWS)",
77             "Class 16 (US-2500Mhz)"
78     };
79 
80     private ListView mBandList;
81     private ArrayAdapter mBandListAdapter;
82     private BandListItem mTargetBand = null;
83     private DialogInterface mProgressPanel;
84 
85     private Phone mPhone = null;
86 
87     @Override
onCreate(Bundle icicle)88     protected void onCreate(Bundle icicle) {
89         super.onCreate(icicle);
90 
91         requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
92         setContentView(R.layout.band_mode);
93 
94         mPhone = PhoneFactory.getDefaultPhone();
95 
96         mBandList = (ListView) findViewById(R.id.band);
97         mBandListAdapter = new ArrayAdapter<BandListItem>(this,
98                 android.R.layout.simple_list_item_1);
99         mBandList.setAdapter(mBandListAdapter);
100         mBandList.setOnItemClickListener(mBandSelectionHandler);
101 
102         loadBandList();
103     }
104 
105     private AdapterView.OnItemClickListener mBandSelectionHandler =
106             new AdapterView.OnItemClickListener() {
107                 public void onItemClick(AdapterView parent, View v,
108                         int position, long id) {
109 
110                     getWindow().setFeatureInt(
111                             Window.FEATURE_INDETERMINATE_PROGRESS,
112                             Window.PROGRESS_VISIBILITY_ON);
113 
114                     mTargetBand = (BandListItem) parent.getAdapter().getItem(position);
115 
116                     if (DBG) log("Select band : " + mTargetBand.toString());
117 
118                     Message msg =
119                             mHandler.obtainMessage(EVENT_BAND_SELECTION_DONE);
120                     mPhone.setBandMode(mTargetBand.getBand(), msg);
121                 }
122             };
123 
124     private static class BandListItem {
125         private int mBandMode = Phone.BM_UNSPECIFIED;
126 
BandListItem(int bm)127         BandListItem(int bm) {
128             mBandMode = bm;
129         }
130 
getBand()131         public int getBand() {
132             return mBandMode;
133         }
134 
toString()135         public String toString() {
136             if (mBandMode >= BAND_NAMES.length) return "Band mode " + mBandMode;
137             return BAND_NAMES[mBandMode];
138         }
139     }
140 
loadBandList()141     private void loadBandList() {
142         String str = getString(R.string.band_mode_loading);
143 
144         if (DBG) log(str);
145 
146 
147         //ProgressDialog.show(this, null, str, true, true, null);
148         mProgressPanel = new AlertDialog.Builder(this)
149             .setMessage(str)
150             .show();
151 
152         Message msg = mHandler.obtainMessage(EVENT_BAND_SCAN_COMPLETED);
153         mPhone.queryAvailableBandMode(msg);
154 
155     }
156 
bandListLoaded(AsyncResult result)157     private void bandListLoaded(AsyncResult result) {
158         if (DBG) log("network list loaded");
159 
160         if (mProgressPanel != null) mProgressPanel.dismiss();
161 
162         clearList();
163 
164         boolean addBandSuccess = false;
165         BandListItem item;
166 
167         if (result.result != null) {
168             int [] bands = (int []) result.result;
169 
170             if (bands.length == 0) {
171                 Log.wtf(LOG_TAG, "No Supported Band Modes");
172                 return;
173             }
174 
175             int size = bands[0];
176 
177             if (size > 0) {
178                 mBandListAdapter.add(
179                         new BandListItem(Phone.BM_UNSPECIFIED)); //Always include AUTOMATIC
180                 for (int i = 1; i <= size; i++) {
181                     if (bands[i] == Phone.BM_UNSPECIFIED) {
182                         continue;
183                     }
184                     item = new BandListItem(bands[i]);
185                     mBandListAdapter.add(item);
186                     if (DBG) log("Add " + item.toString());
187                 }
188                 addBandSuccess = true;
189             }
190         }
191 
192         if (!addBandSuccess) {
193             if (DBG) log("Error in query, add default list");
194             for (int i = 0; i < Phone.BM_NUM_BAND_MODES; i++) {
195                 item = new BandListItem(i);
196                 mBandListAdapter.add(item);
197                 if (DBG) log("Add default " + item.toString());
198             }
199         }
200         mBandList.requestFocus();
201     }
202 
displayBandSelectionResult(Throwable ex)203     private void displayBandSelectionResult(Throwable ex) {
204         String status = getString(R.string.band_mode_set)
205                 + " [" + mTargetBand.toString() + "] ";
206 
207         if (ex != null) {
208             status = status + getString(R.string.band_mode_failed);
209         } else {
210             status = status + getString(R.string.band_mode_succeeded);
211         }
212 
213         mProgressPanel = new AlertDialog.Builder(this)
214             .setMessage(status)
215             .setPositiveButton(android.R.string.ok, null).show();
216     }
217 
clearList()218     private void clearList() {
219         while (mBandListAdapter.getCount() > 0) {
220             mBandListAdapter.remove(
221                     mBandListAdapter.getItem(0));
222         }
223     }
224 
log(String msg)225     private void log(String msg) {
226         Log.d(LOG_TAG, "[BandsList] " + msg);
227     }
228 
229     private Handler mHandler = new Handler() {
230         public void handleMessage(Message msg) {
231             AsyncResult ar;
232             switch (msg.what) {
233                 case EVENT_BAND_SCAN_COMPLETED:
234                     ar = (AsyncResult) msg.obj;
235 
236                     bandListLoaded(ar);
237                     break;
238 
239                 case EVENT_BAND_SELECTION_DONE:
240                     ar = (AsyncResult) msg.obj;
241 
242                     getWindow().setFeatureInt(
243                             Window.FEATURE_INDETERMINATE_PROGRESS,
244                             Window.PROGRESS_VISIBILITY_OFF);
245 
246                     if (!isFinishing()) {
247                         displayBandSelectionResult(ar.exception);
248                     }
249                     break;
250             }
251         }
252     };
253 
254 
255 }
256