TunerAdapter.java revision 0f1776d08b43f5f71a9f89b6a4f1838df4d3d744
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;
27
28/**
29 * Implements the RadioTuner interface by forwarding calls to radio service.
30 */
31class TunerAdapter extends RadioTuner {
32    private static final String TAG = "BroadcastRadio.TunerAdapter";
33
34    @NonNull private final ITuner mTuner;
35    private boolean mIsClosed = false;
36
37    private @RadioManager.Band int mBand;
38
39    TunerAdapter(ITuner tuner, @RadioManager.Band int band) {
40        if (tuner == null) {
41            throw new NullPointerException();
42        }
43        mTuner = tuner;
44        mBand = band;
45    }
46
47    @Override
48    public void close() {
49        synchronized (mTuner) {
50            if (mIsClosed) {
51                Log.v(TAG, "Tuner is already closed");
52                return;
53            }
54            mIsClosed = true;
55        }
56        try {
57            mTuner.close();
58        } catch (RemoteException e) {
59            Log.e(TAG, "Exception trying to close tuner", e);
60        }
61    }
62
63    @Override
64    public int setConfiguration(RadioManager.BandConfig config) {
65        try {
66            mTuner.setConfiguration(config);
67            mBand = config.getType();
68            return RadioManager.STATUS_OK;
69        } catch (IllegalArgumentException e) {
70            Log.e(TAG, "Can't set configuration", e);
71            return RadioManager.STATUS_BAD_VALUE;
72        } catch (RemoteException e) {
73            Log.e(TAG, "service died", e);
74            return RadioManager.STATUS_DEAD_OBJECT;
75        }
76    }
77
78    @Override
79    public int getConfiguration(RadioManager.BandConfig[] config) {
80        if (config == null || config.length != 1) {
81            throw new IllegalArgumentException("The argument must be an array of length 1");
82        }
83        try {
84            config[0] = mTuner.getConfiguration();
85            return RadioManager.STATUS_OK;
86        } catch (RemoteException e) {
87            Log.e(TAG, "service died", e);
88            return RadioManager.STATUS_DEAD_OBJECT;
89        }
90    }
91
92    @Override
93    public int setMute(boolean mute) {
94        try {
95            mTuner.setMuted(mute);
96        } catch (IllegalStateException e) {
97            Log.e(TAG, "Can't set muted", e);
98            return RadioManager.STATUS_ERROR;
99        } catch (RemoteException e) {
100            Log.e(TAG, "service died", e);
101            return RadioManager.STATUS_DEAD_OBJECT;
102        }
103        return RadioManager.STATUS_OK;
104    }
105
106    @Override
107    public boolean getMute() {
108        try {
109            return mTuner.isMuted();
110        } catch (RemoteException e) {
111            Log.e(TAG, "service died", e);
112            return true;
113        }
114    }
115
116    @Override
117    public int step(int direction, boolean skipSubChannel) {
118        try {
119            mTuner.step(direction == RadioTuner.DIRECTION_DOWN, skipSubChannel);
120        } catch (IllegalStateException e) {
121            Log.e(TAG, "Can't step", e);
122            return RadioManager.STATUS_INVALID_OPERATION;
123        } catch (RemoteException e) {
124            Log.e(TAG, "service died", e);
125            return RadioManager.STATUS_DEAD_OBJECT;
126        }
127        return RadioManager.STATUS_OK;
128    }
129
130    @Override
131    public int scan(int direction, boolean skipSubChannel) {
132        try {
133            mTuner.scan(direction == RadioTuner.DIRECTION_DOWN, skipSubChannel);
134        } catch (IllegalStateException e) {
135            Log.e(TAG, "Can't scan", e);
136            return RadioManager.STATUS_INVALID_OPERATION;
137        } catch (RemoteException e) {
138            Log.e(TAG, "service died", e);
139            return RadioManager.STATUS_DEAD_OBJECT;
140        }
141        return RadioManager.STATUS_OK;
142    }
143
144    @Override
145    public int tune(int channel, int subChannel) {
146        try {
147            mTuner.tune(ProgramSelector.createAmFmSelector(mBand, channel, subChannel));
148        } catch (IllegalStateException e) {
149            Log.e(TAG, "Can't tune", e);
150            return RadioManager.STATUS_INVALID_OPERATION;
151        } catch (IllegalArgumentException e) {
152            Log.e(TAG, "Can't tune", e);
153            return RadioManager.STATUS_BAD_VALUE;
154        } catch (RemoteException e) {
155            Log.e(TAG, "service died", e);
156            return RadioManager.STATUS_DEAD_OBJECT;
157        }
158        return RadioManager.STATUS_OK;
159    }
160
161    @Override
162    public void tune(@NonNull ProgramSelector selector) {
163        try {
164            mTuner.tune(selector);
165        } catch (RemoteException e) {
166            throw new RuntimeException("service died", e);
167        }
168    }
169
170    @Override
171    public int cancel() {
172        try {
173            mTuner.cancel();
174        } catch (IllegalStateException e) {
175            Log.e(TAG, "Can't cancel", e);
176            return RadioManager.STATUS_INVALID_OPERATION;
177        } catch (RemoteException e) {
178            Log.e(TAG, "service died", e);
179            return RadioManager.STATUS_DEAD_OBJECT;
180        }
181        return RadioManager.STATUS_OK;
182    }
183
184    @Override
185    public void cancelAnnouncement() {
186        try {
187            mTuner.cancelAnnouncement();
188        } catch (RemoteException e) {
189            throw new RuntimeException("service died", e);
190        }
191    }
192
193    @Override
194    public int getProgramInformation(RadioManager.ProgramInfo[] info) {
195        if (info == null || info.length != 1) {
196            throw new IllegalArgumentException("The argument must be an array of length 1");
197        }
198        try {
199            info[0] = mTuner.getProgramInformation();
200            return RadioManager.STATUS_OK;
201        } catch (RemoteException e) {
202            Log.e(TAG, "service died", e);
203            return RadioManager.STATUS_DEAD_OBJECT;
204        }
205    }
206
207    @Override
208    public @Nullable Bitmap getMetadataImage(int id) {
209        try {
210            return mTuner.getImage(id);
211        } catch (RemoteException e) {
212            throw new RuntimeException("service died", e);
213        }
214    }
215
216    @Override
217    public boolean startBackgroundScan() {
218        try {
219            return mTuner.startBackgroundScan();
220        } catch (RemoteException e) {
221            throw new RuntimeException("service died", e);
222        }
223    }
224
225    @Override
226    public @NonNull List<RadioManager.ProgramInfo>
227            getProgramList(@Nullable Map<String, String> vendorFilter) {
228        try {
229            return mTuner.getProgramList(vendorFilter);
230        } catch (RemoteException e) {
231            throw new RuntimeException("service died", e);
232        }
233    }
234
235    @Override
236    public boolean isAnalogForced() {
237        try {
238            return mTuner.isAnalogForced();
239        } catch (RemoteException e) {
240            throw new RuntimeException("service died", e);
241        }
242    }
243
244    @Override
245    public void setAnalogForced(boolean isForced) {
246        try {
247            mTuner.setAnalogForced(isForced);
248        } catch (RemoteException e) {
249            throw new RuntimeException("service died", e);
250        }
251    }
252
253    @Override
254    public boolean isAntennaConnected() {
255        try {
256            return mTuner.isAntennaConnected();
257        } catch (RemoteException e) {
258            throw new RuntimeException("service died", e);
259        }
260    }
261
262    @Override
263    public boolean hasControl() {
264        try {
265            // don't rely on mIsClosed, as tuner might get closed internally
266            return !mTuner.isClosed();
267        } catch (RemoteException e) {
268            return false;
269        }
270    }
271}
272