blob: a8a896a9c45207ea9b8d9dbd998448ca6ee57f88 [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 Wasilczykd3d53f62017-05-15 12:55:28 -0700239 try {
240 return mTuner.isAnalogForced();
241 } catch (RemoteException e) {
242 throw new RuntimeException("service died", e);
243 }
Tomasz Wasilczyk347192e2017-04-04 11:13:44 -0700244 }
245
246 @Override
247 public void setAnalogForced(boolean isForced) {
Tomasz Wasilczykd3d53f62017-05-15 12:55:28 -0700248 try {
249 mTuner.setAnalogForced(isForced);
250 } catch (RemoteException e) {
251 throw new RuntimeException("service died", e);
252 }
Tomasz Wasilczyk347192e2017-04-04 11:13:44 -0700253 }
254
255 @Override
Tomasz Wasilczyk8e932c62017-11-17 16:18:40 +0000256 public @NonNull Map<String, String> setParameters(@NonNull Map<String, String> parameters) {
257 try {
258 return mTuner.setParameters(Objects.requireNonNull(parameters));
259 } catch (RemoteException e) {
260 throw new RuntimeException("service died", e);
261 }
262 }
263
264 @Override
265 public @NonNull Map<String, String> getParameters(@NonNull List<String> keys) {
266 try {
267 return mTuner.getParameters(Objects.requireNonNull(keys));
268 } catch (RemoteException e) {
269 throw new RuntimeException("service died", e);
270 }
271 }
272
273 @Override
Tomasz Wasilczyk347192e2017-04-04 11:13:44 -0700274 public boolean isAntennaConnected() {
Tomasz Wasilczyk39ac2142017-05-17 14:55:17 -0700275 try {
276 return mTuner.isAntennaConnected();
277 } catch (RemoteException e) {
278 throw new RuntimeException("service died", e);
279 }
Tomasz Wasilczyk347192e2017-04-04 11:13:44 -0700280 }
281
282 @Override
283 public boolean hasControl() {
Tomasz Wasilczyk468a53b2017-06-19 15:19:14 -0700284 try {
285 // don't rely on mIsClosed, as tuner might get closed internally
286 return !mTuner.isClosed();
287 } catch (RemoteException e) {
288 return false;
289 }
Tomasz Wasilczyk347192e2017-04-04 11:13:44 -0700290 }
291}