TunerAdapter.java revision ca98cde254fef6c31634d8a3069a0d7b76ecf908
1/**
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;
21import android.graphics.Bitmap;
22import android.os.RemoteException;
23import android.util.Log;
24
25import java.util.List;
26import java.util.Map;
27import java.util.Objects;
28
29/**
30 * Implements the RadioTuner interface by forwarding calls to radio service.
31 */
32class TunerAdapter extends RadioTuner {
33    private static final String TAG = "BroadcastRadio.TunerAdapter";
34
35    @NonNull private final ITuner mTuner;
36    private boolean mIsClosed = false;
37
38    private @RadioManager.Band int mBand;
39
40    TunerAdapter(ITuner tuner, @RadioManager.Band int band) {
41        if (tuner == null) {
42            throw new NullPointerException();
43        }
44        mTuner = tuner;
45        mBand = band;
46    }
47
48    @Override
49    public void close() {
50        synchronized (mTuner) {
51            if (mIsClosed) {
52                Log.v(TAG, "Tuner is already closed");
53                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        }
62    }
63
64    @Override
65    public int setConfiguration(RadioManager.BandConfig config) {
66        if (config == null) return RadioManager.STATUS_BAD_VALUE;
67        try {
68            mTuner.setConfiguration(config);
69            mBand = config.getType();
70            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) {
75            Log.e(TAG, "service died", e);
76            return RadioManager.STATUS_DEAD_OBJECT;
77        }
78    }
79
80    @Override
81    public int getConfiguration(RadioManager.BandConfig[] config) {
82        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) {
89            Log.e(TAG, "service died", e);
90            return RadioManager.STATUS_DEAD_OBJECT;
91        }
92    }
93
94    @Override
95    public int setMute(boolean mute) {
96        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;
106    }
107
108    @Override
109    public boolean getMute() {
110        try {
111            return mTuner.isMuted();
112        } catch (RemoteException e) {
113            Log.e(TAG, "service died", e);
114            return true;
115        }
116    }
117
118    @Override
119    public int step(int direction, boolean skipSubChannel) {
120        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;
130    }
131
132    @Override
133    public int scan(int direction, boolean skipSubChannel) {
134        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;
144    }
145
146    @Override
147    public int tune(int channel, int subChannel) {
148        try {
149            mTuner.tune(ProgramSelector.createAmFmSelector(mBand, channel, subChannel));
150        } 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;
161    }
162
163    @Override
164    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
173    public int cancel() {
174        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;
184    }
185
186    @Override
187    public void cancelAnnouncement() {
188        try {
189            mTuner.cancelAnnouncement();
190        } catch (RemoteException e) {
191            throw new RuntimeException("service died", e);
192        }
193    }
194
195    @Override
196    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 {
201            info[0] = mTuner.getProgramInformation();
202            return RadioManager.STATUS_OK;
203        } catch (RemoteException e) {
204            Log.e(TAG, "service died", e);
205            return RadioManager.STATUS_DEAD_OBJECT;
206        }
207    }
208
209    @Override
210    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
219    public boolean startBackgroundScan() {
220        try {
221            return mTuner.startBackgroundScan();
222        } catch (RemoteException e) {
223            throw new RuntimeException("service died", e);
224        }
225    }
226
227    @Override
228    public @NonNull List<RadioManager.ProgramInfo>
229            getProgramList(@Nullable Map<String, String> vendorFilter) {
230        try {
231            return mTuner.getProgramList(vendorFilter);
232        } catch (RemoteException e) {
233            throw new RuntimeException("service died", e);
234        }
235    }
236
237    @Override
238    public boolean isAnalogForced() {
239        try {
240            return mTuner.isAnalogForced();
241        } catch (RemoteException e) {
242            throw new RuntimeException("service died", e);
243        }
244    }
245
246    @Override
247    public void setAnalogForced(boolean isForced) {
248        try {
249            mTuner.setAnalogForced(isForced);
250        } catch (RemoteException e) {
251            throw new RuntimeException("service died", e);
252        }
253    }
254
255    @Override
256    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
274    public boolean isAntennaConnected() {
275        try {
276            return mTuner.isAntennaConnected();
277        } catch (RemoteException e) {
278            throw new RuntimeException("service died", e);
279        }
280    }
281
282    @Override
283    public boolean hasControl() {
284        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        }
290    }
291}
292