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