blob: 8ad609d008166c871f64cbbedf8d9675a5a925d3 [file] [log] [blame]
Tomasz Wasilczyk347192e2017-04-04 11:13:44 -07001/**
2 * Copyright (C) 2017 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
17package android.hardware.radio;
18
19import android.annotation.NonNull;
20import android.annotation.Nullable;
Tomasz Wasilczyk4482b142017-07-17 13:57:12 -070021import android.graphics.Bitmap;
Tomasz Wasilczyk347192e2017-04-04 11:13:44 -070022import android.os.RemoteException;
23import android.util.Log;
24
Tomasz Wasilczyk347192e2017-04-04 11:13:44 -070025import java.util.List;
Tomasz Wasilczyk0f1776d2017-08-03 11:03:49 -070026import java.util.Map;
Tomasz Wasilczyk8e932c62017-11-17 16:18:40 +000027import java.util.Objects;
Tomasz Wasilczyk347192e2017-04-04 11:13:44 -070028
29/**
30 * Implements the RadioTuner interface by forwarding calls to radio service.
31 */
32class TunerAdapter extends RadioTuner {
Tomasz Wasilczyk6b4b6462017-07-19 10:52:28 -070033 private static final String TAG = "BroadcastRadio.TunerAdapter";
Tomasz Wasilczyk347192e2017-04-04 11:13:44 -070034
35 @NonNull private final ITuner mTuner;
Tomasz Wasilczykd7c21d32017-04-17 17:02:06 -070036 private boolean mIsClosed = false;
Tomasz Wasilczyk347192e2017-04-04 11:13:44 -070037
Tomasz Wasilczyk8cfb0e82017-07-12 13:59:20 -070038 private @RadioManager.Band int mBand;
39
40 TunerAdapter(ITuner tuner, @RadioManager.Band int band) {
Tomasz Wasilczyk347192e2017-04-04 11:13:44 -070041 if (tuner == null) {
42 throw new NullPointerException();
43 }
44 mTuner = tuner;
Tomasz Wasilczyk8cfb0e82017-07-12 13:59:20 -070045 mBand = band;
Tomasz Wasilczyk347192e2017-04-04 11:13:44 -070046 }
47
48 @Override
49 public void close() {
Tomasz Wasilczykd7c21d32017-04-17 17:02:06 -070050 synchronized (mTuner) {
51 if (mIsClosed) {
Tomasz Wasilczyk14752372017-06-21 11:58:21 -070052 Log.v(TAG, "Tuner is already closed");
Tomasz Wasilczykd7c21d32017-04-17 17:02:06 -070053 return;
54 }
55 mIsClosed = true;
56 }
57 try {
58 mTuner.close();
59 } catch (RemoteException e) {
60 Log.e(TAG, "Exception trying to close tuner", e);
61 }
Tomasz Wasilczyk347192e2017-04-04 11:13:44 -070062 }
63
64 @Override
65 public int setConfiguration(RadioManager.BandConfig config) {
Tomasz Wasilczykca98cde2018-01-04 12:26:40 -080066 if (config == null) return RadioManager.STATUS_BAD_VALUE;
Tomasz Wasilczyk8b6db4f2017-05-01 09:28:36 -070067 try {
68 mTuner.setConfiguration(config);
Tomasz Wasilczyk8cfb0e82017-07-12 13:59:20 -070069 mBand = config.getType();
Tomasz Wasilczyk8b6db4f2017-05-01 09:28:36 -070070 return RadioManager.STATUS_OK;
71 } catch (IllegalArgumentException e) {
72 Log.e(TAG, "Can't set configuration", e);
73 return RadioManager.STATUS_BAD_VALUE;
74 } catch (RemoteException e) {
Tomasz Wasilczyk9fa02872017-05-03 09:09:57 -070075 Log.e(TAG, "service died", e);
76 return RadioManager.STATUS_DEAD_OBJECT;
Tomasz Wasilczyk8b6db4f2017-05-01 09:28:36 -070077 }
Tomasz Wasilczyk347192e2017-04-04 11:13:44 -070078 }
79
80 @Override
81 public int getConfiguration(RadioManager.BandConfig[] config) {
Tomasz Wasilczyk8b6db4f2017-05-01 09:28:36 -070082 if (config == null || config.length != 1) {
83 throw new IllegalArgumentException("The argument must be an array of length 1");
84 }
85 try {
86 config[0] = mTuner.getConfiguration();
87 return RadioManager.STATUS_OK;
88 } catch (RemoteException e) {
Tomasz Wasilczyk9fa02872017-05-03 09:09:57 -070089 Log.e(TAG, "service died", e);
90 return RadioManager.STATUS_DEAD_OBJECT;
Tomasz Wasilczyk8b6db4f2017-05-01 09:28:36 -070091 }
Tomasz Wasilczyk347192e2017-04-04 11:13:44 -070092 }
93
94 @Override
95 public int setMute(boolean mute) {
Tomasz Wasilczyk9fa02872017-05-03 09:09:57 -070096 try {
97 mTuner.setMuted(mute);
98 } catch (IllegalStateException e) {
99 Log.e(TAG, "Can't set muted", e);
100 return RadioManager.STATUS_ERROR;
101 } catch (RemoteException e) {
102 Log.e(TAG, "service died", e);
103 return RadioManager.STATUS_DEAD_OBJECT;
104 }
105 return RadioManager.STATUS_OK;
Tomasz Wasilczyk347192e2017-04-04 11:13:44 -0700106 }
107
108 @Override
109 public boolean getMute() {
Tomasz Wasilczyk9fa02872017-05-03 09:09:57 -0700110 try {
111 return mTuner.isMuted();
112 } catch (RemoteException e) {
113 Log.e(TAG, "service died", e);
114 return true;
115 }
Tomasz Wasilczyk347192e2017-04-04 11:13:44 -0700116 }
117
118 @Override
119 public int step(int direction, boolean skipSubChannel) {
Tomasz Wasilczyk23837932017-05-05 08:42:10 -0700120 try {
121 mTuner.step(direction == RadioTuner.DIRECTION_DOWN, skipSubChannel);
122 } catch (IllegalStateException e) {
123 Log.e(TAG, "Can't step", e);
124 return RadioManager.STATUS_INVALID_OPERATION;
125 } catch (RemoteException e) {
126 Log.e(TAG, "service died", e);
127 return RadioManager.STATUS_DEAD_OBJECT;
128 }
129 return RadioManager.STATUS_OK;
Tomasz Wasilczyk347192e2017-04-04 11:13:44 -0700130 }
131
132 @Override
133 public int scan(int direction, boolean skipSubChannel) {
Tomasz Wasilczyk23837932017-05-05 08:42:10 -0700134 try {
135 mTuner.scan(direction == RadioTuner.DIRECTION_DOWN, skipSubChannel);
136 } catch (IllegalStateException e) {
137 Log.e(TAG, "Can't scan", e);
138 return RadioManager.STATUS_INVALID_OPERATION;
139 } catch (RemoteException e) {
140 Log.e(TAG, "service died", e);
141 return RadioManager.STATUS_DEAD_OBJECT;
142 }
143 return RadioManager.STATUS_OK;
Tomasz Wasilczyk347192e2017-04-04 11:13:44 -0700144 }
145
146 @Override
147 public int tune(int channel, int subChannel) {
Tomasz Wasilczyk37d986d2017-05-08 10:41:32 -0700148 try {
Tomasz Wasilczyk8cfb0e82017-07-12 13:59:20 -0700149 mTuner.tune(ProgramSelector.createAmFmSelector(mBand, channel, subChannel));
Tomasz Wasilczyk37d986d2017-05-08 10:41:32 -0700150 } catch (IllegalStateException e) {
151 Log.e(TAG, "Can't tune", e);
152 return RadioManager.STATUS_INVALID_OPERATION;
153 } catch (IllegalArgumentException e) {
154 Log.e(TAG, "Can't tune", e);
155 return RadioManager.STATUS_BAD_VALUE;
156 } catch (RemoteException e) {
157 Log.e(TAG, "service died", e);
158 return RadioManager.STATUS_DEAD_OBJECT;
159 }
160 return RadioManager.STATUS_OK;
Tomasz Wasilczyk347192e2017-04-04 11:13:44 -0700161 }
162
163 @Override
Tomasz Wasilczyk8cfb0e82017-07-12 13:59:20 -0700164 public void tune(@NonNull ProgramSelector selector) {
165 try {
166 mTuner.tune(selector);
167 } catch (RemoteException e) {
168 throw new RuntimeException("service died", e);
169 }
170 }
171
172 @Override
Tomasz Wasilczyk347192e2017-04-04 11:13:44 -0700173 public int cancel() {
Tomasz Wasilczyk37d986d2017-05-08 10:41:32 -0700174 try {
175 mTuner.cancel();
176 } catch (IllegalStateException e) {
177 Log.e(TAG, "Can't cancel", e);
178 return RadioManager.STATUS_INVALID_OPERATION;
179 } catch (RemoteException e) {
180 Log.e(TAG, "service died", e);
181 return RadioManager.STATUS_DEAD_OBJECT;
182 }
183 return RadioManager.STATUS_OK;
Tomasz Wasilczyk347192e2017-04-04 11:13:44 -0700184 }
185
186 @Override
Tomasz Wasilczykc4cd8232017-07-14 10:46:15 -0700187 public void cancelAnnouncement() {
188 try {
189 mTuner.cancelAnnouncement();
190 } catch (RemoteException e) {
191 throw new RuntimeException("service died", e);
192 }
193 }
194
195 @Override
Tomasz Wasilczyk347192e2017-04-04 11:13:44 -0700196 public int getProgramInformation(RadioManager.ProgramInfo[] info) {
197 if (info == null || info.length != 1) {
198 throw new IllegalArgumentException("The argument must be an array of length 1");
199 }
200 try {
Tomasz Wasilczyk37d986d2017-05-08 10:41:32 -0700201 info[0] = mTuner.getProgramInformation();
202 return RadioManager.STATUS_OK;
Tomasz Wasilczyk347192e2017-04-04 11:13:44 -0700203 } catch (RemoteException e) {
Tomasz Wasilczyk9fa02872017-05-03 09:09:57 -0700204 Log.e(TAG, "service died", e);
205 return RadioManager.STATUS_DEAD_OBJECT;
Tomasz Wasilczyk347192e2017-04-04 11:13:44 -0700206 }
207 }
208
209 @Override
Tomasz Wasilczyk4482b142017-07-17 13:57:12 -0700210 public @Nullable Bitmap getMetadataImage(int id) {
211 try {
212 return mTuner.getImage(id);
213 } catch (RemoteException e) {
214 throw new RuntimeException("service died", e);
215 }
216 }
217
218 @Override
Tomasz Wasilczyk347192e2017-04-04 11:13:44 -0700219 public boolean startBackgroundScan() {
Tomasz Wasilczykd3d53f62017-05-15 12:55:28 -0700220 try {
221 return mTuner.startBackgroundScan();
222 } catch (RemoteException e) {
223 throw new RuntimeException("service died", e);
224 }
Tomasz Wasilczyk347192e2017-04-04 11:13:44 -0700225 }
226
227 @Override
Tomasz Wasilczyk0f1776d2017-08-03 11:03:49 -0700228 public @NonNull List<RadioManager.ProgramInfo>
229 getProgramList(@Nullable Map<String, String> vendorFilter) {
Tomasz Wasilczykd3d53f62017-05-15 12:55:28 -0700230 try {
Tomasz Wasilczyk0f1776d2017-08-03 11:03:49 -0700231 return mTuner.getProgramList(vendorFilter);
Tomasz Wasilczykd3d53f62017-05-15 12:55:28 -0700232 } catch (RemoteException e) {
233 throw new RuntimeException("service died", e);
234 }
Tomasz Wasilczyk347192e2017-04-04 11:13:44 -0700235 }
236
237 @Override
238 public boolean isAnalogForced() {
Tomasz Wasilczykce40fe92018-01-04 20:52:39 -0800239 return isConfigFlagSet(RadioManager.CONFIG_FORCE_ANALOG);
240 }
241
242 @Override
243 public void setAnalogForced(boolean isForced) {
244 setConfigFlag(RadioManager.CONFIG_FORCE_ANALOG, isForced);
245 }
246
247 @Override
248 public boolean isConfigFlagSupported(@RadioManager.ConfigFlag int flag) {
Tomasz Wasilczykd3d53f62017-05-15 12:55:28 -0700249 try {
Tomasz Wasilczykce40fe92018-01-04 20:52:39 -0800250 return mTuner.isConfigFlagSupported(flag);
Tomasz Wasilczykd3d53f62017-05-15 12:55:28 -0700251 } catch (RemoteException e) {
252 throw new RuntimeException("service died", e);
253 }
Tomasz Wasilczyk347192e2017-04-04 11:13:44 -0700254 }
255
256 @Override
Tomasz Wasilczykce40fe92018-01-04 20:52:39 -0800257 public boolean isConfigFlagSet(@RadioManager.ConfigFlag int flag) {
Tomasz Wasilczykd3d53f62017-05-15 12:55:28 -0700258 try {
Tomasz Wasilczykce40fe92018-01-04 20:52:39 -0800259 return mTuner.isConfigFlagSet(flag);
260 } catch (RemoteException e) {
261 throw new RuntimeException("service died", e);
262 }
263 }
264
265 @Override
266 public void setConfigFlag(@RadioManager.ConfigFlag int flag, boolean value) {
267 try {
268 mTuner.setConfigFlag(flag, value);
Tomasz Wasilczykd3d53f62017-05-15 12:55:28 -0700269 } catch (RemoteException e) {
270 throw new RuntimeException("service died", e);
271 }
Tomasz Wasilczyk347192e2017-04-04 11:13:44 -0700272 }
273
274 @Override
Tomasz Wasilczyk8e932c62017-11-17 16:18:40 +0000275 public @NonNull Map<String, String> setParameters(@NonNull Map<String, String> parameters) {
276 try {
277 return mTuner.setParameters(Objects.requireNonNull(parameters));
278 } catch (RemoteException e) {
279 throw new RuntimeException("service died", e);
280 }
281 }
282
283 @Override
284 public @NonNull Map<String, String> getParameters(@NonNull List<String> keys) {
285 try {
286 return mTuner.getParameters(Objects.requireNonNull(keys));
287 } catch (RemoteException e) {
288 throw new RuntimeException("service died", e);
289 }
290 }
291
292 @Override
Tomasz Wasilczyk347192e2017-04-04 11:13:44 -0700293 public boolean isAntennaConnected() {
Tomasz Wasilczyk39ac2142017-05-17 14:55:17 -0700294 try {
295 return mTuner.isAntennaConnected();
296 } catch (RemoteException e) {
297 throw new RuntimeException("service died", e);
298 }
Tomasz Wasilczyk347192e2017-04-04 11:13:44 -0700299 }
300
301 @Override
302 public boolean hasControl() {
Tomasz Wasilczyk468a53b2017-06-19 15:19:14 -0700303 try {
304 // don't rely on mIsClosed, as tuner might get closed internally
305 return !mTuner.isClosed();
306 } catch (RemoteException e) {
307 return false;
308 }
Tomasz Wasilczyk347192e2017-04-04 11:13:44 -0700309 }
310}