blob: be2846f870799e3fa4d7e73d5a0431fd07acd768 [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 Wasilczyk436128f2018-01-08 16:46:09 -080036 @NonNull private final TunerCallbackAdapter mCallback;
Tomasz Wasilczykd7c21d32017-04-17 17:02:06 -070037 private boolean mIsClosed = false;
Tomasz Wasilczyk347192e2017-04-04 11:13:44 -070038
Tomasz Wasilczyk8cfb0e82017-07-12 13:59:20 -070039 private @RadioManager.Band int mBand;
40
Tomasz Wasilczyk436128f2018-01-08 16:46:09 -080041 private ProgramList mLegacyListProxy;
42 private Map<String, String> mLegacyListFilter;
43
44 TunerAdapter(@NonNull ITuner tuner, @NonNull TunerCallbackAdapter callback,
45 @RadioManager.Band int band) {
46 mTuner = Objects.requireNonNull(tuner);
47 mCallback = Objects.requireNonNull(callback);
Tomasz Wasilczyk8cfb0e82017-07-12 13:59:20 -070048 mBand = band;
Tomasz Wasilczyk347192e2017-04-04 11:13:44 -070049 }
50
51 @Override
52 public void close() {
Tomasz Wasilczykd7c21d32017-04-17 17:02:06 -070053 synchronized (mTuner) {
54 if (mIsClosed) {
Tomasz Wasilczyk14752372017-06-21 11:58:21 -070055 Log.v(TAG, "Tuner is already closed");
Tomasz Wasilczykd7c21d32017-04-17 17:02:06 -070056 return;
57 }
58 mIsClosed = true;
Tomasz Wasilczyk436128f2018-01-08 16:46:09 -080059 if (mLegacyListProxy != null) {
60 mLegacyListProxy.close();
61 mLegacyListProxy = null;
62 }
Tomasz Wasilczykd0c78f92018-03-28 17:50:08 -070063 mCallback.close();
Tomasz Wasilczykd7c21d32017-04-17 17:02:06 -070064 }
65 try {
66 mTuner.close();
67 } catch (RemoteException e) {
68 Log.e(TAG, "Exception trying to close tuner", e);
69 }
Tomasz Wasilczyk347192e2017-04-04 11:13:44 -070070 }
71
72 @Override
73 public int setConfiguration(RadioManager.BandConfig config) {
Tomasz Wasilczykca98cde2018-01-04 12:26:40 -080074 if (config == null) return RadioManager.STATUS_BAD_VALUE;
Tomasz Wasilczyk8b6db4f2017-05-01 09:28:36 -070075 try {
76 mTuner.setConfiguration(config);
Tomasz Wasilczyk8cfb0e82017-07-12 13:59:20 -070077 mBand = config.getType();
Tomasz Wasilczyk8b6db4f2017-05-01 09:28:36 -070078 return RadioManager.STATUS_OK;
79 } catch (IllegalArgumentException e) {
80 Log.e(TAG, "Can't set configuration", e);
81 return RadioManager.STATUS_BAD_VALUE;
82 } catch (RemoteException e) {
Tomasz Wasilczyk9fa02872017-05-03 09:09:57 -070083 Log.e(TAG, "service died", e);
84 return RadioManager.STATUS_DEAD_OBJECT;
Tomasz Wasilczyk8b6db4f2017-05-01 09:28:36 -070085 }
Tomasz Wasilczyk347192e2017-04-04 11:13:44 -070086 }
87
88 @Override
89 public int getConfiguration(RadioManager.BandConfig[] config) {
Tomasz Wasilczyk8b6db4f2017-05-01 09:28:36 -070090 if (config == null || config.length != 1) {
91 throw new IllegalArgumentException("The argument must be an array of length 1");
92 }
93 try {
94 config[0] = mTuner.getConfiguration();
95 return RadioManager.STATUS_OK;
96 } catch (RemoteException e) {
Tomasz Wasilczyk9fa02872017-05-03 09:09:57 -070097 Log.e(TAG, "service died", e);
98 return RadioManager.STATUS_DEAD_OBJECT;
Tomasz Wasilczyk8b6db4f2017-05-01 09:28:36 -070099 }
Tomasz Wasilczyk347192e2017-04-04 11:13:44 -0700100 }
101
102 @Override
103 public int setMute(boolean mute) {
Tomasz Wasilczyk9fa02872017-05-03 09:09:57 -0700104 try {
105 mTuner.setMuted(mute);
106 } catch (IllegalStateException e) {
107 Log.e(TAG, "Can't set muted", e);
108 return RadioManager.STATUS_ERROR;
109 } catch (RemoteException e) {
110 Log.e(TAG, "service died", e);
111 return RadioManager.STATUS_DEAD_OBJECT;
112 }
113 return RadioManager.STATUS_OK;
Tomasz Wasilczyk347192e2017-04-04 11:13:44 -0700114 }
115
116 @Override
117 public boolean getMute() {
Tomasz Wasilczyk9fa02872017-05-03 09:09:57 -0700118 try {
119 return mTuner.isMuted();
120 } catch (RemoteException e) {
121 Log.e(TAG, "service died", e);
122 return true;
123 }
Tomasz Wasilczyk347192e2017-04-04 11:13:44 -0700124 }
125
126 @Override
127 public int step(int direction, boolean skipSubChannel) {
Tomasz Wasilczyk23837932017-05-05 08:42:10 -0700128 try {
129 mTuner.step(direction == RadioTuner.DIRECTION_DOWN, skipSubChannel);
130 } catch (IllegalStateException e) {
131 Log.e(TAG, "Can't step", e);
132 return RadioManager.STATUS_INVALID_OPERATION;
133 } catch (RemoteException e) {
134 Log.e(TAG, "service died", e);
135 return RadioManager.STATUS_DEAD_OBJECT;
136 }
137 return RadioManager.STATUS_OK;
Tomasz Wasilczyk347192e2017-04-04 11:13:44 -0700138 }
139
140 @Override
141 public int scan(int direction, boolean skipSubChannel) {
Tomasz Wasilczyk23837932017-05-05 08:42:10 -0700142 try {
143 mTuner.scan(direction == RadioTuner.DIRECTION_DOWN, skipSubChannel);
144 } catch (IllegalStateException e) {
145 Log.e(TAG, "Can't scan", e);
146 return RadioManager.STATUS_INVALID_OPERATION;
147 } catch (RemoteException e) {
148 Log.e(TAG, "service died", e);
149 return RadioManager.STATUS_DEAD_OBJECT;
150 }
151 return RadioManager.STATUS_OK;
Tomasz Wasilczyk347192e2017-04-04 11:13:44 -0700152 }
153
154 @Override
155 public int tune(int channel, int subChannel) {
Tomasz Wasilczyk37d986d2017-05-08 10:41:32 -0700156 try {
Tomasz Wasilczyk8cfb0e82017-07-12 13:59:20 -0700157 mTuner.tune(ProgramSelector.createAmFmSelector(mBand, channel, subChannel));
Tomasz Wasilczyk37d986d2017-05-08 10:41:32 -0700158 } catch (IllegalStateException e) {
159 Log.e(TAG, "Can't tune", e);
160 return RadioManager.STATUS_INVALID_OPERATION;
161 } catch (IllegalArgumentException e) {
162 Log.e(TAG, "Can't tune", e);
163 return RadioManager.STATUS_BAD_VALUE;
164 } catch (RemoteException e) {
165 Log.e(TAG, "service died", e);
166 return RadioManager.STATUS_DEAD_OBJECT;
167 }
168 return RadioManager.STATUS_OK;
Tomasz Wasilczyk347192e2017-04-04 11:13:44 -0700169 }
170
171 @Override
Tomasz Wasilczyk8cfb0e82017-07-12 13:59:20 -0700172 public void tune(@NonNull ProgramSelector selector) {
173 try {
174 mTuner.tune(selector);
175 } catch (RemoteException e) {
176 throw new RuntimeException("service died", e);
177 }
178 }
179
180 @Override
Tomasz Wasilczyk347192e2017-04-04 11:13:44 -0700181 public int cancel() {
Tomasz Wasilczyk37d986d2017-05-08 10:41:32 -0700182 try {
183 mTuner.cancel();
184 } catch (IllegalStateException e) {
185 Log.e(TAG, "Can't cancel", e);
186 return RadioManager.STATUS_INVALID_OPERATION;
187 } catch (RemoteException e) {
188 Log.e(TAG, "service died", e);
189 return RadioManager.STATUS_DEAD_OBJECT;
190 }
191 return RadioManager.STATUS_OK;
Tomasz Wasilczyk347192e2017-04-04 11:13:44 -0700192 }
193
194 @Override
Tomasz Wasilczykc4cd8232017-07-14 10:46:15 -0700195 public void cancelAnnouncement() {
196 try {
197 mTuner.cancelAnnouncement();
198 } catch (RemoteException e) {
199 throw new RuntimeException("service died", e);
200 }
201 }
202
203 @Override
Tomasz Wasilczyk347192e2017-04-04 11:13:44 -0700204 public int getProgramInformation(RadioManager.ProgramInfo[] info) {
205 if (info == null || info.length != 1) {
Tomasz Wasilczyk3b4465e2018-01-14 21:47:44 -0800206 Log.e(TAG, "The argument must be an array of length 1");
207 return RadioManager.STATUS_BAD_VALUE;
Tomasz Wasilczyk347192e2017-04-04 11:13:44 -0700208 }
Tomasz Wasilczyk3b4465e2018-01-14 21:47:44 -0800209
210 RadioManager.ProgramInfo current = mCallback.getCurrentProgramInformation();
211 if (current == null) {
212 Log.w(TAG, "Didn't get program info yet");
213 return RadioManager.STATUS_INVALID_OPERATION;
Tomasz Wasilczyk347192e2017-04-04 11:13:44 -0700214 }
Tomasz Wasilczyk3b4465e2018-01-14 21:47:44 -0800215 info[0] = current;
216 return RadioManager.STATUS_OK;
Tomasz Wasilczyk347192e2017-04-04 11:13:44 -0700217 }
218
219 @Override
Tomasz Wasilczyk4482b142017-07-17 13:57:12 -0700220 public @Nullable Bitmap getMetadataImage(int id) {
221 try {
222 return mTuner.getImage(id);
223 } catch (RemoteException e) {
224 throw new RuntimeException("service died", e);
225 }
226 }
227
228 @Override
Tomasz Wasilczyk347192e2017-04-04 11:13:44 -0700229 public boolean startBackgroundScan() {
Tomasz Wasilczykd3d53f62017-05-15 12:55:28 -0700230 try {
231 return mTuner.startBackgroundScan();
232 } catch (RemoteException e) {
233 throw new RuntimeException("service died", e);
234 }
Tomasz Wasilczyk347192e2017-04-04 11:13:44 -0700235 }
236
237 @Override
Tomasz Wasilczyk0f1776d2017-08-03 11:03:49 -0700238 public @NonNull List<RadioManager.ProgramInfo>
239 getProgramList(@Nullable Map<String, String> vendorFilter) {
Tomasz Wasilczyk436128f2018-01-08 16:46:09 -0800240 synchronized (mTuner) {
241 if (mLegacyListProxy == null || !Objects.equals(mLegacyListFilter, vendorFilter)) {
242 Log.i(TAG, "Program list filter has changed, requesting new list");
243 mLegacyListProxy = new ProgramList();
244 mLegacyListFilter = vendorFilter;
245
246 mCallback.clearLastCompleteList();
247 mCallback.setProgramListObserver(mLegacyListProxy, () -> { });
248 try {
249 mTuner.startProgramListUpdates(new ProgramList.Filter(vendorFilter));
250 } catch (RemoteException ex) {
251 throw new RuntimeException("service died", ex);
252 }
253 }
254
255 List<RadioManager.ProgramInfo> list = mCallback.getLastCompleteList();
256 if (list == null) throw new IllegalStateException("Program list is not ready yet");
257 return list;
258 }
259 }
260
261 @Override
262 public @Nullable ProgramList getDynamicProgramList(@Nullable ProgramList.Filter filter) {
263 synchronized (mTuner) {
264 if (mLegacyListProxy != null) {
265 mLegacyListProxy.close();
266 mLegacyListProxy = null;
267 }
268 mLegacyListFilter = null;
269
270 ProgramList list = new ProgramList();
271 mCallback.setProgramListObserver(list, () -> {
272 try {
273 mTuner.stopProgramListUpdates();
274 } catch (RemoteException ex) {
275 Log.e(TAG, "Couldn't stop program list updates", ex);
276 }
277 });
278
279 try {
280 mTuner.startProgramListUpdates(filter);
281 } catch (UnsupportedOperationException ex) {
Tomasz Wasilczykd0c78f92018-03-28 17:50:08 -0700282 Log.i(TAG, "Program list is not supported with this hardware");
Tomasz Wasilczyk436128f2018-01-08 16:46:09 -0800283 return null;
284 } catch (RemoteException ex) {
285 mCallback.setProgramListObserver(null, () -> { });
286 throw new RuntimeException("service died", ex);
287 }
288
289 return list;
Tomasz Wasilczykd3d53f62017-05-15 12:55:28 -0700290 }
Tomasz Wasilczyk347192e2017-04-04 11:13:44 -0700291 }
292
293 @Override
294 public boolean isAnalogForced() {
Tomasz Wasilczyk3b4465e2018-01-14 21:47:44 -0800295 try {
296 return isConfigFlagSet(RadioManager.CONFIG_FORCE_ANALOG);
297 } catch (UnsupportedOperationException ex) {
298 throw new IllegalStateException(ex);
299 }
Tomasz Wasilczykce40fe92018-01-04 20:52:39 -0800300 }
301
302 @Override
303 public void setAnalogForced(boolean isForced) {
Tomasz Wasilczyk3b4465e2018-01-14 21:47:44 -0800304 try {
305 setConfigFlag(RadioManager.CONFIG_FORCE_ANALOG, isForced);
306 } catch (UnsupportedOperationException ex) {
307 throw new IllegalStateException(ex);
308 }
Tomasz Wasilczykce40fe92018-01-04 20:52:39 -0800309 }
310
311 @Override
312 public boolean isConfigFlagSupported(@RadioManager.ConfigFlag int flag) {
Tomasz Wasilczykd3d53f62017-05-15 12:55:28 -0700313 try {
Tomasz Wasilczykce40fe92018-01-04 20:52:39 -0800314 return mTuner.isConfigFlagSupported(flag);
Tomasz Wasilczykd3d53f62017-05-15 12:55:28 -0700315 } catch (RemoteException e) {
316 throw new RuntimeException("service died", e);
317 }
Tomasz Wasilczyk347192e2017-04-04 11:13:44 -0700318 }
319
320 @Override
Tomasz Wasilczykce40fe92018-01-04 20:52:39 -0800321 public boolean isConfigFlagSet(@RadioManager.ConfigFlag int flag) {
Tomasz Wasilczykd3d53f62017-05-15 12:55:28 -0700322 try {
Tomasz Wasilczykce40fe92018-01-04 20:52:39 -0800323 return mTuner.isConfigFlagSet(flag);
324 } catch (RemoteException e) {
325 throw new RuntimeException("service died", e);
326 }
327 }
328
329 @Override
330 public void setConfigFlag(@RadioManager.ConfigFlag int flag, boolean value) {
331 try {
332 mTuner.setConfigFlag(flag, value);
Tomasz Wasilczykd3d53f62017-05-15 12:55:28 -0700333 } catch (RemoteException e) {
334 throw new RuntimeException("service died", e);
335 }
Tomasz Wasilczyk347192e2017-04-04 11:13:44 -0700336 }
337
338 @Override
Tomasz Wasilczyk8e932c62017-11-17 16:18:40 +0000339 public @NonNull Map<String, String> setParameters(@NonNull Map<String, String> parameters) {
340 try {
341 return mTuner.setParameters(Objects.requireNonNull(parameters));
342 } catch (RemoteException e) {
343 throw new RuntimeException("service died", e);
344 }
345 }
346
347 @Override
348 public @NonNull Map<String, String> getParameters(@NonNull List<String> keys) {
349 try {
350 return mTuner.getParameters(Objects.requireNonNull(keys));
351 } catch (RemoteException e) {
352 throw new RuntimeException("service died", e);
353 }
354 }
355
356 @Override
Tomasz Wasilczyk347192e2017-04-04 11:13:44 -0700357 public boolean isAntennaConnected() {
Tomasz Wasilczyk3b4465e2018-01-14 21:47:44 -0800358 return mCallback.isAntennaConnected();
Tomasz Wasilczyk347192e2017-04-04 11:13:44 -0700359 }
360
361 @Override
362 public boolean hasControl() {
Tomasz Wasilczyk468a53b2017-06-19 15:19:14 -0700363 try {
364 // don't rely on mIsClosed, as tuner might get closed internally
365 return !mTuner.isClosed();
366 } catch (RemoteException e) {
367 return false;
368 }
Tomasz Wasilczyk347192e2017-04-04 11:13:44 -0700369 }
370}