Tomasz Wasilczyk | d65b3ca | 2017-12-13 08:26:25 -0800 | [diff] [blame] | 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 | |
| 17 | package com.android.server.broadcastradio.hal2; |
| 18 | |
| 19 | import android.annotation.NonNull; |
| 20 | import android.annotation.Nullable; |
Tomasz Wasilczyk | f58305d | 2017-12-28 14:03:15 -0800 | [diff] [blame] | 21 | import android.hardware.radio.ITuner; |
Tomasz Wasilczyk | d65b3ca | 2017-12-13 08:26:25 -0800 | [diff] [blame] | 22 | import android.hardware.radio.RadioManager; |
Tomasz Wasilczyk | f58305d | 2017-12-28 14:03:15 -0800 | [diff] [blame] | 23 | import android.hardware.broadcastradio.V2_0.AmFmRegionConfig; |
Tomasz Wasilczyk | d65b3ca | 2017-12-13 08:26:25 -0800 | [diff] [blame] | 24 | import android.hardware.broadcastradio.V2_0.IBroadcastRadio; |
Tomasz Wasilczyk | f58305d | 2017-12-28 14:03:15 -0800 | [diff] [blame] | 25 | import android.hardware.broadcastradio.V2_0.ITunerSession; |
| 26 | import android.hardware.broadcastradio.V2_0.Result; |
| 27 | import android.os.ParcelableException; |
Tomasz Wasilczyk | d65b3ca | 2017-12-13 08:26:25 -0800 | [diff] [blame] | 28 | import android.os.RemoteException; |
Tomasz Wasilczyk | f58305d | 2017-12-28 14:03:15 -0800 | [diff] [blame] | 29 | import android.util.MutableInt; |
Tomasz Wasilczyk | d65b3ca | 2017-12-13 08:26:25 -0800 | [diff] [blame] | 30 | import android.util.Slog; |
| 31 | |
| 32 | import java.util.Objects; |
| 33 | |
| 34 | class RadioModule { |
| 35 | private static final String TAG = "BcRadio2Srv.module"; |
| 36 | |
| 37 | @NonNull private final IBroadcastRadio mService; |
| 38 | @NonNull public final RadioManager.ModuleProperties mProperties; |
| 39 | |
| 40 | private RadioModule(@NonNull IBroadcastRadio service, |
| 41 | @NonNull RadioManager.ModuleProperties properties) { |
| 42 | mProperties = Objects.requireNonNull(properties); |
| 43 | mService = Objects.requireNonNull(service); |
| 44 | } |
| 45 | |
| 46 | public static @Nullable RadioModule tryLoadingModule(int idx, @NonNull String fqName) { |
| 47 | try { |
| 48 | IBroadcastRadio service = IBroadcastRadio.getService(); |
| 49 | if (service == null) return null; |
| 50 | |
Tomasz Wasilczyk | f58305d | 2017-12-28 14:03:15 -0800 | [diff] [blame] | 51 | Mutable<AmFmRegionConfig> amfmConfig = new Mutable<>(); |
| 52 | service.getAmFmRegionConfig(false, (int result, AmFmRegionConfig config) -> { |
| 53 | if (result == Result.OK) amfmConfig.value = config; |
| 54 | }); |
| 55 | |
Tomasz Wasilczyk | d65b3ca | 2017-12-13 08:26:25 -0800 | [diff] [blame] | 56 | RadioManager.ModuleProperties prop = |
Tomasz Wasilczyk | f58305d | 2017-12-28 14:03:15 -0800 | [diff] [blame] | 57 | Convert.propertiesFromHal(idx, fqName, service.getProperties(), amfmConfig.value); |
Tomasz Wasilczyk | d65b3ca | 2017-12-13 08:26:25 -0800 | [diff] [blame] | 58 | |
| 59 | return new RadioModule(service, prop); |
| 60 | } catch (RemoteException ex) { |
| 61 | Slog.e(TAG, "failed to load module " + fqName, ex); |
| 62 | return null; |
| 63 | } |
| 64 | } |
Tomasz Wasilczyk | f58305d | 2017-12-28 14:03:15 -0800 | [diff] [blame] | 65 | |
Tomasz Wasilczyk | 436128f | 2018-01-08 16:46:09 -0800 | [diff] [blame] | 66 | public @NonNull TunerSession openSession(@NonNull android.hardware.radio.ITunerCallback userCb) |
| 67 | throws RemoteException { |
Tomasz Wasilczyk | f58305d | 2017-12-28 14:03:15 -0800 | [diff] [blame] | 68 | TunerCallback cb = new TunerCallback(Objects.requireNonNull(userCb)); |
| 69 | Mutable<ITunerSession> hwSession = new Mutable<>(); |
| 70 | MutableInt halResult = new MutableInt(Result.UNKNOWN_ERROR); |
| 71 | |
Tomasz Wasilczyk | 436128f | 2018-01-08 16:46:09 -0800 | [diff] [blame] | 72 | mService.openSession(cb, (int result, ITunerSession session) -> { |
| 73 | hwSession.value = session; |
| 74 | halResult.value = result; |
| 75 | }); |
Tomasz Wasilczyk | f58305d | 2017-12-28 14:03:15 -0800 | [diff] [blame] | 76 | |
| 77 | Convert.throwOnError("openSession", halResult.value); |
| 78 | Objects.requireNonNull(hwSession.value); |
| 79 | |
Tomasz Wasilczyk | ca98cde | 2018-01-04 12:26:40 -0800 | [diff] [blame] | 80 | return new TunerSession(hwSession.value, cb); |
Tomasz Wasilczyk | f58305d | 2017-12-28 14:03:15 -0800 | [diff] [blame] | 81 | } |
Tomasz Wasilczyk | d65b3ca | 2017-12-13 08:26:25 -0800 | [diff] [blame] | 82 | } |