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